Skip to content

Memory limit exceeded when parsing a simple query with with and subqueries #20388

Closed

Description

@adrian17

Describe the bug
Clickhouse fails to execute with a memory error when trying to parse it.

Does it reproduce on recent release?
Reproduces on v21.2.2.8, not on v21.1.2.15.

How to reproduce
Just enter the following query; don't need to create any tables:

WITH
    (SELECT count(distinct colU) from tabA) AS withA,
    (SELECT count(distinct colU) from tabA) AS withB
SELECT
    withA / withB AS ratio
FROM 
(
    SELECT
        date AS period,
        colX
    FROM 
    (
        SELECT
            date,
            if(colA IN (SELECT colB FROM tabC), 0, colA) AS colX
        FROM tabB
    ) AS tempB
    GROUP BY period, colX
) AS main

Result with stack trace:

clickhouse_driver.errors.ServerException: Code: 241.
DB::Exception: Memory limit (for query) exceeded: would use 9.32 GiB (attempt to allocate chunk of 4194384 bytes), maximum: 9.31 GiB. Stack trace:

0. DB::Exception::Exception<char const*, char const*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, long&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >(int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, char const*&&, char const*&&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&&, long&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&&) @ 0x84eaa95 in /usr/bin/clickhouse
1. MemoryTracker::alloc(long) @ 0x84ea3ed in /usr/bin/clickhouse
2. MemoryTracker::alloc(long) @ 0x84ea154 in /usr/bin/clickhouse
3. DB::ASTSubquery::clone() const @ 0x1145b4a4 in /usr/bin/clickhouse
4. DB::ASTExpressionList::clone() const @ 0x1142dbc2 in /usr/bin/clickhouse
5. DB::ASTSelectQuery::clone() const @ 0x11448c9b in /usr/bin/clickhouse
6. DB::ASTExpressionList::clone() const @ 0x1142dbc2 in /usr/bin/clickhouse
7. DB::ASTSelectWithUnionQuery::clone() const @ 0x11451726 in /usr/bin/clickhouse
8. DB::ASTSubquery::clone() const @ 0x1145b691 in /usr/bin/clickhouse
9. DB::ASTExpressionList::clone() const @ 0x1142dbc2 in /usr/bin/clickhouse
10. DB::ASTSelectQuery::clone() const @ 0x11448c9b in /usr/bin/clickhouse
11. DB::ASTExpressionList::clone() const @ 0x1142dbc2 in /usr/bin/clickhouse
12. DB::ASTSelectWithUnionQuery::clone() const @ 0x11451726 in /usr/bin/clickhouse
13. DB::ASTSubquery::clone() const @ 0x1145b691 in /usr/bin/clickhouse
14. DB::ASTExpressionList::clone() const @ 0x1142dbc2 in /usr/bin/clickhouse
15. DB::ASTSelectQuery::clone() const @ 0x11448c9b in /usr/bin/clickhouse
16. DB::ASTExpressionList::clone() const @ 0x1142dbc2 in /usr/bin/clickhouse
17. DB::ASTSelectWithUnionQuery::clone() const @ 0x11451726 in /usr/bin/clickhouse
18. DB::ASTSubquery::clone() const @ 0x1145b691 in /usr/bin/clickhouse
19. DB::ASTExpressionList::clone() const @ 0x1142dbc2 in /usr/bin/clickhouse
20. DB::ASTSelectQuery::clone() const @ 0x11448c9b in /usr/bin/clickhouse
21. DB::ASTExpressionList::clone() const @ 0x1142dbc2 in /usr/bin/clickhouse
22. DB::ASTSelectWithUnionQuery::clone() const @ 0x11451726 in /usr/bin/clickhouse
23. DB::ASTSubquery::clone() const @ 0x1145b691 in /usr/bin/clickhouse
24. DB::ASTExpressionList::clone() const @ 0x1142dbc2 in /usr/bin/clickhouse
25. DB::ASTSelectQuery::clone() const @ 0x11448c9b in /usr/bin/clickhouse
26. DB::ASTExpressionList::clone() const @ 0x1142dbc2 in /usr/bin/clickhouse
27. DB::ASTSelectWithUnionQuery::clone() const @ 0x11451726 in /usr/bin/clickhouse
28. DB::ASTSubquery::clone() const @ 0x1145b691 in /usr/bin/clickhouse
29. DB::ASTExpressionList::clone() const @ 0x1142dbc2 in /usr/bin/clickhouse
30. DB::ASTSelectQuery::clone() const @ 0x11448c9b in /usr/bin/clickhouse
31. DB::ASTExpressionList::clone() const @ 0x1142dbc2 in /usr/bin/clickhouse

Activity

  1. added
    bug
    on Feb 12, 2021
  2. SaltTan commented on Feb 12, 2021

    @SaltTan
    Contributor

    Interesting

    0 rows in set. Elapsed: 62.633 sec.

    Received exception from server (version 21.2.2):
    Code: 60. DB::Exception: Received from localhost:9000. DB::Exception: Table db.tabB doesn't exist.

    2021.02.12 21:22:50.672543 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Debug> executeQuery: (from [::1]:42136, using production parser) WITH (SELECT count(distinct colU) from tabA) AS withA, (SELECT count(distinct colU) from tabA) AS withB SELECT withA / withB AS ratio FROM ( SELECT date AS period, colX FROM ( SELECT date, if(colA IN (SELECT colB FROM tabC), 0, colA) AS colX FROM tabB ) AS tempB GROUP BY period, colX ) AS main
    2021.02.12 21:22:52.172187 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Debug> MemoryTracker: Current memory usage (for query): 1.00 GiB.
    2021.02.12 21:22:53.659389 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Debug> MemoryTracker: Current memory usage (for query): 2.00 GiB.
    2021.02.12 21:22:55.154235 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Debug> MemoryTracker: Current memory usage (for query): 3.00 GiB.
    2021.02.12 21:22:56.499257 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Debug> MemoryTracker: Current memory usage (total): 12.00 GiB.
    2021.02.12 21:22:56.653397 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Debug> MemoryTracker: Current memory usage (for query): 4.00 GiB.
    2021.02.12 21:22:57.953209 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Debug> MemoryTracker: Current memory usage (total): 13.00 GiB.
    2021.02.12 21:22:58.103359 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Debug> MemoryTracker: Current memory usage (for query): 5.00 GiB.
    2021.02.12 21:22:59.445437 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Debug> MemoryTracker: Current memory usage (total): 14.00 GiB.
    2021.02.12 21:22:59.603553 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Debug> MemoryTracker: Current memory usage (for query): 6.00 GiB.
    2021.02.12 21:23:00.950940 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Debug> MemoryTracker: Current memory usage (total): 15.00 GiB.
    2021.02.12 21:23:01.127453 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Debug> MemoryTracker: Current memory usage (for query): 7.00 GiB.
    2021.02.12 21:23:02.915988 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Debug> MemoryTracker: Current memory usage (total): 16.00 GiB.
    2021.02.12 21:23:03.193529 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Debug> MemoryTracker: Current memory usage (for query): 8.00 GiB.
    2021.02.12 21:23:06.901767 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Debug> MemoryTracker: Current memory usage (for query): 9.00 GiB.
    2021.02.12 21:23:10.094168 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Debug> MemoryTracker: Current memory usage (total): 18.00 GiB.
    2021.02.12 21:23:10.446270 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Debug> MemoryTracker: Current memory usage (for query): 10.00 GiB.
    2021.02.12 21:23:13.360211 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Debug> MemoryTracker: Current memory usage (total): 19.00 GiB.
    2021.02.12 21:23:14.909005 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Debug> MemoryTracker: Current memory usage (for query): 11.00 GiB.
    2021.02.12 21:23:16.440613 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Debug> MemoryTracker: Current memory usage (total): 20.00 GiB.
    2021.02.12 21:23:17.210864 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Debug> MemoryTracker: Current memory usage (total): 21.00 GiB.
    2021.02.12 21:23:17.794105 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Debug> MemoryTracker: Current memory usage (for query): 12.00 GiB.
    2021.02.12 21:23:20.640834 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Debug> MemoryTracker: Current memory usage (for query): 13.00 GiB.
    2021.02.12 21:23:21.940124 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Debug> MemoryTracker: Current memory usage (total): 22.00 GiB.
    2021.02.12 21:23:22.664184 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Debug> MemoryTracker: Current memory usage (for query): 14.00 GiB.
    2021.02.12 21:23:23.179032 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Debug> MemoryTracker: Current memory usage (total): 23.00 GiB.
    2021.02.12 21:23:24.179573 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Debug> MemoryTracker: Current memory usage (for query): 15.00 GiB.
    2021.02.12 21:23:25.139332 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Debug> MemoryTracker: Current memory usage (total): 24.00 GiB.
    2021.02.12 21:23:25.701894 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Debug> MemoryTracker: Current memory usage (for query): 16.00 GiB.
    2021.02.12 21:23:26.639777 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Debug> MemoryTracker: Current memory usage (total): 25.00 GiB.
    2021.02.12 21:23:27.222390 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Debug> MemoryTracker: Current memory usage (for query): 17.00 GiB.
    2021.02.12 21:23:28.022854 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Debug> MemoryTracker: Current memory usage (total): 26.00 GiB.
    2021.02.12 21:23:28.756649 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Debug> MemoryTracker: Current memory usage (for query): 18.00 GiB.
    2021.02.12 21:23:28.963369 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Debug> MemoryTracker: Current memory usage (total): 27.00 GiB.
    2021.02.12 21:23:30.344044 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Debug> MemoryTracker: Current memory usage (for query): 19.00 GiB.
    2021.02.12 21:23:31.055692 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Debug> MemoryTracker: Current memory usage (total): 28.00 GiB.
    2021.02.12 21:23:31.871447 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Debug> MemoryTracker: Current memory usage (for query): 20.00 GiB.
    2021.02.12 21:23:32.595962 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Debug> MemoryTracker: Current memory usage (total): 29.00 GiB.
    2021.02.12 21:23:33.403823 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Debug> MemoryTracker: Current memory usage (for query): 21.00 GiB.
    2021.02.12 21:23:53.295554 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Error> executeQuery: Code: 60, e.displayText() = DB::Exception: Table db.tabB doesn't exist (version 21.2.2.8 (official build)) (from [::1]:42136) (in query: WITH (SELECT count(distinct colU) from tabA) AS withA, (SELECT count(distinct colU) from tabA) AS withB SELECT withA / withB AS ratio FROM ( SELECT date AS period, colX FROM ( SELECT date, if(colA IN (SELECT colB FROM tabC), 0, colA) AS colX FROM tabB ) AS tempB GROUP BY period, colX ) AS main), Stack trace (when copying this message, always include the lines below):
    2021.02.12 21:23:53.304292 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Error> TCPHandler: Code: 60, e.displayText() = DB::Exception: Table db.tabB doesn't exist, Stack trace:
    2021.02.12 21:23:53.304411 [ 17530 ] {23997cf8-7906-429e-a890-2f79ca06a4e2} <Debug> MemoryTracker: Peak memory usage (for query): 21.10 GiB.
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugperformance

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions