allow query parameters in Values

This commit is contained in:
Alexander Tokmakov 2019-10-02 22:54:40 +03:00
parent 565c273eaa
commit a58996817f
6 changed files with 30 additions and 0 deletions

View File

@ -222,6 +222,7 @@ private:
context.makeGlobalContext();
context.setApplicationType(Context::ApplicationType::CLIENT);
context.setQueryParameters(query_parameters);
/// settings and limits could be specified in config file, but passed settings has higher priority
for (auto && setting : context.getSettingsRef())

View File

@ -535,6 +535,7 @@ public:
bool hasQueryParameters() const;
const NameToNameMap & getQueryParameters() const;
void setQueryParameter(const String & name, const String & value);
void setQueryParameters(const NameToNameMap & parameters) { query_parameters = parameters; }
#if USE_EMBEDDED_COMPILER
std::shared_ptr<CompiledExpressionCache> getCompiledExpressionCache() const;

View File

@ -6,12 +6,14 @@
#include <Functions/FunctionsConversion.h>
#include <Functions/FunctionFactory.h>
#include <Interpreters/ExpressionAnalyzer.h>
#include <Interpreters/ReplaceQueryParameterVisitor.h>
#include <Interpreters/SyntaxAnalyzer.h>
#include <IO/ReadHelpers.h>
#include <Parsers/ASTExpressionList.h>
#include <Parsers/ASTFunction.h>
#include <Parsers/ASTIdentifier.h>
#include <Parsers/ASTLiteral.h>
#include <Parsers/ASTQueryParameter.h>
#include <Parsers/CommonParsers.h>
#include <Processors/Formats/Impl/ConstantExpressionTemplate.h>
#include <Parsers/ExpressionElementParsers.h>
@ -67,6 +69,8 @@ public:
return;
if (auto function = ast->as<ASTFunction>())
visit(*function, force_nullable);
else if (ast->as<ASTQueryParameter>())
return;
else if (ast->as<ASTIdentifier>())
throw DB::Exception("Identifier in constant expression", ErrorCodes::SYNTAX_ERROR);
else
@ -295,6 +299,8 @@ ConstantExpressionTemplate::Cache::getFromCacheOrConstruct(const DataTypePtr & r
ASTPtr expression = expression_->clone();
ReplaceLiteralsVisitor visitor(context);
visitor.visit(expression, result_column_type->isNullable());
ReplaceQueryParameterVisitor param_visitor(context.getQueryParameters());
param_visitor.visit(expression);
size_t template_hash = TemplateStructure::getTemplateHash(expression, visitor.replaced_literals, result_column_type, salt);
auto iter = cache.find(template_hash);

View File

@ -1,5 +1,6 @@
DROP TABLE IF EXISTS values_template;
DROP TABLE IF EXISTS values_template_nullable;
DROP TABLE IF EXISTS values_template_fallback;
CREATE TABLE values_template (d Date, s String, u UInt8, i Int64, f Float64, a Array(UInt8)) ENGINE = Memory;
CREATE TABLE values_template_nullable (d Date, s Nullable(String), u Nullable(UInt8), a Array(Nullable(Float32))) ENGINE = Memory;

View File

@ -0,0 +1,4 @@
0 helloparam []
1 worldparam [0.2,0.3]
2 testparam [0.3]
3 paramparam []

View File

@ -0,0 +1,17 @@
#!/usr/bin/env bash
CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
. $CURDIR/../shell_config.sh
$CLICKHOUSE_CLIENT --query="DROP TABLE IF EXISTS insert_values_parametrized";
$CLICKHOUSE_CLIENT --query="CREATE TABLE insert_values_parametrized (n UInt8, s String, a Array(Float32)) ENGINE = Memory";
$CLICKHOUSE_CLIENT --input_format_values_deduce_templates_of_expressions=1 --param_p_n="-1" --param_p_s="param" --param_p_a="[0.2,0.3]" --query="INSERT INTO insert_values_parametrized VALUES
(1 + {p_n:Int8}, lower(concat('Hello', {p_s:String})), arraySort(arrayIntersect([], {p_a:Array(Nullable(Float32))}))),\
(2 + {p_n:Int8}, lower(concat('world', {p_s:String})), arraySort(arrayIntersect([0.1,0.2,0.3], {p_a:Array(Nullable(Float32))}))),\
(3 + {p_n:Int8}, lower(concat('TEST', {p_s:String})), arraySort(arrayIntersect([0.1,0.3,0.4], {p_a:Array(Nullable(Float32))}))),\
(4 + {p_n:Int8}, lower(concat('PaRaM', {p_s:String})), arraySort(arrayIntersect([0.5], {p_a:Array(Nullable(Float32))})))";
$CLICKHOUSE_CLIENT --query="SELECT * FROM insert_values_parametrized ORDER BY n";
$CLICKHOUSE_CLIENT --query="DROP TABLE insert_values_parametrized";