Skip to content
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

Schema not propagated when querying for array of structs #3389

Open
FredrikMeyer opened this issue Jul 15, 2024 · 4 comments
Open

Schema not propagated when querying for array of structs #3389

FredrikMeyer opened this issue Jul 15, 2024 · 4 comments
Labels
api: bigquery Issues related to the googleapis/java-bigquery API.

Comments

@FredrikMeyer
Copy link

Environment details

  1. Specify the API at the beginning of the title. For example, "BigQuery: ...").
        implementation("com.google.cloud:google-cloud-bigquery:2.41.0")
  2. OS type and version: MacOS
  3. Java version: 21

Steps to reproduce

  1. Create a table with a repeated struct schema.
  2. Query the same table, try to access the nested record fields by name. This fails because no schema is attached.

Code example

Reproduced here: https://github.com/FredrikMeyer/bigquery-bug/blob/main/src/test/java/no/fredrikmeyer/MainTest.java

        var query = "select * from my_dataset.my_table";

        QueryJobConfiguration queryJobConfiguration = QueryJobConfiguration.newBuilder(query).setUseLegacySql(false).build();

        var res2 = bigQuery.query(queryJobConfiguration);

        res2.iterateAll().forEach(r -> {
            // Prints true
            System.out.println(r.hasSchema());
            // Prints false, even though the schema is defined above
            System.out.println(r.get("arr").getRepeatedValue().getFirst().getRecordValue().hasSchema());
        });

Any additional information below

When having an array of structs, the schema does not seem to be propagated to the fields. Thanks!

@product-auto-label product-auto-label bot added the api: bigquery Issues related to the googleapis/java-bigquery API. label Jul 15, 2024
@PhongChuong
Copy link
Contributor

Hi @FredrikMeyer ,

Schemas are stored in TableResult and not necessarily FieldValueList. FieldValueList is used for table rows and the schema is not assessable. The schema in FieldValueList is for indexing by field name.

To get the schema for your table, you can simply:

var res2 = bigQuery.query(queryJobConfiguration);

// Read query table schema.
System.out.println(r.hasSchema());
// Get the schema for the field "arr"
System.out.println(r.getSchema().getFields().get("arr"));

res2.iterateAll().forEach(r -> {
    // Read table row results.
});

I hope this helps,

@FredrikMeyer
Copy link
Author

FredrikMeyer commented Jul 17, 2024

Thanks for the answer @PhongChuong

My main problem is that since the schema is not in the FieldValueList, I cannot get the fields by name. So now my code looks like this:

    private fun periodeFromFieldValue(periodeRecord: FieldValue) = Periode(
        // cannot do recordValue.get("fraDato") because schema is not propagated
        fraDato = LocalDate.parse(periodeRecord.recordValue[0].stringValue),
        tilDato = LocalDate.parse(periodeRecord.recordValue[1].stringValue),
    )

Which seems to rely on the order the fields appear - which seem to coincide with the order they were defined in the table schema (by accident or by construction?).

@PhongChuong
Copy link
Contributor

The field will appear as they are defined in the table schema by construction.

If you want, it is possible to provide the schema manually providing the schema by creating FieldValueList. However, it's not pretty and probably not worth the effort.

@FredrikMeyer
Copy link
Author

// Read query table schema.
System.out.println(r.hasSchema());
// Get the schema for the field "arr"
System.out.println(r.getSchema().getFields().get("arr"));

res2.iterateAll().forEach(r -> {
// Read table row results.
});

Hi, sorry for the late reply, but is there really a method getSchema here? Here r is of type FieldValueList.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: bigquery Issues related to the googleapis/java-bigquery API.
Projects
None yet
Development

No branches or pull requests

2 participants