prql icon indicating copy to clipboard operation
prql copied to clipboard

Order of select columns is wrong in complex `select` clauses

Open YuxiLing opened this issue 1 year ago • 0 comments

What happened?

I have an issue with multiple select clauses in a query. The output SQL query for the following PRQL input is not correct with the error message, "no such column: _expr_0". It does not always happen but exists in complex and redundant select clauses.

PRQL input

from tb_0
take  100
select {cc0 = c1,cc1 = c2,cc2 = c1}
select {ccc0 = cc1,ccc1 = 1}
select {cccc0 = 1,cccc1 = ccc0,cccc3 = 1,cccc4 = 0,cccc5 = 0,cccc6 = 0,cccc7 = 0}
derive {cccc8 = 0,cccc9 = 0,cccc10 = 0}

SQL output

WITH table_0 AS (
  SELECT
    _expr_0 AS cccc1,
    1 AS cccc3,
    0 AS cccc4,
    0 AS cccc5,
    0 AS cccc6,
    0 AS cccc7,
    c2 AS _expr_0
  FROM
    tb_0
  LIMIT
    100
)
SELECT
  1 AS cccc0,
  cccc1,
  cccc3,
  cccc4,
  cccc5,
  cccc6,
  cccc7,
  0 AS cccc8,
  0 AS cccc9,
  0 AS cccc10
FROM
  table_0

-- Generated by PRQL compiler version:0.13.2 (https://prql-lang.org)

Expected SQL output

WITH table_0 AS (
  SELECT
    c2 AS _expr_0,
    _expr_0 AS cccc1,
    1 AS cccc3,
    0 AS cccc4,
    0 AS cccc5,
    0 AS cccc6,
    0 AS cccc7
  FROM
    tb_0
  LIMIT
    100
)
SELECT
  1 AS cccc0,
  cccc1,
  cccc3,
  cccc4,
  cccc5,
  cccc6,
  cccc7,
  0 AS cccc8,
  0 AS cccc9,
  0 AS cccc10
FROM
  table_0

MVCE confirmation

  • [X] Minimal example
  • [X] New issue

Anything else?

No response

YuxiLing avatar Nov 05 '24 07:11 YuxiLing