|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +//! Translate Comet's serialized Spark data filters into delta-kernel predicates for file-level |
| 19 | +//! data skipping. |
| 20 | +//! |
| 21 | +//! Translation is deliberately conservative: only an exact, well-understood subset is converted, |
| 22 | +//! and anything else yields `None` and is simply not pushed. Data skipping is best-effort and a |
| 23 | +//! `Filter` above the scan still enforces correctness, so dropping a filter only costs skipping — |
| 24 | +//! never correctness. A *wrong* predicate, by contrast, could skip files that hold matching rows, |
| 25 | +//! so each mapping below is exact (operator and literal type both checked). |
| 26 | +
|
| 27 | +use std::sync::Arc; |
| 28 | + |
| 29 | +use datafusion_comet_proto::spark_expression::{self, expr::ExprStruct, literal::Value}; |
| 30 | +use delta_kernel::expressions::{Expression, Predicate, PredicateRef, Scalar}; |
| 31 | + |
| 32 | +/// Spark `DataTypeId` ids (see `types.proto`) used to validate that a literal's declared type |
| 33 | +/// matches the value we read, so e.g. a date-as-int is never treated as a plain integer. |
| 34 | +mod type_id { |
| 35 | + pub const BOOL: i32 = 0; |
| 36 | + pub const INT8: i32 = 1; |
| 37 | + pub const INT16: i32 = 2; |
| 38 | + pub const INT32: i32 = 3; |
| 39 | + pub const INT64: i32 = 4; |
| 40 | + pub const FLOAT: i32 = 5; |
| 41 | + pub const DOUBLE: i32 = 6; |
| 42 | + pub const STRING: i32 = 7; |
| 43 | + pub const DATE: i32 = 12; |
| 44 | +} |
| 45 | + |
| 46 | +/// Translate a list of AND-ed Spark data filters into a single kernel predicate, dropping any |
| 47 | +/// conjunct that is not in the supported subset. Returns `None` when nothing is translatable. |
| 48 | +pub(crate) fn translate_filters(filters: &[spark_expression::Expr]) -> Option<PredicateRef> { |
| 49 | + let preds: Vec<Predicate> = filters.iter().filter_map(expr_to_predicate).collect(); |
| 50 | + if preds.is_empty() { |
| 51 | + None |
| 52 | + } else { |
| 53 | + Some(Arc::new(Predicate::and_from(preds))) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +fn binary_operands(b: &spark_expression::BinaryExpr) -> Option<(Expression, Expression)> { |
| 58 | + let left = expr_to_expression(b.left.as_deref()?)?; |
| 59 | + let right = expr_to_expression(b.right.as_deref()?)?; |
| 60 | + Some((left, right)) |
| 61 | +} |
| 62 | + |
| 63 | +fn expr_to_predicate(expr: &spark_expression::Expr) -> Option<Predicate> { |
| 64 | + match expr.expr_struct.as_ref()? { |
| 65 | + ExprStruct::Eq(b) => binary_operands(b).map(|(l, r)| Predicate::eq(l, r)), |
| 66 | + ExprStruct::Neq(b) => binary_operands(b).map(|(l, r)| Predicate::ne(l, r)), |
| 67 | + ExprStruct::Gt(b) => binary_operands(b).map(|(l, r)| Predicate::gt(l, r)), |
| 68 | + ExprStruct::GtEq(b) => binary_operands(b).map(|(l, r)| Predicate::ge(l, r)), |
| 69 | + ExprStruct::Lt(b) => binary_operands(b).map(|(l, r)| Predicate::lt(l, r)), |
| 70 | + ExprStruct::LtEq(b) => binary_operands(b).map(|(l, r)| Predicate::le(l, r)), |
| 71 | + ExprStruct::And(b) => { |
| 72 | + let left = expr_to_predicate(b.left.as_deref()?)?; |
| 73 | + let right = expr_to_predicate(b.right.as_deref()?)?; |
| 74 | + Some(Predicate::and(left, right)) |
| 75 | + } |
| 76 | + ExprStruct::Or(b) => { |
| 77 | + let left = expr_to_predicate(b.left.as_deref()?)?; |
| 78 | + let right = expr_to_predicate(b.right.as_deref()?)?; |
| 79 | + Some(Predicate::or(left, right)) |
| 80 | + } |
| 81 | + ExprStruct::Not(u) => Some(Predicate::not(expr_to_predicate(u.child.as_deref()?)?)), |
| 82 | + ExprStruct::IsNull(u) => Some(Predicate::is_null(expr_to_expression(u.child.as_deref()?)?)), |
| 83 | + ExprStruct::IsNotNull(u) => Some(Predicate::is_not_null(expr_to_expression( |
| 84 | + u.child.as_deref()?, |
| 85 | + )?)), |
| 86 | + _ => None, |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +fn expr_to_expression(expr: &spark_expression::Expr) -> Option<Expression> { |
| 91 | + match expr.expr_struct.as_ref()? { |
| 92 | + // Comet serializes scan filters with `binding = false`, so columns arrive as unbound |
| 93 | + // (name) references. |
| 94 | + ExprStruct::Unbound(unbound) => Some(Expression::column([unbound.name.clone()])), |
| 95 | + ExprStruct::Literal(literal) => literal_to_scalar(literal).map(Expression::literal), |
| 96 | + _ => None, |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +fn literal_to_scalar(literal: &spark_expression::Literal) -> Option<Scalar> { |
| 101 | + if literal.is_null { |
| 102 | + return None; |
| 103 | + } |
| 104 | + let declared = literal.datatype.as_ref().map(|d| d.type_id)?; |
| 105 | + match (literal.value.as_ref()?, declared) { |
| 106 | + (Value::BoolVal(v), type_id::BOOL) => Some(Scalar::Boolean(*v)), |
| 107 | + (Value::ByteVal(v), type_id::INT8) => Some(Scalar::Byte(*v as i8)), |
| 108 | + (Value::ShortVal(v), type_id::INT16) => Some(Scalar::Short(*v as i16)), |
| 109 | + (Value::IntVal(v), type_id::INT32) => Some(Scalar::Integer(*v)), |
| 110 | + (Value::IntVal(v), type_id::DATE) => Some(Scalar::Date(*v)), |
| 111 | + (Value::LongVal(v), type_id::INT64) => Some(Scalar::Long(*v)), |
| 112 | + (Value::FloatVal(v), type_id::FLOAT) => Some(Scalar::Float(*v)), |
| 113 | + (Value::DoubleVal(v), type_id::DOUBLE) => Some(Scalar::Double(*v)), |
| 114 | + (Value::StringVal(v), type_id::STRING) => Some(Scalar::String(v.clone())), |
| 115 | + _ => None, |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +#[cfg(test)] |
| 120 | +mod tests { |
| 121 | + use super::*; |
| 122 | + use datafusion_comet_proto::spark_expression::{ |
| 123 | + BinaryExpr, DataType, Expr, Literal, UnaryExpr, UnboundReference, |
| 124 | + }; |
| 125 | + |
| 126 | + fn expr(s: ExprStruct) -> Expr { |
| 127 | + Expr { |
| 128 | + expr_struct: Some(s), |
| 129 | + ..Default::default() |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + fn col(name: &str) -> Expr { |
| 134 | + expr(ExprStruct::Unbound(UnboundReference { |
| 135 | + name: name.to_string(), |
| 136 | + datatype: None, |
| 137 | + })) |
| 138 | + } |
| 139 | + |
| 140 | + fn lit_int(v: i32) -> Expr { |
| 141 | + lit_int_typed(v, type_id::INT32) |
| 142 | + } |
| 143 | + |
| 144 | + fn lit_int_typed(v: i32, type_id: i32) -> Expr { |
| 145 | + expr(ExprStruct::Literal(Literal { |
| 146 | + value: Some(Value::IntVal(v)), |
| 147 | + datatype: Some(DataType { |
| 148 | + type_id, |
| 149 | + type_info: None, |
| 150 | + }), |
| 151 | + is_null: false, |
| 152 | + })) |
| 153 | + } |
| 154 | + |
| 155 | + fn binary(left: Expr, right: Expr) -> Box<BinaryExpr> { |
| 156 | + Box::new(BinaryExpr { |
| 157 | + left: Some(Box::new(left)), |
| 158 | + right: Some(Box::new(right)), |
| 159 | + }) |
| 160 | + } |
| 161 | + |
| 162 | + fn unary(child: Expr) -> Box<UnaryExpr> { |
| 163 | + Box::new(UnaryExpr { |
| 164 | + child: Some(Box::new(child)), |
| 165 | + }) |
| 166 | + } |
| 167 | + |
| 168 | + #[test] |
| 169 | + fn translates_simple_comparison() { |
| 170 | + let gt = expr(ExprStruct::Gt(binary(col("id"), lit_int(10)))); |
| 171 | + let pred = translate_filters(&[gt]).expect("should translate"); |
| 172 | + // id > 10 |
| 173 | + assert!(matches!(pred.as_ref(), Predicate::Binary(_))); |
| 174 | + } |
| 175 | + |
| 176 | + #[test] |
| 177 | + fn translates_and_of_two_comparisons() { |
| 178 | + let lt = expr(ExprStruct::Lt(binary(col("id"), lit_int(100)))); |
| 179 | + let gt = expr(ExprStruct::Gt(binary(col("id"), lit_int(0)))); |
| 180 | + let and = expr(ExprStruct::And(binary(lt, gt))); |
| 181 | + assert!(translate_filters(&[and]).is_some()); |
| 182 | + } |
| 183 | + |
| 184 | + #[test] |
| 185 | + fn translates_is_null_and_is_not_null() { |
| 186 | + let isnull = expr(ExprStruct::IsNull(unary(col("id")))); |
| 187 | + let isnotnull = expr(ExprStruct::IsNotNull(unary(col("score")))); |
| 188 | + assert!(translate_filters(&[isnull, isnotnull]).is_some()); |
| 189 | + } |
| 190 | + |
| 191 | + #[test] |
| 192 | + fn drops_unsupported_filter_but_keeps_supported_ones() { |
| 193 | + // A `like` filter (unsupported) plus a supported comparison: only the comparison is pushed. |
| 194 | + let like = expr(ExprStruct::Like(binary(col("name"), lit_int(1)))); |
| 195 | + let gt = expr(ExprStruct::Gt(binary(col("id"), lit_int(5)))); |
| 196 | + assert!(translate_filters(&[like.clone(), gt]).is_some()); |
| 197 | + // The unsupported filter on its own yields nothing. |
| 198 | + assert!(translate_filters(&[like]).is_none()); |
| 199 | + } |
| 200 | + |
| 201 | + #[test] |
| 202 | + fn rejects_literal_whose_declared_type_mismatches_the_value() { |
| 203 | + // An int value declared as TIMESTAMP (id 9) must not be treated as a plain integer. |
| 204 | + let bad = expr(ExprStruct::Gt(binary(col("ts"), lit_int_typed(10, 9)))); |
| 205 | + assert!(translate_filters(&[bad]).is_none()); |
| 206 | + // A date-typed int, however, maps to Scalar::Date. |
| 207 | + let date = expr(ExprStruct::Gt(binary( |
| 208 | + col("d"), |
| 209 | + lit_int_typed(18262, type_id::DATE), |
| 210 | + ))); |
| 211 | + assert!(translate_filters(&[date]).is_some()); |
| 212 | + } |
| 213 | + |
| 214 | + #[test] |
| 215 | + fn empty_filters_translate_to_none() { |
| 216 | + assert!(translate_filters(&[]).is_none()); |
| 217 | + } |
| 218 | +} |
0 commit comments