Merge pull request #60372 from HowePa/single_arg_merge

Support single-argument version for the merge table function
This commit is contained in:
Kruglov Pavel 2024-02-26 13:43:04 +01:00 committed by GitHub
commit 1a9ee8c318
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 49 additions and 18 deletions

View File

@ -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.

View File

@ -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')");
}
}

View File

@ -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

View File

@ -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;