SQL compatibility: provide POSITION(needle IN haystack) syntax.

This commit is contained in:
jianmei zhang 2021-01-06 11:21:31 +08:00 committed by Alexey Milovidov
parent 417e685830
commit 2256227361
3 changed files with 44 additions and 0 deletions

View File

@ -343,6 +343,22 @@ bool ParserFunction::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
throw Exception("Argument of function toDate is unquoted: toDate(" + contents_str + "), must be: toDate('" + contents_str + "')"
, ErrorCodes::SYNTAX_ERROR);
}
else if (Poco::toLower(getIdentifierName(identifier)) == "position")
{
/// POSITION(needle IN haystack) is equivalent to function position(haystack, needle)
const auto & list = expr_list_args->as<ASTExpressionList &>();
if (list.children.size() == 1)
{
const auto & in_func = list.children[0]->as<ASTFunction &>();
if (in_func.name == "in")
{
// switch the two arguments
const auto & arg_list = in_func.arguments->as<ASTExpressionList &>();
expr_list_args->children = {arg_list.children[1], arg_list.children[0]};
}
}
}
/// The parametric aggregate function has two lists (parameters and arguments) in parentheses. Example: quantile(0.9)(x).
if (allow_function_parameters && pos->type == TokenType::OpeningRoundBracket)

View File

@ -23796,3 +23796,16 @@
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1

View File

@ -13,6 +13,14 @@ select 1 = position('a', '', 1);
select 2 = position('a', '', 2);
select 0 = position('a', '', 3);
select 1 = position('' in '');
select 1 = position('' in 'abc');
select 0 = position('abc' in '');
select 1 = position('abc' in 'abc');
select 2 = position('bc' in 'abc');
select 3 = position('c' in 'abc');
select 6 = position('/' IN s) FROM (SELECT 'Hello/World' AS s);
select [1, 1, 2, 3, 4, 5, 0, 0, 0, 0] = groupArray(position('aaaa', '', number)) from numbers(10);
select [1, 1, 2, 3, 4, 5, 0, 0, 0, 0] = groupArray(position(materialize('aaaa'), '', number)) from numbers(10);
select [1, 1, 2, 3, 4, 5, 0, 0, 0, 0] = groupArray(position('aaaa', materialize(''), number)) from numbers(10);
@ -54,6 +62,13 @@ select 0 = position('abcabc', 'b', 6);
select 2 = position('abcabc', 'bca', 0);
select 0 = position('abcabc', 'bca', 3);
select 1 = position('' in '');
select 1 = position('' in 'абв');
select 0 = position('абв' in '');
select 1 = position('абв' in 'абв');
select 3 = position('бв' in 'абв');
select 5 = position('в' in 'абв');
select 1 = position(materialize(''), '');
select 1 = position(materialize('абв'), '');
select 0 = position(materialize(''), 'абв');