diff --git a/src/db.rs b/src/db.rs index a47b317c..1d575e53 100644 --- a/src/db.rs +++ b/src/db.rs @@ -5,6 +5,8 @@ use crate::execution::{build_write, try_collect}; use crate::expression::function::scala::ScalarFunctionImpl; use crate::expression::function::table::TableFunctionImpl; use crate::expression::function::FunctionSummary; +use crate::function::characterlength::CharacterLength; +use crate::function::charlength::CharLength; use crate::function::current_date::CurrentDate; use crate::function::lower::Lower; use crate::function::numbers::Numbers; @@ -50,6 +52,8 @@ impl DataBaseBuilder { scala_functions: Default::default(), table_functions: Default::default(), }; + builder = builder.register_scala_function(CharLength::new()); + builder = builder.register_scala_function(CharacterLength::new()); builder = builder.register_scala_function(CurrentDate::new()); builder = builder.register_scala_function(Lower::new()); builder = builder.register_scala_function(Upper::new()); diff --git a/src/function/characterlength.rs b/src/function/characterlength.rs new file mode 100644 index 00000000..f1709643 --- /dev/null +++ b/src/function/characterlength.rs @@ -0,0 +1,72 @@ +use crate::catalog::ColumnRef; +use crate::errors::DatabaseError; +use crate::expression::function::scala::FuncMonotonicity; +use crate::expression::function::scala::ScalarFunctionImpl; +use crate::expression::function::FunctionSummary; +use crate::expression::ScalarExpression; +use crate::types::tuple::Tuple; +use crate::types::value::DataValue; +use crate::types::LogicalType; +use serde::Deserialize; +use serde::Serialize; +use sqlparser::ast::CharLengthUnits; +use std::sync::Arc; + +#[derive(Debug, Serialize, Deserialize)] +pub(crate) struct CharacterLength { + summary: FunctionSummary, +} + +impl CharacterLength { + #[allow(unused_mut)] + pub(crate) fn new() -> Arc { + let function_name = "character_length".to_lowercase(); + let arg_types = vec![LogicalType::Varchar(None, CharLengthUnits::Characters)]; + Arc::new(Self { + summary: FunctionSummary { + name: function_name, + arg_types, + }, + }) + } +} + +#[typetag::serde] +impl ScalarFunctionImpl for CharacterLength { + #[allow(unused_variables, clippy::redundant_closure_call)] + fn eval( + &self, + exprs: &[ScalarExpression], + tuples: &Tuple, + columns: &[ColumnRef], + ) -> Result { + let value = exprs[0].eval(tuples, columns)?; + let mut value = DataValue::clone(&value); + if !matches!(value.logical_type(), LogicalType::Varchar(_, _)) { + value = DataValue::clone(&value) + .cast(&LogicalType::Varchar(None, CharLengthUnits::Characters))?; + } + let mut length: u64 = 0; + if let DataValue::Utf8 { + value: Some(value), + ty, + unit, + } = &mut value + { + length = value.len() as u64; + } + Ok(DataValue::UInt64(Some(length))) + } + + fn monotonicity(&self) -> Option { + todo!() + } + + fn return_type(&self) -> &LogicalType { + &LogicalType::Varchar(None, CharLengthUnits::Characters) + } + + fn summary(&self) -> &FunctionSummary { + &self.summary + } +} diff --git a/src/function/charlength.rs b/src/function/charlength.rs new file mode 100644 index 00000000..b67a33e4 --- /dev/null +++ b/src/function/charlength.rs @@ -0,0 +1,72 @@ +use crate::catalog::ColumnRef; +use crate::errors::DatabaseError; +use crate::expression::function::scala::FuncMonotonicity; +use crate::expression::function::scala::ScalarFunctionImpl; +use crate::expression::function::FunctionSummary; +use crate::expression::ScalarExpression; +use crate::types::tuple::Tuple; +use crate::types::value::DataValue; +use crate::types::LogicalType; +use serde::Deserialize; +use serde::Serialize; +use sqlparser::ast::CharLengthUnits; +use std::sync::Arc; + +#[derive(Debug, Serialize, Deserialize)] +pub(crate) struct CharLength { + summary: FunctionSummary, +} + +impl CharLength { + #[allow(unused_mut)] + pub(crate) fn new() -> Arc { + let function_name = "char_length".to_lowercase(); + let arg_types = vec![LogicalType::Varchar(None, CharLengthUnits::Characters)]; + Arc::new(Self { + summary: FunctionSummary { + name: function_name, + arg_types, + }, + }) + } +} + +#[typetag::serde] +impl ScalarFunctionImpl for CharLength { + #[allow(unused_variables, clippy::redundant_closure_call)] + fn eval( + &self, + exprs: &[ScalarExpression], + tuples: &Tuple, + columns: &[ColumnRef], + ) -> Result { + let value = exprs[0].eval(tuples, columns)?; + let mut value = DataValue::clone(&value); + if !matches!(value.logical_type(), LogicalType::Varchar(_, _)) { + value = DataValue::clone(&value) + .cast(&LogicalType::Varchar(None, CharLengthUnits::Characters))?; + } + let mut length: u64 = 0; + if let DataValue::Utf8 { + value: Some(value), + ty, + unit, + } = &mut value + { + length = value.len() as u64; + } + Ok(DataValue::UInt64(Some(length))) + } + + fn monotonicity(&self) -> Option { + todo!() + } + + fn return_type(&self) -> &LogicalType { + &LogicalType::Varchar(None, CharLengthUnits::Characters) + } + + fn summary(&self) -> &FunctionSummary { + &self.summary + } +} diff --git a/src/function/mod.rs b/src/function/mod.rs index e48c7c2d..ad3267df 100644 --- a/src/function/mod.rs +++ b/src/function/mod.rs @@ -1,3 +1,5 @@ +pub(crate) mod characterlength; +pub(crate) mod charlength; pub(crate) mod current_date; pub(crate) mod lower; pub(crate) mod numbers; diff --git a/tests/slt/sql_2016/E021_04.slt b/tests/slt/sql_2016/E021_04.slt index dc7a3a52..085213f4 100644 --- a/tests/slt/sql_2016/E021_04.slt +++ b/tests/slt/sql_2016/E021_04.slt @@ -1,14 +1,21 @@ # E021-04: CHARACTER_LENGTH function -# TODO: CHARACTER_LENGTH()/CHAR_LENGTH() +query I +SELECT CHARACTER_LENGTH ( 'foo' ) +---- +3 -# query I -# SELECT CHARACTER_LENGTH ( 'foo' ) -# ---- -# 3 +query I +SELECT CHARACTER_LENGTH ( 'foooof' ) +---- +6 +query I +SELECT CHAR_LENGTH ( 'foo' ) +---- +3 -# query I -# SELECT CHAR_LENGTH ( 'foo' ) -# ---- -# 3 +query I +SELECT CHAR_LENGTH ( 'foooof' ) +---- +6