Skip to content

Commit 9dfaa99

Browse files
committed
feat(stack-encrypt): compile a jsonpath predicate into one JSON document operand
`match` takes what `jsonb_path_match` takes, `{"predicate", "vars"?, "wildcard_bound"?}`, and derives the `eql_v4.query_json_predicate` operand. A hand-written parser reads the subset of SQL/JSON path predicates the terms can answer: comparisons of a path (`$.a.b`, `$.a[*].b`, `$."a b"`) with a variable or a literal, `exists(path)`, `exists(path ? (@.x == $v && @.y > $w))`, `&&`, `||`, `!` and parentheses. Everything else is refused at the byte it starts, as `Error::Predicate`, naming the construct in the grammar's words and never quoting the text, whose keys and values can be customer data. Each comparison is compiled onto an existing operator's input and derived by that operator's code: `==` to `eq_at`'s needle, `!=` to a `selector` and not the needle, an ordering to a `selector`, not the `null` needle, and the ordering's leaf, so an absent path and a JSON null answer false as jsonpath has them; an ordering at a wildcard and a filter of more than one condition to `element_match`, a filter of one equality to `eq_at`. No new derivation. `!=` at a wildcard, which asks whether some element differs, is refused. New reasons: `json_predicate_syntax`, `json_predicate_unsupported`, `json_predicate_unknown_variable`, `json_predicate_value_type`, `json_predicate_not_equal_at_wildcard` and `json_predicate_too_deep`. The operator is listed only on a type that names the domain (`TargetDescriptor::with_predicate_query_sql_domain`), so a host that does not, such as the Go guest today, sees no new operator. Refs #1441
1 parent d02588e commit 9dfaa99

10 files changed

Lines changed: 3006 additions & 44 deletions

File tree

‎languages/golang/encrypt/guest/src/targets.rs‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,35 @@ mod tests {
268268
));
269269
}
270270

271+
/// The JSON document's query domains this guest names, and so the JSON
272+
/// operators the Go SDK is offered: the leaf operand, for the orderings
273+
/// at a path, and not the element-scoped or compiled predicate, whose
274+
/// operators (`element_match`, `match`) the Go side does not parse yet.
275+
/// Naming one is a change to the Go SDK's operator list: update this
276+
/// test with the Go side that takes the operator.
277+
#[cfg(feature = "eql")]
278+
#[test]
279+
fn a_document_names_the_leaf_domain_and_not_the_predicate_domains() {
280+
let targets = resolver().targets();
281+
let document = targets
282+
.iter()
283+
.find(|t| t.name == "JsonSearch")
284+
.expect("JsonSearch");
285+
assert_eq!(
286+
document.leaf_query_sql_domain.as_deref(),
287+
Some("eql_v4.query_json_leaf")
288+
);
289+
assert_eq!(document.elements_query_sql_domain, None);
290+
assert_eq!(document.predicate_query_sql_domain, None);
291+
let operators = document.operators();
292+
for absent in [
293+
stack_encrypt::dynamic::OpSpec::ElementMatch,
294+
stack_encrypt::dynamic::OpSpec::Match,
295+
] {
296+
assert!(!operators.contains(&absent), "{absent:?}: {operators:?}");
297+
}
298+
}
299+
271300
#[cfg(feature = "eql")]
272301
#[test]
273302
fn eql_refusals_convert_to_the_engines_with_the_field_left_to_the_lowering() {

‎packages/stack-encrypt/src/codes.rs‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,12 @@ mod tests {
212212
Error::UntypedIndex { .. } => Error::UntypedIndex { field: name() },
213213
Error::Source { .. } => Error::bad_source(Reason::FieldMissing),
214214
Error::Record { .. } => Error::bad_record(Reason::NoCiphertextNode),
215+
Error::Predicate { .. } => Error::Predicate {
216+
field: Some(name()),
217+
reason: Reason::JsonPredicateUnsupported,
218+
construct: "`like_regex`",
219+
at: 4,
220+
},
215221
Error::Internal => Error::Internal,
216222
Error::Target(_) => Error::Target(TargetError::NoTargets { name: target() }),
217223
Error::Cipher(_) => Error::Cipher(crate::Error::Aead),
@@ -596,6 +602,23 @@ mod tests {
596602
Box::new(Error::UntypedIndex { field: name() }),
597603
json!({ "field": "email" }),
598604
),
605+
(
606+
Box::new(
607+
Error::Predicate {
608+
field: None,
609+
reason: Reason::JsonPredicateUnsupported,
610+
construct: "`like_regex`",
611+
at: 4,
612+
}
613+
.in_field("email"),
614+
),
615+
json!({
616+
"field": "email",
617+
"reason": "json_predicate_unsupported",
618+
"construct": "`like_regex`",
619+
"at": 4
620+
}),
621+
),
599622
(Box::new(Error::Internal), json!({})),
600623
(
601624
Box::new(TargetError::NoTargets { name: target() }),

0 commit comments

Comments
 (0)