aws-sdk-java-v2 icon indicating copy to clipboard operation
aws-sdk-java-v2 copied to clipboard

Setting the limit in QueryRequest has no effect

Open cct-dai-sato opened this issue 6 years ago • 5 comments

Describe the issue

Setting the limit in QueryRequest has no effect

Steps to Reproduce

   val dynamo = 
     DynamoDbAsyncClient.builder

       .region(...)

       .credentialsProvider(...)
  
       .httpClient(NettyNioAsyncHttpClient.builder().readTimeout(Duration.ofSeconds(60)).build()).build()

   val request =
     QueryRequest

       .builder()
  
       .tableName(...)

       .keyConditionExpression(...)

       .filterExpression(...)
       .expressionAttributeNames(
...)

       .expressionAttributeValues(
...)

       .limit(1)
       .build()

   dynamo.queryPaginator(request)

Expected Behavior

The number of query results should not exceed the limit specified in the query request

Current Behavior

The number of query results exceeds the limit(i.e. > 1)

Your Environment

  • AWS Java SDK version used: 2.10.22
  • JDK version used: 8
  • Operating System and version: Windows 10 Pro / 18362

cct-dai-sato avatar May 01 '20 12:05 cct-dai-sato

Hi @cct-dai-sato, thank you for reaching out. Yes, I can reproduce the issue using queryPaginator. Using the regular dynamo.query(request) works as expected though, it returns one item.

Will investigate the cause for the paginator issue, marking as a bug.

debora-ito avatar May 06 '20 19:05 debora-ito

Hi @cct-dai-sato, I have investigated our code and the reason of this issue is that QueryRequest.Limit() only limits the maximum number of items evaluated in each page, and queryPaginator fetches all the pages automatically. Consequently, there will only be 1 item for each page when QueryRequest.Limit() is set as 1, but we will still get all the pages (all the results for this query operation) by the queryPaginator. Normally, it would be suggested to use DynamoDbAsyncClient.query() to do query operations if you need more control on them.

Moreover, you can also add limitation to the number of items you get by the limit() method of Stream for sync DynamoDbClient and SdkPublisher for async DynamoDbAsyncClient. Here are two examples of them: Sync:

Stream<Map<String, AttributeValue>> oneThousandItemsMaximum =
    dynamoDbClient.queryPaginator(null)
                  .items()
                  .stream()
                  .limit(1000);

Async:

SdkPublisher<Map<String, AttributeValue>> limit = 
    dynamoDbAsyncClient.queryPaginator(request)
                       .items()
                       .limit(1000);

We will adjust our javadoc as well to make this less misunderstanding.

Quanzzzz avatar May 15 '20 00:05 Quanzzzz

@Quanzzzz @debora-ito Hi, isn't it a bug or bad customer experience? What is the use case of setting limit on QueryRequest and intended to limit page result not final result? Could you pleas suggest a way to limit the number of result using DynamoDbEnhancedClient ? Thanks!

HHLLQQZZ avatar Jun 10 '20 18:06 HHLLQQZZ

Hi, we've updated the pagination page in the AWS SDK for Java 2.x Developer Guide. The limit parameter on a QueryRequest sets the page size, not the total number of items, and the paginator keeps fetching pages until none are left. The new sections show how to limit the total number of items:

  • Synchronous (Stream.limit): https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/pagination.html#pagination-limit-results-sync
  • Asynchronous (SdkPublisher.limit): https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/pagination.html#pagination-limit-results-async

Closing this issue, as the behavior is now documented. Feel free to open a new issue if you have other issues.

adev-code avatar Oct 14 '26 18:10 adev-code

This issue is now closed. Comments on closed issues are hard for our team to see. If you need more assistance, please open a new issue that references this one.

github-actions[bot] avatar Oct 14 '26 18:10 github-actions[bot]