Open kgutwin opened 10 months ago
Thanks for the issue — I think this is a dupe of the issue from https://github.com/PRQL/prql/pull/4037...
As an aside, I noticed that even without the panic, the
sort
does not translate into anORDER BY
in thefrom_b
CTE.
Right, good spot, the result should have an ORDER BY
.
Assuming this is a duplicate, shall we copy-paste that narrower example into a new issue?
The panic is in the same spot as #4037 but I'm guessing the cause is different. The backtrace shows that the problem starts when lowering the "sorts" that are defined in the pipeline:
32: prql_compiler::semantic::lowering::Lowerer::lower_pipeline::hd5286dcbc239425a
at /Users/kgutwin/github/prql/prqlc/prql-compiler/src/semantic/lowering.rs:491
489 │ vec![]
490 │ },
491 > sort: self.lower_sorts(transform_call.sort)?,
492 │ };
493 │ self.window = Some(window);
I think there's an issue related to the order of the lowering process in lower_pipeline()
somehow. I ran the compiler with RUST_LOG=debug
and got more informative messages:
[ ... snip ... ]
[DEBUG prql_compiler::semantic::lowering] lookup for main pipeline in []
[DEBUG prql_compiler::semantic::lowering] lowering table None, columns = [Single(Some("xyz")), Single(Some("key")), Wildcard]
[DEBUG prql_compiler::semantic::lowering] lowering table None, columns = [Single(Some("key")), Wildcard]
[DEBUG prql_compiler::semantic::lowering] lowering an instance of table default_db.a (id=127)...
[DEBUG prql_compiler::semantic::lowering] ... columns = [(Single(Some("key")), column-0), (Wildcard, column-1)]
[DEBUG prql_compiler::semantic::lowering] lowering ident this.b.xyz (target Some(121))
The application panicked (crashed).
Message: cannot find cid by id=121
Location: prqlc/prql-compiler/src/semantic/lowering.rs:949
So it looks like the table default_db.b
is not lowered before ident this.b.xyz
. I'm not sure why, though. The table is successfully resolved (earlier in the debug log):
[DEBUG prql_compiler::semantic::resolver::expr] resolving ident b...
[DEBUG prql_compiler::semantic::resolver::expr] ... resolved to default_db.b
[DEBUG prql_compiler::semantic::resolver::expr] ... which is TableDecl: [{*..}] LocalTable
[DEBUG prql_compiler::semantic::resolver::inference] instanced table default_db.b as Lineage { columns: [All { input_id: 121, except: {} }], inputs: [LineageInput { id: 121, name: "b", table: Ident { path: ["default_db"], name: "b" } }], prev_columns: [] }
[DEBUG prql_compiler::semantic::resolver::functions] resolved arg to Ident
[DEBUG prql_compiler::semantic::resolver::functions] resolved arg to Ident
[DEBUG prql_compiler::semantic::resolver::expr] resolving ident xyz...
[DEBUG prql_compiler::semantic::resolver::names] inferring xyz to be from table default_db.b
[DEBUG prql_compiler::semantic::resolver::expr] ... resolved to this.b.xyz
[DEBUG prql_compiler::semantic::resolver::expr] ... which is Column (target 121)
Super, thanks for tracking that down
FYI this is no longer a panic, instead now points to #3870
What happened?
The PRQL query below results in the following message:
Full backtrace
``` The application panicked (crashed). Message: cannot find cid by id=121 Location: prqlc/prql-compiler/src/semantic/lowering.rs:949 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ BACKTRACE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ⋮ 13 frames hidden ⋮ 14: prql_compiler::semantic::lowering::Lowerer::lookup_cid::h2d39b5439c15c5cb at /Users/kgutwin/github/prql/prqlc/prql-compiler/src/semantic/lowering.rs:949 947 │ } 948 │ } 949 > None => panic!("cannot find cid by id={id}"), 950 │ }; 951 │ 15: prql_compiler::semantic::lowering::Lowerer::lower_expr::h43150ba13889efe1 at /Users/kgutwin/github/prql/prqlc/prql-compiler/src/semantic/lowering.rs:818 816 │ } else if let Some(id) = expr.target_id { 817 │ // base case: column ref 818 > let cid = self.lookup_cid(id, Some(&ident.name)).with_span(span)?; 819 │ 820 │ rq::ExprKind::ColumnRef(cid) 16: prql_compiler::semantic::lowering::Lowerer::declare_as_column::hf30114d4b1d0c339 at /Users/kgutwin/github/prql/prqlc/prql-compiler/src/semantic/lowering.rs:753 751 │ 752 │ // lower 753 > let expr = self.lower_expr(expr_ast)?; 754 │ 755 │ // don't create new ColumnDef if expr is just a ColumnRef with no renaming 17: prql_compiler::semantic::lowering::Lowerer::lower_sorts::{{closure}}::hf8b91e5a417d6ef8 at /Users/kgutwin/github/prql/prqlc/prql-compiler/src/semantic/lowering.rs:580 578 │ by.into_iter() 579 │ .map(|ColumnSort { column, direction }| { 580 > let column = self.declare_as_column(*column, false)?; 581 │ Ok(ColumnSort { direction, column }) 582 │ }) 18: core::iter::adapters::map::map_try_fold::{{closure}}::he0b15f4de2861127 at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/core/src/iter/adapters/map.rs:91 19: core::iter::traits::iterator::Iterator::try_fold::hb7a594574057407d at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/core/src/iter/traits/iterator.rs:2461 20:However, when the join relation is expressed as a variable, the panic does not happen.
Adding an explicit definition of the column within the inline relation definition (
from b | select { key, xyz } | sort { -xyz }
) does not avoid the panic.PRQL input
SQL output
Expected SQL output
No response
MVCE confirmation
Anything else?
Built from git, at 36219a1.
As an aside, I noticed that even without the panic, the
sort
does not translate into anORDER BY
in thefrom_b
CTE. Is this intentional, given the ordering guarantee design of PRQL? I tried to review how ordering works in PRQL in the documentation as well as through some relevant issue discussions, but admit that I still find it a bit confusing, especially around joins.