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 #234

Closed
wants to merge 6 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
8 changes: 8 additions & 0 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ 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::char_length::CharLength;
use crate::function::char_length::CharacterLength;
use crate::function::current_date::CurrentDate;
use crate::function::lower::Lower;
use crate::function::numbers::Numbers;
use crate::function::upper::Upper;
use crate::optimizer::heuristic::batch::HepBatchStrategy;
use crate::optimizer::heuristic::optimizer::HepOptimizer;
use crate::optimizer::rule::implementation::ImplementationRuleImpl;
Expand Down Expand Up @@ -48,7 +52,11 @@ impl DataBaseBuilder {
scala_functions: Default::default(),
table_functions: Default::default(),
};
builder = builder.register_scala_function(CharLength::new());
builder = builder.register_scala_function(CharacterLength::new2());
builder = builder.register_scala_function(CurrentDate::new());
builder = builder.register_scala_function(Lower::new());
builder = builder.register_scala_function(Upper::new());
builder = builder.register_table_function(Numbers::new());
builder
}
Expand Down
88 changes: 88 additions & 0 deletions src/function/char_length.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
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,
},
})
}
}

pub type CharacterLength = CharLength;

impl CharacterLength {
#[allow(unused_mut)]
pub(crate) fn new2() -> 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 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
}
}
71 changes: 71 additions & 0 deletions src/function/lower.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
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 Lower {
summary: FunctionSummary,
}

impl Lower {
#[allow(unused_mut)]
pub(crate) fn new() -> Arc<Self> {
let function_name = "lower".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 Lower {
#[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))?;
}
if let DataValue::Utf8 {
value: Some(value),
ty,
unit,
} = &mut value
{
*value = value.to_lowercase();
}
Ok(value)
}

fn monotonicity(&self) -> Option<FuncMonotonicity> {
todo!()
}

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

fn summary(&self) -> &FunctionSummary {
&self.summary
}
}
3 changes: 3 additions & 0 deletions src/function/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
pub(crate) mod char_length;
pub(crate) mod current_date;
pub(crate) mod lower;
pub(crate) mod numbers;
pub(crate) mod upper;
71 changes: 71 additions & 0 deletions src/function/upper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
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 Upper {
summary: FunctionSummary,
}

impl Upper {
#[allow(unused_mut)]
pub(crate) fn new() -> Arc<Self> {
let function_name = "upper".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 Upper {
#[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))?;
}
if let DataValue::Utf8 {
value: Some(value),
ty,
unit,
} = &mut value
{
*value = value.to_uppercase();
}
Ok(value)
}

fn monotonicity(&self) -> Option<FuncMonotonicity> {
todo!()
}

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

fn summary(&self) -> &FunctionSummary {
&self.summary
}
}
21 changes: 9 additions & 12 deletions tests/slt/sql_2016/E021_04.slt
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
# E021-04: CHARACTER_LENGTH function

# TODO: CHARACTER_LENGTH()/CHAR_LENGTH()

# query I
# SELECT CHARACTER_LENGTH ( 'foo' )
# ----
# 3


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

query I
SELECT CHAR_LENGTH ( 'foo' )
----
3
36 changes: 27 additions & 9 deletions tests/slt/sql_2016/E021_08.slt
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
# E021-08: UPPER and LOWER functions

# TODO: LOWER()/UPPER()
query T
SELECT LOWER ( 'FOO' )
----
foo

# query T
# SELECT LOWER ( 'foo' )
# ----
# 'foo'
query T
SELECT LOWER ( 'foo' )
----
foo

# query T
# SELECT UPPER ( 'foo' )
# ----
# 'FOO'
query T
SELECT UPPER ( 'foo' )
----
FOO

query T
SELECT UPPER ( 'FOO' )
----
FOO

query T
SELECT UPPER ( LOWER ( 'FOO' ) )
----
FOO

query T
SELECT LOWER ( UPPER ( 'foo' ) )
----
foo
Loading