Skip to content

Commit 33401ff

Browse files
authored
fix(postprocess): when redirecting sort columns, look up the relation from the matching table_ref (#5486)
Signed-off-by: Luka Peschke <[email protected]>
1 parent ec6b118 commit 33401ff

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

prqlc/prqlc/src/sql/pq/postprocess.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,15 +209,26 @@ impl PqFold for SortingInference<'_> {
209209
cid_redirects_to_add.push((*old_cid, new_cid, col));
210210
}
211211

212-
let riid = RIId::from(cte.tid.get());
213-
let relation_instance =
214-
self.ctx.anchor.relation_instances.get_mut(&riid).unwrap();
212+
let (riid, relation_instance) = self
213+
.ctx
214+
.anchor
215+
.relation_instances
216+
.iter_mut()
217+
.find(|(_riid, rel_inst)| rel_inst.table_ref.source == cte.tid)
218+
.unwrap();
215219

216220
cid_redirects_to_add
217221
.into_iter()
218222
.for_each(|(old_cid, new_cid, col)| {
219-
let def = ColumnDecl::RelationColumn(riid, new_cid, col);
223+
let def = ColumnDecl::RelationColumn(*riid, new_cid, col);
224+
220225
self.ctx.anchor.column_decls.insert(new_cid, def);
226+
log::debug!(
227+
"-- redirecting {old_cid:?} to {new_cid:?} for CTE {cte:?} (RIId: {riid:?})",
228+
cte = cte.tid,
229+
riid = riid
230+
);
231+
221232
relation_instance.cid_redirects.insert(old_cid, new_cid);
222233
});
223234
}

prqlc/prqlc/tests/integration/sql.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6115,6 +6115,55 @@ fn test_sort_cast_filter_join_select() {
61156115
")
61166116
}
61176117

6118+
/// Ensures that the sort happens on `table1`.`artist_id` and not on `table2`.`artist_id`
6119+
#[test]
6120+
fn test_sort_filter_derive_join_select() {
6121+
assert_snapshot!(compile(r###"
6122+
from albums
6123+
sort this.`artist_id`
6124+
filter (this.`artist_id` != null)
6125+
derive { `artist_id` = as `double precision` this.`artist_id` }
6126+
join side:left (from artists
6127+
select {`artist_id_right` = this.`artist_id`}
6128+
) (this.`artist_id` == that.`artist_id_right`)
6129+
select {this.`artist_id`, this.`title`}
6130+
"###
6131+
).unwrap(), @r"
6132+
WITH table_2 AS (
6133+
SELECT
6134+
CAST(artist_id AS double precision) AS artist_id,
6135+
title,
6136+
artist_id AS _expr_0
6137+
FROM
6138+
albums
6139+
WHERE
6140+
artist_id IS NOT NULL
6141+
),
6142+
table_1 AS (
6143+
SELECT
6144+
artist_id,
6145+
title,
6146+
_expr_0
6147+
FROM
6148+
table_2
6149+
),
6150+
table_0 AS (
6151+
SELECT
6152+
artist_id AS artist_id_right
6153+
FROM
6154+
artists
6155+
)
6156+
SELECT
6157+
table_1.artist_id,
6158+
table_1.title
6159+
FROM
6160+
table_1
6161+
LEFT OUTER JOIN table_0 ON table_1.artist_id = table_0.artist_id_right
6162+
ORDER BY
6163+
table_1._expr_0
6164+
")
6165+
}
6166+
61186167
/// Ensures that the sort happens on `table0`.`artist_id` and not on `table0`.`double_artist_id`
61196168
#[test]
61206169
fn test_sort_cast_filter_join_select_with_alias() {

0 commit comments

Comments
 (0)