Skip to content

Hybrid engine returns incorrect values for ALIAS columns when ORDER BY is used #1290

@alsugiliazova

Description

@alsugiliazova

Describe the bug

Introduced by #1272

Order by replaced column values (computed and threshold):

SELECT
    id,
    value,
    computed,
    threshold
FROM hybrid_table_2e23dfe0_efca_11f0_b5a8_de7b9eea3490
ORDER BY id ASC
   ┌─id─┬─value─┬─computed─┬─threshold─┐
1. │  1 │    30 │       50 │        60 │
2. │  2 │    25 │       50 │        50 │
3. │  3 │    20 │       50 │        40 │
4. │  4 │    15 │       50 │        30 │
5. │  5 │    10 │       50 │        20 │
6. │  6 │    40 │       50 │        80 │
   └────┴───────┴──────────┴───────────┘

6 rows in set. Elapsed: 0.008 sec. 
SELECT
    id,
    value,
    computed,
    threshold
FROM hybrid_table_2e23dfe0_efca_11f0_b5a8_de7b9eea3490
   ┌─id─┬─value─┬─computed─┬─threshold─┐
1. │  4 │    15 │       30 │        50 │
2. │  5 │    10 │       20 │        50 │
3. │  6 │    40 │       80 │        50 │
4. │  1 │    30 │       60 │        50 │
5. │  2 │    25 │       50 │        50 │
6. │  3 │    20 │       40 │        50 │
   └────┴───────┴──────────┴───────────┘

6 rows in set. Elapsed: 0.010 sec. 
SHOW TABLES

Query id: d8b73f60-4f95-4eaf-b85a-c1e6a89e5f90

   ┌─name──────────────────────────────────────────────┐
1. │ hybrid_table_2e23dfe0_efca_11f0_b5a8_de7b9eea3490 │
2. │ left_table_2e0effa8_efca_11f0_b5a8_de7b9eea3490   │
3. │ right_table_2e0f000c_efca_11f0_b5a8_de7b9eea3490  │
   └───────────────────────────────────────────────────┘

3 rows in set. Elapsed: 0.001 sec. 


SHOW CREATE TABLE left_table_2e0effa8_efca_11f0_b5a8_de7b9eea3490

Query id: e132c30e-94cd-4892-9ecc-82a94273ed0e

   ┌─statement────────────────────────────────────────────────────────────┐
1. │ CREATE TABLE default.left_table_2e0effa8_efca_11f0_b5a8_de7b9eea3490↴│
   │↳(                                                                   ↴│
   │↳    `id` Int32,                                                     ↴│
   │↳    `value` Int32,                                                  ↴│
   │↳    `date_col` Date,                                                ↴│
   │↳    `computed` Int64 ALIAS value * 2,                               ↴│
   │↳    `threshold` UInt8 ALIAS 50                                      ↴│
   │↳)                                                                   ↴│
   │↳ENGINE = MergeTree                                                  ↴│
   │↳PARTITION BY toYYYYMM(date_col)                                     ↴│
   │↳ORDER BY (date_col, id)                                             ↴│
   │↳SETTINGS index_granularity = 8192                                    │
   └──────────────────────────────────────────────────────────────────────┘

1 row in set. Elapsed: 0.002 sec. 



SELECT
    id,
    value,
    date_col,
    computed,
    threshold
FROM left_table_2e0effa8_efca_11f0_b5a8_de7b9eea3490

Query id: 2b408605-6c62-4e2a-a91a-988626374136

   ┌─id─┬─value─┬───date_col─┬─computed─┬─threshold─┐
1. │  1302025-01-1560502. │  2252025-01-1650503. │  3202025-01-174050 │
   └────┴───────┴────────────┴──────────┴───────────┘

3 rows in set. Elapsed: 0.002 sec. 


SHOW CREATE TABLE right_table_2e0f000c_efca_11f0_b5a8_de7b9eea3490

Query id: 938783c4-137a-4196-9d91-baeeedd1c9c5

   ┌─statement─────────────────────────────────────────────────────────────┐
1. │ CREATE TABLE default.right_table_2e0f000c_efca_11f0_b5a8_de7b9eea3490↴│
   │↳(                                                                    ↴│
   │↳    `id` Int32,                                                      ↴│
   │↳    `value` Int32,                                                   ↴│
   │↳    `date_col` Date,                                                 ↴│
   │↳    `computed` Int64 ALIAS value * 2,                                ↴│
   │↳    `threshold` UInt8 ALIAS 50                                       ↴│
   │↳)                                                                    ↴│
   │↳ENGINE = MergeTree                                                   ↴│
   │↳PARTITION BY toYYYYMM(date_col)                                      ↴│
   │↳ORDER BY (date_col, id)                                              ↴│
   │↳SETTINGS index_granularity = 8192                                     │
   └───────────────────────────────────────────────────────────────────────┘

1 row in set. Elapsed: 0.003 sec. 


SELECT
    id,
    value,
    date_col,
    computed,
    threshold
FROM right_table_2e0f000c_efca_11f0_b5a8_de7b9eea3490

Query id: 747f3a0c-4520-43ff-affc-11f3f74da178

   ┌─id─┬─value─┬───date_col─┬─computed─┬─threshold─┐
1. │  4152025-01-1030502. │  5102025-01-1120503. │  6402025-01-128050 │
   └────┴───────┴────────────┴──────────┴───────────┘

3 rows in set. Elapsed: 0.005 sec. 


SHOW CREATE TABLE hybrid_table_2e23dfe0_efca_11f0_b5a8_de7b9eea3490

Query id: 89221a88-8bc9-472a-a67e-fbf51f264a98

   ┌─statement───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
1. │ CREATE TABLE default.hybrid_table_2e23dfe0_efca_11f0_b5a8_de7b9eea3490                                                                                                                                                                         ↴│
   │↳(                                                                                                                                                                                                                                              ↴│
   │↳    `id` Int32,                                                                                                                                                                                                                                ↴│
   │↳    `value` Int32,                                                                                                                                                                                                                             ↴│
   │↳    `date_col` Date,                                                                                                                                                                                                                           ↴│
   │↳    `computed` Int64 ALIAS value * 2,                                                                                                                                                                                                          ↴│
   │↳    `threshold` UInt8 ALIAS 50                                                                                                                                                                                                                 ↴│
   │↳)                                                                                                                                                                                                                                              ↴│
   │↳ENGINE = Hybrid(remote('localhost', 'default', 'left_table_2e0effa8_efca_11f0_b5a8_de7b9eea3490'), date_col >= '2025-01-15', remote('localhost', currentDatabase(), right_table_2e0f000c_efca_11f0_b5a8_de7b9eea3490), date_col < '2025-01-15') │
   └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

1 row in set. Elapsed: 0.003 sec. 

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions