lance-csr 0.3.1

Graph query engine for Lance datasets with Cypher support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright The lance-csr Authors

//! Arrow values for paths and their entities. Properties live in a marked child so
//! user property names cannot collide with identity or endpoint metadata.

use std::collections::{HashMap, HashSet};
use std::sync::Arc;

use arrow::array::{Array, ArrayRef, AsArray, Int64Array, ListArray, StructArray, UInt32Array};
use arrow::buffer::OffsetBuffer;
use arrow::compute::take;
use arrow_schema::{DataType, Field, Fields};
use datafusion::common::Result;

pub(crate) const PROPERTIES: &str = "__lance_csr_properties";
pub(crate) const IDENTITY: &str = "__lance_csr_identity";
pub(crate) const START: &str = "__lance_csr_start";
pub(crate) const END: &str = "__lance_csr_end";
const ENTITY: &str = "lance_csr.entity";

pub(crate) fn entity_kind(data_type: &DataType) -> Option<&str> {
    let DataType::Struct(fields) = data_type else {
        return None;
    };
    fields.first()?.metadata().get(ENTITY).map(String::as_str)
}

pub(crate) fn entity_type(
    properties: Fields,
    kind: &str,
    node_type: Option<&DataType>,
) -> DataType {
    let mut fields = vec![
        Field::new(PROPERTIES, DataType::Struct(properties), false)
            .with_metadata(HashMap::from([(ENTITY.into(), kind.into())])),
        Field::new(
            IDENTITY,
            DataType::Struct(
                vec![
                    Field::new("label", DataType::Utf8, false),
                    Field::new("id", DataType::Int64, false),
                ]
                .into(),
            ),
            false,
        ),
    ];
    if let Some(node_type) = node_type {
        fields.push(Field::new(START, node_type.clone(), true));
        fields.push(Field::new(END, node_type.clone(), true));
    }
    DataType::Struct(fields.into())
}

pub(crate) fn path_type(node_type: DataType, relationship_type: DataType) -> DataType {
    let list = |t| DataType::List(Arc::new(Field::new("item", t, true)));
    DataType::Struct(
        vec![
            Field::new("length", DataType::Int64, false),
            Field::new("nodes", list(DataType::Int64), false),
            Field::new("node_records", list(node_type), false),
            Field::new("relationships", list(relationship_type), false),
        ]
        .into(),
    )
}

pub(crate) fn struct_array(data_type: &DataType, columns: Vec<ArrayRef>) -> StructArray {
    let DataType::Struct(fields) = data_type else {
        unreachable!("entity/path struct")
    };
    StructArray::new(fields.clone(), columns, None)
}

/// Ordered row references, independent of property equality or edge endpoints.
#[derive(Debug, Clone)]
pub(crate) struct PathRows {
    pub nodes: Vec<u32>,
    pub edges: Vec<u32>,
    /// Node positions at the written segment boundaries (variable segments may have many hops).
    pub boundaries: Vec<usize>,
}

pub(crate) fn paths_array(
    paths: &[PathRows],
    nodes: &StructArray,
    edges: &StructArray,
) -> Result<ArrayRef> {
    let node_rows =
        UInt32Array::from_iter_values(paths.iter().flat_map(|p| p.nodes.iter().copied()));
    let edge_rows =
        UInt32Array::from_iter_values(paths.iter().flat_map(|p| p.edges.iter().copied()));
    let selected_nodes = take(nodes, &node_rows, None)?;
    let selected_edges = take(edges, &edge_rows, None)?;
    let ids = selected_nodes
        .as_struct()
        .column(1)
        .as_struct()
        .column(1)
        .clone();
    let list = |values: ArrayRef, lengths: Vec<usize>| -> ArrayRef {
        Arc::new(ListArray::new(
            Arc::new(Field::new("item", values.data_type().clone(), true)),
            OffsetBuffer::from_lengths(lengths),
            values,
            None,
        ))
    };
    let node_lengths: Vec<_> = paths.iter().map(|p| p.nodes.len()).collect();
    let edge_lengths: Vec<_> = paths.iter().map(|p| p.edges.len()).collect();
    let data_type = path_type(nodes.data_type().clone(), edges.data_type().clone());
    Ok(Arc::new(struct_array(
        &data_type,
        vec![
            Arc::new(Int64Array::from_iter_values(
                paths.iter().map(|p| p.edges.len() as i64),
            )),
            list(ids, node_lengths.clone()),
            list(selected_nodes, node_lengths),
            list(selected_edges, edge_lengths),
        ],
    )))
}

/// Null tests need only the compact path's validity, not its entity inventory.
fn mask_path_null_checks(predicate: &mut crate::ast::BooleanExpression, names: &HashSet<String>) {
    use crate::ast::{BooleanExpression as B, PropertyValue, ValueExpression as V};
    match predicate {
        B::IsNull(value) | B::IsNotNull(value) => {
            if matches!(&*value, V::Variable(name) if names.contains(&name.to_lowercase())) {
                *value = V::Literal(PropertyValue::Null);
            }
        }
        B::And(left, right) | B::Or(left, right) => {
            mask_path_null_checks(left, names);
            mask_path_null_checks(right, names);
        }
        B::Not(inner) => mask_path_null_checks(inner, names),
        _ => {}
    }
}

/// Homogeneous length/nodes queries, including WITH aliases, keep their CSR plan.
/// Returned path values, relationship access and containers need actual path records.
pub(crate) fn needs_values(
    query: &crate::ast::CypherQuery,
    config: &crate::config::GraphConfig,
) -> bool {
    use crate::ast::{GraphPattern, ReadingClause, ValueExpression as V};
    let clauses: Vec<_> = query
        .reading_clauses
        .iter()
        .chain(&query.post_with_reading_clauses)
        .chain(
            query
                .further_with_stages
                .iter()
                .flat_map(|s| &s.reading_clauses),
        )
        .collect();
    let mut names = std::collections::HashSet::new();
    for clause in &clauses {
        if let ReadingClause::Match(m) = clause {
            for pattern in &m.patterns {
                if let GraphPattern::Path(path) = pattern {
                    if path.variable.is_some() {
                        return true;
                    }
                    if path.shortest.is_some() {
                        // Numeric keys alone cannot distinguish nodes in different labels,
                        // even when both endpoints have the same label on an undirected path.
                        let crosses_labels = path.segments.iter().any(|segment| {
                            segment.relationship.types.iter().any(|kind| {
                                config
                                    .get_relationship_mapping(kind)
                                    .is_some_and(|mapping| {
                                        mapping
                                            .source_label
                                            .as_deref()
                                            .zip(mapping.target_label.as_deref())
                                            .is_some_and(|(source, target)| {
                                                !source.eq_ignore_ascii_case(target)
                                            })
                                    })
                            })
                        });
                        let different_ends = path
                            .start_node
                            .labels
                            .first()
                            .zip(path.segments.last().and_then(|s| s.end_node.labels.first()))
                            .is_some_and(|(source, target)| !source.eq_ignore_ascii_case(target));
                        if crosses_labels || different_ends {
                            return true;
                        }
                        if let Some(name) = path.shortest.as_ref().and_then(|s| s.variable.as_ref())
                        {
                            names.insert(name.to_lowercase());
                        }
                    }
                }
            }
        }
    }
    // A bare WITH only renames/carries a path; inspect its eventual consumers instead.
    for with in query
        .with_clause
        .iter()
        .chain(query.further_with_stages.iter().map(|s| &s.with_clause))
    {
        for item in &with.items {
            if let (V::Variable(name), Some(alias)) = (&item.expression, &item.alias) {
                if names.contains(&name.to_lowercase()) {
                    names.insert(alias.to_lowercase());
                }
            }
        }
    }
    let mut needed = query
        .union_branches
        .iter()
        .any(|branch| needs_values(&branch.query, config))
        || clauses.iter().any(|clause| match clause {
            ReadingClause::Call(inner) => {
                needs_values(inner, config)
                    || inner
                        .call_union_imports()
                        .unwrap_or_default()
                        .iter()
                        .any(|name| names.contains(name))
            }
            _ => false,
        });
    let mut inspect = |expression: &V| {
        let mut value = expression.clone();
        crate::ast::transform_value(&mut value, &mut |v| match v {
            V::Boolean(predicate) => mask_path_null_checks(predicate, &names),
            V::Case { whens, .. } => {
                for (predicate, _) in whens {
                    mask_path_null_checks(predicate, &names);
                }
            }
            V::ListComprehension {
                filter: Some(predicate),
                ..
            }
            | V::PatternComprehension {
                filter: Some(predicate),
                ..
            } => {
                mask_path_null_checks(predicate, &names);
            }
            V::ScalarFunction { name, args }
                if matches!(name.to_lowercase().as_str(), "length" | "nodes") =>
            {
                args.clear()
            }
            V::AggregateFunction { args, .. }
                if args.iter().any(
                    |v| matches!(v, V::Variable(name) if names.contains(&name.to_lowercase())),
                ) =>
            {
                needed = true;
            }
            V::ScalarFunction { name, .. }
                if matches!(
                    name.to_lowercase().as_str(),
                    "relationships" | "startnode" | "endnode"
                ) =>
            {
                needed = true
            }
            _ => {}
        });
        crate::ast::transform_value(&mut value, &mut |v| {
            if matches!(v, V::Variable(name) if names.contains(&name.to_lowercase())) {
                needed = true;
            }
        });
    };
    for clause in clauses {
        match clause {
            ReadingClause::Call(_) => {}
            ReadingClause::Unwind(u) => inspect(&u.expression),
            ReadingClause::Match(m) => {
                if let Some(w) = &m.where_clause {
                    inspect(&V::Boolean(Box::new(w.expression.clone())));
                }
            }
        }
    }
    for with in query
        .with_clause
        .iter()
        .chain(query.further_with_stages.iter().map(|s| &s.with_clause))
    {
        for item in &with.items {
            if !matches!(&item.expression, V::Variable(name) if names.contains(&name.to_lowercase()))
            {
                inspect(&item.expression);
            }
        }
        if let Some(order) = &with.order_by {
            for item in &order.items {
                inspect(&item.expression);
            }
        }
        if let Some(filter) = &with.where_clause {
            inspect(&V::Boolean(Box::new(filter.expression.clone())));
        }
    }
    for item in &query.return_clause.items {
        inspect(&item.expression);
    }
    if let Some(order) = &query.order_by {
        for item in &order.items {
            inspect(&item.expression);
        }
    }
    for filter in query
        .where_clause
        .iter()
        .chain(&query.post_with_where_clause)
        .chain(
            query
                .further_with_stages
                .iter()
                .filter_map(|s| s.where_clause.as_ref()),
        )
    {
        inspect(&V::Boolean(Box::new(filter.expression.clone())));
    }
    needed
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn direct_shortest_queries_keep_the_csr_route() {
        let config = crate::config::GraphConfig::builder()
            .node_table("Person", "id")
            .node_table("Place", "id")
            .rel_table("R", "Person", "Person", "src", "dst")
            .rel_table("LIVES", "Person", "Place", "src", "dst")
            .build()
            .unwrap();
        let prefix = "MATCH p = shortestPath((a:Person)-[:R*]-(b:Person)) ";
        for (suffix, rich) in [
            ("RETURN length(p), nodes(p)", false),
            ("RETURN p IS NULL, p IS NOT NULL", false),
            (
                "RETURN CASE p IS NULL WHEN true THEN -1 ELSE length(p) END",
                false,
            ),
            (
                "RETURN CASE WHEN p IS NOT NULL THEN length(p) ELSE -1 END",
                false,
            ),
            (
                "WITH p AS q WHERE q IS NOT NULL OR NOT q IS NULL RETURN length(q)",
                false,
            ),
            ("RETURN p, p IS NULL", true),
            ("RETURN relationships(p) IS NULL", true),
            ("RETURN [n IN nodes(p) | n.id]", false),
            ("WITH p RETURN nodes(p)", false),
            (
                "WITH p AS q WITH q AS r RETURN [n IN nodes(r) | n.id]",
                false,
            ),
            ("WITH p AS q RETURN q", true),
            ("WITH p AS q RETURN relationships(q)", true),
            (
                "WITH p AS q CALL { WITH q RETURN relationships(q) AS edges } RETURN edges",
                true,
            ),
            (
                "WITH p AS q CALL { WITH q RETURN q AS result UNION WITH q RETURN q AS result } RETURN nodes(result)",
                true,
            ),
            (
                "WITH p AS q WITH collect(q) AS ps UNWIND ps AS r RETURN nodes(r)",
                true,
            ),
            ("RETURN p", true),
            ("RETURN relationships(p)", true),
            ("RETURN length(head(collect(p)))", true),
            ("WITH collect(p) AS ps UNWIND ps AS p RETURN nodes(p)", true),
        ] {
            let query = crate::parser::parse_cypher_query(&format!("{prefix}{suffix}")).unwrap();
            assert_eq!(needs_values(&query, &config), rich, "{suffix}");
        }
    }

    #[test]
    fn cross_label_shortest_paths_need_typed_identity() {
        let config = crate::config::GraphConfig::builder()
            .node_table("Person", "id")
            .node_table("Place", "id")
            .rel_table("LIVES", "Person", "Place", "src", "dst")
            .build()
            .unwrap();
        for ends in ["(b:Place)", "(b:Person)", "(b)"] {
            let query = crate::parser::parse_cypher_query(&format!(
                "MATCH p = shortestPath((a:Person)-[:LIVES*0..]-{ends}) RETURN nodes(p)"
            ))
            .unwrap();
            assert!(needs_values(&query, &config));
        }
        for text in [
            "CALL { MATCH p = shortestPath((a:Person)-[:LIVES*0..]->(b:Place)) RETURN length(p) AS n } RETURN n",
            "RETURN 0 AS n UNION MATCH p = shortestPath((a:Person)-[:LIVES*0..]->(b:Place)) RETURN length(p) AS n",
            "CALL { RETURN 0 AS n UNION MATCH p = shortestPath((a:Person)-[:LIVES*0..]-(b:Person)) RETURN length(p) AS n } RETURN n",
        ] {
            let query = crate::parser::parse_cypher_query(text).unwrap();
            assert!(needs_values(&query, &config), "{text}");
        }
    }
}