Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions crates/core/src/expression/key_condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ use crate::types::{
KeySchemaElement, KeyType, hash_key_elements, is_multipart_key_schema, range_key_elements,
};

mod no_hash_equality;
mod references;

/// Parsed key condition: partition key value expression(s) and optional sort key condition.
///
/// For standard (single-attribute) keys, `pk_path`/`pk_value` hold the single PK condition
Expand All @@ -30,7 +33,10 @@ use crate::types::{
/// `extra_pk_conditions` holds additional PK equality conditions.
#[derive(Debug, Clone)]
pub struct KeyCondition {
/// First partition key attribute path (resolved name).
/// First partition key attribute path (resolved name). Empty when the
/// expression held one or two clauses and none is an equality; they are
/// then `sk_condition` and `extra_sk_conditions`, and
/// [`KeyCondition::refuse_without_hash_equality`] refuses them.
pub pk_path: Vec<PathElement>,
/// First partition key value expression (placeholder or literal).
pub pk_value: Expr,
Expand Down Expand Up @@ -681,17 +687,17 @@ fn classify_single(clause: RawClause) -> Result<KeyCondition, DynamoDbError> {
sk_condition: None,
extra_sk_conditions: Vec::new(),
}),
_ => Err(validation_err(
"Invalid KeyConditionExpression: partition key condition must use equality (=)",
)),
// The wording of the refusal depends on the key schema, which is not
// known here. See `KeyCondition::refuse_without_hash_equality`.
other => Ok(KeyCondition::without_equality(raw_to_sk(other), None)),
}
}

fn classify_pair(a: RawClause, b: RawClause) -> Result<KeyCondition, DynamoDbError> {
// When both clauses are Eq, we assign the first as PK and second as SK.
// The caller must call `resolve_pk_sk()` after parsing to correct the
// assignment using the table's key schema (REQ-QUERY-002).
// One must be EQ (the PK), the other is the SK condition
// One is EQ (the PK) and the other is the SK condition
match (a, b) {
(RawClause::Eq(pk_path, pk_value), sk) | (sk, RawClause::Eq(pk_path, pk_value)) => {
Ok(KeyCondition {
Expand All @@ -702,8 +708,11 @@ fn classify_pair(a: RawClause, b: RawClause) -> Result<KeyCondition, DynamoDbErr
extra_sk_conditions: Vec::new(),
})
}
_ => Err(validation_err(
"Invalid KeyConditionExpression: one condition must be an equality on the partition key",
// Neither clause is an equality. As for a single clause, the key
// schema decides the wording. See `KeyCondition::refuse_without_hash_equality`.
(a, b) => Ok(KeyCondition::without_equality(
raw_to_sk(a),
Some(raw_to_sk(b)),
)),
}
}
Expand Down
Loading
Loading