diff --git a/core/function.rs b/core/function.rs index c0baf807a..e6d6ab793 100644 --- a/core/function.rs +++ b/core/function.rs @@ -68,6 +68,7 @@ pub enum ScalarFunc { RTrim, Round, Length, + OctetLength, Min, Max, Nullif, @@ -108,6 +109,7 @@ impl Display for ScalarFunc { ScalarFunc::RTrim => "rtrim".to_string(), ScalarFunc::Round => "round".to_string(), ScalarFunc::Length => "length".to_string(), + ScalarFunc::OctetLength => "octet_length".to_string(), ScalarFunc::Min => "min".to_string(), ScalarFunc::Max => "max".to_string(), ScalarFunc::Nullif => "nullif".to_string(), @@ -186,6 +188,7 @@ impl Func { "rtrim" => Ok(Func::Scalar(ScalarFunc::RTrim)), "round" => Ok(Func::Scalar(ScalarFunc::Round)), "length" => Ok(Func::Scalar(ScalarFunc::Length)), + "octet_length" => Ok(Func::Scalar(ScalarFunc::OctetLength)), "sign" => Ok(Func::Scalar(ScalarFunc::Sign)), "substr" => Ok(Func::Scalar(ScalarFunc::Substr)), "substring" => Ok(Func::Scalar(ScalarFunc::Substring)), diff --git a/core/translate/expr.rs b/core/translate/expr.rs index ac497183a..0d3f357d4 100644 --- a/core/translate/expr.rs +++ b/core/translate/expr.rs @@ -1008,6 +1008,7 @@ pub fn translate_expr( | ScalarFunc::Lower | ScalarFunc::Upper | ScalarFunc::Length + | ScalarFunc::OctetLength | ScalarFunc::Typeof | ScalarFunc::Unicode | ScalarFunc::Quote diff --git a/core/vdbe/mod.rs b/core/vdbe/mod.rs index a42b449cd..da80fe06e 100644 --- a/core/vdbe/mod.rs +++ b/core/vdbe/mod.rs @@ -2133,6 +2133,7 @@ impl Program { | ScalarFunc::Lower | ScalarFunc::Upper | ScalarFunc::Length + | ScalarFunc::OctetLength | ScalarFunc::Typeof | ScalarFunc::Unicode | ScalarFunc::Quote @@ -2146,6 +2147,7 @@ impl Program { ScalarFunc::Lower => exec_lower(reg_value), ScalarFunc::Upper => exec_upper(reg_value), ScalarFunc::Length => Some(exec_length(reg_value)), + ScalarFunc::OctetLength => Some(exec_octet_length(reg_value)), ScalarFunc::Typeof => Some(exec_typeof(reg_value)), ScalarFunc::Unicode => Some(exec_unicode(reg_value)), ScalarFunc::Quote => Some(exec_quote(reg_value)), @@ -2545,6 +2547,17 @@ fn exec_length(reg: &OwnedValue) -> OwnedValue { } } +fn exec_octet_length(reg: &OwnedValue) -> OwnedValue { + match reg { + OwnedValue::Text(_) | OwnedValue::Integer(_) | OwnedValue::Float(_) => { + OwnedValue::Integer(reg.to_string().into_bytes().len() as i64) + } + OwnedValue::Blob(blob) => OwnedValue::Integer(blob.len() as i64), + OwnedValue::Agg(aggctx) => exec_octet_length(aggctx.final_value()), + _ => reg.to_owned(), + } +} + fn exec_upper(reg: &OwnedValue) -> Option { match reg { OwnedValue::Text(t) => Some(OwnedValue::Text(Rc::new(t.to_uppercase()))),