diff --git a/prqlc/prqlc/src/semantic/lowering.rs b/prqlc/prqlc/src/semantic/lowering.rs index 708b67ee71ed..0ab0970ce57a 100644 --- a/prqlc/prqlc/src/semantic/lowering.rs +++ b/prqlc/prqlc/src/semantic/lowering.rs @@ -935,6 +935,17 @@ impl Lowerer { .try_collect()?, ), pl::ExprKind::RqOperator { name, args } => { + // 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)); + } + } + let args = args.into_iter().map(|x| self.lower_expr(x)).try_collect()?; rq::ExprKind::Operator { name, args } diff --git a/prqlc/prqlc/tests/integration/sql.rs b/prqlc/prqlc/tests/integration/sql.rs index a27087b97ed6..e186b83041b5 100644 --- a/prqlc/prqlc/tests/integration/sql.rs +++ b/prqlc/prqlc/tests/integration/sql.rs @@ -4053,6 +4053,30 @@ fn test_direct_table_references() { "); } +#[test] +fn test_table_variable_in_scalar_context() { + // https://github.com/PRQL/prql/issues/5158 + assert_snapshot!(compile( + r#" + let mod_id = (from users | filter login == "nightpool" | select id | take 1) + + from modlog + filter actor_id == mod_id + "#, + ) + .unwrap_err(), @r#" + Error: + ╭─[ :5:24 ] + │ + 5 │ filter actor_id == mod_id + │ ───┬── + │ ╰──── table variable cannot be used as a scalar value + │ + │ Help: use a join instead, or inline the subquery + ───╯ + "#); +} + #[test] fn test_name_shadowing() { assert_snapshot!(compile(