diff --git a/core/functions/datetime.rs b/core/functions/datetime.rs index 8c5c0aaff..aeac63337 100644 --- a/core/functions/datetime.rs +++ b/core/functions/datetime.rs @@ -4,7 +4,6 @@ use crate::Result; use chrono::{ DateTime, Datelike, NaiveDate, NaiveDateTime, NaiveTime, TimeDelta, TimeZone, Timelike, Utc, }; -use std::rc::Rc; /// Execution of date/time/datetime functions #[inline(always)] @@ -48,8 +47,7 @@ enum DateTimeOutput { fn exec_datetime(values: &[OwnedValue], output_type: DateTimeOutput) -> OwnedValue { if values.is_empty() { - let now = - parse_naive_date_time(&OwnedValue::build_text(Rc::new("now".to_string()))).unwrap(); + let now = parse_naive_date_time(&OwnedValue::build_text("now")).unwrap(); let formatted_str = match output_type { DateTimeOutput::DateTime => now.format("%Y-%m-%d %H:%M:%S").to_string(), @@ -59,7 +57,7 @@ fn exec_datetime(values: &[OwnedValue], output_type: DateTimeOutput) -> OwnedVal }; // Parse here - return OwnedValue::build_text(Rc::new(formatted_str)); + return OwnedValue::build_text(&formatted_str); } if let Some(mut dt) = parse_naive_date_time(&values[0]) { // if successful, treat subsequent entries as modifiers @@ -85,17 +83,17 @@ fn modify_dt( match apply_modifier(dt, text_rc.as_str()) { Ok(true) => subsec_requested = true, Ok(false) => {} - Err(_) => return OwnedValue::build_text(Rc::new(String::new())), + Err(_) => return OwnedValue::build_text(""), } } else { - return OwnedValue::build_text(Rc::new(String::new())); + return OwnedValue::build_text(""); } } if is_leap_second(dt) || *dt > get_max_datetime_exclusive() { - return OwnedValue::build_text(Rc::new(String::new())); + return OwnedValue::build_text(""); } let formatted = format_dt(*dt, output_type, subsec_requested); - OwnedValue::build_text(Rc::new(formatted)) + OwnedValue::build_text(&formatted) } fn format_dt(dt: NaiveDateTime, output_type: DateTimeOutput, subsec: bool) -> String { @@ -662,7 +660,6 @@ fn parse_modifier(modifier: &str) -> Result { #[cfg(test)] mod tests { use super::*; - use std::rc::Rc; #[test] fn test_valid_get_date_from_time_value() { @@ -674,201 +671,135 @@ mod tests { let test_cases = vec![ // Format 1: YYYY-MM-DD (no timezone applicable) - ( - OwnedValue::build_text(Rc::new("2024-07-21".to_string())), - test_date_str, - ), + (OwnedValue::build_text("2024-07-21"), test_date_str), // Format 2: YYYY-MM-DD HH:MM + (OwnedValue::build_text("2024-07-21 22:30"), test_date_str), ( - OwnedValue::build_text(Rc::new("2024-07-21 22:30".to_string())), + OwnedValue::build_text("2024-07-21 22:30+02:00"), test_date_str, ), ( - OwnedValue::build_text(Rc::new("2024-07-21 22:30+02:00".to_string())), - test_date_str, - ), - ( - OwnedValue::build_text(Rc::new("2024-07-21 22:30-05:00".to_string())), + OwnedValue::build_text("2024-07-21 22:30-05:00"), next_date_str, ), ( - OwnedValue::build_text(Rc::new("2024-07-21 01:30+05:00".to_string())), + OwnedValue::build_text("2024-07-21 01:30+05:00"), prev_date_str, ), - ( - OwnedValue::build_text(Rc::new("2024-07-21 22:30Z".to_string())), - test_date_str, - ), + (OwnedValue::build_text("2024-07-21 22:30Z"), test_date_str), // Format 3: YYYY-MM-DD HH:MM:SS + (OwnedValue::build_text("2024-07-21 22:30:45"), test_date_str), ( - OwnedValue::build_text(Rc::new("2024-07-21 22:30:45".to_string())), + OwnedValue::build_text("2024-07-21 22:30:45+02:00"), test_date_str, ), ( - OwnedValue::build_text(Rc::new("2024-07-21 22:30:45+02:00".to_string())), - test_date_str, - ), - ( - OwnedValue::build_text(Rc::new("2024-07-21 22:30:45-05:00".to_string())), + OwnedValue::build_text("2024-07-21 22:30:45-05:00"), next_date_str, ), ( - OwnedValue::build_text(Rc::new("2024-07-21 01:30:45+05:00".to_string())), + OwnedValue::build_text("2024-07-21 01:30:45+05:00"), prev_date_str, ), ( - OwnedValue::build_text(Rc::new("2024-07-21 22:30:45Z".to_string())), + OwnedValue::build_text("2024-07-21 22:30:45Z"), test_date_str, ), // Format 4: YYYY-MM-DD HH:MM:SS.SSS ( - OwnedValue::build_text(Rc::new("2024-07-21 22:30:45.123".to_string())), + OwnedValue::build_text("2024-07-21 22:30:45.123"), test_date_str, ), ( - OwnedValue::build_text(Rc::new("2024-07-21 22:30:45.123+02:00".to_string())), + OwnedValue::build_text("2024-07-21 22:30:45.123+02:00"), test_date_str, ), ( - OwnedValue::build_text(Rc::new("2024-07-21 22:30:45.123-05:00".to_string())), + OwnedValue::build_text("2024-07-21 22:30:45.123-05:00"), next_date_str, ), ( - OwnedValue::build_text(Rc::new("2024-07-21 01:30:45.123+05:00".to_string())), + OwnedValue::build_text("2024-07-21 01:30:45.123+05:00"), prev_date_str, ), ( - OwnedValue::build_text(Rc::new("2024-07-21 22:30:45.123Z".to_string())), + OwnedValue::build_text("2024-07-21 22:30:45.123Z"), test_date_str, ), // Format 5: YYYY-MM-DDTHH:MM + (OwnedValue::build_text("2024-07-21T22:30"), test_date_str), ( - OwnedValue::build_text(Rc::new("2024-07-21T22:30".to_string())), + OwnedValue::build_text("2024-07-21T22:30+02:00"), test_date_str, ), ( - OwnedValue::build_text(Rc::new("2024-07-21T22:30+02:00".to_string())), - test_date_str, - ), - ( - OwnedValue::build_text(Rc::new("2024-07-21T22:30-05:00".to_string())), + OwnedValue::build_text("2024-07-21T22:30-05:00"), next_date_str, ), ( - OwnedValue::build_text(Rc::new("2024-07-21T01:30+05:00".to_string())), + OwnedValue::build_text("2024-07-21T01:30+05:00"), prev_date_str, ), - ( - OwnedValue::build_text(Rc::new("2024-07-21T22:30Z".to_string())), - test_date_str, - ), + (OwnedValue::build_text("2024-07-21T22:30Z"), test_date_str), // Format 6: YYYY-MM-DDTHH:MM:SS + (OwnedValue::build_text("2024-07-21T22:30:45"), test_date_str), ( - OwnedValue::build_text(Rc::new("2024-07-21T22:30:45".to_string())), + OwnedValue::build_text("2024-07-21T22:30:45+02:00"), test_date_str, ), ( - OwnedValue::build_text(Rc::new("2024-07-21T22:30:45+02:00".to_string())), - test_date_str, - ), - ( - OwnedValue::build_text(Rc::new("2024-07-21T22:30:45-05:00".to_string())), + OwnedValue::build_text("2024-07-21T22:30:45-05:00"), next_date_str, ), ( - OwnedValue::build_text(Rc::new("2024-07-21T01:30:45+05:00".to_string())), + OwnedValue::build_text("2024-07-21T01:30:45+05:00"), prev_date_str, ), ( - OwnedValue::build_text(Rc::new("2024-07-21T22:30:45Z".to_string())), + OwnedValue::build_text("2024-07-21T22:30:45Z"), test_date_str, ), // Format 7: YYYY-MM-DDTHH:MM:SS.SSS ( - OwnedValue::build_text(Rc::new("2024-07-21T22:30:45.123".to_string())), + OwnedValue::build_text("2024-07-21T22:30:45.123"), test_date_str, ), ( - OwnedValue::build_text(Rc::new("2024-07-21T22:30:45.123+02:00".to_string())), + OwnedValue::build_text("2024-07-21T22:30:45.123+02:00"), test_date_str, ), ( - OwnedValue::build_text(Rc::new("2024-07-21T22:30:45.123-05:00".to_string())), + OwnedValue::build_text("2024-07-21T22:30:45.123-05:00"), next_date_str, ), ( - OwnedValue::build_text(Rc::new("2024-07-21T01:30:45.123+05:00".to_string())), + OwnedValue::build_text("2024-07-21T01:30:45.123+05:00"), prev_date_str, ), ( - OwnedValue::build_text(Rc::new("2024-07-21T22:30:45.123Z".to_string())), + OwnedValue::build_text("2024-07-21T22:30:45.123Z"), test_date_str, ), // Format 8: HH:MM - ( - OwnedValue::build_text(Rc::new("22:30".to_string())), - "2000-01-01", - ), - ( - OwnedValue::build_text(Rc::new("22:30+02:00".to_string())), - "2000-01-01", - ), - ( - OwnedValue::build_text(Rc::new("22:30-05:00".to_string())), - "2000-01-02", - ), - ( - OwnedValue::build_text(Rc::new("01:30+05:00".to_string())), - "1999-12-31", - ), - ( - OwnedValue::build_text(Rc::new("22:30Z".to_string())), - "2000-01-01", - ), + (OwnedValue::build_text("22:30"), "2000-01-01"), + (OwnedValue::build_text("22:30+02:00"), "2000-01-01"), + (OwnedValue::build_text("22:30-05:00"), "2000-01-02"), + (OwnedValue::build_text("01:30+05:00"), "1999-12-31"), + (OwnedValue::build_text("22:30Z"), "2000-01-01"), // Format 9: HH:MM:SS - ( - OwnedValue::build_text(Rc::new("22:30:45".to_string())), - "2000-01-01", - ), - ( - OwnedValue::build_text(Rc::new("22:30:45+02:00".to_string())), - "2000-01-01", - ), - ( - OwnedValue::build_text(Rc::new("22:30:45-05:00".to_string())), - "2000-01-02", - ), - ( - OwnedValue::build_text(Rc::new("01:30:45+05:00".to_string())), - "1999-12-31", - ), - ( - OwnedValue::build_text(Rc::new("22:30:45Z".to_string())), - "2000-01-01", - ), + (OwnedValue::build_text("22:30:45"), "2000-01-01"), + (OwnedValue::build_text("22:30:45+02:00"), "2000-01-01"), + (OwnedValue::build_text("22:30:45-05:00"), "2000-01-02"), + (OwnedValue::build_text("01:30:45+05:00"), "1999-12-31"), + (OwnedValue::build_text("22:30:45Z"), "2000-01-01"), // Format 10: HH:MM:SS.SSS - ( - OwnedValue::build_text(Rc::new("22:30:45.123".to_string())), - "2000-01-01", - ), - ( - OwnedValue::build_text(Rc::new("22:30:45.123+02:00".to_string())), - "2000-01-01", - ), - ( - OwnedValue::build_text(Rc::new("22:30:45.123-05:00".to_string())), - "2000-01-02", - ), - ( - OwnedValue::build_text(Rc::new("01:30:45.123+05:00".to_string())), - "1999-12-31", - ), - ( - OwnedValue::build_text(Rc::new("22:30:45.123Z".to_string())), - "2000-01-01", - ), + (OwnedValue::build_text("22:30:45.123"), "2000-01-01"), + (OwnedValue::build_text("22:30:45.123+02:00"), "2000-01-01"), + (OwnedValue::build_text("22:30:45.123-05:00"), "2000-01-02"), + (OwnedValue::build_text("01:30:45.123+05:00"), "1999-12-31"), + (OwnedValue::build_text("22:30:45.123Z"), "2000-01-01"), // Test Format 11: 'now' - (OwnedValue::build_text(Rc::new("now".to_string())), &now), + (OwnedValue::build_text("now"), &now), // Format 12: DDDDDDDDDD (Julian date as float or integer) (OwnedValue::Float(2460512.5), test_date_str), (OwnedValue::Integer(2460513), test_date_str), @@ -878,7 +809,7 @@ mod tests { let result = exec_date(&[input.clone()]); assert_eq!( result, - OwnedValue::build_text(Rc::new(expected.to_string())), + OwnedValue::build_text(expected), "Failed for input: {:?}", input ); @@ -888,31 +819,31 @@ mod tests { #[test] fn test_invalid_get_date_from_time_value() { let invalid_cases = vec![ - OwnedValue::build_text(Rc::new("2024-07-21 25:00".to_string())), // Invalid hour - OwnedValue::build_text(Rc::new("2024-07-21 24:00:00".to_string())), // Invalid hour - OwnedValue::build_text(Rc::new("2024-07-21 23:60:00".to_string())), // Invalid minute - OwnedValue::build_text(Rc::new("2024-07-21 22:58:60".to_string())), // Invalid second - OwnedValue::build_text(Rc::new("2024-07-32".to_string())), // Invalid day - OwnedValue::build_text(Rc::new("2024-13-01".to_string())), // Invalid month - OwnedValue::build_text(Rc::new("invalid_date".to_string())), // Completely invalid string - OwnedValue::build_text(Rc::new("".to_string())), // Empty string - OwnedValue::Integer(i64::MAX), // Large Julian day - OwnedValue::Integer(-1), // Negative Julian day - OwnedValue::Float(f64::MAX), // Large float - OwnedValue::Float(-1.0), // Negative Julian day as float - OwnedValue::Float(f64::NAN), // NaN - OwnedValue::Float(f64::INFINITY), // Infinity - OwnedValue::Null, // Null value - OwnedValue::Blob(vec![1, 2, 3].into()), // Blob (unsupported type) + OwnedValue::build_text("2024-07-21 25:00"), // Invalid hour + OwnedValue::build_text("2024-07-21 24:00:00"), // Invalid hour + OwnedValue::build_text("2024-07-21 23:60:00"), // Invalid minute + OwnedValue::build_text("2024-07-21 22:58:60"), // Invalid second + OwnedValue::build_text("2024-07-32"), // Invalid day + OwnedValue::build_text("2024-13-01"), // Invalid month + OwnedValue::build_text("invalid_date"), // Completely invalid string + OwnedValue::build_text(""), // Empty string + OwnedValue::Integer(i64::MAX), // Large Julian day + OwnedValue::Integer(-1), // Negative Julian day + OwnedValue::Float(f64::MAX), // Large float + OwnedValue::Float(-1.0), // Negative Julian day as float + OwnedValue::Float(f64::NAN), // NaN + OwnedValue::Float(f64::INFINITY), // Infinity + OwnedValue::Null, // Null value + OwnedValue::Blob(vec![1, 2, 3].into()), // Blob (unsupported type) // Invalid timezone tests - OwnedValue::build_text(Rc::new("2024-07-21T12:00:00+24:00".to_string())), // Invalid timezone offset (too large) - OwnedValue::build_text(Rc::new("2024-07-21T12:00:00-24:00".to_string())), // Invalid timezone offset (too small) - OwnedValue::build_text(Rc::new("2024-07-21T12:00:00+00:60".to_string())), // Invalid timezone minutes - OwnedValue::build_text(Rc::new("2024-07-21T12:00:00+00:00:00".to_string())), // Invalid timezone format (extra seconds) - OwnedValue::build_text(Rc::new("2024-07-21T12:00:00+".to_string())), // Incomplete timezone - OwnedValue::build_text(Rc::new("2024-07-21T12:00:00+Z".to_string())), // Invalid timezone format - OwnedValue::build_text(Rc::new("2024-07-21T12:00:00+00:00Z".to_string())), // Mixing offset and Z - OwnedValue::build_text(Rc::new("2024-07-21T12:00:00UTC".to_string())), // Named timezone (not supported) + OwnedValue::build_text("2024-07-21T12:00:00+24:00"), // Invalid timezone offset (too large) + OwnedValue::build_text("2024-07-21T12:00:00-24:00"), // Invalid timezone offset (too small) + OwnedValue::build_text("2024-07-21T12:00:00+00:60"), // Invalid timezone minutes + OwnedValue::build_text("2024-07-21T12:00:00+00:00:00"), // Invalid timezone format (extra seconds) + OwnedValue::build_text("2024-07-21T12:00:00+"), // Incomplete timezone + OwnedValue::build_text("2024-07-21T12:00:00+Z"), // Invalid timezone format + OwnedValue::build_text("2024-07-21T12:00:00+00:00Z"), // Mixing offset and Z + OwnedValue::build_text("2024-07-21T12:00:00UTC"), // Named timezone (not supported) ]; for case in invalid_cases.iter() { @@ -935,163 +866,94 @@ mod tests { let test_cases = vec![ // Format 1: YYYY-MM-DD (no timezone applicable) - ( - OwnedValue::build_text(Rc::new("2024-07-21".to_string())), - "00:00:00", - ), + (OwnedValue::build_text("2024-07-21"), "00:00:00"), // Format 2: YYYY-MM-DD HH:MM - ( - OwnedValue::build_text(Rc::new("2024-07-21 22:30".to_string())), - "22:30:00", - ), - ( - OwnedValue::build_text(Rc::new("2024-07-21 22:30+02:00".to_string())), - "20:30:00", - ), - ( - OwnedValue::build_text(Rc::new("2024-07-21 22:30-05:00".to_string())), - "03:30:00", - ), - ( - OwnedValue::build_text(Rc::new("2024-07-21 22:30Z".to_string())), - "22:30:00", - ), + (OwnedValue::build_text("2024-07-21 22:30"), "22:30:00"), + (OwnedValue::build_text("2024-07-21 22:30+02:00"), "20:30:00"), + (OwnedValue::build_text("2024-07-21 22:30-05:00"), "03:30:00"), + (OwnedValue::build_text("2024-07-21 22:30Z"), "22:30:00"), // Format 3: YYYY-MM-DD HH:MM:SS + (OwnedValue::build_text("2024-07-21 22:30:45"), test_time_str), ( - OwnedValue::build_text(Rc::new("2024-07-21 22:30:45".to_string())), - test_time_str, - ), - ( - OwnedValue::build_text(Rc::new("2024-07-21 22:30:45+02:00".to_string())), + OwnedValue::build_text("2024-07-21 22:30:45+02:00"), prev_time_str, ), ( - OwnedValue::build_text(Rc::new("2024-07-21 22:30:45-05:00".to_string())), + OwnedValue::build_text("2024-07-21 22:30:45-05:00"), next_time_str, ), ( - OwnedValue::build_text(Rc::new("2024-07-21 22:30:45Z".to_string())), + OwnedValue::build_text("2024-07-21 22:30:45Z"), test_time_str, ), // Format 4: YYYY-MM-DD HH:MM:SS.SSS ( - OwnedValue::build_text(Rc::new("2024-07-21 22:30:45.123".to_string())), + OwnedValue::build_text("2024-07-21 22:30:45.123"), test_time_str, ), ( - OwnedValue::build_text(Rc::new("2024-07-21 22:30:45.123+02:00".to_string())), + OwnedValue::build_text("2024-07-21 22:30:45.123+02:00"), prev_time_str, ), ( - OwnedValue::build_text(Rc::new("2024-07-21 22:30:45.123-05:00".to_string())), + OwnedValue::build_text("2024-07-21 22:30:45.123-05:00"), next_time_str, ), ( - OwnedValue::build_text(Rc::new("2024-07-21 22:30:45.123Z".to_string())), + OwnedValue::build_text("2024-07-21 22:30:45.123Z"), test_time_str, ), // Format 5: YYYY-MM-DDTHH:MM - ( - OwnedValue::build_text(Rc::new("2024-07-21T22:30".to_string())), - "22:30:00", - ), - ( - OwnedValue::build_text(Rc::new("2024-07-21T22:30+02:00".to_string())), - "20:30:00", - ), - ( - OwnedValue::build_text(Rc::new("2024-07-21T22:30-05:00".to_string())), - "03:30:00", - ), - ( - OwnedValue::build_text(Rc::new("2024-07-21T22:30Z".to_string())), - "22:30:00", - ), + (OwnedValue::build_text("2024-07-21T22:30"), "22:30:00"), + (OwnedValue::build_text("2024-07-21T22:30+02:00"), "20:30:00"), + (OwnedValue::build_text("2024-07-21T22:30-05:00"), "03:30:00"), + (OwnedValue::build_text("2024-07-21T22:30Z"), "22:30:00"), // Format 6: YYYY-MM-DDTHH:MM:SS + (OwnedValue::build_text("2024-07-21T22:30:45"), test_time_str), ( - OwnedValue::build_text(Rc::new("2024-07-21T22:30:45".to_string())), - test_time_str, - ), - ( - OwnedValue::build_text(Rc::new("2024-07-21T22:30:45+02:00".to_string())), + OwnedValue::build_text("2024-07-21T22:30:45+02:00"), prev_time_str, ), ( - OwnedValue::build_text(Rc::new("2024-07-21T22:30:45-05:00".to_string())), + OwnedValue::build_text("2024-07-21T22:30:45-05:00"), next_time_str, ), ( - OwnedValue::build_text(Rc::new("2024-07-21T22:30:45Z".to_string())), + OwnedValue::build_text("2024-07-21T22:30:45Z"), test_time_str, ), // Format 7: YYYY-MM-DDTHH:MM:SS.SSS ( - OwnedValue::build_text(Rc::new("2024-07-21T22:30:45.123".to_string())), + OwnedValue::build_text("2024-07-21T22:30:45.123"), test_time_str, ), ( - OwnedValue::build_text(Rc::new("2024-07-21T22:30:45.123+02:00".to_string())), + OwnedValue::build_text("2024-07-21T22:30:45.123+02:00"), prev_time_str, ), ( - OwnedValue::build_text(Rc::new("2024-07-21T22:30:45.123-05:00".to_string())), + OwnedValue::build_text("2024-07-21T22:30:45.123-05:00"), next_time_str, ), ( - OwnedValue::build_text(Rc::new("2024-07-21T22:30:45.123Z".to_string())), + OwnedValue::build_text("2024-07-21T22:30:45.123Z"), test_time_str, ), // Format 8: HH:MM - ( - OwnedValue::build_text(Rc::new("22:30".to_string())), - "22:30:00", - ), - ( - OwnedValue::build_text(Rc::new("22:30+02:00".to_string())), - "20:30:00", - ), - ( - OwnedValue::build_text(Rc::new("22:30-05:00".to_string())), - "03:30:00", - ), - ( - OwnedValue::build_text(Rc::new("22:30Z".to_string())), - "22:30:00", - ), + (OwnedValue::build_text("22:30"), "22:30:00"), + (OwnedValue::build_text("22:30+02:00"), "20:30:00"), + (OwnedValue::build_text("22:30-05:00"), "03:30:00"), + (OwnedValue::build_text("22:30Z"), "22:30:00"), // Format 9: HH:MM:SS - ( - OwnedValue::build_text(Rc::new("22:30:45".to_string())), - test_time_str, - ), - ( - OwnedValue::build_text(Rc::new("22:30:45+02:00".to_string())), - prev_time_str, - ), - ( - OwnedValue::build_text(Rc::new("22:30:45-05:00".to_string())), - next_time_str, - ), - ( - OwnedValue::build_text(Rc::new("22:30:45Z".to_string())), - test_time_str, - ), + (OwnedValue::build_text("22:30:45"), test_time_str), + (OwnedValue::build_text("22:30:45+02:00"), prev_time_str), + (OwnedValue::build_text("22:30:45-05:00"), next_time_str), + (OwnedValue::build_text("22:30:45Z"), test_time_str), // Format 10: HH:MM:SS.SSS - ( - OwnedValue::build_text(Rc::new("22:30:45.123".to_string())), - test_time_str, - ), - ( - OwnedValue::build_text(Rc::new("22:30:45.123+02:00".to_string())), - prev_time_str, - ), - ( - OwnedValue::build_text(Rc::new("22:30:45.123-05:00".to_string())), - next_time_str, - ), - ( - OwnedValue::build_text(Rc::new("22:30:45.123Z".to_string())), - test_time_str, - ), + (OwnedValue::build_text("22:30:45.123"), test_time_str), + (OwnedValue::build_text("22:30:45.123+02:00"), prev_time_str), + (OwnedValue::build_text("22:30:45.123-05:00"), next_time_str), + (OwnedValue::build_text("22:30:45.123Z"), test_time_str), // Format 12: DDDDDDDDDD (Julian date as float or integer) (OwnedValue::Float(2460082.1), "14:24:00"), (OwnedValue::Integer(2460082), "12:00:00"), @@ -1110,31 +972,31 @@ mod tests { #[test] fn test_invalid_get_time_from_datetime_value() { let invalid_cases = vec![ - OwnedValue::build_text(Rc::new("2024-07-21 25:00".to_string())), // Invalid hour - OwnedValue::build_text(Rc::new("2024-07-21 24:00:00".to_string())), // Invalid hour - OwnedValue::build_text(Rc::new("2024-07-21 23:60:00".to_string())), // Invalid minute - OwnedValue::build_text(Rc::new("2024-07-21 22:58:60".to_string())), // Invalid second - OwnedValue::build_text(Rc::new("2024-07-32".to_string())), // Invalid day - OwnedValue::build_text(Rc::new("2024-13-01".to_string())), // Invalid month - OwnedValue::build_text(Rc::new("invalid_date".to_string())), // Completely invalid string - OwnedValue::build_text(Rc::new("".to_string())), // Empty string - OwnedValue::Integer(i64::MAX), // Large Julian day - OwnedValue::Integer(-1), // Negative Julian day - OwnedValue::Float(f64::MAX), // Large float - OwnedValue::Float(-1.0), // Negative Julian day as float - OwnedValue::Float(f64::NAN), // NaN - OwnedValue::Float(f64::INFINITY), // Infinity - OwnedValue::Null, // Null value - OwnedValue::Blob(vec![1, 2, 3].into()), // Blob (unsupported type) + OwnedValue::build_text("2024-07-21 25:00"), // Invalid hour + OwnedValue::build_text("2024-07-21 24:00:00"), // Invalid hour + OwnedValue::build_text("2024-07-21 23:60:00"), // Invalid minute + OwnedValue::build_text("2024-07-21 22:58:60"), // Invalid second + OwnedValue::build_text("2024-07-32"), // Invalid day + OwnedValue::build_text("2024-13-01"), // Invalid month + OwnedValue::build_text("invalid_date"), // Completely invalid string + OwnedValue::build_text(""), // Empty string + OwnedValue::Integer(i64::MAX), // Large Julian day + OwnedValue::Integer(-1), // Negative Julian day + OwnedValue::Float(f64::MAX), // Large float + OwnedValue::Float(-1.0), // Negative Julian day as float + OwnedValue::Float(f64::NAN), // NaN + OwnedValue::Float(f64::INFINITY), // Infinity + OwnedValue::Null, // Null value + OwnedValue::Blob(vec![1, 2, 3].into()), // Blob (unsupported type) // Invalid timezone tests - OwnedValue::build_text(Rc::new("2024-07-21T12:00:00+24:00".to_string())), // Invalid timezone offset (too large) - OwnedValue::build_text(Rc::new("2024-07-21T12:00:00-24:00".to_string())), // Invalid timezone offset (too small) - OwnedValue::build_text(Rc::new("2024-07-21T12:00:00+00:60".to_string())), // Invalid timezone minutes - OwnedValue::build_text(Rc::new("2024-07-21T12:00:00+00:00:00".to_string())), // Invalid timezone format (extra seconds) - OwnedValue::build_text(Rc::new("2024-07-21T12:00:00+".to_string())), // Incomplete timezone - OwnedValue::build_text(Rc::new("2024-07-21T12:00:00+Z".to_string())), // Invalid timezone format - OwnedValue::build_text(Rc::new("2024-07-21T12:00:00+00:00Z".to_string())), // Mixing offset and Z - OwnedValue::build_text(Rc::new("2024-07-21T12:00:00UTC".to_string())), // Named timezone (not supported) + OwnedValue::build_text("2024-07-21T12:00:00+24:00"), // Invalid timezone offset (too large) + OwnedValue::build_text("2024-07-21T12:00:00-24:00"), // Invalid timezone offset (too small) + OwnedValue::build_text("2024-07-21T12:00:00+00:60"), // Invalid timezone minutes + OwnedValue::build_text("2024-07-21T12:00:00+00:00:00"), // Invalid timezone format (extra seconds) + OwnedValue::build_text("2024-07-21T12:00:00+"), // Incomplete timezone + OwnedValue::build_text("2024-07-21T12:00:00+Z"), // Invalid timezone format + OwnedValue::build_text("2024-07-21T12:00:00+00:00Z"), // Mixing offset and Z + OwnedValue::build_text("2024-07-21T12:00:00UTC"), // Named timezone (not supported) ]; for case in invalid_cases { @@ -1441,7 +1303,7 @@ mod tests { } fn text(value: &str) -> OwnedValue { - OwnedValue::build_text(Rc::new(value.to_string())) + OwnedValue::build_text(value) } fn format(dt: NaiveDateTime) -> String { diff --git a/core/functions/printf.rs b/core/functions/printf.rs index 73e4bf4f3..7932aa391 100644 --- a/core/functions/printf.rs +++ b/core/functions/printf.rs @@ -1,5 +1,3 @@ -use std::rc::Rc; - use crate::types::OwnedValue; use crate::LimboError; @@ -73,16 +71,15 @@ pub fn exec_printf(values: &[OwnedValue]) -> crate::Result { } } } - Ok(OwnedValue::build_text(Rc::new(result))) + Ok(OwnedValue::build_text(&result)) } #[cfg(test)] mod tests { use super::*; - use std::rc::Rc; fn text(value: &str) -> OwnedValue { - OwnedValue::build_text(Rc::new(value.to_string())) + OwnedValue::build_text(value) } fn integer(value: i64) -> OwnedValue { diff --git a/core/json/json_operations.rs b/core/json/json_operations.rs index a52b45760..e0e2ff09a 100644 --- a/core/json/json_operations.rs +++ b/core/json/json_operations.rs @@ -197,7 +197,7 @@ mod tests { } fn create_json(s: &str) -> OwnedValue { - OwnedValue::Text(Text::json(Rc::new(s.to_string()))) + OwnedValue::Text(Text::json(s)) } #[test] diff --git a/core/json/mod.rs b/core/json/mod.rs index bca1eb5de..c1a195b49 100644 --- a/core/json/mod.rs +++ b/core/json/mod.rs @@ -4,8 +4,6 @@ mod json_operations; mod json_path; mod ser; -use std::rc::Rc; - pub use crate::json::de::from_str; use crate::json::de::ordered_object; use crate::json::error::Error as JsonError; @@ -47,13 +45,13 @@ pub fn get_json(json_value: &OwnedValue, indent: Option<&str>) -> crate::Result< None => to_string(&json_val)?, }; - Ok(OwnedValue::Text(Text::json(Rc::new(json)))) + Ok(OwnedValue::Text(Text::json(&json))) } OwnedValue::Blob(b) => { // TODO: use get_json_value after we implement a single Struct // to represent both JSON and JSONB if let Ok(json) = jsonb::from_slice(b) { - Ok(OwnedValue::Text(Text::json(Rc::new(json.to_string())))) + Ok(OwnedValue::Text(Text::json(&json.to_string()))) } else { crate::bail_parse_error!("malformed JSON"); } @@ -66,7 +64,7 @@ pub fn get_json(json_value: &OwnedValue, indent: Option<&str>) -> crate::Result< None => to_string(&json_val)?, }; - Ok(OwnedValue::Text(Text::json(Rc::new(json)))) + Ok(OwnedValue::Text(Text::json(&json))) } } } @@ -130,7 +128,7 @@ pub fn json_array(values: &[OwnedValue]) -> crate::Result { } s.push(']'); - Ok(OwnedValue::Text(Text::json(Rc::new(s)))) + Ok(OwnedValue::Text(Text::json(&s))) } pub fn json_array_length( @@ -209,7 +207,7 @@ pub fn json_arrow_extract(value: &OwnedValue, path: &OwnedValue) -> crate::Resul if let Some(val) = extracted { let json = to_string(val)?; - Ok(OwnedValue::Text(Text::json(Rc::new(json)))) + Ok(OwnedValue::Text(Text::json(&json))) } else { Ok(OwnedValue::Null) } @@ -273,7 +271,7 @@ pub fn json_extract(value: &OwnedValue, paths: &[OwnedValue]) -> crate::Result crate::Result { let json = to_string(&extracted)?; if all_as_db { - Ok(OwnedValue::Text(Text::new(Rc::new(json)))) + Ok(OwnedValue::build_text(&json)) } else { - Ok(OwnedValue::Text(Text::json(Rc::new(json)))) + Ok(OwnedValue::Text(Text::json(&json))) } } } @@ -368,7 +366,7 @@ pub fn json_type(value: &OwnedValue, path: Option<&OwnedValue>) -> crate::Result Val::Removed => unreachable!(), }; - Ok(OwnedValue::Text(Text::json(Rc::new(val.to_string())))) + Ok(OwnedValue::Text(Text::json(val))) } /// Returns the value at the given JSON path. If the path does not exist, it returns None. @@ -656,7 +654,7 @@ pub fn json_object(values: &[OwnedValue]) -> crate::Result { .collect::, _>>()?; let result = crate::json::to_string(&value_map)?; - Ok(OwnedValue::Text(Text::json(Rc::new(result)))) + Ok(OwnedValue::Text(Text::json(&result))) } pub fn is_json_valid(json_value: &OwnedValue) -> crate::Result { @@ -698,13 +696,13 @@ pub fn json_quote(value: &OwnedValue) -> crate::Result { } escaped_value.push('"'); - Ok(OwnedValue::Text(Text::new(Rc::new(escaped_value)))) + Ok(OwnedValue::build_text(&escaped_value)) } // Numbers are unquoted in json OwnedValue::Integer(ref int) => Ok(OwnedValue::Integer(int.to_owned())), OwnedValue::Float(ref float) => Ok(OwnedValue::Float(float.to_owned())), OwnedValue::Blob(_) => crate::bail_constraint_error!("JSON cannot hold BLOB values"), - OwnedValue::Null => Ok(OwnedValue::Text(Text::new(Rc::new("null".to_string())))), + OwnedValue::Null => Ok(OwnedValue::build_text("null")), _ => { unreachable!() } @@ -713,12 +711,14 @@ pub fn json_quote(value: &OwnedValue) -> crate::Result { #[cfg(test)] mod tests { + use std::rc::Rc; + use super::*; use crate::types::OwnedValue; #[test] fn test_get_json_valid_json5() { - let input = OwnedValue::build_text(Rc::new("{ key: 'value' }".to_string())); + let input = OwnedValue::build_text("{ key: 'value' }"); let result = get_json(&input, None).unwrap(); if let OwnedValue::Text(result_str) = result { assert!(result_str.as_str().contains("\"key\":\"value\"")); @@ -730,7 +730,7 @@ mod tests { #[test] fn test_get_json_valid_json5_double_single_quotes() { - let input = OwnedValue::build_text(Rc::new("{ key: ''value'' }".to_string())); + let input = OwnedValue::build_text("{ key: ''value'' }"); let result = get_json(&input, None).unwrap(); if let OwnedValue::Text(result_str) = result { assert!(result_str.as_str().contains("\"key\":\"value\"")); @@ -742,7 +742,7 @@ mod tests { #[test] fn test_get_json_valid_json5_infinity() { - let input = OwnedValue::build_text(Rc::new("{ \"key\": Infinity }".to_string())); + let input = OwnedValue::build_text("{ \"key\": Infinity }"); let result = get_json(&input, None).unwrap(); if let OwnedValue::Text(result_str) = result { assert!(result_str.as_str().contains("{\"key\":9e999}")); @@ -754,7 +754,7 @@ mod tests { #[test] fn test_get_json_valid_json5_negative_infinity() { - let input = OwnedValue::build_text(Rc::new("{ \"key\": -Infinity }".to_string())); + let input = OwnedValue::build_text("{ \"key\": -Infinity }"); let result = get_json(&input, None).unwrap(); if let OwnedValue::Text(result_str) = result { assert!(result_str.as_str().contains("{\"key\":-9e999}")); @@ -766,7 +766,7 @@ mod tests { #[test] fn test_get_json_valid_json5_nan() { - let input = OwnedValue::build_text(Rc::new("{ \"key\": NaN }".to_string())); + let input = OwnedValue::build_text("{ \"key\": NaN }"); let result = get_json(&input, None).unwrap(); if let OwnedValue::Text(result_str) = result { assert!(result_str.as_str().contains("{\"key\":null}")); @@ -778,7 +778,7 @@ mod tests { #[test] fn test_get_json_invalid_json5() { - let input = OwnedValue::build_text(Rc::new("{ key: value }".to_string())); + let input = OwnedValue::build_text("{ key: value }"); let result = get_json(&input, None); match result { Ok(_) => panic!("Expected error for malformed JSON"), @@ -788,7 +788,7 @@ mod tests { #[test] fn test_get_json_valid_jsonb() { - let input = OwnedValue::build_text(Rc::new("{\"key\":\"value\"}".to_string())); + let input = OwnedValue::build_text("{\"key\":\"value\"}"); let result = get_json(&input, None).unwrap(); if let OwnedValue::Text(result_str) = result { assert!(result_str.as_str().contains("\"key\":\"value\"")); @@ -800,7 +800,7 @@ mod tests { #[test] fn test_get_json_invalid_jsonb() { - let input = OwnedValue::build_text(Rc::new("{key:\"value\"".to_string())); + let input = OwnedValue::build_text("{key:\"value\""); let result = get_json(&input, None); match result { Ok(_) => panic!("Expected error for malformed JSON"), @@ -845,8 +845,8 @@ mod tests { #[test] fn test_json_array_simple() { - let text = OwnedValue::build_text(Rc::new("value1".to_string())); - let json = OwnedValue::Text(Text::json(Rc::new("\"value2\"".to_string()))); + let text = OwnedValue::build_text("value1"); + let json = OwnedValue::Text(Text::json("\"value2\"")); let input = vec![text, json, OwnedValue::Integer(1), OwnedValue::Float(1.1)]; let result = json_array(&input).unwrap(); @@ -887,7 +887,7 @@ mod tests { #[test] fn test_json_array_length() { - let input = OwnedValue::build_text(Rc::new("[1,2,3,4]".to_string())); + let input = OwnedValue::build_text("[1,2,3,4]"); let result = json_array_length(&input, None).unwrap(); if let OwnedValue::Integer(res) = result { assert_eq!(res, 4); @@ -898,7 +898,7 @@ mod tests { #[test] fn test_json_array_length_empty() { - let input = OwnedValue::build_text(Rc::new("[]".to_string())); + let input = OwnedValue::build_text("[]"); let result = json_array_length(&input, None).unwrap(); if let OwnedValue::Integer(res) = result { assert_eq!(res, 0); @@ -909,12 +909,8 @@ mod tests { #[test] fn test_json_array_length_root() { - let input = OwnedValue::build_text(Rc::new("[1,2,3,4]".to_string())); - let result = json_array_length( - &input, - Some(&OwnedValue::build_text(Rc::new("$".to_string()))), - ) - .unwrap(); + let input = OwnedValue::build_text("[1,2,3,4]"); + let result = json_array_length(&input, Some(&OwnedValue::build_text("$"))).unwrap(); if let OwnedValue::Integer(res) = result { assert_eq!(res, 4); } else { @@ -924,7 +920,7 @@ mod tests { #[test] fn test_json_array_length_not_array() { - let input = OwnedValue::build_text(Rc::new("{one: [1,2,3,4]}".to_string())); + let input = OwnedValue::build_text("{one: [1,2,3,4]}"); let result = json_array_length(&input, None).unwrap(); if let OwnedValue::Integer(res) = result { assert_eq!(res, 0); @@ -935,12 +931,8 @@ mod tests { #[test] fn test_json_array_length_via_prop() { - let input = OwnedValue::build_text(Rc::new("{one: [1,2,3,4]}".to_string())); - let result = json_array_length( - &input, - Some(&OwnedValue::build_text(Rc::new("$.one".to_string()))), - ) - .unwrap(); + let input = OwnedValue::build_text("{one: [1,2,3,4]}"); + let result = json_array_length(&input, Some(&OwnedValue::build_text("$.one"))).unwrap(); if let OwnedValue::Integer(res) = result { assert_eq!(res, 4); } else { @@ -950,12 +942,8 @@ mod tests { #[test] fn test_json_array_length_via_index() { - let input = OwnedValue::build_text(Rc::new("[[1,2,3,4]]".to_string())); - let result = json_array_length( - &input, - Some(&OwnedValue::build_text(Rc::new("$[0]".to_string()))), - ) - .unwrap(); + let input = OwnedValue::build_text("[[1,2,3,4]]"); + let result = json_array_length(&input, Some(&OwnedValue::build_text("$[0]"))).unwrap(); if let OwnedValue::Integer(res) = result { assert_eq!(res, 4); } else { @@ -965,12 +953,8 @@ mod tests { #[test] fn test_json_array_length_via_index_not_array() { - let input = OwnedValue::build_text(Rc::new("[1,2,3,4]".to_string())); - let result = json_array_length( - &input, - Some(&OwnedValue::build_text(Rc::new("$[2]".to_string()))), - ) - .unwrap(); + let input = OwnedValue::build_text("[1,2,3,4]"); + let result = json_array_length(&input, Some(&OwnedValue::build_text("$[2]"))).unwrap(); if let OwnedValue::Integer(res) = result { assert_eq!(res, 0); } else { @@ -980,18 +964,14 @@ mod tests { #[test] fn test_json_array_length_via_index_bad_prop() { - let input = OwnedValue::build_text(Rc::new("{one: [1,2,3,4]}".to_string())); - let result = json_array_length( - &input, - Some(&OwnedValue::build_text(Rc::new("$.two".to_string()))), - ) - .unwrap(); + let input = OwnedValue::build_text("{one: [1,2,3,4]}"); + let result = json_array_length(&input, Some(&OwnedValue::build_text("$.two"))).unwrap(); assert_eq!(OwnedValue::Null, result); } #[test] fn test_json_array_length_simple_json_subtype() { - let input = OwnedValue::build_text(Rc::new("[1,2,3]".to_string())); + let input = OwnedValue::build_text("[1,2,3]"); let wrapped = get_json(&input, None).unwrap(); let result = json_array_length(&wrapped, None).unwrap(); @@ -1005,8 +985,8 @@ mod tests { #[test] fn test_json_extract_missing_path() { let result = json_extract( - &OwnedValue::build_text(Rc::new("{\"a\":2}".to_string())), - &[OwnedValue::build_text(Rc::new("$.x".to_string()))], + &OwnedValue::build_text("{\"a\":2}"), + &[OwnedValue::build_text("$.x")], ); match result { @@ -1016,10 +996,7 @@ mod tests { } #[test] fn test_json_extract_null_path() { - let result = json_extract( - &OwnedValue::build_text(Rc::new("{\"a\":2}".to_string())), - &[OwnedValue::Null], - ); + let result = json_extract(&OwnedValue::build_text("{\"a\":2}"), &[OwnedValue::Null]); match result { Ok(OwnedValue::Null) => (), @@ -1030,7 +1007,7 @@ mod tests { #[test] fn test_json_path_invalid() { let result = json_extract( - &OwnedValue::build_text(Rc::new("{\"a\":2}".to_string())), + &OwnedValue::build_text("{\"a\":2}"), &[OwnedValue::Float(1.1)], ); @@ -1042,28 +1019,28 @@ mod tests { #[test] fn test_json_error_position_no_error() { - let input = OwnedValue::build_text(Rc::new("[1,2,3]".to_string())); + let input = OwnedValue::build_text("[1,2,3]"); let result = json_error_position(&input).unwrap(); assert_eq!(result, OwnedValue::Integer(0)); } #[test] fn test_json_error_position_no_error_more() { - let input = OwnedValue::build_text(Rc::new(r#"{"a":55,"b":72 , }"#.to_string())); + let input = OwnedValue::build_text(r#"{"a":55,"b":72 , }"#); let result = json_error_position(&input).unwrap(); assert_eq!(result, OwnedValue::Integer(0)); } #[test] fn test_json_error_position_object() { - let input = OwnedValue::build_text(Rc::new(r#"{"a":55,"b":72,,}"#.to_string())); + let input = OwnedValue::build_text(r#"{"a":55,"b":72,,}"#); let result = json_error_position(&input).unwrap(); assert_eq!(result, OwnedValue::Integer(16)); } #[test] fn test_json_error_position_array() { - let input = OwnedValue::build_text(Rc::new(r#"["a",55,"b",72,,]"#.to_string())); + let input = OwnedValue::build_text(r#"["a",55,"b",72,,]"#); let result = json_error_position(&input).unwrap(); assert_eq!(result, OwnedValue::Integer(16)); } @@ -1098,8 +1075,8 @@ mod tests { #[test] fn test_json_object_simple() { - let key = OwnedValue::build_text(Rc::new("key".to_string())); - let value = OwnedValue::build_text(Rc::new("value".to_string())); + let key = OwnedValue::build_text("key"); + let value = OwnedValue::build_text("value"); let input = vec![key, value]; let result = json_object(&input).unwrap(); @@ -1111,17 +1088,15 @@ mod tests { #[test] fn test_json_object_multiple_values() { - let text_key = OwnedValue::build_text(Rc::new("text_key".to_string())); - let text_value = OwnedValue::build_text(Rc::new("text_value".to_string())); - let json_key = OwnedValue::build_text(Rc::new("json_key".to_string())); - let json_value = OwnedValue::Text(Text::json(Rc::new( - r#"{"json":"value","number":1}"#.to_string(), - ))); - let integer_key = OwnedValue::build_text(Rc::new("integer_key".to_string())); + let text_key = OwnedValue::build_text("text_key"); + let text_value = OwnedValue::build_text("text_value"); + let json_key = OwnedValue::build_text("json_key"); + let json_value = OwnedValue::Text(Text::json(r#"{"json":"value","number":1}"#)); + let integer_key = OwnedValue::build_text("integer_key"); let integer_value = OwnedValue::Integer(1); - let float_key = OwnedValue::build_text(Rc::new("float_key".to_string())); + let float_key = OwnedValue::build_text("float_key"); let float_value = OwnedValue::Float(1.1); - let null_key = OwnedValue::build_text(Rc::new("null_key".to_string())); + let null_key = OwnedValue::build_text("null_key"); let null_value = OwnedValue::Null; let input = vec![ @@ -1149,8 +1124,8 @@ mod tests { #[test] fn test_json_object_json_value_is_rendered_as_json() { - let key = OwnedValue::build_text(Rc::new("key".to_string())); - let value = OwnedValue::Text(Text::json(Rc::new(r#"{"json":"value"}"#.to_string()))); + let key = OwnedValue::build_text("key"); + let value = OwnedValue::Text(Text::json(r#"{"json":"value"}"#)); let input = vec![key, value]; let result = json_object(&input).unwrap(); @@ -1162,8 +1137,8 @@ mod tests { #[test] fn test_json_object_json_text_value_is_rendered_as_regular_text() { - let key = OwnedValue::build_text(Rc::new("key".to_string())); - let value = OwnedValue::Text(Text::new(Rc::new(r#"{"json":"value"}"#.to_string()))); + let key = OwnedValue::build_text("key"); + let value = OwnedValue::Text(Text::new(r#"{"json":"value"}"#)); let input = vec![key, value]; let result = json_object(&input).unwrap(); @@ -1175,11 +1150,11 @@ mod tests { #[test] fn test_json_object_nested() { - let key = OwnedValue::build_text(Rc::new("key".to_string())); - let value = OwnedValue::build_text(Rc::new("value".to_string())); + let key = OwnedValue::build_text("key"); + let value = OwnedValue::build_text("value"); let input = vec![key, value]; - let parent_key = OwnedValue::build_text(Rc::new("parent_key".to_string())); + let parent_key = OwnedValue::build_text("parent_key"); let parent_value = json_object(&input).unwrap(); let parent_input = vec![parent_key, parent_value]; @@ -1193,8 +1168,8 @@ mod tests { #[test] fn test_json_object_duplicated_keys() { - let key = OwnedValue::build_text(Rc::new("key".to_string())); - let value = OwnedValue::build_text(Rc::new("value".to_string())); + let key = OwnedValue::build_text("key"); + let value = OwnedValue::build_text("value"); let input = vec![key.clone(), value.clone(), key, value]; let result = json_object(&input).unwrap(); @@ -1218,7 +1193,7 @@ mod tests { #[test] fn test_json_object_non_text_key() { let key = OwnedValue::Integer(1); - let value = OwnedValue::build_text(Rc::new("value".to_string())); + let value = OwnedValue::build_text("value"); let input = vec![key, value]; match json_object(&input) { @@ -1229,8 +1204,8 @@ mod tests { #[test] fn test_json_odd_number_of_values() { - let key = OwnedValue::build_text(Rc::new("key".to_string())); - let value = OwnedValue::build_text(Rc::new("value".to_string())); + let key = OwnedValue::build_text("key"); + let value = OwnedValue::build_text("value"); let input = vec![key.clone(), value, key]; match json_object(&input) { @@ -1337,7 +1312,7 @@ mod tests { #[test] fn test_json_path_from_owned_value_root_strict() { - let path = OwnedValue::Text(Text::new(Rc::new("$".to_string()))); + let path = OwnedValue::Text(Text::new("$")); let result = json_path_from_owned_value(&path, true); assert!(result.is_ok()); @@ -1354,7 +1329,7 @@ mod tests { #[test] fn test_json_path_from_owned_value_root_non_strict() { - let path = OwnedValue::Text(Text::new(Rc::new("$".to_string()))); + let path = OwnedValue::Text(Text::new("$")); let result = json_path_from_owned_value(&path, false); assert!(result.is_ok()); @@ -1371,14 +1346,14 @@ mod tests { #[test] fn test_json_path_from_owned_value_named_strict() { - let path = OwnedValue::Text(Text::new(Rc::new("field".to_string()))); + let path = OwnedValue::Text(Text::new("field")); assert!(json_path_from_owned_value(&path, true).is_err()); } #[test] fn test_json_path_from_owned_value_named_non_strict() { - let path = OwnedValue::Text(Text::new(Rc::new("field".to_string()))); + let path = OwnedValue::Text(Text::new("field")); let result = json_path_from_owned_value(&path, false); assert!(result.is_ok()); @@ -1465,10 +1440,10 @@ mod tests { #[test] fn test_json_set_field_empty_object() { let result = json_set( - &OwnedValue::build_text(Rc::new("{}".to_string())), + &OwnedValue::build_text("{}"), &[ - OwnedValue::build_text(Rc::new("$.field".to_string())), - OwnedValue::build_text(Rc::new("value".to_string())), + OwnedValue::build_text("$.field"), + OwnedValue::build_text("value"), ], ); @@ -1476,17 +1451,17 @@ mod tests { assert_eq!( result.unwrap(), - OwnedValue::build_text(Rc::new(r#"{"field":"value"}"#.to_string())) + OwnedValue::build_text(r#"{"field":"value"}"#) ); } #[test] fn test_json_set_replace_field() { let result = json_set( - &OwnedValue::build_text(Rc::new(r#"{"field":"old_value"}"#.to_string())), + &OwnedValue::build_text(r#"{"field":"old_value"}"#), &[ - OwnedValue::build_text(Rc::new("$.field".to_string())), - OwnedValue::build_text(Rc::new("new_value".to_string())), + OwnedValue::build_text("$.field"), + OwnedValue::build_text("new_value"), ], ); @@ -1494,17 +1469,17 @@ mod tests { assert_eq!( result.unwrap(), - OwnedValue::build_text(Rc::new(r#"{"field":"new_value"}"#.to_string())) + OwnedValue::build_text(r#"{"field":"new_value"}"#) ); } #[test] fn test_json_set_set_deeply_nested_key() { let result = json_set( - &OwnedValue::build_text(Rc::new("{}".to_string())), + &OwnedValue::build_text("{}"), &[ - OwnedValue::build_text(Rc::new("$.object.doesnt.exist".to_string())), - OwnedValue::build_text(Rc::new("value".to_string())), + OwnedValue::build_text("$.object.doesnt.exist"), + OwnedValue::build_text("value"), ], ); @@ -1512,36 +1487,31 @@ mod tests { assert_eq!( result.unwrap(), - OwnedValue::build_text(Rc::new( - r#"{"object":{"doesnt":{"exist":"value"}}}"#.to_string() - )) + OwnedValue::build_text(r#"{"object":{"doesnt":{"exist":"value"}}}"#) ); } #[test] fn test_json_set_add_value_to_empty_array() { let result = json_set( - &OwnedValue::build_text(Rc::new("[]".to_string())), + &OwnedValue::build_text("[]"), &[ - OwnedValue::build_text(Rc::new("$[0]".to_string())), - OwnedValue::build_text(Rc::new("value".to_string())), + OwnedValue::build_text("$[0]"), + OwnedValue::build_text("value"), ], ); assert!(result.is_ok()); - assert_eq!( - result.unwrap(), - OwnedValue::build_text(Rc::new(r#"["value"]"#.to_string())) - ); + assert_eq!(result.unwrap(), OwnedValue::build_text(r#"["value"]"#)); } #[test] fn test_json_set_add_value_to_nonexistent_array() { let result = json_set( - &OwnedValue::build_text(Rc::new("{}".to_string())), + &OwnedValue::build_text("{}"), &[ - OwnedValue::build_text(Rc::new("$.some_array[0]".to_string())), + OwnedValue::build_text("$.some_array[0]"), OwnedValue::Integer(123), ], ); @@ -1550,104 +1520,80 @@ mod tests { assert_eq!( result.unwrap(), - OwnedValue::build_text(Rc::new(r#"{"some_array":[123]}"#.to_string())) + OwnedValue::build_text(r#"{"some_array":[123]}"#) ); } #[test] fn test_json_set_add_value_to_array() { let result = json_set( - &OwnedValue::build_text(Rc::new("[123]".to_string())), - &[ - OwnedValue::build_text(Rc::new("$[1]".to_string())), - OwnedValue::Integer(456), - ], + &OwnedValue::build_text("[123]"), + &[OwnedValue::build_text("$[1]"), OwnedValue::Integer(456)], ); assert!(result.is_ok()); - assert_eq!( - result.unwrap(), - OwnedValue::build_text(Rc::new("[123,456]".to_string())) - ); + assert_eq!(result.unwrap(), OwnedValue::build_text("[123,456]")); } #[test] fn test_json_set_add_value_to_array_out_of_bounds() { let result = json_set( - &OwnedValue::build_text(Rc::new("[123]".to_string())), - &[ - OwnedValue::build_text(Rc::new("$[200]".to_string())), - OwnedValue::Integer(456), - ], + &OwnedValue::build_text("[123]"), + &[OwnedValue::build_text("$[200]"), OwnedValue::Integer(456)], ); assert!(result.is_ok()); - assert_eq!( - result.unwrap(), - OwnedValue::build_text(Rc::new("[123]".to_string())) - ); + assert_eq!(result.unwrap(), OwnedValue::build_text("[123]")); } #[test] fn test_json_set_replace_value_in_array() { let result = json_set( - &OwnedValue::build_text(Rc::new("[123]".to_string())), - &[ - OwnedValue::build_text(Rc::new("$[0]".to_string())), - OwnedValue::Integer(456), - ], + &OwnedValue::build_text("[123]"), + &[OwnedValue::build_text("$[0]"), OwnedValue::Integer(456)], ); assert!(result.is_ok()); - assert_eq!( - result.unwrap(), - OwnedValue::build_text(Rc::new("[456]".to_string())) - ); + assert_eq!(result.unwrap(), OwnedValue::build_text("[456]")); } #[test] fn test_json_set_null_path() { let result = json_set( - &OwnedValue::build_text(Rc::new("{}".to_string())), + &OwnedValue::build_text("{}"), &[OwnedValue::Null, OwnedValue::Integer(456)], ); assert!(result.is_ok()); - assert_eq!( - result.unwrap(), - OwnedValue::build_text(Rc::new("{}".to_string())) - ); + assert_eq!(result.unwrap(), OwnedValue::build_text("{}")); } #[test] fn test_json_set_multiple_keys() { let result = json_set( - &OwnedValue::build_text(Rc::new("[123]".to_string())), + &OwnedValue::build_text("[123]"), &[ - OwnedValue::build_text(Rc::new("$[0]".to_string())), + OwnedValue::build_text("$[0]"), OwnedValue::Integer(456), - OwnedValue::build_text(Rc::new("$[1]".to_string())), + OwnedValue::build_text("$[1]"), OwnedValue::Integer(789), ], ); assert!(result.is_ok()); - assert_eq!( - result.unwrap(), - OwnedValue::build_text(Rc::new("[456,789]".to_string())) - ); + assert_eq!(result.unwrap(), OwnedValue::build_text("[456,789]")); } #[test] fn test_json_set_missing_value() { let result = json_set( - &OwnedValue::build_text(Rc::new("[123]".to_string())), - &[OwnedValue::build_text(Rc::new("$[0]".to_string()))], + &OwnedValue::build_text("[123]"), + &[OwnedValue::build_text("$[0]")], ); assert!(result.is_err()); @@ -1656,9 +1602,9 @@ mod tests { #[test] fn test_json_set_add_array_in_nested_object() { let result = json_set( - &OwnedValue::build_text(Rc::new("{}".to_string())), + &OwnedValue::build_text("{}"), &[ - OwnedValue::build_text(Rc::new("$.object[0].field".to_string())), + OwnedValue::build_text("$.object[0].field"), OwnedValue::Integer(123), ], ); @@ -1667,16 +1613,16 @@ mod tests { assert_eq!( result.unwrap(), - OwnedValue::build_text(Rc::new(r#"{"object":[{"field":123}]}"#.to_string())) + OwnedValue::build_text(r#"{"object":[{"field":123}]}"#) ); } #[test] fn test_json_set_add_array_in_array_in_nested_object() { let result = json_set( - &OwnedValue::build_text(Rc::new("{}".to_string())), + &OwnedValue::build_text("{}"), &[ - OwnedValue::build_text(Rc::new("$.object[0][0]".to_string())), + OwnedValue::build_text("$.object[0][0]"), OwnedValue::Integer(123), ], ); @@ -1685,19 +1631,19 @@ mod tests { assert_eq!( result.unwrap(), - OwnedValue::build_text(Rc::new(r#"{"object":[[123]]}"#.to_string())) + OwnedValue::build_text(r#"{"object":[[123]]}"#) ); } #[test] fn test_json_set_add_array_in_array_in_nested_object_out_of_bounds() { let result = json_set( - &OwnedValue::build_text(Rc::new("{}".to_string())), + &OwnedValue::build_text("{}"), &[ - OwnedValue::build_text(Rc::new("$.object[123].another".to_string())), - OwnedValue::build_text(Rc::new("value".to_string())), - OwnedValue::build_text(Rc::new("$.field".to_string())), - OwnedValue::build_text(Rc::new("value".to_string())), + OwnedValue::build_text("$.object[123].another"), + OwnedValue::build_text("value"), + OwnedValue::build_text("$.field"), + OwnedValue::build_text("value"), ], ); @@ -1705,7 +1651,7 @@ mod tests { assert_eq!( result.unwrap(), - OwnedValue::build_text(Rc::new(r#"{"field":"value"}"#.to_string())) + OwnedValue::build_text(r#"{"field":"value"}"#) ); } } diff --git a/core/storage/sqlite3_ondisk.rs b/core/storage/sqlite3_ondisk.rs index 3cfdfca6f..bf2f3058a 100644 --- a/core/storage/sqlite3_ondisk.rs +++ b/core/storage/sqlite3_ondisk.rs @@ -1418,7 +1418,7 @@ mod tests { #[case(&[], SerialType::ConstInt0, OwnedValue::Integer(0))] #[case(&[], SerialType::ConstInt1, OwnedValue::Integer(1))] #[case(&[1, 2, 3], SerialType::Blob(3), OwnedValue::Blob(vec![1, 2, 3].into()))] - #[case(&[65, 66, 67], SerialType::String(3), OwnedValue::build_text("ABC".to_string().into()))] + #[case(&[65, 66, 67], SerialType::String(3), OwnedValue::build_text("ABC"))] fn test_read_value( #[case] buf: &[u8], #[case] serial_type: SerialType, diff --git a/core/types.rs b/core/types.rs index d72ddc284..2ef12d381 100644 --- a/core/types.rs +++ b/core/types.rs @@ -56,17 +56,17 @@ pub struct Text { impl Text { pub fn from_str>(value: S) -> Self { - Self::new(Rc::new(value.into())) + Self::new(&value.into()) } - pub fn new(value: Rc) -> Self { + pub fn new(value: &str) -> Self { Self { value: Rc::new(value.as_bytes().to_vec()), subtype: TextSubtype::Text, } } - pub fn json(value: Rc) -> Self { + pub fn json(value: &str) -> Self { Self { value: Rc::new(value.as_bytes().to_vec()), subtype: TextSubtype::Json, @@ -91,7 +91,7 @@ pub enum OwnedValue { impl OwnedValue { // A helper function that makes building a text OwnedValue easier. - pub fn build_text(text: Rc) -> Self { + pub fn build_text(text: &str) -> Self { Self::Text(Text::new(text)) } @@ -114,7 +114,7 @@ impl OwnedValue { } pub fn from_text(text: &str) -> Self { - OwnedValue::Text(Text::new(Rc::new(text.to_string()))) + OwnedValue::Text(Text::new(text)) } pub fn value_type(&self) -> OwnedValueType { @@ -242,7 +242,7 @@ impl OwnedValue { let Some(text) = v.to_text() else { return Ok(OwnedValue::Null); }; - Ok(OwnedValue::build_text(Rc::new(text.to_string()))) + Ok(OwnedValue::build_text(text)) } ExtValueType::Blob => { let Some(blob) = v.to_blob() else { @@ -381,22 +381,22 @@ impl std::ops::Add for OwnedValue { (Self::Float(float_left), Self::Float(float_right)) => { Self::Float(float_left + float_right) } - (Self::Text(string_left), Self::Text(string_right)) => Self::build_text(Rc::new( - string_left.as_str().to_string() + &string_right.as_str(), - )), - (Self::Text(string_left), Self::Integer(int_right)) => Self::build_text(Rc::new( - string_left.as_str().to_string() + &int_right.to_string(), - )), + (Self::Text(string_left), Self::Text(string_right)) => { + Self::build_text(&(string_left.as_str().to_string() + string_right.as_str())) + } + (Self::Text(string_left), Self::Integer(int_right)) => { + Self::build_text(&(string_left.as_str().to_string() + &int_right.to_string())) + } (Self::Integer(int_left), Self::Text(string_right)) => { - Self::build_text(Rc::new(int_left.to_string() + &string_right.as_str())) + Self::build_text(&(int_left.to_string() + string_right.as_str())) } (Self::Text(string_left), Self::Float(float_right)) => { let string_right = Self::Float(float_right).to_string(); - Self::build_text(Rc::new(string_left.as_str().to_string() + &string_right)) + Self::build_text(&(string_left.as_str().to_string() + &string_right)) } (Self::Float(float_left), Self::Text(string_right)) => { let string_left = Self::Float(float_left).to_string(); - Self::build_text(Rc::new(string_left + &string_right.as_str())) + Self::build_text(&(string_left + string_right.as_str())) } (lhs, Self::Null) => lhs, (Self::Null, rhs) => rhs, @@ -865,8 +865,8 @@ mod tests { #[test] fn test_serialize_text() { - let text = Rc::new("hello".to_string()); - let record = Record::new(vec![OwnedValue::Text(Text::new(text.clone()))]); + let text = "hello"; + let record = Record::new(vec![OwnedValue::Text(Text::new(text))]); let mut buf = Vec::new(); record.serialize(&mut buf); @@ -903,12 +903,12 @@ mod tests { #[test] fn test_serialize_mixed_types() { - let text = Rc::new("test".to_string()); + let text = "test"; let record = Record::new(vec![ OwnedValue::Null, OwnedValue::Integer(42), OwnedValue::Float(3.15), - OwnedValue::Text(Text::new(text.clone())), + OwnedValue::Text(Text::new(text)), ]); let mut buf = Vec::new(); record.serialize(&mut buf); diff --git a/core/vdbe/explain.rs b/core/vdbe/explain.rs index 6f55d5dc1..33ebf225f 100644 --- a/core/vdbe/explain.rs +++ b/core/vdbe/explain.rs @@ -17,7 +17,7 @@ pub fn insn_to_str( 0, target_pc.to_debug_int(), 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!("Start at {}", target_pc.to_debug_int()), ), @@ -26,7 +26,7 @@ pub fn insn_to_str( *lhs as i32, *rhs as i32, *dest as i32, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!("r[{}]=r[{}]+r[{}]", dest, lhs, rhs), ), @@ -35,7 +35,7 @@ pub fn insn_to_str( *lhs as i32, *rhs as i32, *dest as i32, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!("r[{}]=r[{}]-r[{}]", dest, lhs, rhs), ), @@ -44,7 +44,7 @@ pub fn insn_to_str( *lhs as i32, *rhs as i32, *dest as i32, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!("r[{}]=r[{}]*r[{}]", dest, lhs, rhs), ), @@ -53,7 +53,7 @@ pub fn insn_to_str( *lhs as i32, *rhs as i32, *dest as i32, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!("r[{}]=r[{}]/r[{}]", dest, lhs, rhs), ), @@ -62,7 +62,7 @@ pub fn insn_to_str( *lhs as i32, *rhs as i32, *dest as i32, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!("r[{}]=r[{}]&r[{}]", dest, lhs, rhs), ), @@ -71,7 +71,7 @@ pub fn insn_to_str( *lhs as i32, *rhs as i32, *dest as i32, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!("r[{}]=r[{}]|r[{}]", dest, lhs, rhs), ), @@ -80,7 +80,7 @@ pub fn insn_to_str( *reg as i32, *dest as i32, 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!("r[{}]=~r[{}]", dest, reg), ), @@ -93,7 +93,7 @@ pub fn insn_to_str( *database as i32, *dest as i32, 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!("r[{}]=~r[{}]", dest, database), ), @@ -102,7 +102,7 @@ pub fn insn_to_str( *lhs as i32, *rhs as i32, *dest as i32, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!("r[{}]=r[{}]%r[{}]", dest, lhs, rhs), ), @@ -111,7 +111,7 @@ pub fn insn_to_str( 0, *dest as i32, dest_end.map_or(0, |end| end as i32), - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, dest_end.map_or(format!("r[{}]=NULL", dest), |end| { format!("r[{}..{}]=NULL", dest, end) @@ -122,7 +122,7 @@ pub fn insn_to_str( *cursor_id as i32, 0, 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!("Set cursor {} to a (pseudo) NULL row", cursor_id), ), @@ -131,7 +131,7 @@ pub fn insn_to_str( *reg as i32, target_pc.to_debug_int(), 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!("r[{}]!=NULL -> goto {}", reg, target_pc.to_debug_int()), ), @@ -144,7 +144,7 @@ pub fn insn_to_str( *start_reg_a as i32, *start_reg_b as i32, *count as i32, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!( "r[{}..{}]==r[{}..{}]", @@ -163,7 +163,7 @@ pub fn insn_to_str( target_pc_lt.to_debug_int(), target_pc_eq.to_debug_int(), target_pc_gt.to_debug_int(), - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, "".to_string(), ), @@ -176,7 +176,7 @@ pub fn insn_to_str( *source_reg as i32, *dest_reg as i32, *count as i32, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!( "r[{}..{}]=r[{}..{}]", @@ -195,7 +195,7 @@ pub fn insn_to_str( *reg as i32, target_pc.to_debug_int(), 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!( "r[{}]>0 -> r[{}]-={}, goto {}", @@ -215,7 +215,7 @@ pub fn insn_to_str( *lhs as i32, *rhs as i32, target_pc.to_debug_int(), - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!( "if r[{}]==r[{}] goto {}", @@ -234,7 +234,7 @@ pub fn insn_to_str( *lhs as i32, *rhs as i32, target_pc.to_debug_int(), - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!( "if r[{}]!=r[{}] goto {}", @@ -253,7 +253,7 @@ pub fn insn_to_str( *lhs as i32, *rhs as i32, target_pc.to_debug_int(), - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!("if r[{}]r[{}] goto {}", lhs, rhs, target_pc.to_debug_int()), ), @@ -300,7 +300,7 @@ pub fn insn_to_str( *lhs as i32, *rhs as i32, target_pc.to_debug_int(), - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!( "if r[{}]>=r[{}] goto {}", @@ -318,7 +318,7 @@ pub fn insn_to_str( *reg as i32, target_pc.to_debug_int(), *jump_if_null as i32, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!("if r[{}] goto {}", reg, target_pc.to_debug_int()), ), @@ -331,7 +331,7 @@ pub fn insn_to_str( *reg as i32, target_pc.to_debug_int(), *jump_if_null as i32, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!("if !r[{}] goto {}", reg, target_pc.to_debug_int()), ), @@ -343,7 +343,7 @@ pub fn insn_to_str( *cursor_id as i32, *root_page as i32, 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!( "table={}, root={}", @@ -359,7 +359,7 @@ pub fn insn_to_str( 0, 0, 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, "".to_string(), ), @@ -368,7 +368,7 @@ pub fn insn_to_str( *cursor_id as i32, 0, 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, "".to_string(), ), @@ -377,7 +377,7 @@ pub fn insn_to_str( 0, 0, 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, "".to_string(), ), @@ -391,7 +391,7 @@ pub fn insn_to_str( *cursor_id as i32, pc_if_empty.to_debug_int(), *arg_count as i32, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, "".to_string(), ), @@ -404,7 +404,7 @@ pub fn insn_to_str( *cursor_id as i32, *column as i32, *dest as i32, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, "".to_string(), ), @@ -416,7 +416,7 @@ pub fn insn_to_str( *cursor_id as i32, pc_if_next.to_debug_int(), 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, "".to_string(), ), @@ -429,7 +429,7 @@ pub fn insn_to_str( *cursor_id as i32, *content_reg as i32, *num_fields as i32, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!("{} columns in r[{}]", num_fields, content_reg), ), @@ -438,7 +438,7 @@ pub fn insn_to_str( *cursor_id as i32, 0, 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, "".to_string(), ), @@ -450,7 +450,7 @@ pub fn insn_to_str( *cursor_id as i32, pc_if_empty.to_debug_int(), 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!( "Rewind table {}", @@ -487,7 +487,7 @@ pub fn insn_to_str( *cursor_id as i32, *column as i32, *dest as i32, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!( "r[{}]={}.{}", @@ -508,7 +508,7 @@ pub fn insn_to_str( *start_reg as i32, *count as i32, *dest_reg as i32, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!( "r[{}]=mkrec(r[{}..{}])", @@ -522,7 +522,7 @@ pub fn insn_to_str( *start_reg as i32, *count as i32, 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, if *count == 1 { format!("output=r[{}]", start_reg) @@ -535,7 +535,7 @@ pub fn insn_to_str( *cursor_id as i32, 0, 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, "".to_string(), ), @@ -547,7 +547,7 @@ pub fn insn_to_str( *cursor_id as i32, pc_if_next.to_debug_int(), 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, "".to_string(), ), @@ -559,7 +559,7 @@ pub fn insn_to_str( *err_code as i32, 0, 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, "".to_string(), ), @@ -568,7 +568,7 @@ pub fn insn_to_str( 0, *write as i32, 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, "".to_string(), ), @@ -577,7 +577,7 @@ pub fn insn_to_str( 0, target_pc.to_debug_int(), 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, "".to_string(), ), @@ -589,7 +589,7 @@ pub fn insn_to_str( *return_reg as i32, target_pc.to_debug_int(), 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, "".to_string(), ), @@ -598,7 +598,7 @@ pub fn insn_to_str( *return_reg as i32, 0, 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, "".to_string(), ), @@ -607,7 +607,7 @@ pub fn insn_to_str( *value as i32, *dest as i32, 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!("r[{}]={}", dest, value), ), @@ -625,7 +625,7 @@ pub fn insn_to_str( *register as i32, 0, 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, "".to_string(), ), @@ -634,7 +634,7 @@ pub fn insn_to_str( 0, *dest as i32, 0, - OwnedValue::build_text(Rc::new(value.clone())), + OwnedValue::build_text(value), 0, format!("r[{}]='{}'", dest, value), ), @@ -657,7 +657,7 @@ pub fn insn_to_str( *cursor_id as i32, *dest as i32, 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!( "r[{}]={}.rowid", @@ -677,7 +677,7 @@ pub fn insn_to_str( *cursor_id as i32, *src_reg as i32, target_pc.to_debug_int(), - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!( "if (r[{}]!={}.rowid) goto {}", @@ -697,7 +697,7 @@ pub fn insn_to_str( *index_cursor_id as i32, *table_cursor_id as i32, 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, "".to_string(), ), @@ -712,7 +712,7 @@ pub fn insn_to_str( *cursor_id as i32, target_pc.to_debug_int(), *start_reg as i32, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, "".to_string(), ), @@ -727,7 +727,7 @@ pub fn insn_to_str( *cursor_id as i32, target_pc.to_debug_int(), *start_reg as i32, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, "".to_string(), ), @@ -741,7 +741,7 @@ pub fn insn_to_str( *cursor_id as i32, target_pc.to_debug_int(), *start_reg as i32, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, "".to_string(), ), @@ -755,7 +755,7 @@ pub fn insn_to_str( *cursor_id as i32, target_pc.to_debug_int(), *start_reg as i32, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, "".to_string(), ), @@ -764,7 +764,7 @@ pub fn insn_to_str( *reg as i32, target_pc.to_debug_int(), 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!("if (--r[{}]==0) goto {}", reg, target_pc.to_debug_int()), ), @@ -778,7 +778,7 @@ pub fn insn_to_str( 0, *col as i32, *acc_reg as i32, - OwnedValue::build_text(Rc::new(func.to_string().into())), + OwnedValue::build_text(func.to_string()), 0, format!("accum=r[{}] step(r[{}])", *acc_reg, *col), ), @@ -787,7 +787,7 @@ pub fn insn_to_str( 0, *register as i32, 0, - OwnedValue::build_text(Rc::new(func.to_string().into())), + OwnedValue::build_text(func.to_string()), 0, format!("accum=r[{}]", *register), ), @@ -816,11 +816,9 @@ pub fn insn_to_str( *cursor_id as i32, *columns as i32, 0, - OwnedValue::build_text(Rc::new(format!( - "k({},{})", - order.values.len(), - to_print.join(",") - ))), + OwnedValue::build_text( + &(format!("k({},{})", order.values.len(), to_print.join(","))), + ), 0, format!("cursor={}", cursor_id), ) @@ -834,7 +832,7 @@ pub fn insn_to_str( *cursor_id as i32, *dest_reg as i32, *pseudo_cursor as i32, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!("r[{}]=data", dest_reg), ), @@ -858,7 +856,7 @@ pub fn insn_to_str( *cursor_id as i32, pc_if_empty.to_debug_int(), 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, "".to_string(), ), @@ -870,7 +868,7 @@ pub fn insn_to_str( *cursor_id as i32, pc_if_next.to_debug_int(), 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, "".to_string(), ), @@ -884,7 +882,7 @@ pub fn insn_to_str( *constant_mask, *start_reg as i32, *dest as i32, - OwnedValue::build_text(Rc::new(func.func.to_string())), + OwnedValue::build_text(&func.func.to_string()), 0, if func.arg_count == 0 { format!("r[{}]=func()", dest) @@ -908,7 +906,7 @@ pub fn insn_to_str( *yield_reg as i32, jump_on_definition.to_debug_int(), start_offset.to_debug_int(), - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, "".to_string(), ), @@ -917,7 +915,7 @@ pub fn insn_to_str( *yield_reg as i32, 0, 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, "".to_string(), ), @@ -929,7 +927,7 @@ pub fn insn_to_str( *yield_reg as i32, end_offset.to_debug_int(), 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, "".to_string(), ), @@ -943,7 +941,7 @@ pub fn insn_to_str( *cursor as i32, *record_reg as i32, *key_reg as i32, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), *flag as u16, "".to_string(), ), @@ -952,7 +950,7 @@ pub fn insn_to_str( *cursor_id as i32, 0, 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, "".to_string(), ), @@ -961,7 +959,7 @@ pub fn insn_to_str( *cursor_id as i32, 0, 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, "".to_string(), ), @@ -970,7 +968,7 @@ pub fn insn_to_str( *cursor_id as i32, 0, 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, "".to_string(), ), @@ -983,7 +981,7 @@ pub fn insn_to_str( *cursor as i32, *rowid_reg as i32, *prev_largest_reg as i32, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, "".to_string(), ), @@ -992,7 +990,7 @@ pub fn insn_to_str( *reg as i32, 0, 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, "".to_string(), ), @@ -1001,7 +999,7 @@ pub fn insn_to_str( *reg as i32, 0, 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, "".to_string(), ), @@ -1014,7 +1012,7 @@ pub fn insn_to_str( *cursor as i32, target_pc.to_debug_int(), *rowid_reg as i32, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, "".to_string(), ), @@ -1027,7 +1025,7 @@ pub fn insn_to_str( *limit_reg as i32, *combined_reg as i32, *offset_reg as i32, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!( "if r[{}]>0 then r[{}]=r[{}]+max(0,r[{}]) else r[{}]=(-1)", @@ -1042,7 +1040,7 @@ pub fn insn_to_str( *cursor_id as i32, *root_page as i32, 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, "".to_string(), ), @@ -1051,7 +1049,7 @@ pub fn insn_to_str( 0, 0, 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, "".to_string(), ), @@ -1064,7 +1062,7 @@ pub fn insn_to_str( *src_reg as i32, *dst_reg as i32, *amount as i32, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!("r[{}]=r[{}]", dst_reg, src_reg), ), @@ -1073,7 +1071,7 @@ pub fn insn_to_str( *db as i32, *root as i32, *flags as i32, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!("r[{}]=root iDb={} flags={}", root, db, flags), ), @@ -1082,7 +1080,7 @@ pub fn insn_to_str( *cursor_id as i32, 0, 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, "".to_string(), ), @@ -1091,7 +1089,7 @@ pub fn insn_to_str( 0, 0, 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, "".to_string(), ), @@ -1100,7 +1098,7 @@ pub fn insn_to_str( *reg as i32, target_pc.to_debug_int(), 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!("if (r[{}]==NULL) goto {}", reg, target_pc.to_debug_int()), ), @@ -1109,7 +1107,7 @@ pub fn insn_to_str( *db as i32, 0, 0, - OwnedValue::build_text(Rc::new(where_clause.clone())), + OwnedValue::build_text(where_clause), 0, where_clause.clone(), ), @@ -1118,7 +1116,7 @@ pub fn insn_to_str( 0, 0, 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, "".to_string(), ), @@ -1127,7 +1125,7 @@ pub fn insn_to_str( 0, 0, 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, "".to_string(), ), @@ -1136,7 +1134,7 @@ pub fn insn_to_str( 0, 0, 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, "".to_string(), ), @@ -1145,7 +1143,7 @@ pub fn insn_to_str( *rhs as i32, *lhs as i32, *dest as i32, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!("r[{}]=r[{}] >> r[{}]", dest, lhs, rhs), ), @@ -1154,7 +1152,7 @@ pub fn insn_to_str( *rhs as i32, *lhs as i32, *dest as i32, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!("r[{}]=r[{}] << r[{}]", dest, lhs, rhs), ), @@ -1163,7 +1161,7 @@ pub fn insn_to_str( usize::from(*index) as i32, *dest as i32, 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!("r[{}]=parameter({})", *dest, *index), ), @@ -1172,7 +1170,7 @@ pub fn insn_to_str( *rg1 as i32, *dest as i32, *rg2 as i32, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!( "((r[{}]=NULL)|(r[{}]=NULL)) ? r[{}]=NULL : r[{}]=0", @@ -1184,7 +1182,7 @@ pub fn insn_to_str( *reg as i32, *dest as i32, 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!("r[{}]=!r[{}]", dest, reg), ), @@ -1193,7 +1191,7 @@ pub fn insn_to_str( *rhs as i32, *lhs as i32, *dest as i32, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!("r[{}]=r[{}] + r[{}]", dest, lhs, rhs), ), @@ -1202,7 +1200,7 @@ pub fn insn_to_str( *rhs as i32, *lhs as i32, *dest as i32, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!("r[{}]=(r[{}] && r[{}])", dest, lhs, rhs), ), @@ -1211,7 +1209,7 @@ pub fn insn_to_str( *rhs as i32, *lhs as i32, *dest as i32, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, format!("r[{}]=(r[{}] || r[{}])", dest, lhs, rhs), ), @@ -1220,7 +1218,7 @@ pub fn insn_to_str( 0, 0, 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, String::new(), ), @@ -1229,7 +1227,7 @@ pub fn insn_to_str( *db as i32, *dest as i32, 0, - OwnedValue::build_text(Rc::new("".to_string())), + OwnedValue::build_text(""), 0, "".to_string(), ), diff --git a/core/vdbe/insn.rs b/core/vdbe/insn.rs index 2d673b018..751356ced 100644 --- a/core/vdbe/insn.rs +++ b/core/vdbe/insn.rs @@ -1,5 +1,4 @@ use std::num::NonZero; -use std::rc::Rc; use super::{AggFunc, BranchOffset, CursorID, FuncCtx, PageIdx}; use crate::storage::wal::CheckpointMode; @@ -985,56 +984,56 @@ pub fn exec_boolean_not(mut reg: &OwnedValue) -> OwnedValue { pub fn exec_concat(lhs: &OwnedValue, rhs: &OwnedValue) -> OwnedValue { match (lhs, rhs) { (OwnedValue::Text(lhs_text), OwnedValue::Text(rhs_text)) => { - OwnedValue::build_text(Rc::new(lhs_text.as_str().to_string() + &rhs_text.as_str())) + OwnedValue::build_text(&(lhs_text.as_str().to_string() + rhs_text.as_str())) } - (OwnedValue::Text(lhs_text), OwnedValue::Integer(rhs_int)) => OwnedValue::build_text( - Rc::new(lhs_text.as_str().to_string() + &rhs_int.to_string()), + (OwnedValue::Text(lhs_text), OwnedValue::Integer(rhs_int)) => { + OwnedValue::build_text(&(lhs_text.as_str().to_string() + &rhs_int.to_string())) + } + (OwnedValue::Text(lhs_text), OwnedValue::Float(rhs_float)) => { + OwnedValue::build_text(&(lhs_text.as_str().to_string() + &rhs_float.to_string())) + } + (OwnedValue::Text(lhs_text), OwnedValue::Agg(rhs_agg)) => OwnedValue::build_text( + (lhs_text.as_str().to_string() + &rhs_agg.final_value().to_string()).as_str(), ), - (OwnedValue::Text(lhs_text), OwnedValue::Float(rhs_float)) => OwnedValue::build_text( - Rc::new(lhs_text.as_str().to_string() + &rhs_float.to_string()), - ), - (OwnedValue::Text(lhs_text), OwnedValue::Agg(rhs_agg)) => OwnedValue::build_text(Rc::new( - lhs_text.as_str().to_string() + &rhs_agg.final_value().to_string(), - )), (OwnedValue::Integer(lhs_int), OwnedValue::Text(rhs_text)) => { - OwnedValue::build_text(Rc::new(lhs_int.to_string() + rhs_text.as_str())) + OwnedValue::build_text(&(lhs_int.to_string() + rhs_text.as_str())) } (OwnedValue::Integer(lhs_int), OwnedValue::Integer(rhs_int)) => { - OwnedValue::build_text(Rc::new(lhs_int.to_string() + &rhs_int.to_string())) + OwnedValue::build_text(&(lhs_int.to_string() + &rhs_int.to_string())) } (OwnedValue::Integer(lhs_int), OwnedValue::Float(rhs_float)) => { - OwnedValue::build_text(Rc::new(lhs_int.to_string() + &rhs_float.to_string())) + OwnedValue::build_text(&(lhs_int.to_string() + &rhs_float.to_string())) + } + (OwnedValue::Integer(lhs_int), OwnedValue::Agg(rhs_agg)) => { + OwnedValue::build_text(&(lhs_int.to_string() + &rhs_agg.final_value().to_string())) } - (OwnedValue::Integer(lhs_int), OwnedValue::Agg(rhs_agg)) => OwnedValue::build_text( - Rc::new(lhs_int.to_string() + &rhs_agg.final_value().to_string()), - ), (OwnedValue::Float(lhs_float), OwnedValue::Text(rhs_text)) => { - OwnedValue::build_text(Rc::new(lhs_float.to_string() + rhs_text.as_str())) + OwnedValue::build_text(&(lhs_float.to_string() + rhs_text.as_str())) } (OwnedValue::Float(lhs_float), OwnedValue::Integer(rhs_int)) => { - OwnedValue::build_text(Rc::new(lhs_float.to_string() + &rhs_int.to_string())) + OwnedValue::build_text(&(lhs_float.to_string() + &rhs_int.to_string())) } (OwnedValue::Float(lhs_float), OwnedValue::Float(rhs_float)) => { - OwnedValue::build_text(Rc::new(lhs_float.to_string() + &rhs_float.to_string())) + OwnedValue::build_text(&(lhs_float.to_string() + &rhs_float.to_string())) + } + (OwnedValue::Float(lhs_float), OwnedValue::Agg(rhs_agg)) => { + OwnedValue::build_text(&(lhs_float.to_string() + &rhs_agg.final_value().to_string())) } - (OwnedValue::Float(lhs_float), OwnedValue::Agg(rhs_agg)) => OwnedValue::build_text( - Rc::new(lhs_float.to_string() + &rhs_agg.final_value().to_string()), - ), - (OwnedValue::Agg(lhs_agg), OwnedValue::Text(rhs_text)) => OwnedValue::build_text(Rc::new( - lhs_agg.final_value().to_string() + rhs_text.as_str(), - )), - (OwnedValue::Agg(lhs_agg), OwnedValue::Integer(rhs_int)) => OwnedValue::build_text( - Rc::new(lhs_agg.final_value().to_string() + &rhs_int.to_string()), + (OwnedValue::Agg(lhs_agg), OwnedValue::Text(rhs_text)) => { + OwnedValue::build_text(&(lhs_agg.final_value().to_string() + rhs_text.as_str())) + } + (OwnedValue::Agg(lhs_agg), OwnedValue::Integer(rhs_int)) => { + OwnedValue::build_text(&(lhs_agg.final_value().to_string() + &rhs_int.to_string())) + } + (OwnedValue::Agg(lhs_agg), OwnedValue::Float(rhs_float)) => { + OwnedValue::build_text(&(lhs_agg.final_value().to_string() + &rhs_float.to_string())) + } + (OwnedValue::Agg(lhs_agg), OwnedValue::Agg(rhs_agg)) => OwnedValue::build_text( + &(lhs_agg.final_value().to_string() + &rhs_agg.final_value().to_string()), ), - (OwnedValue::Agg(lhs_agg), OwnedValue::Float(rhs_float)) => OwnedValue::build_text( - Rc::new(lhs_agg.final_value().to_string() + &rhs_float.to_string()), - ), - (OwnedValue::Agg(lhs_agg), OwnedValue::Agg(rhs_agg)) => OwnedValue::build_text(Rc::new( - lhs_agg.final_value().to_string() + &rhs_agg.final_value().to_string(), - )), (OwnedValue::Null, _) | (_, OwnedValue::Null) => OwnedValue::Null, (OwnedValue::Blob(_), _) | (_, OwnedValue::Blob(_)) => { diff --git a/core/vdbe/mod.rs b/core/vdbe/mod.rs index a49d11dc9..a0a9c477b 100644 --- a/core/vdbe/mod.rs +++ b/core/vdbe/mod.rs @@ -1234,7 +1234,7 @@ impl Program { state.pc += 1; } Insn::String8 { value, dest } => { - state.registers[*dest] = OwnedValue::build_text(Rc::new(value.into())); + state.registers[*dest] = OwnedValue::build_text(value); state.pc += 1; } Insn::Blob { value, dest } => { @@ -1511,11 +1511,9 @@ impl Program { } } } - AggFunc::GroupConcat | AggFunc::StringAgg => { - OwnedValue::Agg(Box::new(AggContext::GroupConcat( - OwnedValue::build_text(Rc::new("".to_string())), - ))) - } + AggFunc::GroupConcat | AggFunc::StringAgg => OwnedValue::Agg(Box::new( + AggContext::GroupConcat(OwnedValue::build_text("")), + )), AggFunc::External(func) => match func.as_ref() { ExtFunc::Aggregate { init, @@ -2234,18 +2232,15 @@ impl Program { } ScalarFunc::JulianDay => { if *start_reg == 0 { - let julianday: String = exec_julianday( - &OwnedValue::build_text(Rc::new("now".to_string())), - )?; - state.registers[*dest] = - OwnedValue::build_text(Rc::new(julianday)); + let julianday: String = + exec_julianday(&OwnedValue::build_text("now"))?; + state.registers[*dest] = OwnedValue::build_text(&julianday); } else { let datetime_value = &state.registers[*start_reg]; let julianday = exec_julianday(datetime_value); match julianday { Ok(time) => { - state.registers[*dest] = - OwnedValue::build_text(Rc::new(time)) + state.registers[*dest] = OwnedValue::build_text(&time) } Err(e) => { return Err(LimboError::ParseError(format!( @@ -2258,18 +2253,15 @@ impl Program { } ScalarFunc::UnixEpoch => { if *start_reg == 0 { - let unixepoch: String = exec_unixepoch( - &OwnedValue::build_text(Rc::new("now".to_string())), - )?; - state.registers[*dest] = - OwnedValue::build_text(Rc::new(unixepoch)); + let unixepoch: String = + exec_unixepoch(&OwnedValue::build_text("now"))?; + state.registers[*dest] = OwnedValue::build_text(&unixepoch); } else { let datetime_value = &state.registers[*start_reg]; let unixepoch = exec_unixepoch(datetime_value); match unixepoch { Ok(time) => { - state.registers[*dest] = - OwnedValue::build_text(Rc::new(time)) + state.registers[*dest] = OwnedValue::build_text(&time) } Err(e) => { return Err(LimboError::ParseError(format!( @@ -2284,7 +2276,7 @@ impl Program { let version_integer: i64 = DATABASE_VERSION.get().unwrap().parse()?; let version = execute_sqlite_version(version_integer); - state.registers[*dest] = OwnedValue::build_text(Rc::new(version)); + state.registers[*dest] = OwnedValue::build_text(&version); } ScalarFunc::SqliteSourceId => { let src_id = format!( @@ -2292,7 +2284,7 @@ impl Program { info::build::BUILT_TIME_SQLITE, info::build::GIT_COMMIT_HASH.unwrap_or("unknown") ); - state.registers[*dest] = OwnedValue::build_text(Rc::new(src_id)); + state.registers[*dest] = OwnedValue::build_text(&src_id); } ScalarFunc::Replace => { assert_eq!(arg_count, 3); @@ -2841,7 +2833,7 @@ fn get_indent_count(indent_count: usize, curr_insn: &Insn, prev_insn: Option<&In fn exec_lower(reg: &OwnedValue) -> Option { match reg { - OwnedValue::Text(t) => Some(OwnedValue::build_text(Rc::new(t.as_str().to_lowercase()))), + OwnedValue::Text(t) => Some(OwnedValue::build_text(&t.as_str().to_lowercase())), t => Some(t.to_owned()), } } @@ -2870,7 +2862,7 @@ fn exec_octet_length(reg: &OwnedValue) -> OwnedValue { fn exec_upper(reg: &OwnedValue) -> Option { match reg { - OwnedValue::Text(t) => Some(OwnedValue::build_text(Rc::new(t.as_str().to_uppercase()))), + OwnedValue::Text(t) => Some(OwnedValue::build_text(&t.as_str().to_uppercase())), t => Some(t.to_owned()), } } @@ -2888,7 +2880,7 @@ fn exec_concat_strings(registers: &[OwnedValue]) -> OwnedValue { OwnedValue::Record(_) => unreachable!(), } } - OwnedValue::build_text(Rc::new(result)) + OwnedValue::build_text(&result) } fn exec_concat_ws(registers: &[OwnedValue]) -> OwnedValue { @@ -2916,7 +2908,7 @@ fn exec_concat_ws(registers: &[OwnedValue]) -> OwnedValue { } } - OwnedValue::build_text(Rc::new(result)) + OwnedValue::build_text(&result) } fn exec_sign(reg: &OwnedValue) -> Option { @@ -2961,15 +2953,15 @@ fn exec_sign(reg: &OwnedValue) -> Option { /// Generates the Soundex code for a given word pub fn exec_soundex(reg: &OwnedValue) -> OwnedValue { let s = match reg { - OwnedValue::Null => return OwnedValue::build_text(Rc::new("?000".to_string())), + OwnedValue::Null => return OwnedValue::build_text("?000"), OwnedValue::Text(s) => { // return ?000 if non ASCII alphabet character is found if !s.as_str().chars().all(|c| c.is_ascii_alphabetic()) { - return OwnedValue::build_text(Rc::new("?000".to_string())); + return OwnedValue::build_text("?000"); } s.clone() } - _ => return OwnedValue::build_text(Rc::new("?000".to_string())), // For unsupported types, return NULL + _ => return OwnedValue::build_text("?000"), // For unsupported types, return NULL }; // Remove numbers and spaces @@ -2980,7 +2972,7 @@ pub fn exec_soundex(reg: &OwnedValue) -> OwnedValue { .collect::() .replace(" ", ""); if word.is_empty() { - return OwnedValue::build_text(Rc::new("0000".to_string())); + return OwnedValue::build_text("0000"); } let soundex_code = |c| match c { @@ -3046,7 +3038,7 @@ pub fn exec_soundex(reg: &OwnedValue) -> OwnedValue { // Retain the first 4 characters and convert to uppercase result.truncate(4); - OwnedValue::build_text(Rc::new(result.to_uppercase())) + OwnedValue::build_text(&result.to_uppercase()) } fn exec_abs(reg: &OwnedValue) -> Result { @@ -3094,7 +3086,7 @@ fn exec_randomblob(reg: &OwnedValue) -> OwnedValue { fn exec_quote(value: &OwnedValue) -> OwnedValue { match value { - OwnedValue::Null => OwnedValue::build_text(OwnedValue::Null.to_string().into()), + OwnedValue::Null => OwnedValue::build_text(&OwnedValue::Null.to_string()), OwnedValue::Integer(_) | OwnedValue::Float(_) => value.to_owned(), OwnedValue::Blob(_) => todo!(), OwnedValue::Text(s) => { @@ -3111,7 +3103,7 @@ fn exec_quote(value: &OwnedValue) -> OwnedValue { } } quoted.push('\''); - OwnedValue::build_text(Rc::new(quoted)) + OwnedValue::build_text("ed) } _ => OwnedValue::Null, // For unsupported types, return NULL } @@ -3128,7 +3120,7 @@ fn exec_char(values: Vec) -> OwnedValue { } }) .collect(); - OwnedValue::build_text(Rc::new(result)) + OwnedValue::build_text(&result) } fn construct_like_regex(pattern: &str) -> Regex { @@ -3211,7 +3203,7 @@ fn exec_substring( let str_len = str.as_str().len(); if start > str_len { - return OwnedValue::build_text(Rc::new("".to_string())); + return OwnedValue::build_text(""); } let start_idx = start - 1; @@ -3222,19 +3214,19 @@ fn exec_substring( }; let substring = &str.as_str()[start_idx..end.min(str_len)]; - OwnedValue::build_text(Rc::new(substring.to_string())) + OwnedValue::build_text(substring) } else if let (OwnedValue::Text(str), OwnedValue::Integer(start)) = (str_value, start_value) { let start = *start as usize; let str_len = str.as_str().len(); if start > str_len { - return OwnedValue::build_text(Rc::new("".to_string())); + return OwnedValue::build_text(""); } let start_idx = start - 1; let substring = &str.as_str()[start_idx..str_len]; - OwnedValue::build_text(Rc::new(substring.to_string())) + OwnedValue::build_text(substring) } else { OwnedValue::Null } @@ -3279,11 +3271,11 @@ fn exec_instr(reg: &OwnedValue, pattern: &OwnedValue) -> OwnedValue { fn exec_typeof(reg: &OwnedValue) -> OwnedValue { match reg { - OwnedValue::Null => OwnedValue::build_text(Rc::new("null".to_string())), - OwnedValue::Integer(_) => OwnedValue::build_text(Rc::new("integer".to_string())), - OwnedValue::Float(_) => OwnedValue::build_text(Rc::new("real".to_string())), - OwnedValue::Text(_) => OwnedValue::build_text(Rc::new("text".to_string())), - OwnedValue::Blob(_) => OwnedValue::build_text(Rc::new("blob".to_string())), + OwnedValue::Null => OwnedValue::build_text("null"), + OwnedValue::Integer(_) => OwnedValue::build_text("integer"), + OwnedValue::Float(_) => OwnedValue::build_text("real"), + OwnedValue::Text(_) => OwnedValue::build_text("text"), + OwnedValue::Blob(_) => OwnedValue::build_text("blob"), OwnedValue::Agg(ctx) => exec_typeof(ctx.final_value()), OwnedValue::Record(_) => unimplemented!(), } @@ -3296,7 +3288,7 @@ fn exec_hex(reg: &OwnedValue) -> OwnedValue { | OwnedValue::Float(_) | OwnedValue::Blob(_) => { let text = reg.to_string(); - OwnedValue::build_text(Rc::new(hex::encode_upper(text))) + OwnedValue::build_text(&hex::encode_upper(text)) } _ => OwnedValue::Null, } @@ -3380,15 +3372,11 @@ fn exec_trim(reg: &OwnedValue, pattern: Option) -> OwnedValue { (reg, Some(pattern)) => match reg { OwnedValue::Text(_) | OwnedValue::Integer(_) | OwnedValue::Float(_) => { let pattern_chars: Vec = pattern.to_string().chars().collect(); - OwnedValue::build_text(Rc::new( - reg.to_string().trim_matches(&pattern_chars[..]).to_string(), - )) + OwnedValue::build_text(reg.to_string().trim_matches(&pattern_chars[..])) } _ => reg.to_owned(), }, - (OwnedValue::Text(t), None) => { - OwnedValue::build_text(Rc::new(t.as_str().trim().to_string())) - } + (OwnedValue::Text(t), None) => OwnedValue::build_text(t.as_str().trim()), (reg, _) => reg.to_owned(), } } @@ -3399,17 +3387,11 @@ fn exec_ltrim(reg: &OwnedValue, pattern: Option) -> OwnedValue { (reg, Some(pattern)) => match reg { OwnedValue::Text(_) | OwnedValue::Integer(_) | OwnedValue::Float(_) => { let pattern_chars: Vec = pattern.to_string().chars().collect(); - OwnedValue::build_text(Rc::new( - reg.to_string() - .trim_start_matches(&pattern_chars[..]) - .to_string(), - )) + OwnedValue::build_text(reg.to_string().trim_start_matches(&pattern_chars[..])) } _ => reg.to_owned(), }, - (OwnedValue::Text(t), None) => { - OwnedValue::build_text(Rc::new(t.as_str().trim_start().to_string())) - } + (OwnedValue::Text(t), None) => OwnedValue::build_text(t.as_str().trim_start()), (reg, _) => reg.to_owned(), } } @@ -3420,17 +3402,11 @@ fn exec_rtrim(reg: &OwnedValue, pattern: Option) -> OwnedValue { (reg, Some(pattern)) => match reg { OwnedValue::Text(_) | OwnedValue::Integer(_) | OwnedValue::Float(_) => { let pattern_chars: Vec = pattern.to_string().chars().collect(); - OwnedValue::build_text(Rc::new( - reg.to_string() - .trim_end_matches(&pattern_chars[..]) - .to_string(), - )) + OwnedValue::build_text(reg.to_string().trim_end_matches(&pattern_chars[..])) } _ => reg.to_owned(), }, - (OwnedValue::Text(t), None) => { - OwnedValue::build_text(Rc::new(t.as_str().trim_end().to_string())) - } + (OwnedValue::Text(t), None) => OwnedValue::build_text(t.as_str().trim_end()), (reg, _) => reg.to_owned(), } } @@ -3473,7 +3449,7 @@ fn exec_cast(value: &OwnedValue, datatype: &str) -> OwnedValue { Affinity::Text => { // Convert everything to text representation // TODO: handle encoding and whatever sqlite3_snprintf does - OwnedValue::build_text(Rc::new(value.to_string())) + OwnedValue::build_text(&value.to_string()) } Affinity::Real => match value { OwnedValue::Blob(b) => { @@ -3550,7 +3526,7 @@ fn exec_replace(source: &OwnedValue, pattern: &OwnedValue, replacement: &OwnedVa let result = source .as_str() .replace(pattern.as_str(), replacement.as_str()); - OwnedValue::build_text(Rc::new(result)) + OwnedValue::build_text(&result) } _ => unreachable!("text cast should never fail"), } @@ -3810,7 +3786,7 @@ mod tests { #[test] fn test_length() { - let input_str = OwnedValue::build_text(Rc::new(String::from("bob"))); + let input_str = OwnedValue::build_text("bob"); let expected_len = OwnedValue::Integer(3); assert_eq!(exec_length(&input_str), expected_len); @@ -3829,60 +3805,57 @@ mod tests { #[test] fn test_quote() { - let input = OwnedValue::build_text(Rc::new(String::from("abc\0edf"))); - let expected = OwnedValue::build_text(Rc::new(String::from("'abc'"))); + let input = OwnedValue::build_text("abc\0edf"); + let expected = OwnedValue::build_text("'abc'"); assert_eq!(exec_quote(&input), expected); let input = OwnedValue::Integer(123); let expected = OwnedValue::Integer(123); assert_eq!(exec_quote(&input), expected); - let input = OwnedValue::build_text(Rc::new(String::from("hello''world"))); - let expected = OwnedValue::build_text(Rc::new(String::from("'hello''''world'"))); + let input = OwnedValue::build_text("hello''world"); + let expected = OwnedValue::build_text("'hello''''world'"); assert_eq!(exec_quote(&input), expected); } #[test] fn test_typeof() { let input = OwnedValue::Null; - let expected: OwnedValue = OwnedValue::build_text(Rc::new("null".to_string())); + let expected: OwnedValue = OwnedValue::build_text("null"); assert_eq!(exec_typeof(&input), expected); let input = OwnedValue::Integer(123); - let expected: OwnedValue = OwnedValue::build_text(Rc::new("integer".to_string())); + let expected: OwnedValue = OwnedValue::build_text("integer"); assert_eq!(exec_typeof(&input), expected); let input = OwnedValue::Float(123.456); - let expected: OwnedValue = OwnedValue::build_text(Rc::new("real".to_string())); + let expected: OwnedValue = OwnedValue::build_text("real"); assert_eq!(exec_typeof(&input), expected); - let input = OwnedValue::build_text(Rc::new("hello".to_string())); - let expected: OwnedValue = OwnedValue::build_text(Rc::new("text".to_string())); + let input = OwnedValue::build_text("hello"); + let expected: OwnedValue = OwnedValue::build_text("text"); assert_eq!(exec_typeof(&input), expected); let input = OwnedValue::Blob(Rc::new("limbo".as_bytes().to_vec())); - let expected: OwnedValue = OwnedValue::build_text(Rc::new("blob".to_string())); + let expected: OwnedValue = OwnedValue::build_text("blob"); assert_eq!(exec_typeof(&input), expected); let input = OwnedValue::Agg(Box::new(AggContext::Sum(OwnedValue::Integer(123)))); - let expected = OwnedValue::build_text(Rc::new("integer".to_string())); + let expected = OwnedValue::build_text("integer"); assert_eq!(exec_typeof(&input), expected); } #[test] fn test_unicode() { assert_eq!( - exec_unicode(&OwnedValue::build_text(Rc::new("a".to_string()))), + exec_unicode(&OwnedValue::build_text("a")), OwnedValue::Integer(97) ); assert_eq!( - exec_unicode(&OwnedValue::build_text(Rc::new("😊".to_string()))), + exec_unicode(&OwnedValue::build_text("😊")), OwnedValue::Integer(128522) ); - assert_eq!( - exec_unicode(&OwnedValue::build_text(Rc::new("".to_string()))), - OwnedValue::Null - ); + assert_eq!(exec_unicode(&OwnedValue::build_text("")), OwnedValue::Null); assert_eq!( exec_unicode(&OwnedValue::Integer(23)), OwnedValue::Integer(50) @@ -3912,17 +3885,11 @@ mod tests { assert_eq!(exec_min(input_int_vec.clone()), OwnedValue::Integer(-1)); assert_eq!(exec_max(input_int_vec.clone()), OwnedValue::Integer(10)); - let str1 = OwnedValue::build_text(Rc::new(String::from("A"))); - let str2 = OwnedValue::build_text(Rc::new(String::from("z"))); + let str1 = OwnedValue::build_text("A"); + let str2 = OwnedValue::build_text("z"); let input_str_vec = vec![&str2, &str1]; - assert_eq!( - exec_min(input_str_vec.clone()), - OwnedValue::build_text(Rc::new(String::from("A"))) - ); - assert_eq!( - exec_max(input_str_vec.clone()), - OwnedValue::build_text(Rc::new(String::from("z"))) - ); + assert_eq!(exec_min(input_str_vec.clone()), OwnedValue::build_text("A")); + assert_eq!(exec_max(input_str_vec.clone()), OwnedValue::build_text("z")); let input_null_vec = vec![&OwnedValue::Null, &OwnedValue::Null]; assert_eq!(exec_min(input_null_vec.clone()), OwnedValue::Null); @@ -3932,102 +3899,102 @@ mod tests { assert_eq!(exec_min(input_mixed_vec.clone()), OwnedValue::Integer(10)); assert_eq!( exec_max(input_mixed_vec.clone()), - OwnedValue::build_text(Rc::new(String::from("A"))) + OwnedValue::build_text("A") ); } #[test] fn test_trim() { - let input_str = OwnedValue::build_text(Rc::new(String::from(" Bob and Alice "))); - let expected_str = OwnedValue::build_text(Rc::new(String::from("Bob and Alice"))); + let input_str = OwnedValue::build_text(" Bob and Alice "); + let expected_str = OwnedValue::build_text("Bob and Alice"); assert_eq!(exec_trim(&input_str, None), expected_str); - let input_str = OwnedValue::build_text(Rc::new(String::from(" Bob and Alice "))); - let pattern_str = OwnedValue::build_text(Rc::new(String::from("Bob and"))); - let expected_str = OwnedValue::build_text(Rc::new(String::from("Alice"))); + let input_str = OwnedValue::build_text(" Bob and Alice "); + let pattern_str = OwnedValue::build_text("Bob and"); + let expected_str = OwnedValue::build_text("Alice"); assert_eq!(exec_trim(&input_str, Some(pattern_str)), expected_str); } #[test] fn test_ltrim() { - let input_str = OwnedValue::build_text(Rc::new(String::from(" Bob and Alice "))); - let expected_str = OwnedValue::build_text(Rc::new(String::from("Bob and Alice "))); + let input_str = OwnedValue::build_text(" Bob and Alice "); + let expected_str = OwnedValue::build_text("Bob and Alice "); assert_eq!(exec_ltrim(&input_str, None), expected_str); - let input_str = OwnedValue::build_text(Rc::new(String::from(" Bob and Alice "))); - let pattern_str = OwnedValue::build_text(Rc::new(String::from("Bob and"))); - let expected_str = OwnedValue::build_text(Rc::new(String::from("Alice "))); + let input_str = OwnedValue::build_text(" Bob and Alice "); + let pattern_str = OwnedValue::build_text("Bob and"); + let expected_str = OwnedValue::build_text("Alice "); assert_eq!(exec_ltrim(&input_str, Some(pattern_str)), expected_str); } #[test] fn test_rtrim() { - let input_str = OwnedValue::build_text(Rc::new(String::from(" Bob and Alice "))); - let expected_str = OwnedValue::build_text(Rc::new(String::from(" Bob and Alice"))); + let input_str = OwnedValue::build_text(" Bob and Alice "); + let expected_str = OwnedValue::build_text(" Bob and Alice"); assert_eq!(exec_rtrim(&input_str, None), expected_str); - let input_str = OwnedValue::build_text(Rc::new(String::from(" Bob and Alice "))); - let pattern_str = OwnedValue::build_text(Rc::new(String::from("Bob and"))); - let expected_str = OwnedValue::build_text(Rc::new(String::from(" Bob and Alice"))); + let input_str = OwnedValue::build_text(" Bob and Alice "); + let pattern_str = OwnedValue::build_text("Bob and"); + let expected_str = OwnedValue::build_text(" Bob and Alice"); assert_eq!(exec_rtrim(&input_str, Some(pattern_str)), expected_str); - let input_str = OwnedValue::build_text(Rc::new(String::from(" Bob and Alice "))); - let pattern_str = OwnedValue::build_text(Rc::new(String::from("and Alice"))); - let expected_str = OwnedValue::build_text(Rc::new(String::from(" Bob"))); + let input_str = OwnedValue::build_text(" Bob and Alice "); + let pattern_str = OwnedValue::build_text("and Alice"); + let expected_str = OwnedValue::build_text(" Bob"); assert_eq!(exec_rtrim(&input_str, Some(pattern_str)), expected_str); } #[test] fn test_soundex() { - let input_str = OwnedValue::build_text(Rc::new(String::from("Pfister"))); - let expected_str = OwnedValue::build_text(Rc::new(String::from("P236"))); + let input_str = OwnedValue::build_text("Pfister"); + let expected_str = OwnedValue::build_text("P236"); assert_eq!(exec_soundex(&input_str), expected_str); - let input_str = OwnedValue::build_text(Rc::new(String::from("husobee"))); - let expected_str = OwnedValue::build_text(Rc::new(String::from("H210"))); + let input_str = OwnedValue::build_text("husobee"); + let expected_str = OwnedValue::build_text("H210"); assert_eq!(exec_soundex(&input_str), expected_str); - let input_str = OwnedValue::build_text(Rc::new(String::from("Tymczak"))); - let expected_str = OwnedValue::build_text(Rc::new(String::from("T522"))); + let input_str = OwnedValue::build_text("Tymczak"); + let expected_str = OwnedValue::build_text("T522"); assert_eq!(exec_soundex(&input_str), expected_str); - let input_str = OwnedValue::build_text(Rc::new(String::from("Ashcraft"))); - let expected_str = OwnedValue::build_text(Rc::new(String::from("A261"))); + let input_str = OwnedValue::build_text("Ashcraft"); + let expected_str = OwnedValue::build_text("A261"); assert_eq!(exec_soundex(&input_str), expected_str); - let input_str = OwnedValue::build_text(Rc::new(String::from("Robert"))); - let expected_str = OwnedValue::build_text(Rc::new(String::from("R163"))); + let input_str = OwnedValue::build_text("Robert"); + let expected_str = OwnedValue::build_text("R163"); assert_eq!(exec_soundex(&input_str), expected_str); - let input_str = OwnedValue::build_text(Rc::new(String::from("Rupert"))); - let expected_str = OwnedValue::build_text(Rc::new(String::from("R163"))); + let input_str = OwnedValue::build_text("Rupert"); + let expected_str = OwnedValue::build_text("R163"); assert_eq!(exec_soundex(&input_str), expected_str); - let input_str = OwnedValue::build_text(Rc::new(String::from("Rubin"))); - let expected_str = OwnedValue::build_text(Rc::new(String::from("R150"))); + let input_str = OwnedValue::build_text("Rubin"); + let expected_str = OwnedValue::build_text("R150"); assert_eq!(exec_soundex(&input_str), expected_str); - let input_str = OwnedValue::build_text(Rc::new(String::from("Kant"))); - let expected_str = OwnedValue::build_text(Rc::new(String::from("K530"))); + let input_str = OwnedValue::build_text("Kant"); + let expected_str = OwnedValue::build_text("K530"); assert_eq!(exec_soundex(&input_str), expected_str); - let input_str = OwnedValue::build_text(Rc::new(String::from("Knuth"))); - let expected_str = OwnedValue::build_text(Rc::new(String::from("K530"))); + let input_str = OwnedValue::build_text("Knuth"); + let expected_str = OwnedValue::build_text("K530"); assert_eq!(exec_soundex(&input_str), expected_str); - let input_str = OwnedValue::build_text(Rc::new(String::from("x"))); - let expected_str = OwnedValue::build_text(Rc::new(String::from("X000"))); + let input_str = OwnedValue::build_text("x"); + let expected_str = OwnedValue::build_text("X000"); assert_eq!(exec_soundex(&input_str), expected_str); - let input_str = OwnedValue::build_text(Rc::new(String::from("闪电五连鞭"))); - let expected_str = OwnedValue::build_text(Rc::new(String::from("?000"))); + let input_str = OwnedValue::build_text("闪电五连鞭"); + let expected_str = OwnedValue::build_text("?000"); assert_eq!(exec_soundex(&input_str), expected_str); } #[test] fn test_upper_case() { - let input_str = OwnedValue::build_text(Rc::new(String::from("Limbo"))); - let expected_str = OwnedValue::build_text(Rc::new(String::from("LIMBO"))); + let input_str = OwnedValue::build_text("Limbo"); + let expected_str = OwnedValue::build_text("LIMBO"); assert_eq!(exec_upper(&input_str).unwrap(), expected_str); let input_int = OwnedValue::Integer(10); @@ -4037,8 +4004,8 @@ mod tests { #[test] fn test_lower_case() { - let input_str = OwnedValue::build_text(Rc::new(String::from("Limbo"))); - let expected_str = OwnedValue::build_text(Rc::new(String::from("limbo"))); + let input_str = OwnedValue::build_text("Limbo"); + let expected_str = OwnedValue::build_text("limbo"); assert_eq!(exec_lower(&input_str).unwrap(), expected_str); let input_int = OwnedValue::Integer(10); @@ -4048,38 +4015,38 @@ mod tests { #[test] fn test_hex() { - let input_str = OwnedValue::build_text(Rc::new("limbo".to_string())); - let expected_val = OwnedValue::build_text(Rc::new(String::from("6C696D626F"))); + let input_str = OwnedValue::build_text("limbo"); + let expected_val = OwnedValue::build_text("6C696D626F"); assert_eq!(exec_hex(&input_str), expected_val); let input_int = OwnedValue::Integer(100); - let expected_val = OwnedValue::build_text(Rc::new(String::from("313030"))); + let expected_val = OwnedValue::build_text("313030"); assert_eq!(exec_hex(&input_int), expected_val); let input_float = OwnedValue::Float(12.34); - let expected_val = OwnedValue::build_text(Rc::new(String::from("31322E3334"))); + let expected_val = OwnedValue::build_text("31322E3334"); assert_eq!(exec_hex(&input_float), expected_val); } #[test] fn test_unhex() { - let input = OwnedValue::build_text(Rc::new(String::from("6f"))); + let input = OwnedValue::build_text("6f"); let expected = OwnedValue::Blob(Rc::new(vec![0x6f])); assert_eq!(exec_unhex(&input, None), expected); - let input = OwnedValue::build_text(Rc::new(String::from("6f"))); + let input = OwnedValue::build_text("6f"); let expected = OwnedValue::Blob(Rc::new(vec![0x6f])); assert_eq!(exec_unhex(&input, None), expected); - let input = OwnedValue::build_text(Rc::new(String::from("611"))); + let input = OwnedValue::build_text("611"); let expected = OwnedValue::Null; assert_eq!(exec_unhex(&input, None), expected); - let input = OwnedValue::build_text(Rc::new(String::from(""))); + let input = OwnedValue::build_text(""); let expected = OwnedValue::Blob(Rc::new(vec![])); assert_eq!(exec_unhex(&input, None), expected); - let input = OwnedValue::build_text(Rc::new(String::from("61x"))); + let input = OwnedValue::build_text("61x"); let expected = OwnedValue::Null; assert_eq!(exec_unhex(&input, None), expected); @@ -4101,7 +4068,7 @@ mod tests { assert_eq!(exec_abs(&float_negative_reg).unwrap(), float_positive_reg); assert_eq!( - exec_abs(&OwnedValue::build_text(Rc::new(String::from("a")))).unwrap(), + exec_abs(&OwnedValue::build_text("a")).unwrap(), OwnedValue::Float(0.0) ); assert_eq!(exec_abs(&OwnedValue::Null).unwrap(), OwnedValue::Null); @@ -4114,19 +4081,16 @@ mod tests { fn test_char() { assert_eq!( exec_char(vec![OwnedValue::Integer(108), OwnedValue::Integer(105)]), - OwnedValue::build_text(Rc::new("li".to_string())) - ); - assert_eq!( - exec_char(vec![]), - OwnedValue::build_text(Rc::new("".to_string())) + OwnedValue::build_text("li") ); + assert_eq!(exec_char(vec![]), OwnedValue::build_text("")); assert_eq!( exec_char(vec![OwnedValue::Null]), - OwnedValue::build_text(Rc::new("".to_string())) + OwnedValue::build_text("") ); assert_eq!( - exec_char(vec![OwnedValue::build_text(Rc::new("a".to_string()))]), - OwnedValue::build_text(Rc::new("".to_string())) + exec_char(vec![OwnedValue::build_text("a")]), + OwnedValue::build_text("") ); } @@ -4197,19 +4161,19 @@ mod tests { expected_len: 1, }, TestCase { - input: OwnedValue::build_text(Rc::new(String::from(""))), + input: OwnedValue::build_text(""), expected_len: 1, }, TestCase { - input: OwnedValue::build_text(Rc::new(String::from("5"))), + input: OwnedValue::build_text("5"), expected_len: 5, }, TestCase { - input: OwnedValue::build_text(Rc::new(String::from("0"))), + input: OwnedValue::build_text("0"), expected_len: 1, }, TestCase { - input: OwnedValue::build_text(Rc::new(String::from("-1"))), + input: OwnedValue::build_text("-1"), expected_len: 1, }, TestCase { @@ -4249,11 +4213,11 @@ mod tests { assert_eq!(exec_round(&input_val, Some(precision_val)), expected_val); let input_val = OwnedValue::Float(123.456); - let precision_val = OwnedValue::build_text(Rc::new(String::from("1"))); + let precision_val = OwnedValue::build_text("1"); let expected_val = OwnedValue::Float(123.5); assert_eq!(exec_round(&input_val, Some(precision_val)), expected_val); - let input_val = OwnedValue::build_text(Rc::new(String::from("123.456"))); + let input_val = OwnedValue::build_text("123.456"); let precision_val = OwnedValue::Integer(2); let expected_val = OwnedValue::Float(123.46); assert_eq!(exec_round(&input_val, Some(precision_val)), expected_val); @@ -4307,8 +4271,8 @@ mod tests { ); assert_eq!( exec_nullif( - &OwnedValue::build_text(Rc::new("limbo".to_string())), - &OwnedValue::build_text(Rc::new("limbo".to_string())) + &OwnedValue::build_text("limbo"), + &OwnedValue::build_text("limbo") ), OwnedValue::Null ); @@ -4323,55 +4287,55 @@ mod tests { ); assert_eq!( exec_nullif( - &OwnedValue::build_text(Rc::new("limbo".to_string())), - &OwnedValue::build_text(Rc::new("limb".to_string())) + &OwnedValue::build_text("limbo"), + &OwnedValue::build_text("limb") ), - OwnedValue::build_text(Rc::new("limbo".to_string())) + OwnedValue::build_text("limbo") ); } #[test] fn test_substring() { - let str_value = OwnedValue::build_text(Rc::new("limbo".to_string())); + let str_value = OwnedValue::build_text("limbo"); let start_value = OwnedValue::Integer(1); let length_value = OwnedValue::Integer(3); - let expected_val = OwnedValue::build_text(Rc::new(String::from("lim"))); + let expected_val = OwnedValue::build_text("lim"); assert_eq!( exec_substring(&str_value, &start_value, &length_value), expected_val ); - let str_value = OwnedValue::build_text(Rc::new("limbo".to_string())); + let str_value = OwnedValue::build_text("limbo"); let start_value = OwnedValue::Integer(1); let length_value = OwnedValue::Integer(10); - let expected_val = OwnedValue::build_text(Rc::new(String::from("limbo"))); + let expected_val = OwnedValue::build_text("limbo"); assert_eq!( exec_substring(&str_value, &start_value, &length_value), expected_val ); - let str_value = OwnedValue::build_text(Rc::new("limbo".to_string())); + let str_value = OwnedValue::build_text("limbo"); let start_value = OwnedValue::Integer(10); let length_value = OwnedValue::Integer(3); - let expected_val = OwnedValue::build_text(Rc::new(String::from(""))); + let expected_val = OwnedValue::build_text(""); assert_eq!( exec_substring(&str_value, &start_value, &length_value), expected_val ); - let str_value = OwnedValue::build_text(Rc::new("limbo".to_string())); + let str_value = OwnedValue::build_text("limbo"); let start_value = OwnedValue::Integer(3); let length_value = OwnedValue::Null; - let expected_val = OwnedValue::build_text(Rc::new(String::from("mbo"))); + let expected_val = OwnedValue::build_text("mbo"); assert_eq!( exec_substring(&str_value, &start_value, &length_value), expected_val ); - let str_value = OwnedValue::build_text(Rc::new("limbo".to_string())); + let str_value = OwnedValue::build_text("limbo"); let start_value = OwnedValue::Integer(10); let length_value = OwnedValue::Null; - let expected_val = OwnedValue::build_text(Rc::new(String::from(""))); + let expected_val = OwnedValue::build_text(""); assert_eq!( exec_substring(&str_value, &start_value, &length_value), expected_val @@ -4380,43 +4344,43 @@ mod tests { #[test] fn test_exec_instr() { - let input = OwnedValue::build_text(Rc::new(String::from("limbo"))); - let pattern = OwnedValue::build_text(Rc::new(String::from("im"))); + let input = OwnedValue::build_text("limbo"); + let pattern = OwnedValue::build_text("im"); let expected = OwnedValue::Integer(2); assert_eq!(exec_instr(&input, &pattern), expected); - let input = OwnedValue::build_text(Rc::new(String::from("limbo"))); - let pattern = OwnedValue::build_text(Rc::new(String::from("limbo"))); + let input = OwnedValue::build_text("limbo"); + let pattern = OwnedValue::build_text("limbo"); let expected = OwnedValue::Integer(1); assert_eq!(exec_instr(&input, &pattern), expected); - let input = OwnedValue::build_text(Rc::new(String::from("limbo"))); - let pattern = OwnedValue::build_text(Rc::new(String::from("o"))); + let input = OwnedValue::build_text("limbo"); + let pattern = OwnedValue::build_text("o"); let expected = OwnedValue::Integer(5); assert_eq!(exec_instr(&input, &pattern), expected); - let input = OwnedValue::build_text(Rc::new(String::from("liiiiimbo"))); - let pattern = OwnedValue::build_text(Rc::new(String::from("ii"))); + let input = OwnedValue::build_text("liiiiimbo"); + let pattern = OwnedValue::build_text("ii"); let expected = OwnedValue::Integer(2); assert_eq!(exec_instr(&input, &pattern), expected); - let input = OwnedValue::build_text(Rc::new(String::from("limbo"))); - let pattern = OwnedValue::build_text(Rc::new(String::from("limboX"))); + let input = OwnedValue::build_text("limbo"); + let pattern = OwnedValue::build_text("limboX"); let expected = OwnedValue::Integer(0); assert_eq!(exec_instr(&input, &pattern), expected); - let input = OwnedValue::build_text(Rc::new(String::from("limbo"))); - let pattern = OwnedValue::build_text(Rc::new(String::from(""))); + let input = OwnedValue::build_text("limbo"); + let pattern = OwnedValue::build_text(""); let expected = OwnedValue::Integer(1); assert_eq!(exec_instr(&input, &pattern), expected); - let input = OwnedValue::build_text(Rc::new(String::from(""))); - let pattern = OwnedValue::build_text(Rc::new(String::from("limbo"))); + let input = OwnedValue::build_text(""); + let pattern = OwnedValue::build_text("limbo"); let expected = OwnedValue::Integer(0); assert_eq!(exec_instr(&input, &pattern), expected); - let input = OwnedValue::build_text(Rc::new(String::from(""))); - let pattern = OwnedValue::build_text(Rc::new(String::from(""))); + let input = OwnedValue::build_text(""); + let pattern = OwnedValue::build_text(""); let expected = OwnedValue::Integer(1); assert_eq!(exec_instr(&input, &pattern), expected); @@ -4425,13 +4389,13 @@ mod tests { let expected = OwnedValue::Null; assert_eq!(exec_instr(&input, &pattern), expected); - let input = OwnedValue::build_text(Rc::new(String::from("limbo"))); + let input = OwnedValue::build_text("limbo"); let pattern = OwnedValue::Null; let expected = OwnedValue::Null; assert_eq!(exec_instr(&input, &pattern), expected); let input = OwnedValue::Null; - let pattern = OwnedValue::build_text(Rc::new(String::from("limbo"))); + let pattern = OwnedValue::build_text("limbo"); let expected = OwnedValue::Null; assert_eq!(exec_instr(&input, &pattern), expected); @@ -4456,7 +4420,7 @@ mod tests { assert_eq!(exec_instr(&input, &pattern), expected); let input = OwnedValue::Float(12.34); - let pattern = OwnedValue::build_text(Rc::new(String::from("."))); + let pattern = OwnedValue::build_text("."); let expected = OwnedValue::Integer(3); assert_eq!(exec_instr(&input, &pattern), expected); @@ -4471,11 +4435,11 @@ mod tests { assert_eq!(exec_instr(&input, &pattern), expected); let input = OwnedValue::Blob(Rc::new(vec![0x61, 0x62, 0x63, 0x64, 0x65])); - let pattern = OwnedValue::build_text(Rc::new(String::from("cd"))); + let pattern = OwnedValue::build_text("cd"); let expected = OwnedValue::Integer(3); assert_eq!(exec_instr(&input, &pattern), expected); - let input = OwnedValue::build_text(Rc::new(String::from("abcde"))); + let input = OwnedValue::build_text("abcde"); let pattern = OwnedValue::Blob(Rc::new(vec![0x63, 0x64])); let expected = OwnedValue::Integer(3); assert_eq!(exec_instr(&input, &pattern), expected); @@ -4511,19 +4475,19 @@ mod tests { let expected = Some(OwnedValue::Integer(-1)); assert_eq!(exec_sign(&input), expected); - let input = OwnedValue::build_text(Rc::new("abc".to_string())); + let input = OwnedValue::build_text("abc"); let expected = Some(OwnedValue::Null); assert_eq!(exec_sign(&input), expected); - let input = OwnedValue::build_text(Rc::new("42".to_string())); + let input = OwnedValue::build_text("42"); let expected = Some(OwnedValue::Integer(1)); assert_eq!(exec_sign(&input), expected); - let input = OwnedValue::build_text(Rc::new("-42".to_string())); + let input = OwnedValue::build_text("-42"); let expected = Some(OwnedValue::Integer(-1)); assert_eq!(exec_sign(&input), expected); - let input = OwnedValue::build_text(Rc::new("0".to_string())); + let input = OwnedValue::build_text("0"); let expected = Some(OwnedValue::Integer(0)); assert_eq!(exec_sign(&input), expected); @@ -4566,15 +4530,15 @@ mod tests { let expected = OwnedValue::Blob(Rc::new(vec![])); assert_eq!(exec_zeroblob(&input), expected); - let input = OwnedValue::build_text(Rc::new("5".to_string())); + let input = OwnedValue::build_text("5"); let expected = OwnedValue::Blob(Rc::new(vec![0; 5])); assert_eq!(exec_zeroblob(&input), expected); - let input = OwnedValue::build_text(Rc::new("-5".to_string())); + let input = OwnedValue::build_text("-5"); let expected = OwnedValue::Blob(Rc::new(vec![])); assert_eq!(exec_zeroblob(&input), expected); - let input = OwnedValue::build_text(Rc::new("text".to_string())); + let input = OwnedValue::build_text("text"); let expected = OwnedValue::Blob(Rc::new(vec![])); assert_eq!(exec_zeroblob(&input), expected); @@ -4596,101 +4560,101 @@ mod tests { #[test] fn test_replace() { - let input_str = OwnedValue::build_text(Rc::new(String::from("bob"))); - let pattern_str = OwnedValue::build_text(Rc::new(String::from("b"))); - let replace_str = OwnedValue::build_text(Rc::new(String::from("a"))); - let expected_str = OwnedValue::build_text(Rc::new(String::from("aoa"))); + let input_str = OwnedValue::build_text("bob"); + let pattern_str = OwnedValue::build_text("b"); + let replace_str = OwnedValue::build_text("a"); + let expected_str = OwnedValue::build_text("aoa"); assert_eq!( exec_replace(&input_str, &pattern_str, &replace_str), expected_str ); - let input_str = OwnedValue::build_text(Rc::new(String::from("bob"))); - let pattern_str = OwnedValue::build_text(Rc::new(String::from("b"))); - let replace_str = OwnedValue::build_text(Rc::new(String::from(""))); - let expected_str = OwnedValue::build_text(Rc::new(String::from("o"))); + let input_str = OwnedValue::build_text("bob"); + let pattern_str = OwnedValue::build_text("b"); + let replace_str = OwnedValue::build_text(""); + let expected_str = OwnedValue::build_text("o"); assert_eq!( exec_replace(&input_str, &pattern_str, &replace_str), expected_str ); - let input_str = OwnedValue::build_text(Rc::new(String::from("bob"))); - let pattern_str = OwnedValue::build_text(Rc::new(String::from("b"))); - let replace_str = OwnedValue::build_text(Rc::new(String::from("abc"))); - let expected_str = OwnedValue::build_text(Rc::new(String::from("abcoabc"))); + let input_str = OwnedValue::build_text("bob"); + let pattern_str = OwnedValue::build_text("b"); + let replace_str = OwnedValue::build_text("abc"); + let expected_str = OwnedValue::build_text("abcoabc"); assert_eq!( exec_replace(&input_str, &pattern_str, &replace_str), expected_str ); - let input_str = OwnedValue::build_text(Rc::new(String::from("bob"))); - let pattern_str = OwnedValue::build_text(Rc::new(String::from("a"))); - let replace_str = OwnedValue::build_text(Rc::new(String::from("b"))); - let expected_str = OwnedValue::build_text(Rc::new(String::from("bob"))); + let input_str = OwnedValue::build_text("bob"); + let pattern_str = OwnedValue::build_text("a"); + let replace_str = OwnedValue::build_text("b"); + let expected_str = OwnedValue::build_text("bob"); assert_eq!( exec_replace(&input_str, &pattern_str, &replace_str), expected_str ); - let input_str = OwnedValue::build_text(Rc::new(String::from("bob"))); - let pattern_str = OwnedValue::build_text(Rc::new(String::from(""))); - let replace_str = OwnedValue::build_text(Rc::new(String::from("a"))); - let expected_str = OwnedValue::build_text(Rc::new(String::from("bob"))); + let input_str = OwnedValue::build_text("bob"); + let pattern_str = OwnedValue::build_text(""); + let replace_str = OwnedValue::build_text("a"); + let expected_str = OwnedValue::build_text("bob"); assert_eq!( exec_replace(&input_str, &pattern_str, &replace_str), expected_str ); - let input_str = OwnedValue::build_text(Rc::new(String::from("bob"))); + let input_str = OwnedValue::build_text("bob"); let pattern_str = OwnedValue::Null; - let replace_str = OwnedValue::build_text(Rc::new(String::from("a"))); + let replace_str = OwnedValue::build_text("a"); let expected_str = OwnedValue::Null; assert_eq!( exec_replace(&input_str, &pattern_str, &replace_str), expected_str ); - let input_str = OwnedValue::build_text(Rc::new(String::from("bo5"))); + let input_str = OwnedValue::build_text("bo5"); let pattern_str = OwnedValue::Integer(5); - let replace_str = OwnedValue::build_text(Rc::new(String::from("a"))); - let expected_str = OwnedValue::build_text(Rc::new(String::from("boa"))); + let replace_str = OwnedValue::build_text("a"); + let expected_str = OwnedValue::build_text("boa"); assert_eq!( exec_replace(&input_str, &pattern_str, &replace_str), expected_str ); - let input_str = OwnedValue::build_text(Rc::new(String::from("bo5.0"))); + let input_str = OwnedValue::build_text("bo5.0"); let pattern_str = OwnedValue::Float(5.0); - let replace_str = OwnedValue::build_text(Rc::new(String::from("a"))); - let expected_str = OwnedValue::build_text(Rc::new(String::from("boa"))); + let replace_str = OwnedValue::build_text("a"); + let expected_str = OwnedValue::build_text("boa"); assert_eq!( exec_replace(&input_str, &pattern_str, &replace_str), expected_str ); - let input_str = OwnedValue::build_text(Rc::new(String::from("bo5"))); + let input_str = OwnedValue::build_text("bo5"); let pattern_str = OwnedValue::Float(5.0); - let replace_str = OwnedValue::build_text(Rc::new(String::from("a"))); - let expected_str = OwnedValue::build_text(Rc::new(String::from("bo5"))); + let replace_str = OwnedValue::build_text("a"); + let expected_str = OwnedValue::build_text("bo5"); assert_eq!( exec_replace(&input_str, &pattern_str, &replace_str), expected_str ); - let input_str = OwnedValue::build_text(Rc::new(String::from("bo5.0"))); + let input_str = OwnedValue::build_text("bo5.0"); let pattern_str = OwnedValue::Float(5.0); let replace_str = OwnedValue::Float(6.0); - let expected_str = OwnedValue::build_text(Rc::new(String::from("bo6.0"))); + let expected_str = OwnedValue::build_text("bo6.0"); assert_eq!( exec_replace(&input_str, &pattern_str, &replace_str), expected_str ); // todo: change this test to use (0.1 + 0.2) instead of 0.3 when decimals are implemented. - let input_str = OwnedValue::build_text(Rc::new(String::from("tes3"))); + let input_str = OwnedValue::build_text("tes3"); let pattern_str = OwnedValue::Integer(3); let replace_str = OwnedValue::Agg(Box::new(AggContext::Sum(OwnedValue::Float(0.3)))); - let expected_str = OwnedValue::build_text(Rc::new(String::from("tes0.3"))); + let expected_str = OwnedValue::build_text("tes0.3"); assert_eq!( exec_replace(&input_str, &pattern_str, &replace_str), expected_str diff --git a/core/vector/mod.rs b/core/vector/mod.rs index a1c002fdc..60e365f94 100644 --- a/core/vector/mod.rs +++ b/core/vector/mod.rs @@ -56,16 +56,12 @@ pub fn vector_extract(args: &[OwnedValue]) -> Result { }; if blob.is_empty() { - return Ok(OwnedValue::Text(crate::types::Text::new(std::rc::Rc::new( - "[]".to_string(), - )))); + return Ok(OwnedValue::build_text("[]")); } let vector_type = vector_type(blob)?; let vector = vector_deserialize(vector_type, blob)?; - Ok(OwnedValue::Text(crate::types::Text::new(std::rc::Rc::new( - vector_to_text(&vector), - )))) + Ok(OwnedValue::build_text(&vector_to_text(&vector))) } pub fn vector_distance_cos(args: &[OwnedValue]) -> Result {