Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

24.3 Backport of PR #63502 : Support running function for parameterized view value assignment #444

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Interpreters/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1817,7 +1817,7 @@ StoragePtr Context::executeTableFunction(const ASTPtr & table_expression, const
if (table.get()->isView() && table->as<StorageView>() && table->as<StorageView>()->isParameterizedView())
{
auto query = table->getInMemoryMetadataPtr()->getSelectQuery().inner_query->clone();
NameToNameMap parameterized_view_values = analyzeFunctionParamValues(table_expression);
NameToNameMap parameterized_view_values = analyzeFunctionParamValues(table_expression, getQueryContext());
StorageView::replaceQueryParametersIfParametrizedView(query, parameterized_view_values);

ASTCreateQuery create;
Expand Down Expand Up @@ -2031,7 +2031,7 @@ StoragePtr Context::buildParametrizedViewStorage(const ASTPtr & table_expression
return nullptr;

auto query = original_view->getInMemoryMetadataPtr()->getSelectQuery().inner_query->clone();
NameToNameMap parameterized_view_values = analyzeFunctionParamValues(table_expression);
NameToNameMap parameterized_view_values = analyzeFunctionParamValues(table_expression, getQueryContext());
StorageView::replaceQueryParametersIfParametrizedView(query, parameterized_view_values);

ASTCreateQuery create;
Expand Down
14 changes: 11 additions & 3 deletions src/Parsers/FunctionParameterValuesVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <Common/FieldVisitorToString.h>
#include <Parsers/ASTHelpers.h>
#include <Common/assert_cast.h>
#include <Interpreters/evaluateConstantExpression.h>


namespace DB
Expand All @@ -20,8 +21,9 @@ namespace ErrorCodes
class FunctionParameterValuesVisitor
{
public:
explicit FunctionParameterValuesVisitor(NameToNameMap & parameter_values_)
explicit FunctionParameterValuesVisitor(NameToNameMap & parameter_values_, ContextPtr context_)
: parameter_values(parameter_values_)
,context(context_)
{
}

Expand All @@ -35,6 +37,7 @@ class FunctionParameterValuesVisitor

private:
NameToNameMap & parameter_values;
ContextPtr context;

void visitFunction(const ASTFunction & parameter_function)
{
Expand Down Expand Up @@ -64,15 +67,20 @@ class FunctionParameterValuesVisitor
parameter_values[identifier->name()] = convertFieldToString(cast_literal->value);
}
}
else
{
ASTPtr res = evaluateConstantExpressionOrIdentifierAsLiteral(expression_list->children[1], context);
parameter_values[identifier->name()] = convertFieldToString(res->as<ASTLiteral>()->value);
}
}
}
}
};

NameToNameMap analyzeFunctionParamValues(const ASTPtr & ast)
NameToNameMap analyzeFunctionParamValues(const ASTPtr & ast, ContextPtr context)
{
NameToNameMap parameter_values;
FunctionParameterValuesVisitor(parameter_values).visit(ast);
FunctionParameterValuesVisitor(parameter_values, context).visit(ast);
return parameter_values;
}

Expand Down
3 changes: 2 additions & 1 deletion src/Parsers/FunctionParameterValuesVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

#include <Core/Names.h>
#include <Parsers/IAST_fwd.h>
#include <Interpreters/Context_fwd.h>


namespace DB
{

/// Find parameters in a query parameter values and collect them into map.
NameToNameMap analyzeFunctionParamValues(const ASTPtr & ast);
NameToNameMap analyzeFunctionParamValues(const ASTPtr & ast, ContextPtr context);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2 2024-04-01 01:00:00
12 changes: 12 additions & 0 deletions tests/queries/0_stateless/03146_parameterized_view_with_date.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

drop table if exists table_pv;
create table table_pv (id Int32, timestamp_field DateTime) engine = Memory();

insert into table_pv values(1, '2024-03-01 00:00:00');
insert into table_pv values (2, '2024-04-01 01:00:00');

create view pv as select * from table_pv where timestamp_field > {timestamp_param:DateTime};

select * from pv (timestamp_param=toDateTime('2024-04-01 00:00:01'));

drop table table_pv;
Loading