lance_datafusion/
planner.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright The Lance Authors
3
4//! Exec plan planner
5
6use std::borrow::Cow;
7use std::collections::{BTreeSet, VecDeque};
8use std::sync::Arc;
9
10use crate::expr::safe_coerce_scalar;
11use crate::logical_expr::{coerce_filter_type_to_boolean, get_as_string_scalar_opt, resolve_expr};
12use crate::sql::{parse_sql_expr, parse_sql_filter};
13use arrow::compute::CastOptions;
14use arrow_array::ListArray;
15use arrow_buffer::OffsetBuffer;
16use arrow_schema::{DataType as ArrowDataType, Field, SchemaRef, TimeUnit};
17use arrow_select::concat::concat;
18use datafusion::common::tree_node::{TreeNode, TreeNodeRecursion, TreeNodeVisitor};
19use datafusion::common::DFSchema;
20use datafusion::config::ConfigOptions;
21use datafusion::error::Result as DFResult;
22use datafusion::execution::config::SessionConfig;
23use datafusion::execution::context::{SessionContext, SessionState};
24use datafusion::execution::runtime_env::RuntimeEnvBuilder;
25use datafusion::execution::session_state::SessionStateBuilder;
26use datafusion::logical_expr::expr::ScalarFunction;
27use datafusion::logical_expr::planner::{ExprPlanner, PlannerResult, RawFieldAccessExpr};
28use datafusion::logical_expr::{
29    AggregateUDF, ColumnarValue, GetFieldAccess, ScalarFunctionArgs, ScalarUDF, ScalarUDFImpl,
30    Signature, Volatility, WindowUDF,
31};
32use datafusion::optimizer::simplify_expressions::SimplifyContext;
33use datafusion::sql::planner::{
34    ContextProvider, NullOrdering, ParserOptions, PlannerContext, SqlToRel,
35};
36use datafusion::sql::sqlparser::ast::{
37    AccessExpr, Array as SQLArray, BinaryOperator, DataType as SQLDataType, ExactNumberInfo,
38    Expr as SQLExpr, Function, FunctionArg, FunctionArgExpr, FunctionArguments, Ident,
39    ObjectNamePart, Subscript, TimezoneInfo, UnaryOperator, Value, ValueWithSpan,
40};
41use datafusion::{
42    common::Column,
43    logical_expr::{col, Between, BinaryExpr, Like, Operator},
44    physical_expr::execution_props::ExecutionProps,
45    physical_plan::PhysicalExpr,
46    prelude::Expr,
47    scalar::ScalarValue,
48};
49use datafusion_functions::core::getfield::GetFieldFunc;
50use lance_arrow::cast::cast_with_options;
51use lance_core::datatypes::Schema;
52use lance_core::error::LanceOptionExt;
53use snafu::location;
54
55use lance_core::{Error, Result};
56
57#[derive(Debug, Clone, Eq, PartialEq, Hash)]
58struct CastListF16Udf {
59    signature: Signature,
60}
61
62impl CastListF16Udf {
63    pub fn new() -> Self {
64        Self {
65            signature: Signature::any(1, Volatility::Immutable),
66        }
67    }
68}
69
70impl ScalarUDFImpl for CastListF16Udf {
71    fn as_any(&self) -> &dyn std::any::Any {
72        self
73    }
74
75    fn name(&self) -> &str {
76        "_cast_list_f16"
77    }
78
79    fn signature(&self) -> &Signature {
80        &self.signature
81    }
82
83    fn return_type(&self, arg_types: &[ArrowDataType]) -> DFResult<ArrowDataType> {
84        let input = &arg_types[0];
85        match input {
86            ArrowDataType::FixedSizeList(field, size) => {
87                if field.data_type() != &ArrowDataType::Float32
88                    && field.data_type() != &ArrowDataType::Float16
89                {
90                    return Err(datafusion::error::DataFusionError::Execution(
91                        "cast_list_f16 only supports list of float32 or float16".to_string(),
92                    ));
93                }
94                Ok(ArrowDataType::FixedSizeList(
95                    Arc::new(Field::new(
96                        field.name(),
97                        ArrowDataType::Float16,
98                        field.is_nullable(),
99                    )),
100                    *size,
101                ))
102            }
103            ArrowDataType::List(field) => {
104                if field.data_type() != &ArrowDataType::Float32
105                    && field.data_type() != &ArrowDataType::Float16
106                {
107                    return Err(datafusion::error::DataFusionError::Execution(
108                        "cast_list_f16 only supports list of float32 or float16".to_string(),
109                    ));
110                }
111                Ok(ArrowDataType::List(Arc::new(Field::new(
112                    field.name(),
113                    ArrowDataType::Float16,
114                    field.is_nullable(),
115                ))))
116            }
117            _ => Err(datafusion::error::DataFusionError::Execution(
118                "cast_list_f16 only supports FixedSizeList/List arguments".to_string(),
119            )),
120        }
121    }
122
123    fn invoke_with_args(&self, func_args: ScalarFunctionArgs) -> DFResult<ColumnarValue> {
124        let ColumnarValue::Array(arr) = &func_args.args[0] else {
125            return Err(datafusion::error::DataFusionError::Execution(
126                "cast_list_f16 only supports array arguments".to_string(),
127            ));
128        };
129
130        let to_type = match arr.data_type() {
131            ArrowDataType::FixedSizeList(field, size) => ArrowDataType::FixedSizeList(
132                Arc::new(Field::new(
133                    field.name(),
134                    ArrowDataType::Float16,
135                    field.is_nullable(),
136                )),
137                *size,
138            ),
139            ArrowDataType::List(field) => ArrowDataType::List(Arc::new(Field::new(
140                field.name(),
141                ArrowDataType::Float16,
142                field.is_nullable(),
143            ))),
144            _ => {
145                return Err(datafusion::error::DataFusionError::Execution(
146                    "cast_list_f16 only supports array arguments".to_string(),
147                ));
148            }
149        };
150
151        let res = cast_with_options(arr.as_ref(), &to_type, &CastOptions::default())?;
152        Ok(ColumnarValue::Array(res))
153    }
154}
155
156// Adapter that instructs datafusion how lance expects expressions to be interpreted
157struct LanceContextProvider {
158    options: datafusion::config::ConfigOptions,
159    state: SessionState,
160    expr_planners: Vec<Arc<dyn ExprPlanner>>,
161}
162
163impl Default for LanceContextProvider {
164    fn default() -> Self {
165        let config = SessionConfig::new();
166        let runtime = RuntimeEnvBuilder::new().build_arc().unwrap();
167
168        let ctx = SessionContext::new_with_config_rt(config.clone(), runtime.clone());
169        crate::udf::register_functions(&ctx);
170
171        let state = ctx.state();
172
173        // SessionState does not expose expr_planners, so we need to get them separately
174        let mut state_builder = SessionStateBuilder::new()
175            .with_config(config)
176            .with_runtime_env(runtime)
177            .with_default_features();
178
179        // unwrap safe because with_default_features sets expr_planners
180        let expr_planners = state_builder.expr_planners().as_ref().unwrap().clone();
181
182        Self {
183            options: ConfigOptions::default(),
184            state,
185            expr_planners,
186        }
187    }
188}
189
190impl ContextProvider for LanceContextProvider {
191    fn get_table_source(
192        &self,
193        name: datafusion::sql::TableReference,
194    ) -> DFResult<Arc<dyn datafusion::logical_expr::TableSource>> {
195        Err(datafusion::error::DataFusionError::NotImplemented(format!(
196            "Attempt to reference inner table {} not supported",
197            name
198        )))
199    }
200
201    fn get_aggregate_meta(&self, name: &str) -> Option<Arc<AggregateUDF>> {
202        self.state.aggregate_functions().get(name).cloned()
203    }
204
205    fn get_window_meta(&self, name: &str) -> Option<Arc<WindowUDF>> {
206        self.state.window_functions().get(name).cloned()
207    }
208
209    fn get_function_meta(&self, f: &str) -> Option<Arc<ScalarUDF>> {
210        match f {
211            // TODO: cast should go thru CAST syntax instead of UDF
212            // Going thru UDF makes it hard for the optimizer to find no-ops
213            "_cast_list_f16" => Some(Arc::new(ScalarUDF::new_from_impl(CastListF16Udf::new()))),
214            _ => self.state.scalar_functions().get(f).cloned(),
215        }
216    }
217
218    fn get_variable_type(&self, _: &[String]) -> Option<ArrowDataType> {
219        // Variables (things like @@LANGUAGE) not supported
220        None
221    }
222
223    fn options(&self) -> &datafusion::config::ConfigOptions {
224        &self.options
225    }
226
227    fn udf_names(&self) -> Vec<String> {
228        self.state.scalar_functions().keys().cloned().collect()
229    }
230
231    fn udaf_names(&self) -> Vec<String> {
232        self.state.aggregate_functions().keys().cloned().collect()
233    }
234
235    fn udwf_names(&self) -> Vec<String> {
236        self.state.window_functions().keys().cloned().collect()
237    }
238
239    fn get_expr_planners(&self) -> &[Arc<dyn ExprPlanner>] {
240        &self.expr_planners
241    }
242}
243
244pub struct Planner {
245    schema: SchemaRef,
246    context_provider: LanceContextProvider,
247    enable_relations: bool,
248}
249
250impl Planner {
251    pub fn new(schema: SchemaRef) -> Self {
252        Self {
253            schema,
254            context_provider: LanceContextProvider::default(),
255            enable_relations: false,
256        }
257    }
258
259    /// If passed with `true`, then the first identifier in column reference
260    /// is parsed as the relation. For example, `table.field.inner` will be
261    /// read as the nested field `field.inner` (`inner` on struct field `field`)
262    /// on the `table` relation. If `false` (the default), then no relations
263    /// are used and all identifiers are assumed to be a nested column path.
264    pub fn with_enable_relations(mut self, enable_relations: bool) -> Self {
265        self.enable_relations = enable_relations;
266        self
267    }
268
269    fn column(&self, idents: &[Ident]) -> Expr {
270        fn handle_remaining_idents(expr: &mut Expr, idents: &[Ident]) {
271            for ident in idents {
272                *expr = Expr::ScalarFunction(ScalarFunction {
273                    args: vec![
274                        std::mem::take(expr),
275                        Expr::Literal(ScalarValue::Utf8(Some(ident.value.clone())), None),
276                    ],
277                    func: Arc::new(ScalarUDF::new_from_impl(GetFieldFunc::default())),
278                });
279            }
280        }
281
282        if self.enable_relations && idents.len() > 1 {
283            // Create qualified column reference (relation.column)
284            let relation = &idents[0].value;
285            let column_name = &idents[1].value;
286            let column = Expr::Column(Column::new(Some(relation.clone()), column_name.clone()));
287            let mut result = column;
288            handle_remaining_idents(&mut result, &idents[2..]);
289            result
290        } else {
291            // Default behavior - treat as struct field access
292            let mut column = col(&idents[0].value);
293            handle_remaining_idents(&mut column, &idents[1..]);
294            column
295        }
296    }
297
298    fn binary_op(&self, op: &BinaryOperator) -> Result<Operator> {
299        Ok(match op {
300            BinaryOperator::Plus => Operator::Plus,
301            BinaryOperator::Minus => Operator::Minus,
302            BinaryOperator::Multiply => Operator::Multiply,
303            BinaryOperator::Divide => Operator::Divide,
304            BinaryOperator::Modulo => Operator::Modulo,
305            BinaryOperator::StringConcat => Operator::StringConcat,
306            BinaryOperator::Gt => Operator::Gt,
307            BinaryOperator::Lt => Operator::Lt,
308            BinaryOperator::GtEq => Operator::GtEq,
309            BinaryOperator::LtEq => Operator::LtEq,
310            BinaryOperator::Eq => Operator::Eq,
311            BinaryOperator::NotEq => Operator::NotEq,
312            BinaryOperator::And => Operator::And,
313            BinaryOperator::Or => Operator::Or,
314            _ => {
315                return Err(Error::invalid_input(
316                    format!("Operator {op} is not supported"),
317                    location!(),
318                ));
319            }
320        })
321    }
322
323    fn binary_expr(&self, left: &SQLExpr, op: &BinaryOperator, right: &SQLExpr) -> Result<Expr> {
324        Ok(Expr::BinaryExpr(BinaryExpr::new(
325            Box::new(self.parse_sql_expr(left)?),
326            self.binary_op(op)?,
327            Box::new(self.parse_sql_expr(right)?),
328        )))
329    }
330
331    fn unary_expr(&self, op: &UnaryOperator, expr: &SQLExpr) -> Result<Expr> {
332        Ok(match op {
333            UnaryOperator::Not | UnaryOperator::PGBitwiseNot => {
334                Expr::Not(Box::new(self.parse_sql_expr(expr)?))
335            }
336
337            UnaryOperator::Minus => {
338                use datafusion::logical_expr::lit;
339                match expr {
340                    SQLExpr::Value(ValueWithSpan { value: Value::Number(n, _), ..}) => match n.parse::<i64>() {
341                        Ok(n) => lit(-n),
342                        Err(_) => lit(-n
343                            .parse::<f64>()
344                            .map_err(|_e| {
345                                Error::invalid_input(
346                                    format!("negative operator can be only applied to integer and float operands, got: {n}"),
347                                    location!(),
348                                )
349                            })?),
350                    },
351                    _ => {
352                        Expr::Negative(Box::new(self.parse_sql_expr(expr)?))
353                    }
354                }
355            }
356
357            _ => {
358                return Err(Error::invalid_input(
359                    format!("Unary operator '{:?}' is not supported", op),
360                    location!(),
361                ));
362            }
363        })
364    }
365
366    // See datafusion `sqlToRel::parse_sql_number()`
367    fn number(&self, value: &str, negative: bool) -> Result<Expr> {
368        use datafusion::logical_expr::lit;
369        let value: Cow<str> = if negative {
370            Cow::Owned(format!("-{}", value))
371        } else {
372            Cow::Borrowed(value)
373        };
374        if let Ok(n) = value.parse::<i64>() {
375            Ok(lit(n))
376        } else {
377            value.parse::<f64>().map(lit).map_err(|_| {
378                Error::invalid_input(
379                    format!("'{value}' is not supported number value."),
380                    location!(),
381                )
382            })
383        }
384    }
385
386    fn value(&self, value: &Value) -> Result<Expr> {
387        Ok(match value {
388            Value::Number(v, _) => self.number(v.as_str(), false)?,
389            Value::SingleQuotedString(s) => Expr::Literal(ScalarValue::Utf8(Some(s.clone())), None),
390            Value::HexStringLiteral(hsl) => {
391                Expr::Literal(ScalarValue::Binary(Self::try_decode_hex_literal(hsl)), None)
392            }
393            Value::DoubleQuotedString(s) => Expr::Literal(ScalarValue::Utf8(Some(s.clone())), None),
394            Value::Boolean(v) => Expr::Literal(ScalarValue::Boolean(Some(*v)), None),
395            Value::Null => Expr::Literal(ScalarValue::Null, None),
396            _ => todo!(),
397        })
398    }
399
400    fn parse_function_args(&self, func_args: &FunctionArg) -> Result<Expr> {
401        match func_args {
402            FunctionArg::Unnamed(FunctionArgExpr::Expr(expr)) => self.parse_sql_expr(expr),
403            _ => Err(Error::invalid_input(
404                format!("Unsupported function args: {:?}", func_args),
405                location!(),
406            )),
407        }
408    }
409
410    // We now use datafusion to parse functions.  This allows us to use datafusion's
411    // entire collection of functions (previously we had just hard-coded support for two functions).
412    //
413    // Unfortunately, one of those two functions was is_valid and the reason we needed it was because
414    // this is a function that comes from duckdb.  Datafusion does not consider is_valid to be a function
415    // but rather an AST node (Expr::IsNotNull) and so we need to handle this case specially.
416    fn legacy_parse_function(&self, func: &Function) -> Result<Expr> {
417        match &func.args {
418            FunctionArguments::List(args) => {
419                if func.name.0.len() != 1 {
420                    return Err(Error::invalid_input(
421                        format!("Function name must have 1 part, got: {:?}", func.name.0),
422                        location!(),
423                    ));
424                }
425                Ok(Expr::IsNotNull(Box::new(
426                    self.parse_function_args(&args.args[0])?,
427                )))
428            }
429            _ => Err(Error::invalid_input(
430                format!("Unsupported function args: {:?}", &func.args),
431                location!(),
432            )),
433        }
434    }
435
436    fn parse_function(&self, function: SQLExpr) -> Result<Expr> {
437        if let SQLExpr::Function(function) = &function {
438            if let Some(ObjectNamePart::Identifier(name)) = &function.name.0.first() {
439                if &name.value == "is_valid" {
440                    return self.legacy_parse_function(function);
441                }
442            }
443        }
444        let sql_to_rel = SqlToRel::new_with_options(
445            &self.context_provider,
446            ParserOptions {
447                parse_float_as_decimal: false,
448                enable_ident_normalization: false,
449                support_varchar_with_length: false,
450                enable_options_value_normalization: false,
451                collect_spans: false,
452                map_string_types_to_utf8view: false,
453                default_null_ordering: NullOrdering::NullsMax,
454            },
455        );
456
457        let mut planner_context = PlannerContext::default();
458        let schema = DFSchema::try_from(self.schema.as_ref().clone())?;
459        sql_to_rel
460            .sql_to_expr(function, &schema, &mut planner_context)
461            .map_err(|e| Error::invalid_input(format!("Error parsing function: {e}"), location!()))
462    }
463
464    fn parse_type(&self, data_type: &SQLDataType) -> Result<ArrowDataType> {
465        const SUPPORTED_TYPES: [&str; 13] = [
466            "int [unsigned]",
467            "tinyint [unsigned]",
468            "smallint [unsigned]",
469            "bigint [unsigned]",
470            "float",
471            "double",
472            "string",
473            "binary",
474            "date",
475            "timestamp(precision)",
476            "datetime(precision)",
477            "decimal(precision,scale)",
478            "boolean",
479        ];
480        match data_type {
481            SQLDataType::String(_) => Ok(ArrowDataType::Utf8),
482            SQLDataType::Binary(_) => Ok(ArrowDataType::Binary),
483            SQLDataType::Float(_) => Ok(ArrowDataType::Float32),
484            SQLDataType::Double(_) => Ok(ArrowDataType::Float64),
485            SQLDataType::Boolean => Ok(ArrowDataType::Boolean),
486            SQLDataType::TinyInt(_) => Ok(ArrowDataType::Int8),
487            SQLDataType::SmallInt(_) => Ok(ArrowDataType::Int16),
488            SQLDataType::Int(_) | SQLDataType::Integer(_) => Ok(ArrowDataType::Int32),
489            SQLDataType::BigInt(_) => Ok(ArrowDataType::Int64),
490            SQLDataType::TinyIntUnsigned(_) => Ok(ArrowDataType::UInt8),
491            SQLDataType::SmallIntUnsigned(_) => Ok(ArrowDataType::UInt16),
492            SQLDataType::IntUnsigned(_) | SQLDataType::IntegerUnsigned(_) => {
493                Ok(ArrowDataType::UInt32)
494            }
495            SQLDataType::BigIntUnsigned(_) => Ok(ArrowDataType::UInt64),
496            SQLDataType::Date => Ok(ArrowDataType::Date32),
497            SQLDataType::Timestamp(resolution, tz) => {
498                match tz {
499                    TimezoneInfo::None => {}
500                    _ => {
501                        return Err(Error::invalid_input(
502                            "Timezone not supported in timestamp".to_string(),
503                            location!(),
504                        ));
505                    }
506                };
507                let time_unit = match resolution {
508                    // Default to microsecond to match PyArrow
509                    None => TimeUnit::Microsecond,
510                    Some(0) => TimeUnit::Second,
511                    Some(3) => TimeUnit::Millisecond,
512                    Some(6) => TimeUnit::Microsecond,
513                    Some(9) => TimeUnit::Nanosecond,
514                    _ => {
515                        return Err(Error::invalid_input(
516                            format!("Unsupported datetime resolution: {:?}", resolution),
517                            location!(),
518                        ));
519                    }
520                };
521                Ok(ArrowDataType::Timestamp(time_unit, None))
522            }
523            SQLDataType::Datetime(resolution) => {
524                let time_unit = match resolution {
525                    None => TimeUnit::Microsecond,
526                    Some(0) => TimeUnit::Second,
527                    Some(3) => TimeUnit::Millisecond,
528                    Some(6) => TimeUnit::Microsecond,
529                    Some(9) => TimeUnit::Nanosecond,
530                    _ => {
531                        return Err(Error::invalid_input(
532                            format!("Unsupported datetime resolution: {:?}", resolution),
533                            location!(),
534                        ));
535                    }
536                };
537                Ok(ArrowDataType::Timestamp(time_unit, None))
538            }
539            SQLDataType::Decimal(number_info) => match number_info {
540                ExactNumberInfo::PrecisionAndScale(precision, scale) => {
541                    Ok(ArrowDataType::Decimal128(*precision as u8, *scale as i8))
542                }
543                _ => Err(Error::invalid_input(
544                    format!(
545                        "Must provide precision and scale for decimal: {:?}",
546                        number_info
547                    ),
548                    location!(),
549                )),
550            },
551            _ => Err(Error::invalid_input(
552                format!(
553                    "Unsupported data type: {:?}. Supported types: {:?}",
554                    data_type, SUPPORTED_TYPES
555                ),
556                location!(),
557            )),
558        }
559    }
560
561    fn plan_field_access(&self, mut field_access_expr: RawFieldAccessExpr) -> Result<Expr> {
562        let df_schema = DFSchema::try_from(self.schema.as_ref().clone())?;
563        for planner in self.context_provider.get_expr_planners() {
564            match planner.plan_field_access(field_access_expr, &df_schema)? {
565                PlannerResult::Planned(expr) => return Ok(expr),
566                PlannerResult::Original(expr) => {
567                    field_access_expr = expr;
568                }
569            }
570        }
571        Err(Error::invalid_input(
572            "Field access could not be planned",
573            location!(),
574        ))
575    }
576
577    fn parse_sql_expr(&self, expr: &SQLExpr) -> Result<Expr> {
578        match expr {
579            SQLExpr::Identifier(id) => {
580                // Users can pass string literals wrapped in `"`.
581                // (Normally SQL only allows single quotes.)
582                if id.quote_style == Some('"') {
583                    Ok(Expr::Literal(
584                        ScalarValue::Utf8(Some(id.value.clone())),
585                        None,
586                    ))
587                // Users can wrap identifiers with ` to reference non-standard
588                // names, such as uppercase or spaces.
589                } else if id.quote_style == Some('`') {
590                    Ok(Expr::Column(Column::from_name(id.value.clone())))
591                } else {
592                    Ok(self.column(vec![id.clone()].as_slice()))
593                }
594            }
595            SQLExpr::CompoundIdentifier(ids) => Ok(self.column(ids.as_slice())),
596            SQLExpr::BinaryOp { left, op, right } => self.binary_expr(left, op, right),
597            SQLExpr::UnaryOp { op, expr } => self.unary_expr(op, expr),
598            SQLExpr::Value(value) => self.value(&value.value),
599            SQLExpr::Array(SQLArray { elem, .. }) => {
600                let mut values = vec![];
601
602                let array_literal_error = |pos: usize, value: &_| {
603                    Err(Error::invalid_input(
604                        format!(
605                            "Expected a literal value in array, instead got {} at position {}",
606                            value, pos
607                        ),
608                        location!(),
609                    ))
610                };
611
612                for (pos, expr) in elem.iter().enumerate() {
613                    match expr {
614                        SQLExpr::Value(value) => {
615                            if let Expr::Literal(value, _) = self.value(&value.value)? {
616                                values.push(value);
617                            } else {
618                                return array_literal_error(pos, expr);
619                            }
620                        }
621                        SQLExpr::UnaryOp {
622                            op: UnaryOperator::Minus,
623                            expr,
624                        } => {
625                            if let SQLExpr::Value(ValueWithSpan {
626                                value: Value::Number(number, _),
627                                ..
628                            }) = expr.as_ref()
629                            {
630                                if let Expr::Literal(value, _) = self.number(number, true)? {
631                                    values.push(value);
632                                } else {
633                                    return array_literal_error(pos, expr);
634                                }
635                            } else {
636                                return array_literal_error(pos, expr);
637                            }
638                        }
639                        _ => {
640                            return array_literal_error(pos, expr);
641                        }
642                    }
643                }
644
645                let field = if !values.is_empty() {
646                    let data_type = values[0].data_type();
647
648                    for value in &mut values {
649                        if value.data_type() != data_type {
650                            *value = safe_coerce_scalar(value, &data_type).ok_or_else(|| Error::invalid_input(
651                                format!("Array expressions must have a consistent datatype. Expected: {}, got: {}", data_type, value.data_type()),
652                                location!()
653                            ))?;
654                        }
655                    }
656                    Field::new("item", data_type, true)
657                } else {
658                    Field::new("item", ArrowDataType::Null, true)
659                };
660
661                let values = values
662                    .into_iter()
663                    .map(|v| v.to_array().map_err(Error::from))
664                    .collect::<Result<Vec<_>>>()?;
665                let array_refs = values.iter().map(|v| v.as_ref()).collect::<Vec<_>>();
666                let values = concat(&array_refs)?;
667                let values = ListArray::try_new(
668                    field.into(),
669                    OffsetBuffer::from_lengths([values.len()]),
670                    values,
671                    None,
672                )?;
673
674                Ok(Expr::Literal(ScalarValue::List(Arc::new(values)), None))
675            }
676            // For example, DATE '2020-01-01'
677            SQLExpr::TypedString { data_type, value } => {
678                let value = value.clone().into_string().expect_ok()?;
679                Ok(Expr::Cast(datafusion::logical_expr::Cast {
680                    expr: Box::new(Expr::Literal(ScalarValue::Utf8(Some(value)), None)),
681                    data_type: self.parse_type(data_type)?,
682                }))
683            }
684            SQLExpr::IsFalse(expr) => Ok(Expr::IsFalse(Box::new(self.parse_sql_expr(expr)?))),
685            SQLExpr::IsNotFalse(expr) => Ok(Expr::IsNotFalse(Box::new(self.parse_sql_expr(expr)?))),
686            SQLExpr::IsTrue(expr) => Ok(Expr::IsTrue(Box::new(self.parse_sql_expr(expr)?))),
687            SQLExpr::IsNotTrue(expr) => Ok(Expr::IsNotTrue(Box::new(self.parse_sql_expr(expr)?))),
688            SQLExpr::IsNull(expr) => Ok(Expr::IsNull(Box::new(self.parse_sql_expr(expr)?))),
689            SQLExpr::IsNotNull(expr) => Ok(Expr::IsNotNull(Box::new(self.parse_sql_expr(expr)?))),
690            SQLExpr::InList {
691                expr,
692                list,
693                negated,
694            } => {
695                let value_expr = self.parse_sql_expr(expr)?;
696                let list_exprs = list
697                    .iter()
698                    .map(|e| self.parse_sql_expr(e))
699                    .collect::<Result<Vec<_>>>()?;
700                Ok(value_expr.in_list(list_exprs, *negated))
701            }
702            SQLExpr::Nested(inner) => self.parse_sql_expr(inner.as_ref()),
703            SQLExpr::Function(_) => self.parse_function(expr.clone()),
704            SQLExpr::ILike {
705                negated,
706                expr,
707                pattern,
708                escape_char,
709                any: _,
710            } => Ok(Expr::Like(Like::new(
711                *negated,
712                Box::new(self.parse_sql_expr(expr)?),
713                Box::new(self.parse_sql_expr(pattern)?),
714                match escape_char {
715                    Some(Value::SingleQuotedString(char)) => char.chars().next(),
716                    Some(value) => return Err(Error::invalid_input(
717                        format!("Invalid escape character in LIKE expression. Expected a single character wrapped with single quotes, got {}", value),
718                        location!()
719                    )),
720                    None => None,
721                },
722                true,
723            ))),
724            SQLExpr::Like {
725                negated,
726                expr,
727                pattern,
728                escape_char,
729                any: _,
730            } => Ok(Expr::Like(Like::new(
731                *negated,
732                Box::new(self.parse_sql_expr(expr)?),
733                Box::new(self.parse_sql_expr(pattern)?),
734                match escape_char {
735                    Some(Value::SingleQuotedString(char)) => char.chars().next(),
736                    Some(value) => return Err(Error::invalid_input(
737                        format!("Invalid escape character in LIKE expression. Expected a single character wrapped with single quotes, got {}", value),
738                        location!()
739                    )),
740                    None => None,
741                },
742                false,
743            ))),
744            SQLExpr::Cast {
745                expr,
746                data_type,
747                kind,
748                ..
749            } => match kind {
750                datafusion::sql::sqlparser::ast::CastKind::TryCast
751                | datafusion::sql::sqlparser::ast::CastKind::SafeCast => {
752                    Ok(Expr::TryCast(datafusion::logical_expr::TryCast {
753                        expr: Box::new(self.parse_sql_expr(expr)?),
754                        data_type: self.parse_type(data_type)?,
755                    }))
756                }
757                _ => Ok(Expr::Cast(datafusion::logical_expr::Cast {
758                    expr: Box::new(self.parse_sql_expr(expr)?),
759                    data_type: self.parse_type(data_type)?,
760                })),
761            },
762            SQLExpr::JsonAccess { .. } => Err(Error::invalid_input(
763                "JSON access is not supported",
764                location!(),
765            )),
766            SQLExpr::CompoundFieldAccess { root, access_chain } => {
767                let mut expr = self.parse_sql_expr(root)?;
768
769                for access in access_chain {
770                    let field_access = match access {
771                        // x.y or x['y']
772                        AccessExpr::Dot(SQLExpr::Identifier(Ident { value: s, .. }))
773                        | AccessExpr::Subscript(Subscript::Index {
774                            index:
775                                SQLExpr::Value(ValueWithSpan {
776                                    value:
777                                        Value::SingleQuotedString(s) | Value::DoubleQuotedString(s),
778                                    ..
779                                }),
780                        }) => GetFieldAccess::NamedStructField {
781                            name: ScalarValue::from(s.as_str()),
782                        },
783                        AccessExpr::Subscript(Subscript::Index { index }) => {
784                            let key = Box::new(self.parse_sql_expr(index)?);
785                            GetFieldAccess::ListIndex { key }
786                        }
787                        AccessExpr::Subscript(Subscript::Slice { .. }) => {
788                            return Err(Error::invalid_input(
789                                "Slice subscript is not supported",
790                                location!(),
791                            ));
792                        }
793                        _ => {
794                            // Handle other cases like JSON access
795                            // Note: JSON access is not supported in lance
796                            return Err(Error::invalid_input(
797                                "Only dot notation or index access is supported for field access",
798                                location!(),
799                            ));
800                        }
801                    };
802
803                    let field_access_expr = RawFieldAccessExpr { expr, field_access };
804                    expr = self.plan_field_access(field_access_expr)?;
805                }
806
807                Ok(expr)
808            }
809            SQLExpr::Between {
810                expr,
811                negated,
812                low,
813                high,
814            } => {
815                // Parse the main expression and bounds
816                let expr = self.parse_sql_expr(expr)?;
817                let low = self.parse_sql_expr(low)?;
818                let high = self.parse_sql_expr(high)?;
819
820                let between = Expr::Between(Between::new(
821                    Box::new(expr),
822                    *negated,
823                    Box::new(low),
824                    Box::new(high),
825                ));
826                Ok(between)
827            }
828            _ => Err(Error::invalid_input(
829                format!("Expression '{expr}' is not supported SQL in lance"),
830                location!(),
831            )),
832        }
833    }
834
835    /// Create Logical [Expr] from a SQL filter clause.
836    ///
837    /// Note: the returned expression must be passed through [optimize_expr()]
838    /// before being passed to [create_physical_expr()].
839    pub fn parse_filter(&self, filter: &str) -> Result<Expr> {
840        // Allow sqlparser to parse filter as part of ONE SQL statement.
841        let ast_expr = parse_sql_filter(filter)?;
842        let expr = self.parse_sql_expr(&ast_expr)?;
843        let schema = Schema::try_from(self.schema.as_ref())?;
844        let resolved = resolve_expr(&expr, &schema).map_err(|e| {
845            Error::invalid_input(
846                format!("Error resolving filter expression {filter}: {e}"),
847                location!(),
848            )
849        })?;
850
851        Ok(coerce_filter_type_to_boolean(resolved))
852    }
853
854    /// Create Logical [Expr] from a SQL expression.
855    ///
856    /// Note: the returned expression must be passed through [optimize_filter()]
857    /// before being passed to [create_physical_expr()].
858    pub fn parse_expr(&self, expr: &str) -> Result<Expr> {
859        let ast_expr = parse_sql_expr(expr)?;
860        let expr = self.parse_sql_expr(&ast_expr)?;
861        let schema = Schema::try_from(self.schema.as_ref())?;
862        let resolved = resolve_expr(&expr, &schema)?;
863        Ok(resolved)
864    }
865
866    /// Try to decode bytes from hex literal string.
867    ///
868    /// Copied from datafusion because this is not public.
869    ///
870    /// TODO: use SqlToRel from Datafusion directly?
871    fn try_decode_hex_literal(s: &str) -> Option<Vec<u8>> {
872        let hex_bytes = s.as_bytes();
873        let mut decoded_bytes = Vec::with_capacity(hex_bytes.len().div_ceil(2));
874
875        let start_idx = hex_bytes.len() % 2;
876        if start_idx > 0 {
877            // The first byte is formed of only one char.
878            decoded_bytes.push(Self::try_decode_hex_char(hex_bytes[0])?);
879        }
880
881        for i in (start_idx..hex_bytes.len()).step_by(2) {
882            let high = Self::try_decode_hex_char(hex_bytes[i])?;
883            let low = Self::try_decode_hex_char(hex_bytes[i + 1])?;
884            decoded_bytes.push((high << 4) | low);
885        }
886
887        Some(decoded_bytes)
888    }
889
890    /// Try to decode a byte from a hex char.
891    ///
892    /// None will be returned if the input char is hex-invalid.
893    const fn try_decode_hex_char(c: u8) -> Option<u8> {
894        match c {
895            b'A'..=b'F' => Some(c - b'A' + 10),
896            b'a'..=b'f' => Some(c - b'a' + 10),
897            b'0'..=b'9' => Some(c - b'0'),
898            _ => None,
899        }
900    }
901
902    /// Optimize the filter expression and coerce data types.
903    pub fn optimize_expr(&self, expr: Expr) -> Result<Expr> {
904        let df_schema = Arc::new(DFSchema::try_from(self.schema.as_ref().clone())?);
905
906        // DataFusion needs the simplify and coerce passes to be applied before
907        // expressions can be handled by the physical planner.
908        let props = ExecutionProps::default();
909        let simplify_context = SimplifyContext::new(&props).with_schema(df_schema.clone());
910        let simplifier =
911            datafusion::optimizer::simplify_expressions::ExprSimplifier::new(simplify_context);
912
913        let expr = simplifier.simplify(expr)?;
914        let expr = simplifier.coerce(expr, &df_schema)?;
915
916        Ok(expr)
917    }
918
919    /// Create the [`PhysicalExpr`] from a logical [`Expr`]
920    pub fn create_physical_expr(&self, expr: &Expr) -> Result<Arc<dyn PhysicalExpr>> {
921        let df_schema = Arc::new(DFSchema::try_from(self.schema.as_ref().clone())?);
922        Ok(datafusion::physical_expr::create_physical_expr(
923            expr,
924            df_schema.as_ref(),
925            &Default::default(),
926        )?)
927    }
928
929    /// Collect the columns in the expression.
930    ///
931    /// The columns are returned in sorted order.
932    ///
933    /// If the expr refers to nested columns these will be returned
934    /// as dotted paths (x.y.z)
935    pub fn column_names_in_expr(expr: &Expr) -> Vec<String> {
936        let mut visitor = ColumnCapturingVisitor {
937            current_path: VecDeque::new(),
938            columns: BTreeSet::new(),
939        };
940        expr.visit(&mut visitor).unwrap();
941        visitor.columns.into_iter().collect()
942    }
943}
944
945struct ColumnCapturingVisitor {
946    // Current column path. If this is empty, we are not in a column expression.
947    current_path: VecDeque<String>,
948    columns: BTreeSet<String>,
949}
950
951impl TreeNodeVisitor<'_> for ColumnCapturingVisitor {
952    type Node = Expr;
953
954    fn f_down(&mut self, node: &Self::Node) -> DFResult<TreeNodeRecursion> {
955        match node {
956            Expr::Column(Column { name, .. }) => {
957                // Build the field path from the column name and any nested fields
958                // The nested field names from get_field already come as literal strings,
959                // so we just need to concatenate them properly
960                let mut path = name.clone();
961                for part in self.current_path.drain(..) {
962                    path.push('.');
963                    // Check if the part needs quoting (contains dots)
964                    if part.contains('.') || part.contains('`') {
965                        // Quote the field name with backticks and escape any existing backticks
966                        let escaped = part.replace('`', "``");
967                        path.push('`');
968                        path.push_str(&escaped);
969                        path.push('`');
970                    } else {
971                        path.push_str(&part);
972                    }
973                }
974                self.columns.insert(path);
975                self.current_path.clear();
976            }
977            Expr::ScalarFunction(udf) => {
978                if udf.name() == GetFieldFunc::default().name() {
979                    if let Some(name) = get_as_string_scalar_opt(&udf.args[1]) {
980                        self.current_path.push_front(name.to_string())
981                    } else {
982                        self.current_path.clear();
983                    }
984                } else {
985                    self.current_path.clear();
986                }
987            }
988            _ => {
989                self.current_path.clear();
990            }
991        }
992
993        Ok(TreeNodeRecursion::Continue)
994    }
995}
996
997#[cfg(test)]
998mod tests {
999
1000    use crate::logical_expr::ExprExt;
1001
1002    use super::*;
1003
1004    use arrow::datatypes::Float64Type;
1005    use arrow_array::{
1006        ArrayRef, BooleanArray, Float32Array, Int32Array, Int64Array, RecordBatch, StringArray,
1007        StructArray, TimestampMicrosecondArray, TimestampMillisecondArray,
1008        TimestampNanosecondArray, TimestampSecondArray,
1009    };
1010    use arrow_schema::{DataType, Fields, Schema};
1011    use datafusion::{
1012        logical_expr::{lit, Cast},
1013        prelude::{array_element, get_field},
1014    };
1015    use datafusion_functions::core::expr_ext::FieldAccessor;
1016
1017    #[test]
1018    fn test_parse_filter_simple() {
1019        let schema = Arc::new(Schema::new(vec![
1020            Field::new("i", DataType::Int32, false),
1021            Field::new("s", DataType::Utf8, true),
1022            Field::new(
1023                "st",
1024                DataType::Struct(Fields::from(vec![
1025                    Field::new("x", DataType::Float32, false),
1026                    Field::new("y", DataType::Float32, false),
1027                ])),
1028                true,
1029            ),
1030        ]));
1031
1032        let planner = Planner::new(schema.clone());
1033
1034        let expected = col("i")
1035            .gt(lit(3_i32))
1036            .and(col("st").field_newstyle("x").lt_eq(lit(5.0_f32)))
1037            .and(
1038                col("s")
1039                    .eq(lit("str-4"))
1040                    .or(col("s").in_list(vec![lit("str-4"), lit("str-5")], false)),
1041            );
1042
1043        // double quotes
1044        let expr = planner
1045            .parse_filter("i > 3 AND st.x <= 5.0 AND (s == 'str-4' OR s in ('str-4', 'str-5'))")
1046            .unwrap();
1047        assert_eq!(expr, expected);
1048
1049        // single quote
1050        let expr = planner
1051            .parse_filter("i > 3 AND st.x <= 5.0 AND (s = 'str-4' OR s in ('str-4', 'str-5'))")
1052            .unwrap();
1053
1054        let physical_expr = planner.create_physical_expr(&expr).unwrap();
1055
1056        let batch = RecordBatch::try_new(
1057            schema,
1058            vec![
1059                Arc::new(Int32Array::from_iter_values(0..10)) as ArrayRef,
1060                Arc::new(StringArray::from_iter_values(
1061                    (0..10).map(|v| format!("str-{}", v)),
1062                )),
1063                Arc::new(StructArray::from(vec![
1064                    (
1065                        Arc::new(Field::new("x", DataType::Float32, false)),
1066                        Arc::new(Float32Array::from_iter_values((0..10).map(|v| v as f32)))
1067                            as ArrayRef,
1068                    ),
1069                    (
1070                        Arc::new(Field::new("y", DataType::Float32, false)),
1071                        Arc::new(Float32Array::from_iter_values(
1072                            (0..10).map(|v| (v * 10) as f32),
1073                        )),
1074                    ),
1075                ])),
1076            ],
1077        )
1078        .unwrap();
1079        let predicates = physical_expr.evaluate(&batch).unwrap();
1080        assert_eq!(
1081            predicates.into_array(0).unwrap().as_ref(),
1082            &BooleanArray::from(vec![
1083                false, false, false, false, true, true, false, false, false, false
1084            ])
1085        );
1086    }
1087
1088    #[test]
1089    fn test_nested_col_refs() {
1090        let schema = Arc::new(Schema::new(vec![
1091            Field::new("s0", DataType::Utf8, true),
1092            Field::new(
1093                "st",
1094                DataType::Struct(Fields::from(vec![
1095                    Field::new("s1", DataType::Utf8, true),
1096                    Field::new(
1097                        "st",
1098                        DataType::Struct(Fields::from(vec![Field::new(
1099                            "s2",
1100                            DataType::Utf8,
1101                            true,
1102                        )])),
1103                        true,
1104                    ),
1105                ])),
1106                true,
1107            ),
1108        ]));
1109
1110        let planner = Planner::new(schema);
1111
1112        fn assert_column_eq(planner: &Planner, expr: &str, expected: &Expr) {
1113            let expr = planner.parse_filter(&format!("{expr} = 'val'")).unwrap();
1114            assert!(matches!(
1115                expr,
1116                Expr::BinaryExpr(BinaryExpr {
1117                    left: _,
1118                    op: Operator::Eq,
1119                    right: _
1120                })
1121            ));
1122            if let Expr::BinaryExpr(BinaryExpr { left, .. }) = expr {
1123                assert_eq!(left.as_ref(), expected);
1124            }
1125        }
1126
1127        let expected = Expr::Column(Column::new_unqualified("s0"));
1128        assert_column_eq(&planner, "s0", &expected);
1129        assert_column_eq(&planner, "`s0`", &expected);
1130
1131        let expected = Expr::ScalarFunction(ScalarFunction {
1132            func: Arc::new(ScalarUDF::new_from_impl(GetFieldFunc::default())),
1133            args: vec![
1134                Expr::Column(Column::new_unqualified("st")),
1135                Expr::Literal(ScalarValue::Utf8(Some("s1".to_string())), None),
1136            ],
1137        });
1138        assert_column_eq(&planner, "st.s1", &expected);
1139        assert_column_eq(&planner, "`st`.`s1`", &expected);
1140        assert_column_eq(&planner, "st.`s1`", &expected);
1141
1142        let expected = Expr::ScalarFunction(ScalarFunction {
1143            func: Arc::new(ScalarUDF::new_from_impl(GetFieldFunc::default())),
1144            args: vec![
1145                Expr::ScalarFunction(ScalarFunction {
1146                    func: Arc::new(ScalarUDF::new_from_impl(GetFieldFunc::default())),
1147                    args: vec![
1148                        Expr::Column(Column::new_unqualified("st")),
1149                        Expr::Literal(ScalarValue::Utf8(Some("st".to_string())), None),
1150                    ],
1151                }),
1152                Expr::Literal(ScalarValue::Utf8(Some("s2".to_string())), None),
1153            ],
1154        });
1155
1156        assert_column_eq(&planner, "st.st.s2", &expected);
1157        assert_column_eq(&planner, "`st`.`st`.`s2`", &expected);
1158        assert_column_eq(&planner, "st.st.`s2`", &expected);
1159        assert_column_eq(&planner, "st['st'][\"s2\"]", &expected);
1160    }
1161
1162    #[test]
1163    fn test_nested_list_refs() {
1164        let schema = Arc::new(Schema::new(vec![Field::new(
1165            "l",
1166            DataType::List(Arc::new(Field::new(
1167                "item",
1168                DataType::Struct(Fields::from(vec![Field::new("f1", DataType::Utf8, true)])),
1169                true,
1170            ))),
1171            true,
1172        )]));
1173
1174        let planner = Planner::new(schema);
1175
1176        let expected = array_element(col("l"), lit(0_i64));
1177        let expr = planner.parse_expr("l[0]").unwrap();
1178        assert_eq!(expr, expected);
1179
1180        let expected = get_field(array_element(col("l"), lit(0_i64)), "f1");
1181        let expr = planner.parse_expr("l[0]['f1']").unwrap();
1182        assert_eq!(expr, expected);
1183
1184        // FIXME: This should work, but sqlparser doesn't recognize anything
1185        // after the period for some reason.
1186        // let expr = planner.parse_expr("l[0].f1").unwrap();
1187        // assert_eq!(expr, expected);
1188    }
1189
1190    #[test]
1191    fn test_negative_expressions() {
1192        let schema = Arc::new(Schema::new(vec![Field::new("x", DataType::Int64, false)]));
1193
1194        let planner = Planner::new(schema.clone());
1195
1196        let expected = col("x")
1197            .gt(lit(-3_i64))
1198            .and(col("x").lt(-(lit(-5_i64) + lit(3_i64))));
1199
1200        let expr = planner.parse_filter("x > -3 AND x < -(-5 + 3)").unwrap();
1201
1202        assert_eq!(expr, expected);
1203
1204        let physical_expr = planner.create_physical_expr(&expr).unwrap();
1205
1206        let batch = RecordBatch::try_new(
1207            schema,
1208            vec![Arc::new(Int64Array::from_iter_values(-5..5)) as ArrayRef],
1209        )
1210        .unwrap();
1211        let predicates = physical_expr.evaluate(&batch).unwrap();
1212        assert_eq!(
1213            predicates.into_array(0).unwrap().as_ref(),
1214            &BooleanArray::from(vec![
1215                false, false, false, true, true, true, true, false, false, false
1216            ])
1217        );
1218    }
1219
1220    #[test]
1221    fn test_negative_array_expressions() {
1222        let schema = Arc::new(Schema::new(vec![Field::new("x", DataType::Int64, false)]));
1223
1224        let planner = Planner::new(schema);
1225
1226        let expected = Expr::Literal(
1227            ScalarValue::List(Arc::new(
1228                ListArray::from_iter_primitive::<Float64Type, _, _>(vec![Some(
1229                    [-1_f64, -2.0, -3.0, -4.0, -5.0].map(Some),
1230                )]),
1231            )),
1232            None,
1233        );
1234
1235        let expr = planner
1236            .parse_expr("[-1.0, -2.0, -3.0, -4.0, -5.0]")
1237            .unwrap();
1238
1239        assert_eq!(expr, expected);
1240    }
1241
1242    #[test]
1243    fn test_sql_like() {
1244        let schema = Arc::new(Schema::new(vec![Field::new("s", DataType::Utf8, true)]));
1245
1246        let planner = Planner::new(schema.clone());
1247
1248        let expected = col("s").like(lit("str-4"));
1249        // single quote
1250        let expr = planner.parse_filter("s LIKE 'str-4'").unwrap();
1251        assert_eq!(expr, expected);
1252        let physical_expr = planner.create_physical_expr(&expr).unwrap();
1253
1254        let batch = RecordBatch::try_new(
1255            schema,
1256            vec![Arc::new(StringArray::from_iter_values(
1257                (0..10).map(|v| format!("str-{}", v)),
1258            ))],
1259        )
1260        .unwrap();
1261        let predicates = physical_expr.evaluate(&batch).unwrap();
1262        assert_eq!(
1263            predicates.into_array(0).unwrap().as_ref(),
1264            &BooleanArray::from(vec![
1265                false, false, false, false, true, false, false, false, false, false
1266            ])
1267        );
1268    }
1269
1270    #[test]
1271    fn test_not_like() {
1272        let schema = Arc::new(Schema::new(vec![Field::new("s", DataType::Utf8, true)]));
1273
1274        let planner = Planner::new(schema.clone());
1275
1276        let expected = col("s").not_like(lit("str-4"));
1277        // single quote
1278        let expr = planner.parse_filter("s NOT LIKE 'str-4'").unwrap();
1279        assert_eq!(expr, expected);
1280        let physical_expr = planner.create_physical_expr(&expr).unwrap();
1281
1282        let batch = RecordBatch::try_new(
1283            schema,
1284            vec![Arc::new(StringArray::from_iter_values(
1285                (0..10).map(|v| format!("str-{}", v)),
1286            ))],
1287        )
1288        .unwrap();
1289        let predicates = physical_expr.evaluate(&batch).unwrap();
1290        assert_eq!(
1291            predicates.into_array(0).unwrap().as_ref(),
1292            &BooleanArray::from(vec![
1293                true, true, true, true, false, true, true, true, true, true
1294            ])
1295        );
1296    }
1297
1298    #[test]
1299    fn test_sql_is_in() {
1300        let schema = Arc::new(Schema::new(vec![Field::new("s", DataType::Utf8, true)]));
1301
1302        let planner = Planner::new(schema.clone());
1303
1304        let expected = col("s").in_list(vec![lit("str-4"), lit("str-5")], false);
1305        // single quote
1306        let expr = planner.parse_filter("s IN ('str-4', 'str-5')").unwrap();
1307        assert_eq!(expr, expected);
1308        let physical_expr = planner.create_physical_expr(&expr).unwrap();
1309
1310        let batch = RecordBatch::try_new(
1311            schema,
1312            vec![Arc::new(StringArray::from_iter_values(
1313                (0..10).map(|v| format!("str-{}", v)),
1314            ))],
1315        )
1316        .unwrap();
1317        let predicates = physical_expr.evaluate(&batch).unwrap();
1318        assert_eq!(
1319            predicates.into_array(0).unwrap().as_ref(),
1320            &BooleanArray::from(vec![
1321                false, false, false, false, true, true, false, false, false, false
1322            ])
1323        );
1324    }
1325
1326    #[test]
1327    fn test_sql_is_null() {
1328        let schema = Arc::new(Schema::new(vec![Field::new("s", DataType::Utf8, true)]));
1329
1330        let planner = Planner::new(schema.clone());
1331
1332        let expected = col("s").is_null();
1333        let expr = planner.parse_filter("s IS NULL").unwrap();
1334        assert_eq!(expr, expected);
1335        let physical_expr = planner.create_physical_expr(&expr).unwrap();
1336
1337        let batch = RecordBatch::try_new(
1338            schema,
1339            vec![Arc::new(StringArray::from_iter((0..10).map(|v| {
1340                if v % 3 == 0 {
1341                    Some(format!("str-{}", v))
1342                } else {
1343                    None
1344                }
1345            })))],
1346        )
1347        .unwrap();
1348        let predicates = physical_expr.evaluate(&batch).unwrap();
1349        assert_eq!(
1350            predicates.into_array(0).unwrap().as_ref(),
1351            &BooleanArray::from(vec![
1352                false, true, true, false, true, true, false, true, true, false
1353            ])
1354        );
1355
1356        let expr = planner.parse_filter("s IS NOT NULL").unwrap();
1357        let physical_expr = planner.create_physical_expr(&expr).unwrap();
1358        let predicates = physical_expr.evaluate(&batch).unwrap();
1359        assert_eq!(
1360            predicates.into_array(0).unwrap().as_ref(),
1361            &BooleanArray::from(vec![
1362                true, false, false, true, false, false, true, false, false, true,
1363            ])
1364        );
1365    }
1366
1367    #[test]
1368    fn test_sql_invert() {
1369        let schema = Arc::new(Schema::new(vec![Field::new("s", DataType::Boolean, true)]));
1370
1371        let planner = Planner::new(schema.clone());
1372
1373        let expr = planner.parse_filter("NOT s").unwrap();
1374        let physical_expr = planner.create_physical_expr(&expr).unwrap();
1375
1376        let batch = RecordBatch::try_new(
1377            schema,
1378            vec![Arc::new(BooleanArray::from_iter(
1379                (0..10).map(|v| Some(v % 3 == 0)),
1380            ))],
1381        )
1382        .unwrap();
1383        let predicates = physical_expr.evaluate(&batch).unwrap();
1384        assert_eq!(
1385            predicates.into_array(0).unwrap().as_ref(),
1386            &BooleanArray::from(vec![
1387                false, true, true, false, true, true, false, true, true, false
1388            ])
1389        );
1390    }
1391
1392    #[test]
1393    fn test_sql_cast() {
1394        let cases = &[
1395            (
1396                "x = cast('2021-01-01 00:00:00' as timestamp)",
1397                ArrowDataType::Timestamp(TimeUnit::Microsecond, None),
1398            ),
1399            (
1400                "x = cast('2021-01-01 00:00:00' as timestamp(0))",
1401                ArrowDataType::Timestamp(TimeUnit::Second, None),
1402            ),
1403            (
1404                "x = cast('2021-01-01 00:00:00.123' as timestamp(9))",
1405                ArrowDataType::Timestamp(TimeUnit::Nanosecond, None),
1406            ),
1407            (
1408                "x = cast('2021-01-01 00:00:00.123' as datetime(9))",
1409                ArrowDataType::Timestamp(TimeUnit::Nanosecond, None),
1410            ),
1411            ("x = cast('2021-01-01' as date)", ArrowDataType::Date32),
1412            (
1413                "x = cast('1.238' as decimal(9,3))",
1414                ArrowDataType::Decimal128(9, 3),
1415            ),
1416            ("x = cast(1 as float)", ArrowDataType::Float32),
1417            ("x = cast(1 as double)", ArrowDataType::Float64),
1418            ("x = cast(1 as tinyint)", ArrowDataType::Int8),
1419            ("x = cast(1 as smallint)", ArrowDataType::Int16),
1420            ("x = cast(1 as int)", ArrowDataType::Int32),
1421            ("x = cast(1 as integer)", ArrowDataType::Int32),
1422            ("x = cast(1 as bigint)", ArrowDataType::Int64),
1423            ("x = cast(1 as tinyint unsigned)", ArrowDataType::UInt8),
1424            ("x = cast(1 as smallint unsigned)", ArrowDataType::UInt16),
1425            ("x = cast(1 as int unsigned)", ArrowDataType::UInt32),
1426            ("x = cast(1 as integer unsigned)", ArrowDataType::UInt32),
1427            ("x = cast(1 as bigint unsigned)", ArrowDataType::UInt64),
1428            ("x = cast(1 as boolean)", ArrowDataType::Boolean),
1429            ("x = cast(1 as string)", ArrowDataType::Utf8),
1430        ];
1431
1432        for (sql, expected_data_type) in cases {
1433            let schema = Arc::new(Schema::new(vec![Field::new(
1434                "x",
1435                expected_data_type.clone(),
1436                true,
1437            )]));
1438            let planner = Planner::new(schema.clone());
1439            let expr = planner.parse_filter(sql).unwrap();
1440
1441            // Get the thing after 'cast(` but before ' as'.
1442            let expected_value_str = sql
1443                .split("cast(")
1444                .nth(1)
1445                .unwrap()
1446                .split(" as")
1447                .next()
1448                .unwrap();
1449            // Remove any quote marks
1450            let expected_value_str = expected_value_str.trim_matches('\'');
1451
1452            match expr {
1453                Expr::BinaryExpr(BinaryExpr { right, .. }) => match right.as_ref() {
1454                    Expr::Cast(Cast { expr, data_type }) => {
1455                        match expr.as_ref() {
1456                            Expr::Literal(ScalarValue::Utf8(Some(value_str)), _) => {
1457                                assert_eq!(value_str, expected_value_str);
1458                            }
1459                            Expr::Literal(ScalarValue::Int64(Some(value)), _) => {
1460                                assert_eq!(*value, 1);
1461                            }
1462                            _ => panic!("Expected cast to be applied to literal"),
1463                        }
1464                        assert_eq!(data_type, expected_data_type);
1465                    }
1466                    _ => panic!("Expected right to be a cast"),
1467                },
1468                _ => panic!("Expected binary expression"),
1469            }
1470        }
1471    }
1472
1473    #[test]
1474    fn test_sql_literals() {
1475        let cases = &[
1476            (
1477                "x = timestamp '2021-01-01 00:00:00'",
1478                ArrowDataType::Timestamp(TimeUnit::Microsecond, None),
1479            ),
1480            (
1481                "x = timestamp(0) '2021-01-01 00:00:00'",
1482                ArrowDataType::Timestamp(TimeUnit::Second, None),
1483            ),
1484            (
1485                "x = timestamp(9) '2021-01-01 00:00:00.123'",
1486                ArrowDataType::Timestamp(TimeUnit::Nanosecond, None),
1487            ),
1488            ("x = date '2021-01-01'", ArrowDataType::Date32),
1489            ("x = decimal(9,3) '1.238'", ArrowDataType::Decimal128(9, 3)),
1490        ];
1491
1492        for (sql, expected_data_type) in cases {
1493            let schema = Arc::new(Schema::new(vec![Field::new(
1494                "x",
1495                expected_data_type.clone(),
1496                true,
1497            )]));
1498            let planner = Planner::new(schema.clone());
1499            let expr = planner.parse_filter(sql).unwrap();
1500
1501            let expected_value_str = sql.split('\'').nth(1).unwrap();
1502
1503            match expr {
1504                Expr::BinaryExpr(BinaryExpr { right, .. }) => match right.as_ref() {
1505                    Expr::Cast(Cast { expr, data_type }) => {
1506                        match expr.as_ref() {
1507                            Expr::Literal(ScalarValue::Utf8(Some(value_str)), _) => {
1508                                assert_eq!(value_str, expected_value_str);
1509                            }
1510                            _ => panic!("Expected cast to be applied to literal"),
1511                        }
1512                        assert_eq!(data_type, expected_data_type);
1513                    }
1514                    _ => panic!("Expected right to be a cast"),
1515                },
1516                _ => panic!("Expected binary expression"),
1517            }
1518        }
1519    }
1520
1521    #[test]
1522    fn test_sql_array_literals() {
1523        let cases = [
1524            (
1525                "x = [1, 2, 3]",
1526                ArrowDataType::List(Arc::new(Field::new("item", ArrowDataType::Int64, true))),
1527            ),
1528            (
1529                "x = [1, 2, 3]",
1530                ArrowDataType::FixedSizeList(
1531                    Arc::new(Field::new("item", ArrowDataType::Int64, true)),
1532                    3,
1533                ),
1534            ),
1535        ];
1536
1537        for (sql, expected_data_type) in cases {
1538            let schema = Arc::new(Schema::new(vec![Field::new(
1539                "x",
1540                expected_data_type.clone(),
1541                true,
1542            )]));
1543            let planner = Planner::new(schema.clone());
1544            let expr = planner.parse_filter(sql).unwrap();
1545            let expr = planner.optimize_expr(expr).unwrap();
1546
1547            match expr {
1548                Expr::BinaryExpr(BinaryExpr { right, .. }) => match right.as_ref() {
1549                    Expr::Literal(value, _) => {
1550                        assert_eq!(&value.data_type(), &expected_data_type);
1551                    }
1552                    _ => panic!("Expected right to be a literal"),
1553                },
1554                _ => panic!("Expected binary expression"),
1555            }
1556        }
1557    }
1558
1559    #[test]
1560    fn test_sql_between() {
1561        use arrow_array::{Float64Array, Int32Array, TimestampMicrosecondArray};
1562        use arrow_schema::{DataType, Field, Schema, TimeUnit};
1563        use std::sync::Arc;
1564
1565        let schema = Arc::new(Schema::new(vec![
1566            Field::new("x", DataType::Int32, false),
1567            Field::new("y", DataType::Float64, false),
1568            Field::new(
1569                "ts",
1570                DataType::Timestamp(TimeUnit::Microsecond, None),
1571                false,
1572            ),
1573        ]));
1574
1575        let planner = Planner::new(schema.clone());
1576
1577        // Test integer BETWEEN
1578        let expr = planner
1579            .parse_filter("x BETWEEN CAST(3 AS INT) AND CAST(7 AS INT)")
1580            .unwrap();
1581        let physical_expr = planner.create_physical_expr(&expr).unwrap();
1582
1583        // Create timestamp array with values representing:
1584        // 2024-01-01 00:00:00 to 2024-01-01 00:00:09 (in microseconds)
1585        let base_ts = 1704067200000000_i64; // 2024-01-01 00:00:00
1586        let ts_array = TimestampMicrosecondArray::from_iter_values(
1587            (0..10).map(|i| base_ts + i * 1_000_000), // Each value is 1 second apart
1588        );
1589
1590        let batch = RecordBatch::try_new(
1591            schema,
1592            vec![
1593                Arc::new(Int32Array::from_iter_values(0..10)) as ArrayRef,
1594                Arc::new(Float64Array::from_iter_values((0..10).map(|v| v as f64))),
1595                Arc::new(ts_array),
1596            ],
1597        )
1598        .unwrap();
1599
1600        let predicates = physical_expr.evaluate(&batch).unwrap();
1601        assert_eq!(
1602            predicates.into_array(0).unwrap().as_ref(),
1603            &BooleanArray::from(vec![
1604                false, false, false, true, true, true, true, true, false, false
1605            ])
1606        );
1607
1608        // Test NOT BETWEEN
1609        let expr = planner
1610            .parse_filter("x NOT BETWEEN CAST(3 AS INT) AND CAST(7 AS INT)")
1611            .unwrap();
1612        let physical_expr = planner.create_physical_expr(&expr).unwrap();
1613
1614        let predicates = physical_expr.evaluate(&batch).unwrap();
1615        assert_eq!(
1616            predicates.into_array(0).unwrap().as_ref(),
1617            &BooleanArray::from(vec![
1618                true, true, true, false, false, false, false, false, true, true
1619            ])
1620        );
1621
1622        // Test floating point BETWEEN
1623        let expr = planner.parse_filter("y BETWEEN 2.5 AND 6.5").unwrap();
1624        let physical_expr = planner.create_physical_expr(&expr).unwrap();
1625
1626        let predicates = physical_expr.evaluate(&batch).unwrap();
1627        assert_eq!(
1628            predicates.into_array(0).unwrap().as_ref(),
1629            &BooleanArray::from(vec![
1630                false, false, false, true, true, true, true, false, false, false
1631            ])
1632        );
1633
1634        // Test timestamp BETWEEN
1635        let expr = planner
1636            .parse_filter(
1637                "ts BETWEEN timestamp '2024-01-01 00:00:03' AND timestamp '2024-01-01 00:00:07'",
1638            )
1639            .unwrap();
1640        let physical_expr = planner.create_physical_expr(&expr).unwrap();
1641
1642        let predicates = physical_expr.evaluate(&batch).unwrap();
1643        assert_eq!(
1644            predicates.into_array(0).unwrap().as_ref(),
1645            &BooleanArray::from(vec![
1646                false, false, false, true, true, true, true, true, false, false
1647            ])
1648        );
1649    }
1650
1651    #[test]
1652    fn test_sql_comparison() {
1653        // Create a batch with all data types
1654        let batch: Vec<(&str, ArrayRef)> = vec![
1655            (
1656                "timestamp_s",
1657                Arc::new(TimestampSecondArray::from_iter_values(0..10)),
1658            ),
1659            (
1660                "timestamp_ms",
1661                Arc::new(TimestampMillisecondArray::from_iter_values(0..10)),
1662            ),
1663            (
1664                "timestamp_us",
1665                Arc::new(TimestampMicrosecondArray::from_iter_values(0..10)),
1666            ),
1667            (
1668                "timestamp_ns",
1669                Arc::new(TimestampNanosecondArray::from_iter_values(4995..5005)),
1670            ),
1671        ];
1672        let batch = RecordBatch::try_from_iter(batch).unwrap();
1673
1674        let planner = Planner::new(batch.schema());
1675
1676        // Each expression is meant to select the final 5 rows
1677        let expressions = &[
1678            "timestamp_s >= TIMESTAMP '1970-01-01 00:00:05'",
1679            "timestamp_ms >= TIMESTAMP '1970-01-01 00:00:00.005'",
1680            "timestamp_us >= TIMESTAMP '1970-01-01 00:00:00.000005'",
1681            "timestamp_ns >= TIMESTAMP '1970-01-01 00:00:00.000005'",
1682        ];
1683
1684        let expected: ArrayRef = Arc::new(BooleanArray::from_iter(
1685            std::iter::repeat_n(Some(false), 5).chain(std::iter::repeat_n(Some(true), 5)),
1686        ));
1687        for expression in expressions {
1688            // convert to physical expression
1689            let logical_expr = planner.parse_filter(expression).unwrap();
1690            let logical_expr = planner.optimize_expr(logical_expr).unwrap();
1691            let physical_expr = planner.create_physical_expr(&logical_expr).unwrap();
1692
1693            // Evaluate and assert they have correct results
1694            let result = physical_expr.evaluate(&batch).unwrap();
1695            let result = result.into_array(batch.num_rows()).unwrap();
1696            assert_eq!(&expected, &result, "unexpected result for {}", expression);
1697        }
1698    }
1699
1700    #[test]
1701    fn test_columns_in_expr() {
1702        let expr = col("s0").gt(lit("value")).and(
1703            col("st")
1704                .field("st")
1705                .field("s2")
1706                .eq(lit("value"))
1707                .or(col("st")
1708                    .field("s1")
1709                    .in_list(vec![lit("value 1"), lit("value 2")], false)),
1710        );
1711
1712        let columns = Planner::column_names_in_expr(&expr);
1713        assert_eq!(columns, vec!["s0", "st.s1", "st.st.s2"]);
1714    }
1715
1716    #[test]
1717    fn test_parse_binary_expr() {
1718        let bin_str = "x'616263'";
1719
1720        let schema = Arc::new(Schema::new(vec![Field::new(
1721            "binary",
1722            DataType::Binary,
1723            true,
1724        )]));
1725        let planner = Planner::new(schema);
1726        let expr = planner.parse_expr(bin_str).unwrap();
1727        assert_eq!(
1728            expr,
1729            Expr::Literal(ScalarValue::Binary(Some(vec![b'a', b'b', b'c'])), None)
1730        );
1731    }
1732
1733    #[test]
1734    fn test_lance_context_provider_expr_planners() {
1735        let ctx_provider = LanceContextProvider::default();
1736        assert!(!ctx_provider.get_expr_planners().is_empty());
1737    }
1738}