Skip to content
Merged
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
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nightly-2024-04-27
nightly-2024-10-18
4 changes: 4 additions & 0 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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::current_date::CurrentDate;
use crate::function::lower::Lower;
use crate::function::numbers::Numbers;
Expand Down Expand Up @@ -50,6 +51,9 @@ impl DataBaseBuilder {
scala_functions: Default::default(),
table_functions: Default::default(),
};
builder = builder.register_scala_function(CharLength::new("char_length".to_lowercase()));
builder =
builder.register_scala_function(CharLength::new("character_length".to_lowercase()));
builder = builder.register_scala_function(CurrentDate::new());
builder = builder.register_scala_function(Lower::new());
builder = builder.register_scala_function(Upper::new());
Expand Down
70 changes: 70 additions & 0 deletions src/function/char_length.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
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 {
pub(crate) fn new(function_name: String) -> Arc<Self> {
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
}
}
1 change: 1 addition & 0 deletions src/function/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub(crate) mod char_length;
pub(crate) mod current_date;
pub(crate) mod lower;
pub(crate) mod numbers;
Expand Down
18 changes: 8 additions & 10 deletions tests/slt/sql_2016/E021_04.slt
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
# 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 CHAR_LENGTH ( 'foo' )
# ----
# 3
query I
SELECT CHAR_LENGTH ( 'foo' )
----
3
Loading