diff --git a/crates/lance-csr/src/logical_plan.rs b/crates/lance-csr/src/logical_plan.rs index a7ab27a..24e8d89 100644 --- a/crates/lance-csr/src/logical_plan.rs +++ b/crates/lance-csr/src/logical_plan.rs @@ -718,10 +718,20 @@ impl<'a> LogicalPlanner<'a> { ValueExpression::Variable(list) => self.node_lists.get(list).cloned(), _ => None, }; + let mut expression = unwind_clause.expression.clone(); + // Comprehensions become operators on the input rows, as in WITH and RETURN. + let base = if crate::ast::needs_relational_evaluation(&expression) { + let input = base.unwrap_or(LogicalOperator::SingleRow); + let saved = self.seed_source.replace(input.clone()); + let rewritten = self.rewrite_exists_in_value(input, &mut expression); + self.seed_source = saved; + Some(rewritten?) + } else { + base + }; // A list of node ids unwinds to nodes; anything else to plain values. let kind = label.clone().unwrap_or_else(|| "Unwound".to_string()); self.variables.insert(unwind_clause.alias.clone(), kind); - let mut expression = unwind_clause.expression.clone(); self.resolve_value(&mut expression); Ok(LogicalOperator::Unwind { @@ -2500,6 +2510,28 @@ mod tests { use super::*; use crate::{parser::parse_cypher_query, NodeMapping}; + #[test] + fn unwind_lowers_list_comprehension() { + let config = GraphConfig::default(); + let query = + parse_cypher_query("UNWIND [x IN [1, 2, 3] WHERE x > 1] AS y RETURN y").unwrap(); + let LogicalOperator::Project { input, .. } = + LogicalPlanner::new(&config).plan(&query).unwrap() + else { + panic!("expected projection"); + }; + let LogicalOperator::Unwind { + input: Some(input), + expression, + .. + } = *input + else { + panic!("expected unwind"); + }; + assert_eq!(expression, ValueExpression::Variable("__list_1".into())); + assert!(matches!(*input, LogicalOperator::ListComprehension { .. })); + } + #[test] fn return_alias_shadows_node_only_after_projection() { let config = GraphConfig::builder() diff --git a/python/python/tests/test_unwind_comprehension.py b/python/python/tests/test_unwind_comprehension.py new file mode 100644 index 0000000..60c69e8 --- /dev/null +++ b/python/python/tests/test_unwind_comprehension.py @@ -0,0 +1,26 @@ +# SPDX-License-Identifier: Apache-2.0 +# SPDX-FileCopyrightText: Copyright The lance-csr Authors + +"""UNWIND over a list comprehension, filtered or mapped.""" + +import pyarrow as pa +import pytest +from lance_csr import CypherEngine, GraphConfig + + +@pytest.mark.parametrize( + ("query", "expected"), + [ + ("UNWIND [x IN [1, 2, 3] WHERE x > 1] AS y RETURN y", [2, 3]), + ("UNWIND [x IN [1, 2, 3] | x * 10] AS y RETURN y", [10, 20, 30]), + ( + "MATCH (n:N) WITH collect(n.id) AS ids " + "UNWIND [i IN ids WHERE i > 1] AS y RETURN y", + [2, 3], + ), + ], +) +def test_unwind_list_comprehension(query, expected): + config = GraphConfig.builder().node_table("N", "id").build() + engine = CypherEngine(config, {"N": pa.table({"id": [1, 2, 3]})}) + assert sorted(engine.execute(query).column("y").to_pylist()) == expected