fix(dynamodb): fix FilterExpression for BOOL types, List/Set contains, and nested attribute paths (#137)

* fix(dynamodb): fix FilterExpression handling for BOOL, List/Set contains, and nested attribute paths

- Add BOOL type support to extractScalarValue() so comparison operators
  like <> work correctly with boolean attributes
- Extend contains() to support L (List), SS (String Set), and NS
  (Number Set) types instead of only scalar string substring matching
- Support dotted attribute paths in attribute_exists/attribute_not_exists
  by navigating through DynamoDB Map (M) structure

Fixes #126
This commit is contained in:
nakano16180 authored and GitHub committed 2026-04-01 00:59:04 -05:00
1 parent 6bbc9d9375
commit 453555a124
2 files changed
+350 -9

No files matched your search

@@ -1072,15 +1072,13 @@ public class DynamoDbService {
if (condLower.startsWith("attribute_exists")) {
String attr = extractFunctionArg(condition);
String attrName = resolveAttributeName(attr, exprAttrNames);
// If item is null, attribute cannot exist
return item != null && item.has(attrName);
String resolvedPath = resolveAttributePath(attr, exprAttrNames);
return item != null && resolveNestedAttribute(item, resolvedPath) != null;
}
if (condLower.startsWith("attribute_not_exists")) {
String attr = extractFunctionArg(condition);
String attrName = resolveAttributeName(attr, exprAttrNames);
// If item is null, attribute definitely doesn't exist
return item == null || !item.has(attrName);
String resolvedPath = resolveAttributePath(attr, exprAttrNames);
return item == null || resolveNestedAttribute(item, resolvedPath) == null;
}
if (condLower.startsWith("begins_with")) {
String[] args = extractFunctionArgs(condition);
@@ -1096,9 +1094,54 @@ public class DynamoDbService {
String[] args = extractFunctionArgs(condition);
if (args.length == 2) {
String attrName = resolveAttributeName(args[0], exprAttrNames);
String substring = resolveExprValue(args[1], exprAttrValues);
String actual = item != null ? extractScalarValue(item.get(attrName)) : null;
return actual != null && substring != null && actual.contains(substring);
if (item == null) return false;
JsonNode attrNode = item.get(attrName);
if (attrNode == null) return false;
// Resolve the raw AttributeValue node for type-aware comparisons
JsonNode searchAttrValue = exprAttrValues != null
? exprAttrValues.get(args[1].trim()) : null;
if (searchAttrValue == null) return false;
// List type: type-aware element membership check
if (attrNode.has("L")) {
for (JsonNode element : attrNode.get("L")) {
if (attributeValuesEqual(element, searchAttrValue)) return true;
}
return false;
}
// SS (String Set): operand must be S type
if (attrNode.has("SS")) {
if (!searchAttrValue.has("S")) return false;
String target = searchAttrValue.get("S").asText();
for (JsonNode element : attrNode.get("SS")) {
if (target.equals(element.asText())) return true;
}
return false;
}
// NS (Number Set): operand must be N type, compare numerically
if (attrNode.has("NS")) {
if (!searchAttrValue.has("N")) return false;
try {
java.math.BigDecimal target = new java.math.BigDecimal(searchAttrValue.get("N").asText());
for (JsonNode element : attrNode.get("NS")) {
if (target.compareTo(new java.math.BigDecimal(element.asText())) == 0) return true;
}
} catch (NumberFormatException ignored) {}
return false;
}
// BS (Binary Set): operand must be B type
if (attrNode.has("BS")) {
if (!searchAttrValue.has("B")) return false;
String target = searchAttrValue.get("B").asText();
for (JsonNode element : attrNode.get("BS")) {
if (target.equals(element.asText())) return true;
}
return false;
}
// String type: operand must be S type, check substring
if (attrNode.has("S") && searchAttrValue.has("S")) {
return attrNode.get("S").asText().contains(searchAttrValue.get("S").asText());
}
return false;
}
return false;
}
@@ -1139,6 +1182,52 @@ public class DynamoDbService {
return placeholder;
}
private boolean attributeValuesEqual(JsonNode a, JsonNode b) {
if (a == null || b == null) return a == b;
// Scalar types: S, B, BOOL, NULL
for (String type : new String[]{"S", "B", "BOOL", "NULL"}) {
if (a.has(type) && b.has(type)) {
return a.get(type).asText().equals(b.get(type).asText());
}
if (a.has(type) || b.has(type)) return false; // type mismatch
}
// Numeric comparison with normalization
if (a.has("N") && b.has("N")) {
try {
return new java.math.BigDecimal(a.get("N").asText())
.compareTo(new java.math.BigDecimal(b.get("N").asText())) == 0;
} catch (NumberFormatException e) {
return false;
}
}
if (a.has("N") || b.has("N")) return false;
// Map type: compare all entries recursively
if (a.has("M") && b.has("M")) {
JsonNode aMap = a.get("M");
JsonNode bMap = b.get("M");
if (aMap.size() != bMap.size()) return false;
var fields = aMap.fields();
while (fields.hasNext()) {
var entry = fields.next();
if (!bMap.has(entry.getKey())) return false;
if (!attributeValuesEqual(entry.getValue(), bMap.get(entry.getKey()))) return false;
}
return true;
}
// List type: compare element by element
if (a.has("L") && b.has("L")) {
JsonNode aList = a.get("L");
JsonNode bList = b.get("L");
if (aList.size() != bList.size()) return false;
for (int i = 0; i < aList.size(); i++) {
if (!attributeValuesEqual(aList.get(i), bList.get(i))) return false;
}
return true;
}
// Different types are never equal
return false;
}
private int compareValues(String a, String b) {
// Try numeric comparison first
try {
@@ -1148,6 +1237,48 @@ public class DynamoDbService {
}
}
private static final String DOT_ESCAPE = "\uFF0E";
private String resolveAttributePath(String path, JsonNode exprAttrNames) {
// Resolve each segment of a dotted path, e.g. "passengerInformation.#name"
// ExpressionAttributeNames may resolve to names containing dots (e.g. "#a" -> "foo.bar").
// Escape those dots so resolveNestedAttribute treats them as single keys.
String[] segments = path.split("\\.");
StringBuilder resolved = new StringBuilder();
for (int i = 0; i < segments.length; i++) {
if (i > 0) resolved.append(".");
String original = segments[i];
String resolvedSegment = resolveAttributeName(original, exprAttrNames);
if (original.startsWith("#") && resolvedSegment != null) {
resolvedSegment = resolvedSegment.replace(".", DOT_ESCAPE);
}
resolved.append(resolvedSegment);
}
return resolved.toString();
}
private JsonNode resolveNestedAttribute(JsonNode item, String path) {
// Navigate a dotted path through DynamoDB's {"M": {...}} structure
String[] segments = path.split("\\.");
JsonNode current = item;
for (int i = 0; i < segments.length; i++) {
if (current == null) return null;
String segment = segments[i].replace(DOT_ESCAPE, ".");
if (i == 0) {
// First segment: resolve against the top-level item map
current = current.get(segment);
} else {
// Subsequent segments: descend into DynamoDB Map type
if (current.has("M")) {
current = current.get("M").get(segment);
} else {
current = current.get(segment);
}
}
}
return current;
}
private String extractFunctionArg(String funcCall) {
int open = funcCall.indexOf('(');
int close = funcCall.lastIndexOf(')');
@@ -1230,6 +1361,7 @@ public class DynamoDbService {
if (attrValue.has("S")) return attrValue.get("S").asText();
if (attrValue.has("N")) return attrValue.get("N").asText();
if (attrValue.has("B")) return attrValue.get("B").asText();
if (attrValue.has("BOOL")) return attrValue.get("BOOL").asText();
return attrValue.asText();
}