GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Iterating BigQuery query results produces incorrect number of rows · Issue #506 · GoogleCloudPlatform/java-docs-samples

Iterating BigQuery query results produces incorrect number of rows #506

New issue
Copy link
New issue
Copy link
Closed
Closed
Iterating BigQuery query results produces incorrect number of rows#506
Copy link

Description

@polleyg
polleyg
opened on Feb 8, 2017
Issue body actions

This is in relation to post on Stack Overflow post here.

As per the sample code here, a user should be be able to paginate the results of a BigQuery query using the following code structure:

while (result != null) {
      Iterator<List<FieldValue>> iter = result.iterateAll();
      while (iter.hasNext()) {
        List<FieldValue> row = iter.next();
        //do something with row/data
        System.out.println(row);
      }
      result = result.getNextPage();
    }
  }

However, when the result set is large (in my tests >31,000 rows), more rows are returned/iterated even though calling result.getTotalRows() returns the correct/expected number of rows.

For example, I have a table with 85,250 rows (9.45 MB). When I query it via the Java API, and use the code from the example above, it actually iterates 160,296 times.

Even if I limit the result set in the query using limit 5000, and set setPageSize(1000L), then it iterates 15,000 times e.g:

QueryRequest queryRequest = QueryRequest
                .newBuilder("SELECT * FROM [<my-project-id>:<dataset>.<table_with_85250_rows>] limit 5000")
                .setUseLegacySql(true)
                .setPageSize(1000L)
                .build();
        QueryResponse response = bigQuery.query(queryRequest);
        QueryResult result = response.getResult();
        System.out.println("Total rows: " + result.getTotalRows());
        Integer rowNumber = 1;
        while(result != null){
            Iterator<List<FieldValue>> iter = result.iterateAll();
            while(iter.hasNext()){
                List<FieldValue> row = iter.next();
                System.out.println("Row: " + rowNumber + ", with number of columns: " + row.size());
                rowNumber++;
            }
            result = result.getNextPage();
        }

Output:

Total rows: 5000
Row: 1, with number of columns: 11
Row: 2, with number of columns: 11
Row: 3, with number of columns: 11
Row: 4, with number of columns: 11
Row: 5, with number of columns: 11
Row: 6, with number of columns: 11
Row: 7, with number of columns: 11
Row: 8, with number of columns: 11
Row: 9, with number of columns: 11
Row: 10, with number of columns: 11
Row: 11, with number of columns: 11
[.....]
Row: 14997, with number of columns: 11
Row: 14998, with number of columns: 11
Row: 14999, with number of columns: 11
Row: 15000, with number of columns: 11

Activity

  1. kamakay commented on Feb 8, 2017

    @kamakay
    kamakay
    on Feb 8, 2017
    More actions

    Hi, I ve found the issue when querying BQ using "nested SQL queries" and legacy SQL. WEb interface UI states the amount N of rows retrieved that equals the number returned from
    result.getTotalRows()

  2. polleyg commented on Feb 10, 2017

    @polleyg
    polleyg
    on Feb 10, 2017
    Author
    More actions

    Moved this issues to the actual client lib issues - googleapis/google-cloud-java#1612

  3. polleyg
    closed this as completedon Feb 10, 2017
  4. ivanmkc
    added a commit that references this issue on Nov 3, 2022

    chore(deps): update dependency com.google.cloud.samples:shared-config…

    695b819
  5. averikitsch
    added a commit that references this issue on Nov 9, 2022

    chore(deps): update dependency com.google.cloud.samples:shared-config…

    b948cfe
  6. minherz
    added 3 commits that reference this issue on Nov 9, 2022

    chore: release 0.120.24 (#503)

    56cd3ad

    chore: release 0.120.24 (#503)

    1c260ef

    chore: release 0.120.24 (#503)

    7ddf8b2
  7. Shabirmean
    added a commit that references this issue on Nov 10, 2022

    chore (samples) Refactoring: added invalid catalog name in import pro…

    373f4ca
  8. olivi-eh
    added a commit that references this issue on Nov 11, 2022

    chore(deps): update dependency com.google.cloud:libraries-bom to v20 (#…

    807c130
  9. Shabirmean
    added 3 commits that reference this issue on Nov 15, 2022

    chore (samples) Refactoring: added invalid catalog name in import pro…

    ec2566e

    chore(deps): update dependency com.google.cloud.samples:shared-config…

    4ea60a0

    chore(deps): update dependency com.google.cloud.samples:shared-config…

    042b30b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.