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
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "bugfix",
"category": "Amazon DynamoDB Enhanced Client",
"description": "Fix projection expression placeholders for list-index attributes in the enhanced DynamoDB client. Resolves [#7102](https://github.com/aws/aws-sdk-java-v2/issues/7102).",
"contributor": "Arnab"
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@
public class ProjectionExpression {

private static final String AMZN_MAPPED = "#AMZN_MAPPED_";
private static final UnaryOperator<String> PROJECTION_EXPRESSION_KEY_MAPPER = k -> AMZN_MAPPED + cleanAttributeName(k);
private static final UnaryOperator<String> PROJECTION_EXPRESSION_KEY_MAPPER =
k -> AMZN_MAPPED + cleanAttributeName(attributeNameWithoutListDereference(k));

private final Optional<String> projectionExpressionAsString;
private final Map<String, String> expressionAttributeNames;
Expand Down Expand Up @@ -132,6 +133,7 @@ private static Map<String, String> createAttributePlaceholders(List<NestedAttrib
Map<String, List<String>> placeholderToAttributeNames =
nestedAttributeNames.stream()
.flatMap(n -> n.elements().stream())
.map(ProjectionExpression::attributeNameWithoutListDereference)
.distinct()
.collect(Collectors.groupingBy(PROJECTION_EXPRESSION_KEY_MAPPER, Collectors.toList()));

Expand Down Expand Up @@ -180,8 +182,45 @@ private static String convertToNameExpression(NestedAttributeName nestedAttribut
Map<String, String> attributeToSanitizedMap) {
return nestedAttributeName.elements()
.stream()
.map(attributeToSanitizedMap::get)
.map(element -> attributeToSanitizedMap.get(attributeNameWithoutListDereference(element))
+ listDereferenceSuffix(element))
.collect(Collectors.joining("."));
}

private static String attributeNameWithoutListDereference(String attributeName) {
int firstListDereferenceIndex = firstListDereferenceIndex(attributeName);
return firstListDereferenceIndex == -1 ? attributeName : attributeName.substring(0, firstListDereferenceIndex);
}

private static String listDereferenceSuffix(String attributeName) {
int firstListDereferenceIndex = firstListDereferenceIndex(attributeName);
return firstListDereferenceIndex == -1 ? "" : attributeName.substring(firstListDereferenceIndex);
}

private static int firstListDereferenceIndex(String attributeName) {
int position = attributeName.length();
int firstListDereferenceIndex = position;

while (position > 0 && attributeName.charAt(position - 1) == ']') {
int openBracketIndex = attributeName.lastIndexOf('[', position - 1);
if (openBracketIndex == -1 || openBracketIndex == position - 1 ||
!containsOnlyDigits(attributeName, openBracketIndex + 1, position - 1)) {
return -1;
}
firstListDereferenceIndex = openBracketIndex;
position = openBracketIndex;
}

return firstListDereferenceIndex == attributeName.length() ? -1 : firstListDereferenceIndex;
}

private static boolean containsOnlyDigits(String value, int beginIndex, int endIndex) {
for (int i = beginIndex; i < endIndex; i++) {
if (!Character.isDigit(value.charAt(i))) {
return false;
}
}
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class ProjectionExpressionTest {
static {
EXPECTED_ATTRIBUTE_NAMES.put("#AMZN_MAPPED_attribute", "attribute");
EXPECTED_ATTRIBUTE_NAMES.put("#AMZN_MAPPED_Attribute", "Attribute");
EXPECTED_ATTRIBUTE_NAMES.put("#AMZN_MAPPED_firstiteminlist[0]", "firstiteminlist[0]");
EXPECTED_ATTRIBUTE_NAMES.put("#AMZN_MAPPED_firstiteminlist", "firstiteminlist");
EXPECTED_ATTRIBUTE_NAMES.put("#AMZN_MAPPED_March_2021", "March-2021");
EXPECTED_ATTRIBUTE_NAMES.put("#AMZN_MAPPED_Why_Make_This_An_Attribute_Name", "Why.Make-This*An:Attribute:Name");
}
Expand Down Expand Up @@ -104,6 +104,23 @@ public void nonUniquePlaceholders_AreDisambiguated() {
assertProjectionExpression(attributeNames, expectedAttributeNames, expectedProjectionExpression);
}

@Test
public void listDereferenceAttributes_AreMappedToAttributeName() {
Map<String, String> expectedAttributeNames = new HashMap<>();
expectedAttributeNames.put("#AMZN_MAPPED_tags", "tags");
expectedAttributeNames.put("#AMZN_MAPPED_metadata", "metadata");
expectedAttributeNames.put("#AMZN_MAPPED_values", "values");

List<NestedAttributeName> attributeNames = Arrays.asList(
NestedAttributeName.create("tags[0]"),
NestedAttributeName.create("metadata", "values[1][2]")
);

String expectedProjectionExpression = "#AMZN_MAPPED_tags[0],#AMZN_MAPPED_metadata.#AMZN_MAPPED_values[1][2]";

assertProjectionExpression(attributeNames, expectedAttributeNames, expectedProjectionExpression);
}

private void assertProjectionExpression(List<NestedAttributeName> attributeNames,
Map<String, String> expectedAttributeNames,
String expectedProjectionExpression) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,25 @@ public void generateRequest_projectionExpression() {
assertThat(request, is(expectedRequest));
}

@Test
public void generateRequest_projectionExpressionWithListDereference() {
ScanOperation<FakeItem> operation = ScanOperation.create(
ScanEnhancedRequest.builder()
.addAttributeToProject("tags[0]")
.build()
);
ScanRequest request = operation.generateRequest(FakeItem.getTableSchema(),
PRIMARY_CONTEXT,
null);

ScanRequest expectedRequest = ScanRequest.builder()
.tableName(TABLE_NAME)
.projectionExpression("#AMZN_MAPPED_tags[0]")
.expressionAttributeNames(singletonMap("#AMZN_MAPPED_tags", "tags"))
.build();
assertThat(request, is(expectedRequest));
}

@Test
public void generateRequest_hashKeyOnly_exclusiveStartKey() {
FakeItem exclusiveStartKey = createUniqueFakeItem();
Expand Down