Adapt OwnedValues in uuid ext to new LimboText

This commit is contained in:
PThorpe92
2024-12-20 16:03:16 -05:00
parent 2fcae80902
commit c06c4115f1
6 changed files with 40 additions and 55 deletions

View File

@@ -16,7 +16,6 @@ path = "lib.rs"
[features]
default = ["fs", "json", "uuid"]
fs = []
uuid = ["dep:uuid"]
json = [
"dep:jsonb",
"dep:pest",

View File

@@ -20,11 +20,11 @@ impl std::fmt::Display for ExtFunc {
}
impl ExtFunc {
pub fn resolve_function(name: &str, num_args: usize) -> Result<ExtFunc, ()> {
pub fn resolve_function(name: &str, num_args: usize) -> Option<ExtFunc> {
match name {
#[cfg(feature = "uuid")]
name => UuidFunc::resolve_function(name, num_args),
_ => Err(()),
_ => None,
}
}
}

View File

@@ -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<ExtFunc, ()> {
pub fn resolve_function(name: &str, num_args: usize) -> Option<ExtFunc> {
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<Owne
UuidFunc::Uuid4 => 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<OwnedValue> {
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<OwnedValue> {
pub fn exec_uuidblob(reg: &OwnedValue) -> crate::Result<OwnedValue> {
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);
}

View File

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

View File

@@ -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 => {

View File

@@ -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 => {