diff --git a/CHANGELOG.md b/CHANGELOG.md index 28b76a46f4e4..52f0d88f3d43 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,10 @@ - `prqlc fmt` keeps the parentheses around an aliased parameter default, such as `func x y:(a = 1) -> x + y`. Dropping them produced output that failed to parse. (@prql-bot, #6539) +- A table variable used as a value in `derive`, `select`, `group`, `sort` or a + `case` branch reports `table variable cannot be used as a scalar value`, as it + already did as an operator argument. `derive x = t` previously compiled to the + bare name `t`. (@prql-bot, #6525) - Joining a pipeline that selects two or more unnamed expressions references its later columns through the joined CTE: `x` in `join (from u | select {a + 1, b + 1, x}) (==x)` previously compiled to the diff --git a/prqlc/prqlc/src/semantic/lowering.rs b/prqlc/prqlc/src/semantic/lowering.rs index ac2fcf0ca922..4bc4640a2e25 100644 --- a/prqlc/prqlc/src/semantic/lowering.rs +++ b/prqlc/prqlc/src/semantic/lowering.rs @@ -902,6 +902,8 @@ impl Lowerer { let cid = self.lookup_cid(id, Some(&ident.name)).with_span(span)?; rq::ExprKind::ColumnRef(cid) + } else if expr.ty.as_ref().is_some_and(|x| x.is_relation()) { + return Err(table_variable_as_scalar(span)); } else { // fallback: unresolved ident // Let's hope that the database engine can resolve it. @@ -953,11 +955,7 @@ impl Lowerer { // Check for relation types used as operator arguments for arg in &args { if arg.ty.as_ref().is_some_and(|x| x.is_relation()) { - return Err(Error::new_simple( - "table variable cannot be used as a scalar value", - ) - .push_hint("use a join instead, or inline the subquery") - .with_span(arg.span)); + return Err(table_variable_as_scalar(arg.span)); } } @@ -1011,6 +1009,22 @@ impl Lowerer { .map(|i| { Ok(match i { InterpolateItem::String(s) => InterpolateItem::String(s), + // A table variable interpolated into an s-string is + // written as its name, for the database to resolve. + InterpolateItem::Expr { expr, .. } + if expr.kind.is_ident() + && expr.target_id.is_none() + && expr.ty.as_ref().is_some_and(|x| x.is_relation()) => + { + let name = expr.kind.into_ident().unwrap().name; + InterpolateItem::Expr { + expr: Box::new(rq::Expr { + kind: rq::ExprKind::SString(vec![InterpolateItem::String(name)]), + span: expr.span, + }), + format: None, + } + } InterpolateItem::Expr { expr, .. } => InterpolateItem::Expr { expr: Box::new(self.lower_expr(*expr)?), format: None, @@ -1150,6 +1164,12 @@ fn str_lit(string: String) -> rq::Expr { } } +fn table_variable_as_scalar(span: Option) -> Error { + Error::new_simple("table variable cannot be used as a scalar value") + .push_hint("use a join instead, or inline the subquery") + .with_span(span) +} + fn validate_take_range(range: &Range, span: Option) -> Result<()> { fn bound_as_int(bound: &Option) -> Option> { bound diff --git a/prqlc/prqlc/tests/integration/sql.rs b/prqlc/prqlc/tests/integration/sql.rs index 3ac339b88a2e..3c163f65c9f4 100644 --- a/prqlc/prqlc/tests/integration/sql.rs +++ b/prqlc/prqlc/tests/integration/sql.rs @@ -4798,6 +4798,37 @@ fn test_table_variable_in_scalar_context() { │ Help: use a join instead, or inline the subquery ───╯ "); + + // Outside an operator, a table variable was compiled to its bare name: + // `derive is_mod = mod_id` gave `SELECT *, mod_id FROM modlog`. + for (transform, column) in [ + ("derive is_mod = mod_id", 17), + ("select {actor_id, m = mod_id}", 23), + ("group mod_id (aggregate {c = count this})", 7), + ("sort mod_id", 6), + ("derive x = case [actor_id == 1 => mod_id]", 35), + ] { + let query = + format!("let mod_id = (from users | select id | take 1)\nfrom modlog\n{transform}"); + let err = compile(&query).unwrap_err().to_string(); + assert!( + err.contains("table variable cannot be used as a scalar value") + && err.contains(&format!("[ :3:{column} ]")), + "{transform}:\n{err}" + ); + } + + // Interpolated into an s-string, a table variable still compiles, written + // by name. (Its CTE is dropped, which is #1762.) + compile( + r#" + let mod_id = (from users | select id | take 1) + + from modlog + derive is_mod = s"actor_id IN (SELECT id FROM {mod_id})" + "#, + ) + .unwrap(); } #[test]