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: 2 additions & 0 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::function::char_length::CharLength;
use crate::function::current_date::CurrentDate;
use crate::function::lower::Lower;
use crate::function::numbers::Numbers;
use crate::function::octet_length::OctetLength;
use crate::function::upper::Upper;
use crate::optimizer::heuristic::batch::HepBatchStrategy;
use crate::optimizer::heuristic::optimizer::HepOptimizer;
Expand Down Expand Up @@ -61,6 +62,7 @@ impl DataBaseBuilder {
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(OctetLength::new());
builder = builder.register_scala_function(Upper::new());
builder = builder.register_table_function(Numbers::new());
builder
Expand Down
2 changes: 1 addition & 1 deletion src/function/char_length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl ScalarFunctionImpl for CharLength {
}
let mut length: u64 = 0;
if let DataValue::Utf8 { value, ty, unit } = &mut value {
length = value.len() as u64;
length = value.chars().count() as u64;
}
Ok(DataValue::UInt64(length))
}
Expand Down
1 change: 1 addition & 0 deletions src/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ pub(crate) mod char_length;
pub(crate) mod current_date;
pub(crate) mod lower;
pub(crate) mod numbers;
pub(crate) mod octet_length;
pub(crate) mod upper;
63 changes: 63 additions & 0 deletions src/function/octet_length.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
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 OctetLength {
summary: FunctionSummary,
}

impl OctetLength {
pub(crate) fn new() -> Arc<Self> {
let function_name = "octet_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 OctetLength {
#[allow(unused_variables, clippy::redundant_closure_call)]
fn eval(
&self,
exprs: &[ScalarExpression],
tuples: Option<(&Tuple, &[ColumnRef])>,
) -> Result<DataValue, DatabaseError> {
let mut value = exprs[0].eval(tuples)?;
if !matches!(value.logical_type(), LogicalType::Varchar(_, _)) {
value = value.cast(&LogicalType::Varchar(None, CharLengthUnits::Characters))?;
}
let mut length: u64 = 0;
if let DataValue::Utf8 { value, ty, unit } = &mut value {
length = value.len() as u64;
}
Ok(DataValue::UInt64(length))
}

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

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

fn summary(&self) -> &FunctionSummary {
&self.summary
}
}
11 changes: 10 additions & 1 deletion tests/slt/sql_2016/E021_04.slt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@ SELECT CHARACTER_LENGTH ( 'foo' )
----
3


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

query I
SELECT CHARACTER_LENGTH ( '测试' )
----
2

query I
SELECT CHAR_LENGTH ( '测试' )
----
2
15 changes: 9 additions & 6 deletions tests/slt/sql_2016/E021_05.slt
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# E021-05: OCTET_LENGTH function
#E021-05: OCTET_LENGTH function

# TODO: OCTET_LENGTH()
query I
SELECT OCTET_LENGTH ( 'foo' )
----
3

# query I
# SELECT OCTET_LENGTH ( 'foo' )
# ----
# 3
query I
SELECT OCTET_LENGTH ( '测试' )
----
6
Loading