Skip to content

fix(sql): fix unexpected columns being added to the SQL projection from order by expression#5748

Merged
bluestreak01 merged 3 commits intomasterfrom
vi_order_by
Jun 17, 2025
Merged

fix(sql): fix unexpected columns being added to the SQL projection from order by expression#5748
bluestreak01 merged 3 commits intomasterfrom
vi_order_by

Conversation

@bluestreak01
Copy link
Copy Markdown
Member

@bluestreak01 bluestreak01 commented Jun 16, 2025

These SQLs on Demo generate unexpected projections

  1. order by duplicates expression already used in the projection, wildcard usage is the culprit:
-- order by duplicates columns and creates invalid timestamp index
WITH
  offsets AS (
    SELECT offs*100000 AS usec_offs, offs
    FROM (
      SELECT (x-51) AS offs
      FROM long_sequence(101)
    )
  ),
  orders AS (
    SELECT *  
    FROM trades
    WHERE timestamp IN '2025-05-21'
    LIMIT 10
  ),
  points AS (
    SELECT orders.*, offsets.usec_offs*0.001 AS msec_offs, offs, timestamp + usec_offs AS tsXYZ
    FROM orders CROSS JOIN offsets
    ORDER BY timestamp + usec_offs ASC
  )
select * from points;
  1. order by uses expression not referenced in the projection, again, wildcard is the culprit:
WITH
  offsets AS (
    SELECT offs*100000 AS usec_offs, offs
    FROM (
      SELECT (x-51) AS offs
      FROM long_sequence(101)
    )
  ),
  orders AS (
    SELECT *  
    FROM trades
    WHERE timestamp IN '2025-05-21'
    LIMIT 10
  ),
  points AS (
    SELECT orders.*, offsets.usec_offs*0.001 AS msec_offs, offs, timestamp + usec_offs AS tsXYZ
    FROM orders CROSS JOIN offsets
    ORDER BY timestamp + offs ASC
  )
select * from points;

@glasstiger
Copy link
Copy Markdown
Contributor

[PR Coverage check]

😍 pass : 11 / 11 (100.00%)

file detail

path covered line new line coverage
🔵 io/questdb/griffin/model/QueryColumn.java 2 2 100.00%
🔵 io/questdb/griffin/SqlOptimiser.java 9 9 100.00%

@bluestreak01 bluestreak01 merged commit 4059529 into master Jun 17, 2025
37 checks passed
@bluestreak01 bluestreak01 deleted the vi_order_by branch June 17, 2025 12:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants