fix and add test

This commit is contained in:
feng lv 2021-06-08 03:11:27 +00:00
parent b40bb00b8d
commit 41b518012f
5 changed files with 211 additions and 5 deletions

View File

@ -103,9 +103,13 @@ StorageMerge::StorageMerge(
const StorageID & table_id_, const StorageID & table_id_,
const ColumnsDescription & columns_, const ColumnsDescription & columns_,
const String & comment, const String & comment,
const String & source_database_regexp_,
const std::unordered_map<String, std::unordered_set<String>> & source_databases_and_tables_, const std::unordered_map<String, std::unordered_set<String>> & source_databases_and_tables_,
ContextPtr context_) ContextPtr context_)
: IStorage(table_id_), WithContext(context_->getGlobalContext()), source_databases_and_tables(source_databases_and_tables_) : IStorage(table_id_)
, WithContext(context_->getGlobalContext())
, source_database_regexp(source_database_regexp_)
, source_databases_and_tables(source_databases_and_tables_)
{ {
StorageInMemoryMetadata storage_metadata; StorageInMemoryMetadata storage_metadata;
storage_metadata.setColumns(columns_); storage_metadata.setColumns(columns_);
@ -684,6 +688,10 @@ void registerStorageMerge(StorageFactory & factory)
String source_database_regexp = engine_args[0]->as<ASTLiteral &>().value.safeGet<String>(); String source_database_regexp = engine_args[0]->as<ASTLiteral &>().value.safeGet<String>();
String table_name_regexp = engine_args[1]->as<ASTLiteral &>().value.safeGet<String>(); String table_name_regexp = engine_args[1]->as<ASTLiteral &>().value.safeGet<String>();
/// If database argument is not String literal, we should not treat it as regexp
if (!engine_args[0]->as<ASTLiteral>())
source_database_regexp = "^" + source_database_regexp + "$";
return StorageMerge::create( return StorageMerge::create(
args.table_id, args.columns, args.comment, source_database_regexp, table_name_regexp, args.getContext()); args.table_id, args.columns, args.comment, source_database_regexp, table_name_regexp, args.getContext());
}); });

View File

@ -74,6 +74,7 @@ protected:
const StorageID & table_id_, const StorageID & table_id_,
const ColumnsDescription & columns_, const ColumnsDescription & columns_,
const String & comment, const String & comment,
const String & source_database_regexp_,
const std::unordered_map<String, std::unordered_set<String>> & source_databases_and_tables_, const std::unordered_map<String, std::unordered_set<String>> & source_databases_and_tables_,
ContextPtr context_); ContextPtr context_);

View File

@ -49,11 +49,15 @@ void TableFunctionMerge::parseArguments(const ASTPtr & ast_function, ContextPtr
" - name of source database and regexp for table names.", " - name of source database and regexp for table names.",
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH); ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
args[0] = evaluateConstantExpressionForDatabaseName(args[0], context); auto db_arg = evaluateConstantExpressionForDatabaseName(args[0], context);
args[1] = evaluateConstantExpressionAsLiteral(args[1], context); auto table_arg = evaluateConstantExpressionAsLiteral(args[1], context);
source_database_regexp = args[0]->as<ASTLiteral &>().value.safeGet<String>(); source_database_regexp = db_arg->as<ASTLiteral &>().value.safeGet<String>();
source_table_regexp = args[1]->as<ASTLiteral &>().value.safeGet<String>(); source_table_regexp = table_arg->as<ASTLiteral &>().value.safeGet<String>();
/// If database argument is not String literal, we should not treat it as regexp
if (!args[0]->as<ASTLiteral>())
source_database_regexp = "^" + source_database_regexp + "$";
} }
@ -122,6 +126,7 @@ StoragePtr TableFunctionMerge::executeImpl(const ASTPtr & /*ast_function*/, Cont
StorageID(getDatabaseName(), table_name), StorageID(getDatabaseName(), table_name),
getActualTableStructure(context), getActualTableStructure(context),
String{}, String{},
source_database_regexp,
getSourceDatabasesAndTables(context), getSourceDatabasesAndTables(context),
context); context);

View File

@ -0,0 +1,146 @@
CREATE TABLE t_merge as db.t ENGINE=Merge(^db, ^t)
SELECT _database, _table, n FROM db.t_merge ORDER BY _database, _table, n
db t 0
db t 1
db t 2
db t 3
db t 4
db t 5
db t 6
db t 7
db t 8
db t 9
db1 t1 0
db1 t1 1
db1 t1 2
db1 t1 3
db1 t1 4
db1 t1 5
db1 t1 6
db1 t1 7
db1 t1 8
db1 t1 9
db2 t2 0
db2 t2 1
db2 t2 2
db2 t2 3
db2 t2 4
db2 t2 5
db2 t2 6
db2 t2 7
db2 t2 8
db2 t2 9
db3 t3 0
db3 t3 1
db3 t3 2
db3 t3 3
db3 t3 4
db3 t3 5
db3 t3 6
db3 t3 7
db3 t3 8
db3 t3 9
SELECT _database, _table, n FROM merge(^db, ^t) ORDER BY _database, _table, n
db t 0
db t 1
db t 2
db t 3
db t 4
db t 5
db t 6
db t 7
db t 8
db t 9
db t_merge 0
db t_merge 0
db t_merge 0
db t_merge 0
db t_merge 1
db t_merge 1
db t_merge 1
db t_merge 1
db t_merge 2
db t_merge 2
db t_merge 2
db t_merge 2
db t_merge 3
db t_merge 3
db t_merge 3
db t_merge 3
db t_merge 4
db t_merge 4
db t_merge 4
db t_merge 4
db t_merge 5
db t_merge 5
db t_merge 5
db t_merge 5
db t_merge 6
db t_merge 6
db t_merge 6
db t_merge 6
db t_merge 7
db t_merge 7
db t_merge 7
db t_merge 7
db t_merge 8
db t_merge 8
db t_merge 8
db t_merge 8
db t_merge 9
db t_merge 9
db t_merge 9
db t_merge 9
db1 t1 0
db1 t1 1
db1 t1 2
db1 t1 3
db1 t1 4
db1 t1 5
db1 t1 6
db1 t1 7
db1 t1 8
db1 t1 9
db2 t2 0
db2 t2 1
db2 t2 2
db2 t2 3
db2 t2 4
db2 t2 5
db2 t2 6
db2 t2 7
db2 t2 8
db2 t2 9
db3 t3 0
db3 t3 1
db3 t3 2
db3 t3 3
db3 t3 4
db3 t3 5
db3 t3 6
db3 t3 7
db3 t3 8
db3 t3 9
CREATE TABLE t_merge_1 as db.t ENGINE=Merge(currentDatabase(), ^t)
SELECT _database, _table, n FROM db.t_merge_1 ORDER BY _database, _table, n
db1 t1 0
db1 t1 1
db1 t1 2
db1 t1 3
db1 t1 4
db1 t1 5
db1 t1 6
db1 t1 7
db1 t1 8
db1 t1 9
SELECT _database, _table, n FROM merge(currentDatabase(), ^t) ORDER BY _database, _table, n
db1 t1 0
db1 t1 1
db1 t1 2
db1 t1 3
db1 t1 4
db1 t1 5
db1 t1 6
db1 t1 7
db1 t1 8
db1 t1 9

View File

@ -0,0 +1,46 @@
DROP DATABASE IF EXISTS db;
DROP DATABASE IF EXISTS db1;
DROP DATABASE IF EXISTS db2;
DROP DATABASE IF EXISTS db3;
CREATE DATABASE db;
CREATE DATABASE db1;
CREATE DATABASE db2;
CREATE DATABASE db3;
CREATE TABLE db.t (n Int8) ENGINE=MergeTree ORDER BY n;
CREATE TABLE db1.t1 (n Int8) ENGINE=MergeTree ORDER BY n;
CREATE TABLE db2.t2 (n Int8) ENGINE=MergeTree ORDER BY n;
CREATE TABLE db3.t3 (n Int8) ENGINE=MergeTree ORDER BY n;
INSERT INTO db.t SELECT * FROM numbers(10);
INSERT INTO db1.t1 SELECT * FROM numbers(10);
INSERT INTO db2.t2 SELECT * FROM numbers(10);
INSERT INTO db3.t3 SELECT * FROM numbers(10);
SELECT 'CREATE TABLE t_merge as db.t ENGINE=Merge(^db, ^t)';
CREATE TABLE db.t_merge as db.t ENGINE=Merge('^db', '^t');
SELECT 'SELECT _database, _table, n FROM db.t_merge ORDER BY _database, _table, n';
SELECT _database, _table, n FROM db.t_merge ORDER BY _database, _table, n;
SELECT 'SELECT _database, _table, n FROM merge(^db, ^t) ORDER BY _database, _table, n';
SELECT _database, _table, n FROM merge('^db', '^t') ORDER BY _database, _table, n;
USE db1;
-- evaluated value of expression should not be treat as repr
SELECT 'CREATE TABLE t_merge_1 as db.t ENGINE=Merge(currentDatabase(), ^t)';
CREATE TABLE db.t_merge_1 as db.t ENGINE=Merge(currentDatabase(), '^t');
SELECT 'SELECT _database, _table, n FROM db.t_merge_1 ORDER BY _database, _table, n';
SELECT _database, _table, n FROM db.t_merge_1 ORDER BY _database, _table, n;
-- evaluated value of expression should not be treat as repr
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 db;
DROP DATABASE db1;
DROP DATABASE db2;
DROP DATABASE db3;