add octet_length scalar function

This commit is contained in:
Kacper Kołodziej
2024-12-10 22:55:59 +01:00
parent f9b300a608
commit d4bff2c93e
3 changed files with 17 additions and 0 deletions

View File

@@ -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)),

View File

@@ -1008,6 +1008,7 @@ pub fn translate_expr(
| ScalarFunc::Lower
| ScalarFunc::Upper
| ScalarFunc::Length
| ScalarFunc::OctetLength
| ScalarFunc::Typeof
| ScalarFunc::Unicode
| ScalarFunc::Quote

View File

@@ -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<OwnedValue> {
match reg {
OwnedValue::Text(t) => Some(OwnedValue::Text(Rc::new(t.to_uppercase()))),