Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion prqlc/prqlc/src/sql/pq/positional_mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ impl PositionalMapper {
/// Reorder or remove columns to make `Union` happy.
pub(crate) fn apply_active_mapping(&mut self, output: Vec<CId>) -> Vec<CId> {
if let Some(mapping) = &self.active_positional_mapping {
// Check if the mapping indices are valid for the output
if mapping.iter().any(|idx| *idx >= output.len()) {
log::warn!(
"positional mapping indices out of bounds: mapping={mapping:?}, output_len={}",
output.len()
);
// If mapping is invalid, don't apply it
return output;
}

let new_output = mapping.iter().map(|idx| output[*idx]).collect();
log::debug!("remapping {output:?} to {new_output:?} via {mapping:?}");
new_output
Expand All @@ -48,9 +58,18 @@ impl PositionalMapper {
})
.collect();

if !self.relation_positional_mapping.contains_key(riid) {
// Only store the mapping if it's complete (all columns matched)
// If mapping is incomplete, it means the bottom relation hasn't been fully
// compiled yet, so we shouldn't apply any mapping
if mapping.len() == after.len() && !self.relation_positional_mapping.contains_key(riid) {
log::debug!(".. relation {riid:?} will be mapped: {mapping:?}");
self.relation_positional_mapping.insert(*riid, mapping);
} else if mapping.len() != after.len() {
log::debug!(
".. skipping incomplete mapping for {riid:?}: {}/{} columns matched",
mapping.len(),
after.len()
);
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions prqlc/prqlc/tests/integration/queries/append_cte_complex.prql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
prql target:sql.postgres

let invoices_wrap = (
from invoices
select { invoice_id, billing_country }
)

let employees_wrap = (
from employees
select { employee_id, country }
)

let invoices_final = (
from invoices_wrap
select { invoice_id, billing_country }
derive { source = f"{billing_country} from invoices" }
)

let employees_final = (
from employees_wrap
select { employee_id, country }
derive { source = f"{country} from employees" }
)

from invoices_final
append employees_final
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
source: prqlc/prqlc/tests/integration/queries.rs
expression: "prql target:sql.postgres\n\nlet invoices_wrap = (\n from invoices\n select { invoice_id, billing_country }\n)\n\nlet employees_wrap = (\n from employees\n select { employee_id, country }\n)\n\nlet invoices_final = (\n from invoices_wrap\n select { invoice_id, billing_country }\n derive { source = f\"{billing_country} from invoices\" }\n)\n\nlet employees_final = (\n from employees_wrap\n select { employee_id, country }\n derive { source = f\"{country} from employees\" }\n)\n\nfrom invoices_final\nappend employees_final\n"
input_file: prqlc/prqlc/tests/integration/queries/append_cte_complex.prql
snapshot_kind: text
---
WITH invoices_wrap AS (
SELECT
invoice_id,
billing_country
FROM
invoices
),
invoices_final AS (
SELECT
invoice_id,
billing_country,
CONCAT(billing_country, ' from invoices') AS source
FROM
invoices_wrap
),
employees_wrap AS (
SELECT
employee_id,
country
FROM
employees
)
SELECT
invoice_id,
billing_country,
source
FROM
invoices_final
UNION
ALL
SELECT
employee_id,
country,
CONCAT(country, ' from employees') AS source
FROM
employees_wrap
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
source: prqlc/prqlc/tests/integration/queries.rs
expression: "from invoices\nselect { customer_id, invoice_id, billing_country }\ntake 10..15\nappend (\n from invoices\n select { customer_id, invoice_id, billing_country }\n take 40..45\n)\nselect { billing_country, invoice_id }\n"
input_file: prqlc/prqlc/tests/integration/queries/append_select.prql
snapshot_kind: text
---
SELECT
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
source: prqlc/prqlc/tests/integration/queries.rs
expression: "from invoices\nderive total = case [total < 10 => total * 2, true => total]\nselect { customer_id, invoice_id, total }\ntake 5\nappend (\n from invoice_items\n derive unit_price = case [unit_price < 1 => unit_price * 2, true => unit_price]\n select { invoice_line_id, invoice_id, unit_price }\n take 5\n)\nselect { a = customer_id * 2, b = math.round 1 (invoice_id * total) }\n"
input_file: prqlc/prqlc/tests/integration/queries/append_select_compute.prql
snapshot_kind: text
---
WITH table_1 AS (
SELECT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
source: prqlc/prqlc/tests/integration/queries.rs
expression: "from invoices\nselect { customer_id, invoice_id, billing_country }\ntake 5\nappend (\n from employees\n select { employee_id, employee_id, country }\n take 5\n)\nappend (\n from invoice_items\n select { invoice_line_id, invoice_id, null }\n take 5\n)\nselect { billing_country, invoice_id }\n"
input_file: prqlc/prqlc/tests/integration/queries/append_select_multiple_with_null.prql
snapshot_kind: text
---
SELECT
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
source: prqlc/prqlc/tests/integration/queries.rs
expression: "prql target:sql.postgres\n\nlet invoices_wrap = (\n from invoices\n select { invoice_id, billing_country }\n)\n\nlet employees_wrap = (\n from employees\n select { employee_id, country }\n)\n\nlet invoices_final = (\n from invoices_wrap\n select { invoice_id, billing_country }\n derive { source = f\"{billing_country} from invoices\" }\n)\n\nlet employees_final = (\n from employees_wrap\n select { employee_id, country }\n derive { source = f\"{country} from employees\" }\n)\n\nfrom invoices_final\nappend employees_final\n"
input_file: prqlc/prqlc/tests/integration/queries/append_cte_complex.prql
snapshot_kind: text
---
--- generic
+++ sqlite
@@ -2,21 +2,21 @@
SELECT
invoice_id,
billing_country
FROM
invoices
),
invoices_final AS (
SELECT
invoice_id,
billing_country,
- CONCAT(billing_country, ' from invoices') AS source
+ billing_country || ' from invoices' AS source
FROM
invoices_wrap
),
employees_wrap AS (
SELECT
employee_id,
country
FROM
employees
)
@@ -24,13 +24,13 @@
invoice_id,
billing_country,
source
FROM
invoices_final
UNION
ALL
SELECT
employee_id,
country,
- CONCAT(country, ' from employees') AS source
+ country || ' from employees' AS source
FROM
employees_wrap
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
source: prqlc/prqlc/tests/integration/queries.rs
expression: "from invoices\nselect { customer_id, invoice_id, billing_country }\ntake 10..15\nappend (\n from invoices\n select { customer_id, invoice_id, billing_country }\n take 40..45\n)\nselect { billing_country, invoice_id }\n"
input_file: prqlc/prqlc/tests/integration/queries/append_select.prql
snapshot_kind: text
---
--- generic
+++ postgres
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
source: prqlc/prqlc/tests/integration/queries.rs
expression: "from invoices\nderive total = case [total < 10 => total * 2, true => total]\nselect { customer_id, invoice_id, total }\ntake 5\nappend (\n from invoice_items\n derive unit_price = case [unit_price < 1 => unit_price * 2, true => unit_price]\n select { invoice_line_id, invoice_id, unit_price }\n take 5\n)\nselect { a = customer_id * 2, b = math.round 1 (invoice_id * total) }\n"
input_file: prqlc/prqlc/tests/integration/queries/append_select_compute.prql
snapshot_kind: text
---
--- generic
+++ glaredb
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
source: prqlc/prqlc/tests/integration/queries.rs
expression: "from invoices\nselect { customer_id, invoice_id, billing_country }\ntake 5\nappend (\n from employees\n select { employee_id, employee_id, country }\n take 5\n)\nappend (\n from invoice_items\n select { invoice_line_id, invoice_id, null }\n take 5\n)\nselect { billing_country, invoice_id }\n"
input_file: prqlc/prqlc/tests/integration/queries/append_select_multiple_with_null.prql
snapshot_kind: text
---
--- generic
+++ postgres
Expand Down
Loading
Loading