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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright The lance-csr Authors

//! Conservative expression checks against the current catalog and lexical scope.

use super::*;
use arrow_schema::{DataType, TimeUnit};
use datafusion_expr::binary::BinaryTypeCoercer;
use datafusion_expr::Operator;

#[derive(Clone, Debug, PartialEq)]
pub(super) enum ExpressionType {
    Unknown,
    Scalar(DataType),
    Entity(VariableType, Vec<String>),
    List(Box<Self>),
    Map(Vec<(String, Self)>),
}

impl ExpressionType {
    pub(super) fn element(self) -> Self {
        match self {
            Self::List(element) => *element,
            Self::Scalar(
                DataType::List(field)
                | DataType::LargeList(field)
                | DataType::FixedSizeList(field, _),
            ) => Self::Scalar(field.data_type().clone()),
            _ => Self::Unknown,
        }
    }

    fn known(&self) -> bool {
        !matches!(self, Self::Unknown | Self::Scalar(DataType::Null))
    }
}

impl SemanticAnalyzer {
    fn variable_expression_type(&self, name: &str) -> ExpressionType {
        if let Some(value_type) = self.expression_types.get(&name.to_lowercase()) {
            return value_type.clone();
        }
        match self.variables.get_ci(name) {
            Some(info) if info.variable_type != VariableType::Property => {
                ExpressionType::Entity(info.variable_type.clone(), info.labels.clone())
            }
            _ => ExpressionType::Unknown,
        }
    }

    fn property_expression_type(&self, base: ExpressionType, property: &str) -> ExpressionType {
        match base {
            ExpressionType::Entity(kind, labels) => {
                let Some(catalog) = &self.catalog else {
                    return ExpressionType::Unknown;
                };
                let types = labels
                    .iter()
                    .map(|label| {
                        let source = match kind {
                            VariableType::Node => catalog.node_source(label),
                            VariableType::Relationship => catalog.relationship_source(label),
                            _ => None,
                        }?;
                        source
                            .schema()
                            .fields()
                            .iter()
                            .find(|field| field.name().eq_ignore_ascii_case(property))
                            .map(|field| ExpressionType::Scalar(field.data_type().clone()))
                    })
                    .collect::<Option<Vec<_>>>();
                types.map(common_type).unwrap_or(ExpressionType::Unknown)
            }
            ExpressionType::Map(fields) => fields
                .into_iter()
                .find(|(name, _)| name.eq_ignore_ascii_case(property))
                .map(|(_, value_type)| value_type)
                .unwrap_or(ExpressionType::Unknown),
            ExpressionType::Scalar(DataType::Struct(fields)) => fields
                .iter()
                .find(|field| field.name().eq_ignore_ascii_case(property))
                .map(|field| ExpressionType::Scalar(field.data_type().clone()))
                .unwrap_or(ExpressionType::Unknown),
            // Date component access and function-specific fields remain with DataFusion.
            _ => ExpressionType::Unknown,
        }
    }

    pub(super) fn expression_type(&self, expression: &ValueExpression) -> ExpressionType {
        use ExpressionType::{List, Map, Scalar, Unknown};
        match expression {
            ValueExpression::Variable(name) | ValueExpression::LocalVariable(name) => {
                self.variable_expression_type(name)
            }
            ValueExpression::Property(property)
            | ValueExpression::Literal(PropertyValue::Property(property)) => self
                .property_expression_type(
                    self.variable_expression_type(&property.variable),
                    &property.property,
                ),
            ValueExpression::Literal(value) => match value {
                PropertyValue::Integer(_) => Scalar(DataType::Int64),
                PropertyValue::Float(_) => Scalar(DataType::Float64),
                PropertyValue::String(_) => Scalar(DataType::Utf8),
                PropertyValue::Boolean(_) => Scalar(DataType::Boolean),
                PropertyValue::Null => Scalar(DataType::Null),
                PropertyValue::Variable(name) => self.variable_expression_type(name),
                _ => Unknown,
            },
            ValueExpression::Arithmetic {
                left,
                operator,
                right,
            } => match (self.expression_type(left), self.expression_type(right)) {
                (Scalar(left), Scalar(right)) => {
                    BinaryTypeCoercer::new(&left, &arithmetic_operator(operator), &right)
                        .get_result_type()
                        .map(Scalar)
                        .unwrap_or(Unknown)
                }
                _ => Unknown,
            },
            ValueExpression::ScalarFunction { name, args }
            | ValueExpression::AggregateFunction { name, args, .. } => {
                let first = || {
                    args.first()
                        .map(|value| self.expression_type(value))
                        .unwrap_or(Unknown)
                };
                match name.to_lowercase().as_str() {
                    "count" | "size" | "length" | "tointeger" | "to_epoch_ms" => {
                        Scalar(DataType::Int64)
                    }
                    "abs" => match first() {
                        Scalar(value) if value.is_integer() => Scalar(DataType::Int64),
                        Scalar(value) if value.is_floating() => Scalar(DataType::Float64),
                        Scalar(DataType::Null) => Scalar(DataType::Null),
                        _ => Unknown,
                    },
                    "tofloat" | "round" | "floor" | "ceil" | "sqrt" => Scalar(DataType::Float64),
                    "tolower" | "lower" | "toupper" | "upper" | "tostring" | "left" | "right"
                    | "substring" | "trim" | "ltrim" | "rtrim" | "replace" => {
                        Scalar(DataType::Utf8)
                    }
                    "date" => Scalar(DataType::Date32),
                    "datetime" => Scalar(DataType::Timestamp(
                        TimeUnit::Millisecond,
                        Some("UTC".into()),
                    )),
                    "collect" => List(Box::new(first())),
                    "head" | "last" | "__index" => first().element(),
                    "split" | "keys" | "labels" => List(Box::new(Scalar(DataType::Utf8))),
                    "tail" | "reverse" => match first() {
                        Scalar(DataType::FixedSizeList(field, _))
                            if name.eq_ignore_ascii_case("tail") =>
                        {
                            Scalar(DataType::List(field))
                        }
                        value => value,
                    },
                    "range" => List(Box::new(Scalar(DataType::Int64))),
                    "nodes" => List(Box::new(Unknown)),
                    "__list" => List(Box::new(common_type(
                        args.iter()
                            .map(|value| self.expression_type(value))
                            .collect(),
                    ))),
                    "coalesce" => common_type(
                        args.iter()
                            .map(|value| self.expression_type(value))
                            .collect(),
                    ),
                    // SUM/AVG output widths depend on DataFusion's accumulator type.
                    "min" | "max" => first(),
                    _ => Unknown,
                }
            }
            ValueExpression::Case { whens, otherwise } => common_type(
                whens
                    .iter()
                    .map(|(_, value)| self.expression_type(value))
                    .chain(otherwise.iter().map(|value| self.expression_type(value)))
                    .collect(),
            ),
            ValueExpression::Boolean(_) | ValueExpression::ListPredicate { .. } => {
                Scalar(DataType::Boolean)
            }
            ValueExpression::Map(fields) => Map(fields
                .iter()
                .map(|(name, value)| (name.clone(), self.expression_type(value)))
                .collect()),
            ValueExpression::Field { base, field } => {
                self.property_expression_type(self.expression_type(base), field)
            }
            ValueExpression::VectorLiteral(_) => List(Box::new(Scalar(DataType::Float32))),
            ValueExpression::VectorDistance { .. } | ValueExpression::VectorSimilarity { .. } => {
                Scalar(DataType::Float64)
            }
            // Their local scopes have ended; do not infer from similarly named outer variables.
            ValueExpression::Reduce { .. }
            | ValueExpression::ListComprehension { .. }
            | ValueExpression::PatternComprehension { .. }
            | ValueExpression::Parameter(_) => Unknown,
        }
    }

    pub(super) fn validate_binary(
        &self,
        left: &ValueExpression,
        operator: Operator,
        right: &ValueExpression,
    ) -> Result<()> {
        use ExpressionType::{Entity, Scalar};
        let left_type = self.expression_type(left);
        let right_type = self.expression_type(right);
        if !left_type.known() || !right_type.known() {
            return Ok(());
        }
        let comparison = matches!(
            operator,
            Operator::Eq
                | Operator::NotEq
                | Operator::Lt
                | Operator::LtEq
                | Operator::Gt
                | Operator::GtEq
        );
        let compatible = match (&left_type, &right_type) {
            // DataFusion decides scalar compatibility; it casts the string side of a
            // string-number comparison, as queries did before this check existed.
            (Scalar(left), Scalar(right)) => BinaryTypeCoercer::new(left, &operator, right)
                .get_result_type()
                .is_ok(),
            (Entity(left, _), Entity(right, _)) => comparison && left == right,
            // Collection comparison rules remain with the planner; arithmetic on entities does not.
            (Entity(..), _) | (_, Entity(..)) => false,
            _ => comparison,
        };
        if compatible {
            Ok(())
        } else {
            Err(GraphError::PlanError {
                message: format!(
                    "Type mismatch in '{} {} {}': {} and {}",
                    display_value(left),
                    operator,
                    display_value(right),
                    display_type(&left_type),
                    display_type(&right_type)
                ),
                location: snafu::Location::new(file!(), line!(), column!()),
            })
        }
    }
}

fn common_type(types: Vec<ExpressionType>) -> ExpressionType {
    let mut known = types
        .into_iter()
        .filter(|value| !matches!(value, ExpressionType::Scalar(DataType::Null)));
    let Some(first) = known.next() else {
        return ExpressionType::Unknown;
    };
    if known.all(|value| value == first) {
        first
    } else {
        ExpressionType::Unknown
    }
}

fn display_type(value: &ExpressionType) -> String {
    match value {
        ExpressionType::Scalar(data_type) => data_type.to_string(),
        ExpressionType::Entity(kind, _) => format!("{kind:?}"),
        _ => format!("{value:?}"),
    }
}

fn display_value(value: &ValueExpression) -> String {
    match value {
        ValueExpression::Property(property) => {
            format!("{}.{}", property.variable, property.property)
        }
        ValueExpression::Variable(name) => name.clone(),
        ValueExpression::Literal(PropertyValue::Integer(value)) => value.to_string(),
        ValueExpression::Literal(PropertyValue::Float(value)) => value.to_string(),
        ValueExpression::Literal(PropertyValue::String(value)) => format!("{value:?}"),
        ValueExpression::Arithmetic {
            left,
            operator,
            right,
        } => format!(
            "({} {} {})",
            display_value(left),
            arithmetic_operator(operator),
            display_value(right)
        ),
        _ => format!("{value:?}"),
    }
}

pub(super) fn arithmetic_operator(operator: &ArithmeticOperator) -> Operator {
    match operator {
        ArithmeticOperator::Add => Operator::Plus,
        ArithmeticOperator::Subtract => Operator::Minus,
        ArithmeticOperator::Multiply => Operator::Multiply,
        ArithmeticOperator::Divide => Operator::Divide,
        ArithmeticOperator::Modulo => Operator::Modulo,
    }
}

pub(super) fn comparison_operator(operator: &ComparisonOperator) -> Operator {
    match operator {
        ComparisonOperator::Equal => Operator::Eq,
        ComparisonOperator::NotEqual => Operator::NotEq,
        ComparisonOperator::LessThan => Operator::Lt,
        ComparisonOperator::LessThanOrEqual => Operator::LtEq,
        ComparisonOperator::GreaterThan => Operator::Gt,
        ComparisonOperator::GreaterThanOrEqual => Operator::GtEq,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use arrow_schema::{Field, Schema};
    use lance_csr_catalog::{InMemoryCatalog, SimpleTableSource};

    fn analyze(query: &str) -> SemanticResult {
        let schema = Arc::new(Schema::new(vec![
            Field::new("id", DataType::Int64, false),
            Field::new("name", DataType::Utf8, true),
            Field::new("age", DataType::Int32, true),
            Field::new(
                "created",
                DataType::Timestamp(TimeUnit::Microsecond, Some("UTC".into())),
                true,
            ),
        ]));
        let catalog = InMemoryCatalog::new()
            .with_node_source("Person", Arc::new(SimpleTableSource::new(schema)));
        let config = GraphConfig::builder()
            .node_table("Person", "id")
            .build()
            .unwrap();
        SemanticAnalyzer::new(config)
            .with_catalog(Arc::new(catalog))
            .analyze(
                &crate::parser::parse_cypher_query(query).unwrap(),
                &HashMap::new(),
            )
            .unwrap()
    }

    #[test]
    fn rejects_known_operand_types_in_their_scope() {
        for (query, expression, types) in [
            ("MATCH (p:Person) RETURN p + 1", "p + 1", ["Node", "Int64"]),
            ("MATCH (p:Person) WITH p.name AS value RETURN value * 2", "value * 2", ["Utf8", "Int64"]),
            ("MATCH (p:Person) WITH collect(p.name) AS names UNWIND names AS name RETURN name / 2", "name / 2", ["Utf8", "Int64"]),
            ("MATCH (p:Person) RETURN [p IN [p.name] | p + 1]", "p + 1", ["Utf8", "Int64"]),
        ] {
            let errors = analyze(query).errors.join("\n");
            assert!(errors.contains("Type mismatch") && errors.contains(expression)
                && types.iter().all(|name| errors.contains(name)), "{query}: {errors}");
        }
    }

    #[test]
    fn scalar_return_types_follow_with_and_unwind_scopes() {
        for query in [
            "RETURN left('12', 1) + 1",
            "WITH split('1,2', ',') AS values UNWIND values AS value RETURN value + 1",
            "RETURN last(reverse(['1'])) + 1",
            "WITH tail(['1','2']) AS values UNWIND values AS value RETURN value + 1",
        ] {
            let errors = analyze(query).errors.join("\n");
            assert!(errors.contains("Type mismatch"), "{query}: {errors}");
        }
        // String-number comparisons are left to DataFusion, which casts the string side.
        for query in [
            "RETURN abs(1) = '1'",
            "WITH sqrt(4) AS value RETURN value = '2'",
            "UNWIND keys({one: 1}) AS key RETURN key = 1",
            "MATCH (p:Person) UNWIND labels(p) AS label RETURN label = 1",
            "MATCH (p:Person) WHERE p.name = 1 RETURN p.id",
        ] {
            let errors = analyze(query).errors.join("\n");
            assert!(!errors.contains("Type mismatch"), "{query}: {errors}");
        }
    }

    #[test]
    fn preserves_coercions_nulls_and_unknown_types() {
        for query in [
            "MATCH (p:Person) RETURN p.age + 0.5",
            "MATCH (p:Person) WHERE p.created > '2020-01-01T00:00:00Z' RETURN p.id",
            "MATCH (p:Person) RETURN p.age + null",
            "MATCH (p:Person) RETURN p.unknown + 1",
            "MATCH (p:Person) WITH p AS q RETURN q.age + 1",
            "MATCH (p:Person) RETURN [p IN [p.age] | p + 1], p.name",
            "MATCH (p:Person) RETURN [p IN [p.name, p.age] | p + 1]",
            "MATCH (p:Person) WITH collect(p) AS people UNWIND people AS person RETURN person.age + 1",
        ] {
            let errors = analyze(query).errors;
            assert!(errors.is_empty(), "{query}: {errors:?}");
        }
    }
}