Skip to content
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
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
}
}
1 change: 1 addition & 0 deletions src/optimizer/core/histogram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ impl Histogram {
| LogicalType::Bigint
| LogicalType::UBigint
| LogicalType::Float
| LogicalType::Real
| LogicalType::Double
| LogicalType::Decimal(_, _) => value.clone().cast(&LogicalType::Double)?.double(),
LogicalType::Tuple(_) => match value {
Expand Down
1 change: 1 addition & 0 deletions src/types/evaluator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ impl EvaluatorFactory {
LogicalType::UInteger => numeric_binary_evaluator!(UInt32, op, LogicalType::UInteger),
LogicalType::UBigint => numeric_binary_evaluator!(UInt64, op, LogicalType::UBigint),
LogicalType::Float => numeric_binary_evaluator!(Float32, op, LogicalType::Float),
LogicalType::Real => numeric_binary_evaluator!(Float32, op, LogicalType::Real),
LogicalType::Double => numeric_binary_evaluator!(Float64, op, LogicalType::Double),
LogicalType::Date => numeric_binary_evaluator!(Date, op, LogicalType::Date),
LogicalType::DateTime => numeric_binary_evaluator!(DateTime, op, LogicalType::DateTime),
Expand Down
1 change: 1 addition & 0 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ impl TryFrom<sqlparser::ast::DataType> for LogicalType {
Ok(LogicalType::Varchar(None, CharLengthUnits::Characters))
}
sqlparser::ast::DataType::Float(_) => Ok(LogicalType::Float),
sqlparser::ast::DataType::Real => Ok(LogicalType::Float),
sqlparser::ast::DataType::Double | sqlparser::ast::DataType::DoublePrecision => {
Ok(LogicalType::Double)
}
Expand Down
8 changes: 8 additions & 0 deletions src/types/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ impl DataValue {
LogicalType::Bigint => DataValue::Int64(0),
LogicalType::UBigint => DataValue::UInt64(0),
LogicalType::Float => DataValue::Float32(OrderedFloat(0.0)),
LogicalType::Real => DataValue::Float32(OrderedFloat(0.0)),
LogicalType::Double => DataValue::Float64(OrderedFloat(0.0)),
LogicalType::Char(len, unit) => DataValue::Utf8 {
value: String::new(),
Expand Down Expand Up @@ -605,6 +606,13 @@ impl DataValue {
}
DataValue::Float32(OrderedFloat(reader.read_f32::<LittleEndian>()?))
}
LogicalType::Real => {
if !is_projection {
reader.seek(SeekFrom::Current(4))?;
return Ok(None);
}
DataValue::Float32(OrderedFloat(reader.read_f32::<LittleEndian>()?))
}
LogicalType::Double => {
if !is_projection {
reader.seek(SeekFrom::Current(8))?;
Expand Down
5 changes: 2 additions & 3 deletions tests/slt/sql_2016/E011_02.slt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ CREATE TABLE TABLE_E011_02_01_02 ( ID INT PRIMARY KEY, A FLOAT ( 2 ) )
statement ok
CREATE TABLE TABLE_E011_02_01_03 ( ID INT PRIMARY KEY, A FLOAT )

# TODO: REAL
# statement ok
# CREATE TABLE TABLE_E011_02_01_04 ( ID INT PRIMARY KEY, A REAL )
statement ok
CREATE TABLE TABLE_E011_02_01_04 ( ID INT PRIMARY KEY, A REAL )

query R
SELECT +7.8
Expand Down
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