|
| 1 | +use std::sync::{Arc, OnceLock}; |
| 2 | + |
| 3 | +use anyhow::{anyhow, bail, Context, Result}; |
| 4 | +use datafusion_common::{Column, ScalarValue}; |
| 5 | +use datafusion_expr::{Expr, ScalarUDF}; |
| 6 | +use datafusion_expr::expr::{InList, ScalarFunction}; |
| 7 | +use datafusion_functions::core::getfield::GetFieldFunc; |
| 8 | + |
| 9 | +const MAGIC: &[u8; 4] = b"LFT1"; |
| 10 | +const VERSION: u8 = 1; |
| 11 | + |
| 12 | +const TAG_COLUMN_REF: u8 = 1; |
| 13 | +const TAG_LITERAL: u8 = 2; |
| 14 | +const TAG_AND: u8 = 3; |
| 15 | +const TAG_OR: u8 = 4; |
| 16 | +const TAG_NOT: u8 = 5; |
| 17 | +const TAG_COMPARISON: u8 = 6; |
| 18 | +const TAG_IS_NULL: u8 = 7; |
| 19 | +const TAG_IS_NOT_NULL: u8 = 8; |
| 20 | +const TAG_IN_LIST: u8 = 9; |
| 21 | + |
| 22 | +const LIT_NULL: u8 = 0; |
| 23 | +const LIT_BOOL: u8 = 1; |
| 24 | +const LIT_I64: u8 = 2; |
| 25 | +const LIT_U64: u8 = 3; |
| 26 | +const LIT_F32: u8 = 4; |
| 27 | +const LIT_F64: u8 = 5; |
| 28 | +const LIT_STRING: u8 = 6; |
| 29 | + |
| 30 | +const OP_EQ: u8 = 0; |
| 31 | +const OP_NOT_EQ: u8 = 1; |
| 32 | +const OP_LT: u8 = 2; |
| 33 | +const OP_LT_EQ: u8 = 3; |
| 34 | +const OP_GT: u8 = 4; |
| 35 | +const OP_GT_EQ: u8 = 5; |
| 36 | + |
| 37 | +static GETFIELD_UDF: OnceLock<Arc<ScalarUDF>> = OnceLock::new(); |
| 38 | + |
| 39 | +fn getfield_udf() -> Arc<ScalarUDF> { |
| 40 | + GETFIELD_UDF |
| 41 | + .get_or_init(|| Arc::new(ScalarUDF::new_from_impl(GetFieldFunc::default()))) |
| 42 | + .clone() |
| 43 | +} |
| 44 | + |
| 45 | +pub fn parse_filter_ir(filter_ir: &[u8]) -> Result<Expr> { |
| 46 | + if filter_ir.len() < MAGIC.len() + 1 { |
| 47 | + bail!("filter_ir is too short"); |
| 48 | + } |
| 49 | + if &filter_ir[0..MAGIC.len()] != MAGIC { |
| 50 | + bail!("filter_ir magic mismatch"); |
| 51 | + } |
| 52 | + if filter_ir[MAGIC.len()] != VERSION { |
| 53 | + bail!("unsupported filter_ir version: {}", filter_ir[MAGIC.len()]); |
| 54 | + } |
| 55 | + |
| 56 | + let mut cursor = Cursor::new(&filter_ir[MAGIC.len() + 1..]); |
| 57 | + let expr = parse_node(&mut cursor)?; |
| 58 | + if cursor.remaining() != 0 { |
| 59 | + bail!("trailing bytes in filter_ir: {}", cursor.remaining()); |
| 60 | + } |
| 61 | + Ok(expr) |
| 62 | +} |
| 63 | + |
| 64 | +struct Cursor<'a> { |
| 65 | + buf: &'a [u8], |
| 66 | + pos: usize, |
| 67 | +} |
| 68 | + |
| 69 | +impl<'a> Cursor<'a> { |
| 70 | + fn new(buf: &'a [u8]) -> Self { |
| 71 | + Self { buf, pos: 0 } |
| 72 | + } |
| 73 | + |
| 74 | + fn remaining(&self) -> usize { |
| 75 | + self.buf.len().saturating_sub(self.pos) |
| 76 | + } |
| 77 | + |
| 78 | + fn read_u8(&mut self) -> Result<u8> { |
| 79 | + if self.remaining() < 1 { |
| 80 | + bail!("unexpected end of input"); |
| 81 | + } |
| 82 | + let v = self.buf[self.pos]; |
| 83 | + self.pos += 1; |
| 84 | + Ok(v) |
| 85 | + } |
| 86 | + |
| 87 | + fn read_u32_le(&mut self) -> Result<u32> { |
| 88 | + let bytes = self.read_exact(4)?; |
| 89 | + Ok(u32::from_le_bytes(bytes.try_into().unwrap())) |
| 90 | + } |
| 91 | + |
| 92 | + fn read_i64_le(&mut self) -> Result<i64> { |
| 93 | + let bytes = self.read_exact(8)?; |
| 94 | + Ok(i64::from_le_bytes(bytes.try_into().unwrap())) |
| 95 | + } |
| 96 | + |
| 97 | + fn read_u64_le(&mut self) -> Result<u64> { |
| 98 | + let bytes = self.read_exact(8)?; |
| 99 | + Ok(u64::from_le_bytes(bytes.try_into().unwrap())) |
| 100 | + } |
| 101 | + |
| 102 | + fn read_f32_le(&mut self) -> Result<f32> { |
| 103 | + let bytes = self.read_exact(4)?; |
| 104 | + Ok(f32::from_le_bytes(bytes.try_into().unwrap())) |
| 105 | + } |
| 106 | + |
| 107 | + fn read_f64_le(&mut self) -> Result<f64> { |
| 108 | + let bytes = self.read_exact(8)?; |
| 109 | + Ok(f64::from_le_bytes(bytes.try_into().unwrap())) |
| 110 | + } |
| 111 | + |
| 112 | + fn read_exact(&mut self, len: usize) -> Result<&'a [u8]> { |
| 113 | + if self.remaining() < len { |
| 114 | + bail!("unexpected end of input"); |
| 115 | + } |
| 116 | + let start = self.pos; |
| 117 | + self.pos += len; |
| 118 | + Ok(&self.buf[start..start + len]) |
| 119 | + } |
| 120 | + |
| 121 | + fn read_len_prefixed_slice(&mut self) -> Result<&'a [u8]> { |
| 122 | + let len = usize::try_from(self.read_u32_le()?)?; |
| 123 | + self.read_exact(len) |
| 124 | + } |
| 125 | + |
| 126 | + fn read_len_prefixed_string(&mut self) -> Result<String> { |
| 127 | + let bytes = self.read_len_prefixed_slice()?; |
| 128 | + Ok(std::str::from_utf8(bytes) |
| 129 | + .context("invalid utf8 string")? |
| 130 | + .to_string()) |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +fn parse_subexpr(bytes: &[u8]) -> Result<Expr> { |
| 135 | + let mut cursor = Cursor::new(bytes); |
| 136 | + let expr = parse_node(&mut cursor)?; |
| 137 | + if cursor.remaining() != 0 { |
| 138 | + bail!("trailing bytes in subexpr: {}", cursor.remaining()); |
| 139 | + } |
| 140 | + Ok(expr) |
| 141 | +} |
| 142 | + |
| 143 | +fn parse_node(cursor: &mut Cursor<'_>) -> Result<Expr> { |
| 144 | + let tag = cursor.read_u8()?; |
| 145 | + match tag { |
| 146 | + TAG_COLUMN_REF => parse_column_ref(cursor), |
| 147 | + TAG_LITERAL => parse_literal(cursor), |
| 148 | + TAG_AND => parse_conjunction(cursor, true), |
| 149 | + TAG_OR => parse_conjunction(cursor, false), |
| 150 | + TAG_NOT => { |
| 151 | + let child = parse_len_prefixed_node(cursor)?; |
| 152 | + Ok(Expr::Not(Box::new(child))) |
| 153 | + } |
| 154 | + TAG_COMPARISON => parse_comparison(cursor), |
| 155 | + TAG_IS_NULL => { |
| 156 | + let child = parse_len_prefixed_node(cursor)?; |
| 157 | + Ok(Expr::IsNull(Box::new(child))) |
| 158 | + } |
| 159 | + TAG_IS_NOT_NULL => { |
| 160 | + let child = parse_len_prefixed_node(cursor)?; |
| 161 | + Ok(Expr::IsNotNull(Box::new(child))) |
| 162 | + } |
| 163 | + TAG_IN_LIST => parse_in_list(cursor), |
| 164 | + other => Err(anyhow!("unknown node tag: {other}")), |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +fn parse_len_prefixed_node(cursor: &mut Cursor<'_>) -> Result<Expr> { |
| 169 | + let bytes = cursor.read_len_prefixed_slice()?; |
| 170 | + parse_subexpr(bytes) |
| 171 | +} |
| 172 | + |
| 173 | +fn parse_column_ref(cursor: &mut Cursor<'_>) -> Result<Expr> { |
| 174 | + let segments_len = usize::try_from(cursor.read_u32_le()?)?; |
| 175 | + if segments_len == 0 { |
| 176 | + bail!("column ref has no segments"); |
| 177 | + } |
| 178 | + let mut segments = Vec::with_capacity(segments_len); |
| 179 | + for _ in 0..segments_len { |
| 180 | + segments.push(cursor.read_len_prefixed_string()?); |
| 181 | + } |
| 182 | + |
| 183 | + let mut expr = Expr::Column(Column::new_unqualified(segments[0].clone())); |
| 184 | + for segment in segments.into_iter().skip(1) { |
| 185 | + expr = Expr::ScalarFunction(ScalarFunction { |
| 186 | + func: getfield_udf(), |
| 187 | + args: vec![ |
| 188 | + std::mem::take(&mut expr), |
| 189 | + Expr::Literal(ScalarValue::Utf8(Some(segment)), None), |
| 190 | + ], |
| 191 | + }); |
| 192 | + } |
| 193 | + Ok(expr) |
| 194 | +} |
| 195 | + |
| 196 | +fn parse_literal(cursor: &mut Cursor<'_>) -> Result<Expr> { |
| 197 | + let lit_tag = cursor.read_u8()?; |
| 198 | + let scalar = match lit_tag { |
| 199 | + LIT_NULL => ScalarValue::Null, |
| 200 | + LIT_BOOL => ScalarValue::Boolean(Some(cursor.read_u8()? != 0)), |
| 201 | + LIT_I64 => ScalarValue::Int64(Some(cursor.read_i64_le()?)), |
| 202 | + LIT_U64 => ScalarValue::UInt64(Some(cursor.read_u64_le()?)), |
| 203 | + LIT_F32 => ScalarValue::Float32(Some(cursor.read_f32_le()?)), |
| 204 | + LIT_F64 => ScalarValue::Float64(Some(cursor.read_f64_le()?)), |
| 205 | + LIT_STRING => ScalarValue::Utf8(Some(cursor.read_len_prefixed_string()?)), |
| 206 | + other => return Err(anyhow!("unknown literal tag: {other}")), |
| 207 | + }; |
| 208 | + Ok(Expr::Literal(scalar, None)) |
| 209 | +} |
| 210 | + |
| 211 | +fn parse_conjunction(cursor: &mut Cursor<'_>, is_and: bool) -> Result<Expr> { |
| 212 | + let children_len = usize::try_from(cursor.read_u32_le()?)?; |
| 213 | + if children_len == 0 { |
| 214 | + bail!("conjunction has no children"); |
| 215 | + } |
| 216 | + let mut children = Vec::with_capacity(children_len); |
| 217 | + for _ in 0..children_len { |
| 218 | + children.push(parse_len_prefixed_node(cursor)?); |
| 219 | + } |
| 220 | + |
| 221 | + let mut iter = children.into_iter(); |
| 222 | + let mut expr = iter.next().unwrap(); |
| 223 | + for child in iter { |
| 224 | + expr = if is_and { expr.and(child) } else { expr.or(child) }; |
| 225 | + } |
| 226 | + Ok(expr) |
| 227 | +} |
| 228 | + |
| 229 | +fn parse_comparison(cursor: &mut Cursor<'_>) -> Result<Expr> { |
| 230 | + let op = cursor.read_u8()?; |
| 231 | + let left = parse_len_prefixed_node(cursor)?; |
| 232 | + let right = parse_len_prefixed_node(cursor)?; |
| 233 | + Ok(match op { |
| 234 | + OP_EQ => left.eq(right), |
| 235 | + OP_NOT_EQ => left.not_eq(right), |
| 236 | + OP_LT => left.lt(right), |
| 237 | + OP_LT_EQ => left.lt_eq(right), |
| 238 | + OP_GT => left.gt(right), |
| 239 | + OP_GT_EQ => left.gt_eq(right), |
| 240 | + other => return Err(anyhow!("unknown comparison op: {other}")), |
| 241 | + }) |
| 242 | +} |
| 243 | + |
| 244 | +fn parse_in_list(cursor: &mut Cursor<'_>) -> Result<Expr> { |
| 245 | + let negated = cursor.read_u8()? != 0; |
| 246 | + let expr = parse_len_prefixed_node(cursor)?; |
| 247 | + let list_len = usize::try_from(cursor.read_u32_le()?)?; |
| 248 | + let mut list = Vec::with_capacity(list_len); |
| 249 | + for _ in 0..list_len { |
| 250 | + list.push(parse_len_prefixed_node(cursor)?); |
| 251 | + } |
| 252 | + Ok(Expr::InList(InList::new(Box::new(expr), list, negated))) |
| 253 | +} |
0 commit comments