diff --git a/src/Parsers/ExpressionElementParsers.cpp b/src/Parsers/ExpressionElementParsers.cpp index c51201750c5..ef236388a04 100644 --- a/src/Parsers/ExpressionElementParsers.cpp +++ b/src/Parsers/ExpressionElementParsers.cpp @@ -505,32 +505,34 @@ namespace bool parseExtract(IParser::Pos & pos, ASTPtr & node, Expected & expected) { - ASTPtr expr; - + IParser::Pos begin = pos; IntervalKind interval_kind; - if (!parseIntervalKind(pos, expected, interval_kind)) - { - ASTPtr expr_list; - if (!ParserExpressionList(false, false).parse(pos, expr_list, expected)) - return false; - auto res = std::make_shared(); - res->name = "extract"; - res->arguments = expr_list; - res->children.push_back(res->arguments); - node = std::move(res); - return true; + if (parseIntervalKind(pos, expected, interval_kind)) + { + ASTPtr expr; + + ParserKeyword s_from("FROM"); + ParserExpression elem_parser; + + if (s_from.ignore(pos, expected) && elem_parser.parse(pos, expr, expected)) + { + node = makeASTFunction(interval_kind.toNameOfFunctionExtractTimePart(), expr); + return true; + } } - ParserKeyword s_from("FROM"); - if (!s_from.ignore(pos, expected)) + pos = begin; + + ASTPtr expr_list; + if (!ParserExpressionList(false, false).parse(pos, expr_list, expected)) return false; - ParserExpression elem_parser; - if (!elem_parser.parse(pos, expr, expected)) - return false; - - node = makeASTFunction(interval_kind.toNameOfFunctionExtractTimePart(), expr); + auto res = std::make_shared(); + res->name = "extract"; + res->arguments = expr_list; + res->children.push_back(res->arguments); + node = std::move(res); return true; } diff --git a/tests/queries/0_stateless/02247_fix_extract_parser.reference b/tests/queries/0_stateless/02247_fix_extract_parser.reference new file mode 100644 index 00000000000..01e79c32a8c --- /dev/null +++ b/tests/queries/0_stateless/02247_fix_extract_parser.reference @@ -0,0 +1,3 @@ +1 +2 +3 diff --git a/tests/queries/0_stateless/02247_fix_extract_parser.sql b/tests/queries/0_stateless/02247_fix_extract_parser.sql new file mode 100644 index 00000000000..9b721a6e830 --- /dev/null +++ b/tests/queries/0_stateless/02247_fix_extract_parser.sql @@ -0,0 +1,3 @@ +WITH 'number: 1' as year SELECT extract(year, '\\d+'); +WITH 'number: 2' as mm SELECT extract(mm, '\\d+'); +WITH 'number: 3' as s SELECT extract(s, '\\d+');