|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +use std::fmt::Formatter; |
| 5 | + |
| 6 | +use arrow_schema::DataType; |
| 7 | +use vortex_error::VortexResult; |
| 8 | +use vortex_error::vortex_bail; |
| 9 | +use vortex_error::vortex_ensure; |
| 10 | +use vortex_error::vortex_err; |
| 11 | +use vortex_session::VortexSession; |
| 12 | + |
| 13 | +use crate::ArrayRef; |
| 14 | +use crate::ExecutionCtx; |
| 15 | +use crate::arrow::ArrowArrayExecutor; |
| 16 | +use crate::arrow::from_arrow_array_with_len; |
| 17 | +use crate::dtype::DType; |
| 18 | +use crate::expr::Expression; |
| 19 | +use crate::expr::and; |
| 20 | +use crate::scalar_fn::Arity; |
| 21 | +use crate::scalar_fn::ChildName; |
| 22 | +use crate::scalar_fn::EmptyOptions; |
| 23 | +use crate::scalar_fn::ExecutionArgs; |
| 24 | +use crate::scalar_fn::ScalarFnId; |
| 25 | +use crate::scalar_fn::ScalarFnVTable; |
| 26 | + |
| 27 | +/// SQL SUBSTRING / SUBSTR expression. |
| 28 | +#[derive(Clone)] |
| 29 | +pub struct Substring; |
| 30 | + |
| 31 | +impl ScalarFnVTable for Substring { |
| 32 | + type Options = EmptyOptions; |
| 33 | + |
| 34 | + fn id(&self) -> ScalarFnId { |
| 35 | + ScalarFnId::new("vortex.substring") |
| 36 | + } |
| 37 | + |
| 38 | + fn serialize(&self, _instance: &Self::Options) -> VortexResult<Option<Vec<u8>>> { |
| 39 | + Ok(Some(vec![])) |
| 40 | + } |
| 41 | + |
| 42 | + fn deserialize( |
| 43 | + &self, |
| 44 | + _metadata: &[u8], |
| 45 | + _session: &VortexSession, |
| 46 | + ) -> VortexResult<Self::Options> { |
| 47 | + Ok(EmptyOptions) |
| 48 | + } |
| 49 | + |
| 50 | + fn arity(&self, _options: &Self::Options) -> Arity { |
| 51 | + Arity::Variadic { |
| 52 | + min: 2, |
| 53 | + max: Some(3), |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + fn child_name(&self, _instance: &Self::Options, child_idx: usize) -> ChildName { |
| 58 | + match child_idx { |
| 59 | + 0 => ChildName::from("string"), |
| 60 | + 1 => ChildName::from("start"), |
| 61 | + 2 => ChildName::from("length"), |
| 62 | + _ => unreachable!("Invalid child index {child_idx} for Substring expression"), |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + fn fmt_sql( |
| 67 | + &self, |
| 68 | + _options: &Self::Options, |
| 69 | + expr: &Expression, |
| 70 | + f: &mut Formatter<'_>, |
| 71 | + ) -> std::fmt::Result { |
| 72 | + write!(f, "substr(")?; |
| 73 | + expr.child(0).fmt_sql(f)?; |
| 74 | + write!(f, ", ")?; |
| 75 | + expr.child(1).fmt_sql(f)?; |
| 76 | + if expr.children().len() > 2 { |
| 77 | + write!(f, ", ")?; |
| 78 | + expr.child(2).fmt_sql(f)?; |
| 79 | + } |
| 80 | + write!(f, ")") |
| 81 | + } |
| 82 | + |
| 83 | + fn return_dtype(&self, _options: &Self::Options, arg_dtypes: &[DType]) -> VortexResult<DType> { |
| 84 | + let input = &arg_dtypes[0]; |
| 85 | + vortex_ensure!(input.is_utf8(), "Substring: expected UTF8 input"); |
| 86 | + let nullable = arg_dtypes.iter().any(|dt| dt.is_nullable()); |
| 87 | + Ok(DType::Utf8(nullable.into())) |
| 88 | + } |
| 89 | + |
| 90 | + fn execute( |
| 91 | + &self, |
| 92 | + _options: &Self::Options, |
| 93 | + args: &dyn ExecutionArgs, |
| 94 | + ctx: &mut ExecutionCtx, |
| 95 | + ) -> VortexResult<ArrayRef> { |
| 96 | + let string_arr = args.get(0)?; |
| 97 | + let start_arr = args.get(1)?; |
| 98 | + let len = args.row_count(); |
| 99 | + |
| 100 | + let start = start_arr |
| 101 | + .as_constant() |
| 102 | + .ok_or_else(|| vortex_err!("Substring: start must be a constant"))? |
| 103 | + .as_primitive_opt() |
| 104 | + .ok_or_else(|| vortex_err!("Substring: start must be a primitive integer"))? |
| 105 | + .as_::<i64>() |
| 106 | + .ok_or_else(|| vortex_err!("Substring: start must be non-null"))?; |
| 107 | + vortex_ensure!(start >= 1, "Substring: start must be >0, got {start}"); |
| 108 | + |
| 109 | + let length = if args.num_inputs() > 2 { |
| 110 | + let length = args |
| 111 | + .get(2)? |
| 112 | + .as_constant() |
| 113 | + .ok_or_else(|| vortex_err!("Substring: length must be a constant"))? |
| 114 | + .as_primitive_opt() |
| 115 | + .ok_or_else(|| vortex_err!("Substring: length must be a primitive integer"))? |
| 116 | + .as_::<u64>() |
| 117 | + .ok_or_else(|| vortex_err!("Substring: length must be non-null"))?; |
| 118 | + vortex_ensure!( |
| 119 | + length > 0, |
| 120 | + "Substring: length must be non-negative, got {length}" |
| 121 | + ); |
| 122 | + Some(length) |
| 123 | + } else { |
| 124 | + None |
| 125 | + }; |
| 126 | + |
| 127 | + let nullable = string_arr.dtype().is_nullable(); |
| 128 | + // Execute string array to Arrow as Utf8: arrow_string::substring does |
| 129 | + // not support Utf8View. |
| 130 | + let arrow_array = string_arr.execute_arrow(Some(&DataType::Utf8), ctx)?; |
| 131 | + let result = arrow_string::substring::substring(arrow_array.as_ref(), start - 1, length)?; |
| 132 | + from_arrow_array_with_len(result.as_ref(), len, nullable) |
| 133 | + } |
| 134 | + |
| 135 | + fn validity( |
| 136 | + &self, |
| 137 | + _options: &Self::Options, |
| 138 | + expression: &Expression, |
| 139 | + ) -> VortexResult<Option<Expression>> { |
| 140 | + let string_validity = expression.child(0).validity()?; |
| 141 | + let start_validity = expression.child(1).validity()?; |
| 142 | + let combined = and(string_validity, start_validity); |
| 143 | + if expression.children().len() > 2 { |
| 144 | + let length_validity = expression.child(2).validity()?; |
| 145 | + Ok(Some(and(combined, length_validity))) |
| 146 | + } else { |
| 147 | + Ok(Some(combined)) |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + fn is_null_sensitive(&self, _instance: &Self::Options) -> bool { |
| 152 | + false |
| 153 | + } |
| 154 | + |
| 155 | + fn is_fallible(&self, _options: &Self::Options) -> bool { |
| 156 | + false |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +#[cfg(test)] |
| 161 | +mod tests { |
| 162 | + use std::sync::LazyLock; |
| 163 | + |
| 164 | + use vortex_error::VortexResult; |
| 165 | + use vortex_session::VortexSession; |
| 166 | + |
| 167 | + use crate::IntoArray; |
| 168 | + use crate::VortexSessionExecute; |
| 169 | + use crate::arrays::VarBinViewArray; |
| 170 | + use crate::assert_arrays_eq; |
| 171 | + use crate::expr::lit; |
| 172 | + use crate::expr::root; |
| 173 | + use crate::expr::substr; |
| 174 | + |
| 175 | + static SESSION: LazyLock<VortexSession> = LazyLock::new(|| VortexSession::empty()); |
| 176 | + |
| 177 | + #[test] |
| 178 | + fn test_display() { |
| 179 | + let expr = substr(root(), lit(1i64), None); |
| 180 | + assert_eq!(expr.to_string(), "substr($, 1i64)"); |
| 181 | + |
| 182 | + let expr = substr(root(), lit(1i64), Some(lit(3i64))); |
| 183 | + assert_eq!(expr.to_string(), "substr($, 1i64, 3i64)"); |
| 184 | + } |
| 185 | + |
| 186 | + #[test] |
| 187 | + fn test_start() -> VortexResult<()> { |
| 188 | + let arr = VarBinViewArray::from_iter_str(["hello", "world"]).into_array(); |
| 189 | + let result = arr |
| 190 | + .apply(&substr(root(), lit(2i64), None))? |
| 191 | + .execute::<VarBinViewArray>(&mut SESSION.create_execution_ctx())?; |
| 192 | + assert_arrays_eq!(result, VarBinViewArray::from_iter_str(["ello", "orld"])); |
| 193 | + Ok(()) |
| 194 | + } |
| 195 | + |
| 196 | + #[test] |
| 197 | + fn test_start_length() -> VortexResult<()> { |
| 198 | + let arr = VarBinViewArray::from_iter_str(["hello", "world"]).into_array(); |
| 199 | + let result = arr |
| 200 | + .apply(&substr(root(), lit(2i64), Some(lit(3i64))))? |
| 201 | + .execute::<VarBinViewArray>(&mut SESSION.create_execution_ctx())?; |
| 202 | + assert_arrays_eq!(result, VarBinViewArray::from_iter_str(["ell", "orl"])); |
| 203 | + Ok(()) |
| 204 | + } |
| 205 | +} |
0 commit comments