Skip to content

Incorrect scalar subquery result #33825

Closed

Description

@Algunenano

Describe what's wrong

For some reason, the first column of a select is not returning the scalar result and instead returns something else:

CREATE TABLE t1 (i Int64, j Int64) ENGINE = Memory;
INSERT INTO t1 SELECT number, number FROM system.numbers LIMIT 10;
SELECT
        (SELECT max(i) FROM t1) as i,
        (SELECT max(i) FROM t1) as j,
        (SELECT max(i) FROM t1) as k,
        (SELECT max(i) FROM t1) as l
FROM t1
  • Result:
┌─i─┬─j─┬─k─┬─l─┐
│ 0 │ 9 │ 9 │ 9 │
│ 1 │ 9 │ 9 │ 9 │
│ 2 │ 9 │ 9 │ 9 │
│ 3 │ 9 │ 9 │ 9 │
│ 4 │ 9 │ 9 │ 9 │
│ 5 │ 9 │ 9 │ 9 │
│ 6 │ 9 │ 9 │ 9 │
│ 7 │ 9 │ 9 │ 9 │
│ 8 │ 9 │ 9 │ 9 │
│ 9 │ 9 │ 9 │ 9 │
└───┴───┴───┴───┘
  • Expected:
┌─i─┬─j─┬─k─┬─l─┐
│ 9 │ 9 │ 9 │ 9 │
│ 9 │ 9 │ 9 │ 9 │
│ 9 │ 9 │ 9 │ 9 │
│ 9 │ 9 │ 9 │ 9 │
│ 9 │ 9 │ 9 │ 9 │
│ 9 │ 9 │ 9 │ 9 │
│ 9 │ 9 │ 9 │ 9 │
│ 9 │ 9 │ 9 │ 9 │
│ 9 │ 9 │ 9 │ 9 │
│ 9 │ 9 │ 9 │ 9 │
└───┴───┴───┴───┘


This also happens if I use a CTE to declare once the subquery:

WITH (
        SELECT max(i)
        FROM t1
    ) AS value
SELECT
    value AS i,
    value AS j,
    value AS k,
    value AS l
FROM t1

Query id: 8a85ed02-0ad7-4fe4-b01f-eabbb7521d52

┌─i─┬─j─┬─k─┬─l─┐
│ 0 │ 9 │ 9 │ 9 │
│ 1 │ 9 │ 9 │ 9 │
│ 2 │ 9 │ 9 │ 9 │
│ 3 │ 9 │ 9 │ 9 │
│ 4 │ 9 │ 9 │ 9 │
│ 5 │ 9 │ 9 │ 9 │
│ 6 │ 9 │ 9 │ 9 │
│ 7 │ 9 │ 9 │ 9 │
│ 8 │ 9 │ 9 │ 9 │
│ 9 │ 9 │ 9 │ 9 │
└───┴───┴───┴───┘

Does it reproduce on recent release?

Tested on 20.7 (the oldest I keep around) and master and both exhibit the same behaviour so I guess everything is affected.

On master, describing the table shows that the first column didn't receive the scalar replacement, otherwise it would have been Nullable(Int64):

DESCRIBE TABLE 
(
    SELECT
        (
            SELECT max(i)
            FROM t1
        ) AS i,
        (
            SELECT max(i)
            FROM t1
        ) AS j,
        (
            SELECT max(i)
            FROM t1
        ) AS k,
        (
            SELECT max(i)
            FROM t1
        ) AS l
    FROM t1
)

Query id: 0f58496d-7d51-40a1-8ae9-fd68b8e431b6

┌─name─┬─type────────────┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┬─ttl_expression─┐
│ i    │ Int64           │              │                    │         │                  │                │
│ j    │ Nullable(Int64) │              │                    │         │                  │                │
│ k    │ Nullable(Int64) │              │                    │         │                  │                │
│ l    │ Nullable(Int64) │              │                    │         │                  │                │
└──────┴─────────────────┴──────────────┴────────────────────┴─────────┴──────────────────┴────────────────┘

4 rows in set. Elapsed: 0.002 sec. 

Activity

  1. added
    potential bug
    on Jan 20, 2022
  2. Algunenano commented on Jan 20, 2022

    @Algunenano
    MemberAuthor

    It seems that the issue is because AS i has the same alias as the source table column name, and using other name leads to proper results:

    SELECT
                      (SELECT max(i) FROM t1) as c1,
                      (SELECT max(i) FROM t1) as j,
                      (SELECT max(i) FROM t1) as k,
                      (SELECT max(i) FROM t1) as l
              FROM t1
    
    SELECT
        (
            SELECT max(i)
            FROM t1
        ) AS c1,
        (
            SELECT max(i)
            FROM t1
        ) AS j,
        (
            SELECT max(i)
            FROM t1
        ) AS k,
        (
            SELECT max(i)
            FROM t1
        ) AS l
    FROM t1
    
    Query id: 749d8533-02ba-409c-aa8d-7f9af47fc848
    
    ┌─c1─┬─j─┬─k─┬─l─┐
    │  9 │ 9 │ 9 │ 9 │
    │  9 │ 9 │ 9 │ 9 │
    │  9 │ 9 │ 9 │ 9 │
    │  9 │ 9 │ 9 │ 9 │
    │  9 │ 9 │ 9 │ 9 │
    │  9 │ 9 │ 9 │ 9 │
    │  9 │ 9 │ 9 │ 9 │
    │  9 │ 9 │ 9 │ 9 │
    │  9 │ 9 │ 9 │ 9 │
    │  9 │ 9 │ 9 │ 9 │
    └────┴───┴───┴───┘
    

    Now, why that happens and why if only affects i and not j I still don't know.

  3. Algunenano commented on Jan 20, 2022

    @Algunenano
    MemberAuthor

    Even more weird:

    CREATE TABLE t2 (first_column Int64, second_column Int64) ENGINE = TinyLog;
    INSERT INTO t2 SELECT number, number FROM system.numbers LIMIT 10;
    
    
    SELECT (
            SELECT 111111111111
        ) AS first_column
    FROM t2
    
    Query id: e76c6d98-dffc-496a-9045-cf3c2391c66b
    
    ┌─first_column─┐
    │            0 │
    │            1 │
    │            2 │
    │            3 │
    │            4 │
    │            5 │
    │            6 │
    │            7 │
    │            8 │
    │            9 │
    └──────────────┘
    
    SELECT (
            SELECT 2222222222
        ) AS second_column
    FROM t2
    
    Query id: 03074d44-08df-46b6-a73c-2841e14a2ecc
    
    ┌─second_column─┐
    │    2222222222 │
    │    2222222222 │
    │    2222222222 │
    │    2222222222 │
    │    2222222222 │
    │    2222222222 │
    │    2222222222 │
    │    2222222222 │
    │    2222222222 │
    │    2222222222 │
    └───────────────┘

    It appears to be matching the first column from the table and only that column, all other seem to be fine.

  4. Algunenano commented on Jan 20, 2022

    @Algunenano
    MemberAuthor

    This one fails two, so the issue isn't with the first column or something like that:

    SELECT (
            SELECT 2222222222
        ) AS whatever
    FROM
    (
        SELECT number AS whatever
        FROM numbers(10)
    )
    

    The issue goes away if I disabled setting prefer_alias_to_column_name to 0 for that subquery so something, at some point of the executing, is using the alias as a column name from the source instead of using the AST that has replaced the subquery (a bunch of casts).

  5. Algunenano commented on Jan 21, 2022

    @Algunenano
    MemberAuthor

    I've been trying to understand how CH decides where it should get its data from and just to be nice I'll say it's really complex and messy. I'm not surprised there is a ticket (#23194) to say how things are (or were at some point in time) and how things should be.

    There are "a few" hacks, like prefer_alias_to_column_name or unique_column_name, to work around some of the issues that come from years of evolution and they are more complexity to the problem so getting a full picture of how things work it's pretty hard. It also seems to follow a bottom-up approach which makes discarding unused columns / subqueries harder.

    All of this is to say that a) I've been unable to fix this and will leave it here for a while and b) I think that CH would benefit from rethinking / rebuilding the interpreters with a full view of what should be possible and how things should work. If that ever happened I'd also include there the learnings from other databases to optimize queries. I really liked Materialize blog about decorrelation and I'd hope to have an internal structure able to do these kinds of things.

  6. added
    bug
    and removed
    potential bug
    on Apr 2, 2024
  7. fm4v commented on Apr 3, 2024

    @fm4v
    Member

    Seems to be fixed with new analyzer. @Algunenano could you please close the task if this is the expected behavior.

    SET allow_experimental_analyzer=1; -- enabled by default
    
    SELECT
        (
            SELECT max(i)
            FROM t1
        ) AS i,
        (
            SELECT max(i)
            FROM t1
        ) AS j,
        (
            SELECT max(i)
            FROM t1
        ) AS k,
        (
            SELECT max(i)
            FROM t1
        ) AS l
    FROM t1
    
    Query id: 21d60264-14eb-49c1-91d7-5568532632c4
    
        ┌─i─┬─j─┬─k─┬─l─┐
     1. │ 9 │ 9 │ 9 │ 9 │
     2. │ 9 │ 9 │ 9 │ 9 │
     3. │ 9 │ 9 │ 9 │ 9 │
     4. │ 9 │ 9 │ 9 │ 9 │
     5. │ 9 │ 9 │ 9 │ 9 │
     6. │ 9 │ 9 │ 9 │ 9 │
     7. │ 9 │ 9 │ 9 │ 9 │
     8. │ 9 │ 9 │ 9 │ 9 │
     9. │ 9 │ 9 │ 9 │ 9 │
    10. │ 9 │ 9 │ 9 │ 9 │
        └───┴───┴───┴───┘
    
    WITH (
            SELECT max(i)
            FROM t1
        ) AS value
    SELECT
        value AS i,
        value AS j,
        value AS k,
        value AS l
    FROM t1
    
    Query id: 66fde8a5-393d-4cc1-84d6-062c3d95de88
    
        ┌─i─┬─j─┬─k─┬─l─┐
     1. │ 9 │ 9 │ 9 │ 9 │
     2. │ 9 │ 9 │ 9 │ 9 │
     3. │ 9 │ 9 │ 9 │ 9 │
     4. │ 9 │ 9 │ 9 │ 9 │
     5. │ 9 │ 9 │ 9 │ 9 │
     6. │ 9 │ 9 │ 9 │ 9 │
     7. │ 9 │ 9 │ 9 │ 9 │
     8. │ 9 │ 9 │ 9 │ 9 │
     9. │ 9 │ 9 │ 9 │ 9 │
    10. │ 9 │ 9 │ 9 │ 9 │
        └───┴───┴───┴───┘
    
    SELECT (
            SELECT 111111111111
        ) AS first_column
    FROM t2
    
    Query id: 59cf4cff-8cbe-4e20-8b30-88833e6003c5
    
        ┌─first_column─┐
     1. │ 111111111111 │
     2. │ 111111111111 │
     3. │ 111111111111 │
     4. │ 111111111111 │
     5. │ 111111111111 │
     6. │ 111111111111 │
     7. │ 111111111111 │
     8. │ 111111111111 │
     9. │ 111111111111 │
    10. │ 111111111111 │
        └──────────────┘
    
    SELECT (
            SELECT 2222222222
        ) AS second_column
    FROM t2
    
    Query id: d4a42d53-2b44-4c55-933a-3bb80f42731a
    
        ┌─second_column─┐
     1. │    2222222222 │
     2. │    2222222222 │
     3. │    2222222222 │
     4. │    2222222222 │
     5. │    2222222222 │
     6. │    2222222222 │
     7. │    2222222222 │
     8. │    2222222222 │
     9. │    2222222222 │
    10. │    2222222222 │
        └───────────────┘
    
    
    
  8. Algunenano commented on Apr 3, 2024

    @Algunenano
    MemberAuthor

    LGTM

  9. den-crane commented on Apr 3, 2024

    @den-crane
    Contributor

    It's the interesting case. I would add a test.

  10. added a commit that references this issue on Apr 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugminor

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions