This commit is contained in:
feng lv 2021-06-27 06:09:23 +00:00
parent a947565de7
commit 06242f85e6
6 changed files with 28 additions and 11 deletions

View File

@ -100,7 +100,7 @@ ASTPtr evaluateConstantExpressionForDatabaseName(const ASTPtr & node, ContextPtr
return res;
}
std::tuple<bool, String> evaluateDatabaseNameForMergeEngine(const ASTPtr & node, ContextPtr context)
std::tuple<bool, ASTPtr> evaluateDatabaseNameForMergeEngine(const ASTPtr & node, ContextPtr context)
{
if (const auto * func = node->as<ASTFunction>(); func && func->name == "REGEXP")
{
@ -111,11 +111,11 @@ std::tuple<bool, String> evaluateDatabaseNameForMergeEngine(const ASTPtr & node,
if (!literal || literal->value.safeGet<String>().empty())
throw Exception("Argument for REGEXP in Merge ENGINE should be a non empty String Literal", ErrorCodes::BAD_ARGUMENTS);
return std::tuple{true, literal->value.safeGet<String>()};
return std::tuple{true, func->arguments->children[0]};
}
auto ast = evaluateConstantExpressionForDatabaseName(node, context);
return std::tuple{false, ast->as<ASTLiteral>()->value.safeGet<String>()};
return std::tuple{false, ast};
}
namespace

View File

@ -54,5 +54,5 @@ ASTPtr evaluateConstantExpressionForDatabaseName(const ASTPtr & node, ContextPtr
std::optional<Blocks> evaluateExpressionOverConstantCondition(const ASTPtr & node, const ExpressionActionsPtr & target_expr, size_t & limit);
// Evaluate database name or regexp for StorageMerge and TableFunction merge
std::tuple<bool, String> evaluateDatabaseNameForMergeEngine(const ASTPtr & node, ContextPtr context);
std::tuple<bool, ASTPtr> evaluateDatabaseNameForMergeEngine(const ASTPtr & node, ContextPtr context);
}

View File

@ -670,7 +670,11 @@ void registerStorageMerge(StorageFactory & factory)
" - name of source database and regexp for table names.",
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
auto [is_regexp, source_database_name_or_regexp] = evaluateDatabaseNameForMergeEngine(engine_args[0], args.getLocalContext());
auto [is_regexp, database_ast] = evaluateDatabaseNameForMergeEngine(engine_args[0], args.getLocalContext());
if (!is_regexp)
engine_args[0] = database_ast;
String source_database_name_or_regexp = database_ast->as<ASTLiteral &>().value.safeGet<String>();
engine_args[1] = evaluateConstantExpressionAsLiteral(engine_args[1], args.getLocalContext());
String table_name_regexp = engine_args[1]->as<ASTLiteral &>().value.safeGet<String>();

View File

@ -49,10 +49,13 @@ void TableFunctionMerge::parseArguments(const ASTPtr & ast_function, ContextPtr
" - name of source database and regexp for table names.",
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
auto [is_regexp, source_database_name_or_regexp_] = evaluateDatabaseNameForMergeEngine(args[0], context);
auto [is_regexp, database_ast] = evaluateDatabaseNameForMergeEngine(args[0], context);
database_is_regexp = is_regexp;
source_database_name_or_regexp = source_database_name_or_regexp_;
if (!is_regexp)
args[0] = database_ast;
source_database_name_or_regexp = database_ast->as<ASTLiteral &>().value.safeGet<String>();
args[1] = evaluateConstantExpressionAsLiteral(args[1], context);
source_table_regexp = args[1]->as<ASTLiteral &>().value.safeGet<String>();

View File

@ -40,6 +40,8 @@ SELECT _database, _table, n FROM 01902_db.t_merge ORDER BY _database, _table, n
01902_db3 t3 7
01902_db3 t3 8
01902_db3 t3 9
SHOW CREATE TABLE 01902_db.t_merge
CREATE TABLE `01902_db`.t_merge\n(\n `n` Int8\n)\nENGINE = Merge(REGEXP(\'^01902_db\'), \'^t\')
SELECT _database, _table, n FROM merge(REGEXP(^01902_db), ^t) ORDER BY _database, _table, n
01902_db t 0
01902_db t 1
@ -178,6 +180,8 @@ SELECT _database, _table, n FROM 01902_db.t_merge_1 ORDER BY _database, _table,
01902_db1 t1 7
01902_db1 t1 8
01902_db1 t1 9
SHOW CREATE TABLE 01902_db.t_merge_1
CREATE TABLE `01902_db`.t_merge_1\n(\n `n` Int8\n)\nENGINE = Merge(\'01902_db1\', \'^t\')
SELECT _database, _table, n FROM merge(currentDatabase(), ^t) ORDER BY _database, _table, n
01902_db1 t1 0
01902_db1 t1 1

View File

@ -24,6 +24,9 @@ CREATE TABLE 01902_db.t_merge as 01902_db.t ENGINE=Merge(REGEXP('^01902_db'), '^
SELECT 'SELECT _database, _table, n FROM 01902_db.t_merge ORDER BY _database, _table, n';
SELECT _database, _table, n FROM 01902_db.t_merge ORDER BY _database, _table, n;
SELECT 'SHOW CREATE TABLE 01902_db.t_merge';
SHOW CREATE TABLE 01902_db.t_merge;
SELECT 'SELECT _database, _table, n FROM merge(REGEXP(^01902_db), ^t) ORDER BY _database, _table, n';
SELECT _database, _table, n FROM merge(REGEXP('^01902_db'), '^t') ORDER BY _database, _table, n;
@ -51,10 +54,13 @@ CREATE TABLE 01902_db.t_merge_1 as 01902_db.t ENGINE=Merge(currentDatabase(), '^
SELECT 'SELECT _database, _table, n FROM 01902_db.t_merge_1 ORDER BY _database, _table, n';
SELECT _database, _table, n FROM 01902_db.t_merge_1 ORDER BY _database, _table, n;
SELECT 'SHOW CREATE TABLE 01902_db.t_merge_1';
SHOW CREATE TABLE 01902_db.t_merge_1;
SELECT 'SELECT _database, _table, n FROM merge(currentDatabase(), ^t) ORDER BY _database, _table, n';
SELECT _database, _table, n FROM merge(currentDatabase(), '^t') ORDER BY _database, _table, n;
-- DROP DATABASE 01902_db;
-- DROP DATABASE 01902_db1;
-- DROP DATABASE 01902_db2;
-- DROP DATABASE 01902_db3;
DROP DATABASE 01902_db;
DROP DATABASE 01902_db1;
DROP DATABASE 01902_db2;
DROP DATABASE 01902_db3;