From c06c4115f1e5ec6ed59f1f0daddf296847b7fb71 Mon Sep 17 00:00:00 2001 From: PThorpe92 Date: Fri, 20 Dec 2024 16:03:16 -0500 Subject: [PATCH] Adapt OwnedValues in uuid ext to new LimboText --- core/Cargo.toml | 1 - core/ext/mod.rs | 4 ++-- core/ext/uuid.rs | 53 ++++++++++++++++++++++++------------------ core/function.rs | 8 +++---- core/translate/expr.rs | 2 +- core/vdbe/mod.rs | 27 +++------------------ 6 files changed, 40 insertions(+), 55 deletions(-) diff --git a/core/Cargo.toml b/core/Cargo.toml index 25a0c6c90..4ef87b469 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -16,7 +16,6 @@ path = "lib.rs" [features] default = ["fs", "json", "uuid"] fs = [] -uuid = ["dep:uuid"] json = [ "dep:jsonb", "dep:pest", diff --git a/core/ext/mod.rs b/core/ext/mod.rs index fea543869..312ebfcea 100644 --- a/core/ext/mod.rs +++ b/core/ext/mod.rs @@ -20,11 +20,11 @@ impl std::fmt::Display for ExtFunc { } impl ExtFunc { - pub fn resolve_function(name: &str, num_args: usize) -> Result { + pub fn resolve_function(name: &str, num_args: usize) -> Option { match name { #[cfg(feature = "uuid")] name => UuidFunc::resolve_function(name, num_args), - _ => Err(()), + _ => None, } } } diff --git a/core/ext/uuid.rs b/core/ext/uuid.rs index aa717c13d..00ce23d9b 100644 --- a/core/ext/uuid.rs +++ b/core/ext/uuid.rs @@ -1,5 +1,8 @@ use super::ExtFunc; -use crate::{types::OwnedValue, LimboError}; +use crate::{ + types::{LimboText, OwnedValue}, + LimboError, +}; use std::rc::Rc; use uuid::{ContextV7, Timestamp, Uuid}; @@ -14,17 +17,17 @@ pub enum UuidFunc { } impl UuidFunc { - pub fn resolve_function(name: &str, num_args: usize) -> Result { + pub fn resolve_function(name: &str, num_args: usize) -> Option { match name { - "uuid4_str" => Ok(ExtFunc::Uuid(UuidFunc::Uuid4Str)), - "uuid4" => Ok(ExtFunc::Uuid(UuidFunc::Uuid4)), - "uuid7" if num_args < 2 => Ok(ExtFunc::Uuid(UuidFunc::Uuid7)), - "uuid_str" if num_args == 1 => Ok(ExtFunc::Uuid(UuidFunc::UuidStr)), - "uuid_blob" if num_args == 1 => Ok(ExtFunc::Uuid(UuidFunc::UuidBlob)), - "uuid7_timestamp_ms" if num_args == 1 => Ok(ExtFunc::Uuid(UuidFunc::Uuid7TS)), + "uuid4_str" => Some(ExtFunc::Uuid(UuidFunc::Uuid4Str)), + "uuid4" => Some(ExtFunc::Uuid(UuidFunc::Uuid4)), + "uuid7" if num_args < 2 => Some(ExtFunc::Uuid(UuidFunc::Uuid7)), + "uuid_str" if num_args == 1 => Some(ExtFunc::Uuid(UuidFunc::UuidStr)), + "uuid_blob" if num_args == 1 => Some(ExtFunc::Uuid(UuidFunc::UuidBlob)), + "uuid7_timestamp_ms" if num_args == 1 => Some(ExtFunc::Uuid(UuidFunc::Uuid7TS)), // postgres_compatability - "gen_random_uuid" => Ok(ExtFunc::Uuid(UuidFunc::Uuid4Str)), - _ => Err(()), + "gen_random_uuid" => Some(ExtFunc::Uuid(UuidFunc::Uuid4Str)), + _ => None, } } } @@ -47,7 +50,9 @@ pub fn exec_uuid(var: &UuidFunc, sec: Option<&OwnedValue>) -> crate::Result Ok(OwnedValue::Blob(Rc::new( Uuid::new_v4().into_bytes().to_vec(), ))), - UuidFunc::Uuid4Str => Ok(OwnedValue::Text(Rc::new(Uuid::new_v4().to_string()))), + UuidFunc::Uuid4Str => Ok(OwnedValue::Text(LimboText::new(Rc::new( + Uuid::new_v4().to_string(), + )))), UuidFunc::Uuid7 => { let uuid = match sec { Some(OwnedValue::Integer(ref seconds)) => { @@ -70,11 +75,12 @@ pub fn exec_uuidstr(reg: &OwnedValue) -> crate::Result { match reg { OwnedValue::Blob(blob) => { let uuid = Uuid::from_slice(blob).map_err(|e| LimboError::ParseError(e.to_string()))?; - Ok(OwnedValue::Text(Rc::new(uuid.to_string()))) + Ok(OwnedValue::Text(LimboText::new(Rc::new(uuid.to_string())))) } - OwnedValue::Text(val) => { - let uuid = Uuid::parse_str(val).map_err(|e| LimboError::ParseError(e.to_string()))?; - Ok(OwnedValue::Text(Rc::new(uuid.to_string()))) + OwnedValue::Text(ref val) => { + let uuid = + Uuid::parse_str(&val.value).map_err(|e| LimboError::ParseError(e.to_string()))?; + Ok(OwnedValue::Text(LimboText::new(Rc::new(uuid.to_string())))) } OwnedValue::Null => Ok(OwnedValue::Null), _ => Err(LimboError::ParseError( @@ -86,7 +92,8 @@ pub fn exec_uuidstr(reg: &OwnedValue) -> crate::Result { pub fn exec_uuidblob(reg: &OwnedValue) -> crate::Result { match reg { OwnedValue::Text(val) => { - let uuid = Uuid::parse_str(val).map_err(|e| LimboError::ParseError(e.to_string()))?; + let uuid = + Uuid::parse_str(&val.value).map_err(|e| LimboError::ParseError(e.to_string()))?; Ok(OwnedValue::Blob(Rc::new(uuid.as_bytes().to_vec()))) } OwnedValue::Blob(blob) => { @@ -106,7 +113,7 @@ pub fn exec_ts_from_uuid7(reg: &OwnedValue) -> OwnedValue { Uuid::from_slice(blob).map_err(|e| LimboError::ParseError(e.to_string())) } OwnedValue::Text(val) => { - Uuid::parse_str(val).map_err(|e| LimboError::ParseError(e.to_string())) + Uuid::parse_str(&val.value).map_err(|e| LimboError::ParseError(e.to_string())) } _ => Err(LimboError::ParseError( "Invalid argument type for UUID function".to_string(), @@ -159,8 +166,8 @@ pub mod test { let owned_val = exec_uuid(&func, None); match owned_val { Ok(OwnedValue::Text(v4str)) => { - assert_eq!(v4str.len(), 36); - let uuid = Uuid::parse_str(&v4str); + assert_eq!(v4str.value.len(), 36); + let uuid = Uuid::parse_str(&v4str.value); assert!(uuid.is_ok()); assert_eq!(uuid.unwrap().get_version_num(), 4); } @@ -303,8 +310,8 @@ pub mod test { exec_uuidstr(&exec_uuid(&UuidFunc::Uuid4, None).expect("uuid v7 blob to generate")); match owned_val { Ok(OwnedValue::Text(v4str)) => { - assert_eq!(v4str.len(), 36); - let uuid = Uuid::parse_str(&v4str); + assert_eq!(v4str.value.len(), 36); + let uuid = Uuid::parse_str(&v4str.value); assert!(uuid.is_ok()); assert_eq!(uuid.unwrap().get_version_num(), 4); } @@ -323,8 +330,8 @@ pub mod test { ); match owned_val { Ok(OwnedValue::Text(v7str)) => { - assert_eq!(v7str.len(), 36); - let uuid = Uuid::parse_str(&v7str); + assert_eq!(v7str.value.len(), 36); + let uuid = Uuid::parse_str(&v7str.value); assert!(uuid.is_ok()); assert_eq!(uuid.unwrap().get_version_num(), 7); } diff --git a/core/function.rs b/core/function.rs index 7d97432ca..8681a4fdf 100644 --- a/core/function.rs +++ b/core/function.rs @@ -263,7 +263,7 @@ pub enum Func { Math(MathFunc), #[cfg(feature = "json")] Json(JsonFunc), - Extention(ExtFunc), + Extension(ExtFunc), } impl Display for Func { @@ -274,7 +274,7 @@ impl Display for Func { Func::Math(math_func) => write!(f, "{}", math_func), #[cfg(feature = "json")] Func::Json(json_func) => write!(f, "{}", json_func), - Func::Extention(ext_func) => write!(f, "{}", ext_func), + Func::Extension(ext_func) => write!(f, "{}", ext_func), } } } @@ -369,8 +369,8 @@ impl Func { "tanh" => Ok(Func::Math(MathFunc::Tanh)), "trunc" => Ok(Func::Math(MathFunc::Trunc)), _ => match ExtFunc::resolve_function(name, arg_count) { - Ok(ext_func) => Ok(Func::Extention(ext_func)), - Err(_) => Err(()), + Some(ext_func) => Ok(Func::Extension(ext_func)), + None => Err(()), }, } } diff --git a/core/translate/expr.rs b/core/translate/expr.rs index 523be6e51..512e8e394 100644 --- a/core/translate/expr.rs +++ b/core/translate/expr.rs @@ -1615,7 +1615,7 @@ pub fn translate_expr( } } } - Func::Extention(ext_func) => match ext_func { + Func::Extension(ext_func) => match ext_func { #[cfg(feature = "uuid")] ExtFunc::Uuid(ref uuid_fn) => match uuid_fn { UuidFunc::UuidStr | UuidFunc::UuidBlob | UuidFunc::Uuid7TS => { diff --git a/core/vdbe/mod.rs b/core/vdbe/mod.rs index b5eb9d60f..520100463 100644 --- a/core/vdbe/mod.rs +++ b/core/vdbe/mod.rs @@ -26,7 +26,7 @@ mod datetime; use crate::error::{LimboError, SQLITE_CONSTRAINT_PRIMARYKEY}; #[cfg(feature = "uuid")] use crate::ext::{exec_ts_from_uuid7, exec_uuid, exec_uuidblob, exec_uuidstr, ExtFunc, UuidFunc}; -use crate::function::{AggFunc, Func, FuncCtx, MathFunc, MathFuncArity, ScalarFunc}; +use crate::function::{AggFunc, FuncCtx, MathFunc, MathFuncArity, ScalarFunc}; use crate::pseudo::PseudoCursor; use crate::schema::Table; use crate::storage::sqlite3_ondisk::DatabaseHeader; @@ -39,6 +39,7 @@ use crate::util::parse_schema_rows; use crate::{function::JsonFunc, json::get_json, json::json_array}; use crate::{Connection, Result, TransactionState}; use crate::{Rows, DATABASE_VERSION}; +use macros::Description; use datetime::{exec_date, exec_time, exec_unixepoch}; @@ -50,33 +51,11 @@ use std::cell::RefCell; use std::collections::{BTreeMap, HashMap}; use std::rc::{Rc, Weak}; -use uuid::{ContextV7, Timestamp, Uuid}; - pub type BranchOffset = i64; -use macros::Description; pub type CursorID = usize; pub type PageIdx = usize; -#[allow(dead_code)] -#[derive(Debug)] -pub enum Func { - Scalar(ScalarFunc), - #[cfg(feature = "json")] - Json(JsonFunc), -} - -impl Display for Func { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let str = match self { - Func::Scalar(scalar_func) => scalar_func.to_string(), - #[cfg(feature = "json")] - Func::Json(json_func) => json_func.to_string(), - }; - write!(f, "{}", str) - } -} - #[derive(Description, Debug)] pub enum Insn { // Initialize the program state and jump to the given PC. @@ -2535,7 +2514,7 @@ impl Program { state.registers[*dest] = exec_replace(source, pattern, replacement); } }, - Func::Extention(extfn) => match extfn { + crate::function::Func::Extension(extfn) => match extfn { #[cfg(feature = "uuid")] ExtFunc::Uuid(uuidfn) => match uuidfn { UuidFunc::Uuid4 | UuidFunc::Uuid4Str => {