mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-26 01:22:04 +00:00
Merge pull request #60372 from HowePa/single_arg_merge
Support single-argument version for the merge table function
This commit is contained in:
commit
1a9ee8c318
@ -11,11 +11,11 @@ Creates a temporary [Merge](../../engines/table-engines/special/merge.md) table.
|
||||
**Syntax**
|
||||
|
||||
```sql
|
||||
merge('db_name', 'tables_regexp')
|
||||
merge(['db_name',] 'tables_regexp')
|
||||
```
|
||||
**Arguments**
|
||||
|
||||
- `db_name` — Possible values:
|
||||
- `db_name` — Possible values (optional, default is `currentDatabase()`):
|
||||
- database name,
|
||||
- constant expression that returns a string with a database name, for example, `currentDatabase()`,
|
||||
- `REGEXP(expression)`, where `expression` is a regular expression to match the DB names.
|
||||
|
@ -88,26 +88,38 @@ void TableFunctionMerge::parseArguments(const ASTPtr & ast_function, ContextPtr
|
||||
|
||||
if (args_func.size() != 1)
|
||||
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH,
|
||||
"Table function 'merge' requires exactly 2 arguments - name "
|
||||
"of source database and regexp for table names.");
|
||||
"Table function 'merge' requires from 1 to 2 parameters: "
|
||||
"merge(['db_name',] 'tables_regexp')");
|
||||
|
||||
ASTs & args = args_func.at(0)->children;
|
||||
|
||||
if (args.size() != 2)
|
||||
if (args.size() == 1)
|
||||
{
|
||||
database_is_regexp = false;
|
||||
source_database_name_or_regexp = context->getCurrentDatabase();
|
||||
|
||||
args[0] = evaluateConstantExpressionAsLiteral(args[0], context);
|
||||
source_table_regexp = checkAndGetLiteralArgument<String>(args[0], "table_name_regexp");
|
||||
}
|
||||
else if (args.size() == 2)
|
||||
{
|
||||
auto [is_regexp, database_ast] = StorageMerge::evaluateDatabaseName(args[0], context);
|
||||
|
||||
database_is_regexp = is_regexp;
|
||||
|
||||
if (!is_regexp)
|
||||
args[0] = database_ast;
|
||||
source_database_name_or_regexp = checkAndGetLiteralArgument<String>(database_ast, "database_name");
|
||||
|
||||
args[1] = evaluateConstantExpressionAsLiteral(args[1], context);
|
||||
source_table_regexp = checkAndGetLiteralArgument<String>(args[1], "table_name_regexp");
|
||||
}
|
||||
else
|
||||
{
|
||||
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH,
|
||||
"Table function 'merge' requires exactly 2 arguments - name "
|
||||
"of source database and regexp for table names.");
|
||||
|
||||
auto [is_regexp, database_ast] = StorageMerge::evaluateDatabaseName(args[0], context);
|
||||
|
||||
database_is_regexp = is_regexp;
|
||||
|
||||
if (!is_regexp)
|
||||
args[0] = database_ast;
|
||||
source_database_name_or_regexp = checkAndGetLiteralArgument<String>(database_ast, "database_name");
|
||||
|
||||
args[1] = evaluateConstantExpressionAsLiteral(args[1], context);
|
||||
source_table_regexp = checkAndGetLiteralArgument<String>(args[1], "table_name_regexp");
|
||||
"Table function 'merge' requires from 1 to 2 parameters: "
|
||||
"merge(['db_name',] 'tables_regexp')");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -0,0 +1,6 @@
|
||||
01902_db_params t 0
|
||||
01902_db_params t 1
|
||||
01902_db_params t 2
|
||||
01902_db_params t 0
|
||||
01902_db_params t 1
|
||||
01902_db_params t 2
|
@ -0,0 +1,13 @@
|
||||
DROP DATABASE IF EXISTS 01902_db_params;
|
||||
CREATE DATABASE 01902_db_params;
|
||||
CREATE TABLE 01902_db_params.t(n Int8) ENGINE=MergeTree ORDER BY n;
|
||||
INSERT INTO 01902_db_params.t SELECT * FROM numbers(3);
|
||||
SELECT _database, _table, n FROM merge(REGEXP('^01902_db_params'), '^t') ORDER BY _database, _table, n;
|
||||
|
||||
SELECT _database, _table, n FROM merge() ORDER BY _database, _table, n; -- {serverError NUMBER_OF_ARGUMENTS_DOESNT_MATCH}
|
||||
SELECT _database, _table, n FROM merge('^t') ORDER BY _database, _table, n; -- {serverError BAD_ARGUMENTS}
|
||||
|
||||
USE 01902_db_params;
|
||||
SELECT _database, _table, n FROM merge('^t') ORDER BY _database, _table, n;
|
||||
|
||||
DROP DATABASE 01902_db_params;
|
Loading…
Reference in New Issue
Block a user