Skip to content

Allow string value for select for enhanced query request #5869

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

Merged
Show file tree
Hide file tree
Changes from 2 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": "feature",
"category": "Amazon DynamoDB Enhanced Client",
"contributor": "",
"description": "Use String instead of Select enum for ProjectionExpression to support future values"
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.ConsumedCapacity;
import software.amazon.awssdk.services.dynamodb.model.ReturnConsumedCapacity;
import software.amazon.awssdk.services.dynamodb.model.Select;

public class ScanQueryIntegrationTest extends DynamoDbEnhancedIntegrationTestBase {

Expand Down Expand Up @@ -188,4 +189,40 @@ private Map<String, AttributeValue> getKeyMap(int sort) {
result.put("sort", numberValue(RECORDS.get(sort).getSort()));
return Collections.unmodifiableMap(result);
}

@Test
public void query_withStringSelect_returnsSpecifiedAttributes() {
insertRecords();

QueryEnhancedRequest request = QueryEnhancedRequest.builder()
.queryConditional(sortBetween(k -> k.partitionValue("id-value").sortValue(2),
k -> k.partitionValue("id-value").sortValue(6)))
.select("ALL_ATTRIBUTES")
.build();

Iterator<Page<Record>> results = mappedTable.query(request).iterator();

while (results.hasNext()) {
Page<Record> page = results.next();
for (Record record : page.items()) {
assertThat(record.getId(), is(notNullValue()));
assertThat(record.getSort(), is(notNullValue()));
assertThat(record.getValue(), is(notNullValue()));
assertThat(record.getStringAttribute(), is(notNullValue()));
}
}
}

@Test
public void query_withInvalidStringSelect_returnsUnknown() {
insertRecords();

QueryEnhancedRequest request = QueryEnhancedRequest.builder()
.queryConditional(sortBetween(k -> k.partitionValue("id-value").sortValue(2),
k -> k.partitionValue("id-value").sortValue(6)))
.select("INVALID_SELECT")
.build();

assertThat(request.select(), is(equalTo(Select.UNKNOWN_TO_SDK_VERSION)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,17 @@ public Builder select(Select select) {
return this;
}

/**
* Determines the attributes to be returned in the result. See {@link Select} for examples and constraints.
* If not found, returns {@link Select#UNKNOWN_TO_SDK_VERSION}.
* @param select the selection criteria as a string
* @return a builder of this type
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this API doc seems to be copied from a getter, not setter? Can we update it

public Builder select(String select) {
this.select = Select.fromValue(select);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to change the select variable to string and pass it to the low level client to make it actually work. If customers pass an unsupported string, it'd be initialized as UNKNOWN_TO_SDK_VERSION

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed the implementation accordingly, please re-check - thanks!

return this;
}

/**
* The primary key of the first item that this operation will evaluate. By default, the operation will evaluate
* the whole dataset. If used, normally this parameter is populated with the value that was returned for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;

import java.util.*;
import software.amazon.awssdk.services.dynamodb.model.Select;

import static java.util.Collections.singletonMap;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
Expand Down Expand Up @@ -183,4 +184,12 @@ public void toBuilder() {
assertThat(copiedObject, is(builtObject));
}

@org.junit.jupiter.api.Test
public void testSelectWithStringInput() {
QueryEnhancedRequest request = QueryEnhancedRequest.builder()
.select("ALL_ATTRIBUTES")
.build();

assertThat(Select.ALL_ATTRIBUTES, is(request.select()));
}
}