Repository navigation
fix(query): bound begins_with on a string sort key by the next prefix #588
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3b9e9fa
fadc6fb
529b4bf
dd5c7ed
bdc5c55
ab43c05
0d701bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,7 @@ use extenddb_core::types::{ | |
| }; | ||
| use extenddb_storage::error::StorageError; | ||
| use extenddb_storage::util::SortKeyValue; | ||
| use extenddb_storage::util::{parse_sk, pk_to_text, sk_info}; | ||
| use extenddb_storage::util::{parse_sk, pk_to_text, sk_info, string_prefix_upper_bound}; | ||
|
|
||
| /// Extra pagination bind values for index queries. | ||
| /// | ||
|
|
@@ -82,8 +82,9 @@ pub(crate) struct SkSqlInfo { | |
| pub(crate) fn build_sk_sql( | ||
| sk_cond: &SortKeyCondition, | ||
| sk_col: &str, | ||
| maps: &ExpressionMaps, | ||
| param_idx: &mut u32, | ||
| ) -> SkSqlInfo { | ||
| ) -> Result<SkSqlInfo, StorageError> { | ||
| // Apply COLLATE "C" for string sort key columns to get byte-order comparison. | ||
| let collate = if sk_col == "sk_s" || sk_col.ends_with("_s") { | ||
| " COLLATE \"C\"" | ||
|
|
@@ -102,7 +103,7 @@ pub(crate) fn build_sk_sql( | |
| }; | ||
| let frag = format!(" AND {sk_col}{collate} {sql_op} ${param_idx}"); | ||
| *param_idx += 1; | ||
| SkSqlInfo { fragment: frag } | ||
| Ok(SkSqlInfo { fragment: frag }) | ||
| } | ||
| SortKeyCondition::Between { .. } => { | ||
| let frag = format!( | ||
|
|
@@ -111,9 +112,9 @@ pub(crate) fn build_sk_sql( | |
| hi = *param_idx + 1 | ||
| ); | ||
| *param_idx += 2; | ||
| SkSqlInfo { fragment: frag } | ||
| Ok(SkSqlInfo { fragment: frag }) | ||
| } | ||
| SortKeyCondition::BeginsWith { .. } => { | ||
| SortKeyCondition::BeginsWith { prefix, .. } => { | ||
| let is_binary = sk_col == "sk_b" || sk_col.ends_with("_b"); | ||
| if is_binary { | ||
| // For binary sort keys, bind both prefix and incremented-prefix | ||
|
|
@@ -125,22 +126,50 @@ pub(crate) fn build_sk_sql( | |
| hi = *param_idx + 1 | ||
| ); | ||
| *param_idx += 2; | ||
| SkSqlInfo { fragment: frag } | ||
| } else { | ||
| // For string sort keys, append the max Unicode code point. | ||
| // With COLLATE "C" (byte order), prefix || chr(1114111) is | ||
| // strictly greater than any string starting with prefix. | ||
| Ok(SkSqlInfo { fragment: frag }) | ||
| } else if string_prefix_bounded(prefix, sk_col, maps)? { | ||
| // For string sort keys the exclusive upper bound is the next | ||
| // prefix in code point order, computed in Rust (see | ||
| // bind_sk_condition). With COLLATE "C" the comparison is in | ||
| // byte order, which is code point order. | ||
| let frag = format!( | ||
| " AND {sk_col}{collate} >= ${p} AND {sk_col}{collate} < (${p} || chr(1114111))", | ||
| p = *param_idx | ||
| " AND {sk_col}{collate} >= ${lo} AND {sk_col}{collate} < ${hi}", | ||
| lo = *param_idx, | ||
| hi = *param_idx + 1 | ||
| ); | ||
| *param_idx += 2; | ||
| Ok(SkSqlInfo { fragment: frag }) | ||
| } else { | ||
| // A prefix of only U+10FFFF has no upper bound. | ||
| let frag = format!(" AND {sk_col}{collate} >= ${p}", p = *param_idx); | ||
| *param_idx += 1; | ||
| SkSqlInfo { fragment: frag } | ||
| Ok(SkSqlInfo { fragment: frag }) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Whether a string `begins_with` prefix has a finite upper bound, so the SQL | ||
| /// carries a placeholder for it. [`bind_sk_condition`] binds that bound. | ||
| /// | ||
| /// A numeric prefix on a numeric column is refused here: it passes `parse_sk` | ||
| /// in [`bind_sk_condition`] and would bind one value for two placeholders. | ||
| /// Other non-string prefixes are refused by `parse_sk` before anything is | ||
| /// bound. | ||
| fn string_prefix_bounded( | ||
| prefix: &Expr, | ||
| sk_col: &str, | ||
| maps: &ExpressionMaps, | ||
| ) -> Result<bool, StorageError> { | ||
| match resolve_expr_to_av(prefix, maps)? { | ||
| AttributeValue::S(s) => Ok(string_prefix_upper_bound(&s).is_some()), | ||
| AttributeValue::N(_) if sk_col.ends_with("_n") => Err(StorageError::Internal( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since #429, |
||
| "begins_with is not supported on numeric sort keys".to_owned(), | ||
| )), | ||
| _ => Ok(true), | ||
| } | ||
| } | ||
|
|
||
| /// Execute a query SQL statement with dynamic parameter binding. | ||
| /// | ||
| /// # Parameter binding order: | ||
|
|
@@ -249,7 +278,14 @@ fn bind_sk_condition<'q>( | |
| let q = bind_sk_value(query, &sk); | ||
| Ok(q.bind(upper)) | ||
| } else { | ||
| Ok(bind_sk_value(query, &sk)) | ||
| let q = bind_sk_value(query, &sk); | ||
| match &sk { | ||
| SortKeyValue::S(p) => match string_prefix_upper_bound(p) { | ||
| Some(upper) => Ok(q.bind(upper)), | ||
| None => Ok(q), | ||
| }, | ||
| _ => Ok(q), | ||
| } | ||
| } | ||
| } | ||
| SortKeyCondition::Between { low, high, .. } => { | ||
|
|
@@ -359,3 +395,48 @@ pub(crate) async fn execute_scan_sql( | |
| pub(crate) fn build_key(item: &Item, key_schema: &[KeySchemaElement]) -> Item { | ||
| extract_key(item, key_schema) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use std::collections::HashMap; | ||
|
|
||
| use extenddb_core::expression::{Expr, ExpressionMaps, PathElement, SortKeyCondition}; | ||
| use extenddb_core::types::AttributeValue; | ||
|
|
||
| use super::build_sk_sql; | ||
|
|
||
| fn begins_with(value: AttributeValue) -> (SortKeyCondition, ExpressionMaps) { | ||
| let cond = SortKeyCondition::BeginsWith { | ||
| path: vec![PathElement::Attribute("sk".to_owned())], | ||
| prefix: Expr::Placeholder(":v".to_owned()), | ||
| }; | ||
| let maps = ExpressionMaps::new(HashMap::new(), HashMap::from([(":v".to_owned(), value)])); | ||
| (cond, maps) | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_string_prefix_gets_an_upper_bound_placeholder_unless_it_has_none() { | ||
| let (cond, maps) = begins_with(AttributeValue::S("ab".to_owned())); | ||
| let mut idx = 2; | ||
| let info = build_sk_sql(&cond, "sk_s", &maps, &mut idx).unwrap(); | ||
| assert_eq!( | ||
| info.fragment, | ||
| " AND sk_s COLLATE \"C\" >= $2 AND sk_s COLLATE \"C\" < $3" | ||
| ); | ||
| assert_eq!(idx, 4); | ||
|
|
||
| let (cond, maps) = begins_with(AttributeValue::S("\u{10FFFF}".to_owned())); | ||
| let mut idx = 2; | ||
| let info = build_sk_sql(&cond, "sk_s", &maps, &mut idx).unwrap(); | ||
| assert_eq!(info.fragment, " AND sk_s COLLATE \"C\" >= $2"); | ||
| assert_eq!(idx, 3); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_numeric_prefix_on_a_numeric_column_is_refused_before_any_sql() { | ||
| let (cond, maps) = begins_with(AttributeValue::N("1".to_owned())); | ||
| let mut idx = 2; | ||
| assert!(build_sk_sql(&cond, "sk_n", &maps, &mut idx).is_err()); | ||
| assert_eq!(idx, 2); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An empty prefix takes this branch too and reaches storage, so "a prefix of only U+10FFFF" can say "an empty prefix or one of only U+10FFFF". The same comment is in
storage-sqlite/src/data/query.rs:108.