Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add functions CharLength & CharacterLength #232

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
72 changes: 72 additions & 0 deletions src/function/characterlength.rs
Original file line number Diff line number Diff line change
@@ -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<Self> {
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<DataValue, DatabaseError> {
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<FuncMonotonicity> {
todo!()
}

fn return_type(&self) -> &LogicalType {
&LogicalType::Varchar(None, CharLengthUnits::Characters)
}

fn summary(&self) -> &FunctionSummary {
&self.summary
}
}
72 changes: 72 additions & 0 deletions src/function/charlength.rs
Original file line number Diff line number Diff line change
@@ -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<Self> {
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<DataValue, DatabaseError> {
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<FuncMonotonicity> {
todo!()
}

fn return_type(&self) -> &LogicalType {
&LogicalType::Varchar(None, CharLengthUnits::Characters)
}

fn summary(&self) -> &FunctionSummary {
&self.summary
}
}
2 changes: 2 additions & 0 deletions src/function/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
25 changes: 16 additions & 9 deletions tests/slt/sql_2016/E021_04.slt
Original file line number Diff line number Diff line change
@@ -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' )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please restore the test content

----
6

query I
SELECT CHAR_LENGTH ( 'foo' )
----
3

# query I
# SELECT CHAR_LENGTH ( 'foo' )
# ----
# 3
query I
SELECT CHAR_LENGTH ( 'foooof' )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please restore the test content too

----
6
Loading