diff --git a/src/bound_plan/lower/tests.rs b/src/bound_plan/lower/tests.rs index 28b3e8505..651cf26a5 100644 --- a/src/bound_plan/lower/tests.rs +++ b/src/bound_plan/lower/tests.rs @@ -5386,6 +5386,152 @@ fn datetime_constructors_lower_to_now_or_a_parse_1487() { assert!(!got.contains("now64(parseDateTime64BestEffort"), "{got}"); } +#[test] +fn an_undeclared_scalar_property_unwinds_as_a_one_element_list_1419() { + // #1419: an undeclared property (no `property_types`) whose column is a + // known scalar must not be read as a list (arrayConcat fails on a scalar). + // It is wrapped in a one-element list instead, matching Neo4j's "UNWIND of + // a non-list value gives one row of itself". The column type is supplied + // via `column_types` (T1), since unit tests have no live database. + let schema = GraphSchemaConfig::from_yaml_str( + r#" +name: t +graph_schema: + nodes: + - label: User + database: db + table: users + node_id: user_id + property_mappings: { user_id: user_id, name: full_name } + column_types: { user_id: UInt64, full_name: String } + edges: [] +"#, + ) + .unwrap() + .to_graph_schema() + .unwrap(); + let got = translate_bound_plan( + "MATCH (u:User) UNWIND u.name AS x RETURN x", + &schema, + &ReadOptions::default(), + ) + .unwrap() + .sql; + assert!( + got.contains("if(isNull(v0.full_name), [], [v0.full_name])"), + "expected value_list wrap, got: {got}" + ); + assert!( + !got.contains("arrayConcat"), + "must not use arrayConcat, got: {got}" + ); +} + +/// #1419: `UNWIND` of an undeclared property spreads a column that can hold +/// a list (`Array`, `SimpleAggregateFunction(…, Array(…))`, `Dynamic`, +/// `Variant`) as it did, and wraps only a column known to be a plain value. +/// A wrapped list would be one row holding the list, not its elements. +#[test] +fn an_unwind_wraps_only_a_column_known_to_be_a_plain_value_1419() { + // `UNWIND t.v` over one label whose `v` is a column of type `ty`. + let unwind_of = |ty: &str| { + let yaml = format!( + r#" +name: t +graph_schema: + nodes: + - label: T + database: db + table: t + node_id: id + property_mappings: {{ id: id, v: v }} + column_types: {{ id: UInt64, v: "{ty}" }} + edges: [] +"# + ); + let schema = GraphSchemaConfig::from_yaml_str(&yaml) + .unwrap() + .to_graph_schema() + .unwrap(); + squash( + &translate_bound_plan( + "MATCH (t:T) UNWIND t.v AS x RETURN x", + &schema, + &ReadOptions::default(), + ) + .unwrap() + .sql, + ) + }; + // A plain value: one row of itself (none for NULL). + for ty in [ + "String", + "UInt64", + "Nullable(Int32)", + "Date", + "LowCardinality(String)", + ] { + let got = unwind_of(ty); + assert!(got.contains("if(isNull(v0.v), [], [v0.v])"), "{ty}: {got}"); + assert!(!got.contains("arrayConcat"), "{ty}: {got}"); + } + // A list: spread element by element. + let got = unwind_of("Array(String)"); + assert!(got.contains("ARRAY JOIN v0.v AS __cg_element"), "{got}"); + assert!(!got.contains("if(isNull("), "{got}"); + // A column that can hold a list, not known to be a value: as before. + for ty in [ + "SimpleAggregateFunction(anyLast, Array(Int64))", + "Dynamic", + "Variant(Array(Int64), String)", + "Map(String, Int64)", + ] { + let got = unwind_of(ty); + assert!(got.contains("arrayConcat(v0.v)"), "{ty}: {got}"); + assert!(!got.contains("[v0.v])"), "{ty}: {got}"); + } +} + +/// #1419: a property held as a list by one label and as a value by another +/// is not known to be a value: `UNWIND` spreads it as before. +#[test] +fn an_unwind_of_a_property_that_is_a_list_in_one_label_is_not_wrapped_1419() { + let schema = GraphSchemaConfig::from_yaml_str( + r#" +name: t +graph_schema: + nodes: + - label: A + database: db + table: a + node_id: id + property_mappings: { id: id, v: v } + column_types: { id: UInt64, v: String } + - label: B + database: db + table: b + node_id: id + property_mappings: { id: id, v: v } + column_types: { id: UInt64, v: "Array(String)" } + edges: [] +"#, + ) + .unwrap() + .to_graph_schema() + .unwrap(); + let got = squash( + &translate_bound_plan( + "MATCH (n) UNWIND n.v AS x RETURN x", + &schema, + &ReadOptions::default(), + ) + .unwrap() + .sql, + ); + assert!(got.contains("arrayConcat("), "{got}"); + assert!(!got.contains("if(isNull("), "{got}"); +} + /// S7c: rows in an order keep it, each row's elements after it in list /// order: the rows are numbered first. #[test] diff --git a/src/bound_plan/lower/unwind.rs b/src/bound_plan/lower/unwind.rs index 56ac16351..7007a3941 100644 --- a/src/bound_plan/lower/unwind.rs +++ b/src/bound_plan/lower/unwind.rs @@ -489,6 +489,49 @@ impl<'s> Lowerer<'s> { } } + /// What `UNWIND e` spreads (#1419): `kind(e)`, and, where that is `Unknown`, + /// the kind of a property whose column is a plain value in every label / + /// type (`plain_column_kind`). Read by `UNWIND` only: a scalar column's + /// kind anywhere else is a later slice's (`column_list_kind`). + fn unwind_kind(&self, e: &LogicalExpr) -> Kind { + match self.kind(e) { + Kind::Unknown => self.plain_column_kind(e).unwrap_or(Kind::Unknown), + k => k, + } + } + + /// A property not declared typed (so its kind is read from its columns) + /// whose column in every label / type is a value of one plain type (a + /// number, text, date, `Bool`...: `column_kind`'s `Scalar` / `Boolean`): + /// that kind. `None` for anything `column_kind` does not know to be one + /// (an `Array`, and a `Dynamic` / `Variant` / `JSON` / aggregate-function + /// column, which can hold one), or when a column type is not known. + fn plain_column_kind(&self, e: &LogicalExpr) -> Option { + if self.no_column_kinds.get() { + return None; + } + let LogicalExpr::PropertyAccessExp(pa) = e else { + return None; + }; + let PropertyValue::Column(p) = &pa.column else { + return None; + }; + let v = parse_var(&pa.table_alias.0)?; + let kinds: Vec = self + .property_columns(v, p) + .into_iter() + .map(|t| t.map(column_kind)) + .collect::>()?; + if kinds.is_empty() + || !kinds + .iter() + .all(|k| matches!(k, Kind::Scalar | Kind::Boolean)) + { + return None; + } + kinds.into_iter().reduce(Kind::either) + } + /// The kind of `e` with properties' column types left unread: what a /// check written before they were read (T2a) decides on. pub(super) fn kind_before_column_types(&self, e: &LogicalExpr) -> Kind { @@ -722,7 +765,7 @@ impl<'s> Lowerer<'s> { let Some(spelling) = current_function_mapper().unwind() else { return unsupported("UNWIND in this SQL dialect"); }; - let kind = self.kind(expr); + let kind = self.unwind_kind(expr); let before = self.kind_before_column_types(expr).element(); self.set_kind(var, kind.element(), before); // A list of no elements, or NULL: no rows.