|
| 1 | +use crate::catalog::ColumnRef; |
| 2 | +use crate::errors::DatabaseError; |
| 3 | +use crate::expression::function::scala::FuncMonotonicity; |
| 4 | +use crate::expression::function::scala::ScalarFunctionImpl; |
| 5 | +use crate::expression::function::FunctionSummary; |
| 6 | +use crate::expression::ScalarExpression; |
| 7 | +use crate::types::tuple::Tuple; |
| 8 | +use crate::types::value::DataValue; |
| 9 | +use crate::types::LogicalType; |
| 10 | +use serde::Deserialize; |
| 11 | +use serde::Serialize; |
| 12 | +use sqlparser::ast::CharLengthUnits; |
| 13 | +use std::sync::Arc; |
| 14 | + |
| 15 | +#[derive(Debug, Serialize, Deserialize)] |
| 16 | +pub(crate) struct Lower { |
| 17 | + summary: FunctionSummary, |
| 18 | +} |
| 19 | + |
| 20 | +impl Lower { |
| 21 | + #[allow(unused_mut)] |
| 22 | + pub(crate) fn new() -> Arc<Self> { |
| 23 | + let function_name = "lower".to_lowercase(); |
| 24 | + let arg_types = vec![LogicalType::Varchar(None, CharLengthUnits::Characters)]; |
| 25 | + Arc::new(Self { |
| 26 | + summary: FunctionSummary { |
| 27 | + name: function_name, |
| 28 | + arg_types, |
| 29 | + }, |
| 30 | + }) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +#[typetag::serde] |
| 35 | +impl ScalarFunctionImpl for Lower { |
| 36 | + #[allow(unused_variables, clippy::redundant_closure_call)] |
| 37 | + fn eval( |
| 38 | + &self, |
| 39 | + exprs: &[ScalarExpression], |
| 40 | + tuples: &Tuple, |
| 41 | + columns: &[ColumnRef], |
| 42 | + ) -> Result<DataValue, DatabaseError> { |
| 43 | + let value = exprs[0].eval(tuples, columns)?; |
| 44 | + let mut value = DataValue::clone(&value); |
| 45 | + if !matches!(value.logical_type(), LogicalType::Varchar(_, _)) { |
| 46 | + value = DataValue::clone(&value) |
| 47 | + .cast(&LogicalType::Varchar(None, CharLengthUnits::Characters))?; |
| 48 | + } |
| 49 | + if let DataValue::Utf8 { |
| 50 | + value: Some(value), |
| 51 | + ty, |
| 52 | + unit, |
| 53 | + } = &mut value |
| 54 | + { |
| 55 | + *value = value.to_lowercase(); |
| 56 | + } |
| 57 | + Ok(value) |
| 58 | + } |
| 59 | + |
| 60 | + fn monotonicity(&self) -> Option<FuncMonotonicity> { |
| 61 | + todo!() |
| 62 | + } |
| 63 | + |
| 64 | + fn return_type(&self) -> &LogicalType { |
| 65 | + &LogicalType::Varchar(None, CharLengthUnits::Characters) |
| 66 | + } |
| 67 | + |
| 68 | + fn summary(&self) -> &FunctionSummary { |
| 69 | + &self.summary |
| 70 | + } |
| 71 | +} |
0 commit comments