Skip to content

better CTE and with global - #18684

Merged
alexey-milovidov merged 10 commits into
ClickHouse:masterfrom
amosbird:betterwith
Jan 27, 2021
Merged

alexey-milovidov merged 10 commits into
ClickHouse:masterfrom
amosbird:betterwith

Conversation

@amosbird

@amosbird amosbird commented Jan 2, 2021

Copy link
Copy Markdown
Collaborator

I hereby agree to the terms of the CLA available at: https://yandex.ru/legal/cla/?lang=en

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):

Allow CTE to be further aliased. Propagate CSE to subqueries in the same level when enable_global_with_statement = 1. This fixes #17378 . This fixes #16575 (comment) .

Detailed description / Documentation draft:

.

@robot-clickhouse robot-clickhouse added the pr-improvement Pull request with some product improvements label Jan 2, 2021
@amosbird

amosbird commented Jan 2, 2021

Copy link
Copy Markdown
Collaborator Author

Test failure doesn't look like related

2021-01-02 12:42:05 01133_max_result_rows:                                                  [ FAIL ] 0.80 sec. - result differs with reference:
2021-01-02 12:42:05 --- /usr/share/clickhouse-test/queries/0_stateless/01133_max_result_rows.reference	2021-01-02 10:31:04.000000000 +0300
2021-01-02 12:42:05 +++ /tmp/clickhouse-test/0_stateless/01133_max_result_rows.stdout	2021-01-02 12:42:05.133011030 +0300
2021-01-02 12:42:05 @@ -4,20 +4,6 @@
2021-01-02 12:42:05  3
2021-01-02 12:42:05  4
2021-01-02 12:42:05  5
2021-01-02 12:42:05 -6
2021-01-02 12:42:05 -7
2021-01-02 12:42:05 -8
2021-01-02 12:42:05 -9
2021-01-02 12:42:05 -10
2021-01-02 12:42:05 -11
2021-01-02 12:42:05 -12
2021-01-02 12:42:05 -13
2021-01-02 12:42:05 -14
2021-01-02 12:42:05 -15
2021-01-02 12:42:05 -16
2021-01-02 12:42:05 -17
2021-01-02 12:42:05 -18
2021-01-02 12:42:05 -19
2021-01-02 12:42:05  0
2021-01-02 12:42:05  1
2021-01-02 12:42:05  2

@alexey-milovidov

Copy link
Copy Markdown
Member

See also #18705 (comment)

@amosbird

amosbird commented Jan 4, 2021

Copy link
Copy Markdown
Collaborator Author

See also #18705 (comment)

Ah, it's broken after union distinct refactoring. I've fixed it and also enables the enable_global_with_statement by default to test.

@amosbird
amosbird force-pushed the betterwith branch 2 times, most recently from ec2884f to 366e601 Compare January 6, 2021 17:59
@alexey-milovidov

Copy link
Copy Markdown
Member

For some reason all test_materialize_mysql_database have failed.

Comment thread src/Parsers/ASTSubquery.h Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing comment.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now the following query will lead to endless recursion and eventually fails due to memory limits:

SELECT *
FROM 
(
    WITH
        
        (
            SELECT 1
        ) AS r1,
        
        (
            SELECT 2
        ) AS r2,
        
        (
            SELECT 3
        ) AS r3
    SELECT 10
)
SETTINGS max_ast_elements = 1000, max_memory_usage = '10Mi'

And this is because of ExecuteScalarSubqueriesMatcher, that visits AST and create InterpreterSelectQuery for each subquery (ASTSubquery), while ApplyWithAliasVisitor will create more each time.

So how about the following patch?

$ git di src/Interpreters/ExecuteScalarSubqueriesVisitor.cpp
diff --git a/src/Interpreters/ExecuteScalarSubqueriesVisitor.cpp b/src/Interpreters/ExecuteScalarSubqueriesVisitor.cpp
index b3e8af90f2..4a6ec3505c 100644
--- a/src/Interpreters/ExecuteScalarSubqueriesVisitor.cpp
+++ b/src/Interpreters/ExecuteScalarSubqueriesVisitor.cpp
@@ -92,6 +92,12 @@ void ExecuteScalarSubqueriesMatcher::visit(const ASTSubquery & subquery, ASTPtr
         Settings subquery_settings = data.context.getSettings();
         subquery_settings.max_result_rows = 1;
         subquery_settings.extremes = false;
+        /// Otherwise ApplyWithAliasVisitor will be applied and this will
+        /// create new nodes in AST over and over and lead to endless recursion,
+        /// which eventually lead to one of the following errors:
+        /// - Memory limit exceeded
+        /// - AST too big
+        subquery_settings.enable_global_with_statement = false;
         subquery_context.setSettings(subquery_settings);
 
         ASTPtr subquery_select = subquery.children.at(0);

Also not that there is also DB::JoinedTables::resolveTables that will create InterpreterSelectQuery, and so it will also expand aliases one more time, which is pointless.

So maybe it is worth disabling enable_global_with_statement once ApplyWithAliasVisitor was applied (with changed=false)? Since there are also other places where interpreter will be created multiple times.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm fixing this and other related issues. Thanks for the suggestion. Actually we can just disable with propagation when subquery_depth is not zero.

@amosbird

Copy link
Copy Markdown
Collaborator Author

all test_materialize_mysql_database are still failing. @zhang2014 perhaps you can help...

@zhang2014

Copy link
Copy Markdown
Contributor

all test_materialize_mysql_database are still failing. @zhang2014 perhaps you can help...

fixes in #19344

@alexey-milovidov

Copy link
Copy Markdown
Member

Let's merge / rebase with master to rerun tests.

@amosbird

Copy link
Copy Markdown
Collaborator Author

fuzzer error looks real

https://clickhouse-test-reports.s3.yandex.net/18684/5998383a3b16a33c95dd71b3dd30614a0fbf5741/fuzzer_ubsan/report.html#fail1

SELECT gcd(-9223372036854775808, 1024)

Error on processing query 'SELECT gcd(-9223372036854775808, 1024)': Code: 32, e.displayText() = DB::Exception: Attempt to read after eof: while receiving packet from localhost:9000, Stack trace (when copying this message, always include the lines below):

0. ./obj-x86_64-linux-gnu/../contrib/libcxx/include/exception:133: Poco::Exception::Exception(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int) @ 0x1c18c972 in /workspace/clickhouse
1. ./obj-x86_64-linux-gnu/../src/Common/Exception.cpp:56: DB::Exception::Exception(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int, bool) @ 0xe3d2eda in /workspace/clickhouse
2. ./obj-x86_64-linux-gnu/../src/IO/VarInt.h:0: DB::throwReadAfterEOF() @ 0xe3ec8bb in /workspace/clickhouse
3. ./obj-x86_64-linux-gnu/../src/IO/VarInt.h:133: void DB::readVarUIntImpl<false>(unsigned long&, DB::ReadBuffer&) @ 0xe41998f in /workspace/clickhouse
4. ./obj-x86_64-linux-gnu/../src/Client/Connection.cpp:0: DB::Connection::receivePacket(std::__1::function<void (Poco::Net::Socket&)>) @ 0x1b20a726 in /workspace/clickhouse
5. ./obj-x86_64-linux-gnu/../programs/client/Client.cpp:0: DB::Client::receiveAndProcessPacket(bool) @ 0xe4e729a in /workspace/clickhouse
6. ./obj-x86_64-linux-gnu/../programs/client/Client.cpp:0: DB::Client::receiveResult() @ 0xe4edda2 in /workspace/clickhouse
7. ./obj-x86_64-linux-gnu/../programs/client/Client.cpp:0: DB::Client::processOrdinaryQuery() @ 0xe4d78b8 in /workspace/clickhouse
8. ./obj-x86_64-linux-gnu/../programs/client/Client.cpp:0: DB::Client::processParsedSingleQuery() @ 0xe4d3857 in /workspace/clickhouse
9. ./obj-x86_64-linux-gnu/../programs/client/Client.cpp:0: DB::Client::processWithFuzzing(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) @ 0xe4cdd49 in /workspace/clickhouse
10. ./obj-x86_64-linux-gnu/../programs/client/Client.cpp:0: DB::Client::processMultiQuery(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) @ 0xe4cfc90 in /workspace/clickhouse
11. ./obj-x86_64-linux-gnu/../programs/client/Client.cpp:0: DB::Client::nonInteractive()::'lambda'(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)::operator()(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) const @ 0xe4f1e71 in /workspace/clickhouse
12. ./obj-x86_64-linux-gnu/../programs/client/Client.cpp:0: DB::Client::nonInteractive() @ 0xe4c1a9f in /workspace/clickhouse
13. ./obj-x86_64-linux-gnu/../programs/client/Client.cpp:715: DB::Client::mainImpl() @ 0xe4ba2cd in /workspace/clickhouse
14. ./obj-x86_64-linux-gnu/../programs/client/Client.cpp:0: DB::Client::main(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&) @ 0xe4b36bb in /workspace/clickhouse
15. ./obj-x86_64-linux-gnu/../contrib/poco/Util/src/Application.cpp:0: Poco::Util::Application::run() @ 0x1c11719a in /workspace/clickhouse
16. ./obj-x86_64-linux-gnu/../programs/client/Client.cpp:0: mainEntryClickHouseClient(int, char**) @ 0xe4a6f84 in /workspace/clickhouse
17. ./obj-x86_64-linux-gnu/../programs/main.cpp:0: main @ 0xe3c6f14 in /workspace/clickhouse
18. __libc_start_main @ 0x270b3 in /usr/lib/x86_64-linux-gnu/libc-2.31.so
19. _start @ 0xe3a502e in /workspace/clickhouse
 (version 21.2.1.5803)

@azat

azat commented Jan 26, 2021

Copy link
Copy Markdown
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Can't find column after renaming a table twice with "WITH" clause and "AS" clause

5 participants