Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 25 additions & 5 deletions prqlc/prqlc/src/semantic/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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));
}
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -1150,6 +1164,12 @@ fn str_lit(string: String) -> rq::Expr {
}
}

fn table_variable_as_scalar(span: Option<Span>) -> 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<rq::Expr>, span: Option<Span>) -> Result<()> {
fn bound_as_int(bound: &Option<rq::Expr>) -> Option<Option<&i64>> {
bound
Expand Down
31 changes: 31 additions & 0 deletions prqlc/prqlc/tests/integration/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Loading