From 3ad95c9884b05d2ab96fd790793b78c628bf25e2 Mon Sep 17 00:00:00 2001 From: Lisen <38773813+yl-lisen@users.noreply.github.com> Date: Thu, 15 Aug 2024 20:22:46 +0800 Subject: [PATCH] Bugfix/issue 820 fix incorrect code name (#821) * fix incorrect code name * add testing --- src/Interpreters/Streaming/PartitionByVisitor.cpp | 2 +- .../Streaming/SubstituteStreamingFunction.cpp | 3 +-- src/Parsers/ASTFunction.cpp | 14 ++++++++++++++ src/Parsers/ASTFunction.h | 4 ++++ .../0_stateless/99005_explain_syntax_bug.reference | 6 ++++++ .../0_stateless/99005_explain_syntax_bug.sql | 6 ++++++ 6 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 tests/queries_ported/0_stateless/99005_explain_syntax_bug.reference create mode 100644 tests/queries_ported/0_stateless/99005_explain_syntax_bug.sql diff --git a/src/Interpreters/Streaming/PartitionByVisitor.cpp b/src/Interpreters/Streaming/PartitionByVisitor.cpp index 897ac8cf208..525eef506c5 100644 --- a/src/Interpreters/Streaming/PartitionByVisitor.cpp +++ b/src/Interpreters/Streaming/PartitionByVisitor.cpp @@ -55,7 +55,7 @@ void PartitionByMatcher::visit(ASTPtr & ast, Data & data) /// Convert function to window function. /// Always show original function - node_func->code_name = DB::serializeAST(*node_func); + node_func->makeCurrentCodeName(); node_func->window_definition = data.win_define->clone(); node_func->is_window_function = true; } diff --git a/src/Interpreters/Streaming/SubstituteStreamingFunction.cpp b/src/Interpreters/Streaming/SubstituteStreamingFunction.cpp index 6c00b4ac06e..6b54501452b 100644 --- a/src/Interpreters/Streaming/SubstituteStreamingFunction.cpp +++ b/src/Interpreters/Streaming/SubstituteStreamingFunction.cpp @@ -146,8 +146,7 @@ void StreamingFunctionData::visit(DB::ASTFunction & func, DB::ASTPtr) if (!func_alias_name->empty()) { /// Always show original function - if (func.code_name.empty()) - func.code_name = DB::serializeAST(func); + func.makeCurrentCodeName(); func.name = *func_alias_name; if (!func.arguments) diff --git a/src/Parsers/ASTFunction.cpp b/src/Parsers/ASTFunction.cpp index e44c31c2be1..400788c675a 100644 --- a/src/Parsers/ASTFunction.cpp +++ b/src/Parsers/ASTFunction.cpp @@ -641,6 +641,20 @@ void ASTFunction::formatImplWithoutAlias(const FormatSettings & settings, Format } } +/// proton: starts. +void ASTFunction::makeCurrentCodeName() +{ + if (!code_name.empty()) + return; + + WriteBufferFromOwnString buf; + IAST::FormatSettings settings(buf, /*one_line=*/true); + FormatState state; + formatImplWithoutAlias(settings, state, FormatStateStacked()); + code_name = buf.str(); +} +/// proton: ends. + String getFunctionName(const IAST * ast) { String res; diff --git a/src/Parsers/ASTFunction.h b/src/Parsers/ASTFunction.h index 95e4d23909f..2b12fcd40b7 100644 --- a/src/Parsers/ASTFunction.h +++ b/src/Parsers/ASTFunction.h @@ -59,6 +59,10 @@ class ASTFunction : public ASTWithAlias std::string getWindowDescription() const; + /// proton: starts. Generates the one-line formatting string (without aliases) for the current function and assigns it to code_name. + void makeCurrentCodeName(); + /// proton: ends. + protected: void formatImplWithoutAlias(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const override; void appendColumnNameImpl(WriteBuffer & ostr) const override; diff --git a/tests/queries_ported/0_stateless/99005_explain_syntax_bug.reference b/tests/queries_ported/0_stateless/99005_explain_syntax_bug.reference new file mode 100644 index 00000000000..31c82b34bd7 --- /dev/null +++ b/tests/queries_ported/0_stateless/99005_explain_syntax_bug.reference @@ -0,0 +1,6 @@ +SELECT + count() OVER (PARTITION BY i) AS x, lag(x) +FROM + changelog(`99005_stream`, i) +GROUP BY + i diff --git a/tests/queries_ported/0_stateless/99005_explain_syntax_bug.sql b/tests/queries_ported/0_stateless/99005_explain_syntax_bug.sql new file mode 100644 index 00000000000..e0bfb97b652 --- /dev/null +++ b/tests/queries_ported/0_stateless/99005_explain_syntax_bug.sql @@ -0,0 +1,6 @@ +DROP STREAM IF EXISTS 99005_stream; +CREATE STREAM 99005_stream(i int, v int); + +EXPLAIN SYNTAX select count() over (partition by i) as x, lag(x) from changelog(99005_stream, i); + +DROP STREAM 99005_stream;