pabloem commented on code in PR #22926:
URL: https://github.com/apache/beam/pull/22926#discussion_r956758177


##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/BigQueryIO.java:
##########
@@ -1232,22 +1232,49 @@ void cleanup(PassThroughThenCleanup.ContextContainer c) 
throws Exception {
       return rows;
     }
 
-    private PCollection<T> expandForDirectRead(PBegin input, Coder<T> 
outputCoder) {
+    private PCollection<T> expandForDirectRead(
+        PBegin input, Coder<T> outputCoder, boolean beamSchemaEnabled) {
       ValueProvider<TableReference> tableProvider = getTableProvider();
       Pipeline p = input.getPipeline();
+      BigQuerySourceDef srcDef = createSourceDef();
       if (tableProvider != null) {
         // No job ID is required. Read directly from BigQuery storage.
-        return p.apply(
-            org.apache.beam.sdk.io.Read.from(
-                BigQueryStorageTableSource.create(
-                    tableProvider,
-                    getFormat(),
-                    getSelectedFields(),
-                    getRowRestriction(),
-                    getParseFn(),
-                    outputCoder,
-                    getBigQueryServices(),
-                    getProjectionPushdownApplied())));
+        PCollection<T> rows =
+            p.apply(
+                org.apache.beam.sdk.io.Read.from(
+                    BigQueryStorageTableSource.create(
+                        tableProvider,
+                        getFormat(),
+                        getSelectedFields(),
+                        getRowRestriction(),
+                        getParseFn(),
+                        outputCoder,
+                        getBigQueryServices(),
+                        getProjectionPushdownApplied())));
+        if (beamSchemaEnabled) {
+          BigQueryOptions bqOptions = p.getOptions().as(BigQueryOptions.class);
+          Schema beamSchema = srcDef.getBeamSchema(bqOptions);
+
+          List<Schema.Field> flds =
+              beamSchema.getFields().stream()
+                  .filter(
+                      field -> {
+                        if (getSelectedFields() != null
+                            && getSelectedFields().isAccessible()
+                            && getSelectedFields().get() != null) {
+                          return 
getSelectedFields().get().contains(field.getName());
+                        } else {
+                          return true;
+                        }
+                      })
+                  .collect(Collectors.toList());
+
+          beamSchema = Schema.builder().addFields(flds).build();
+          SerializableFunction<T, Row> toBeamRow = 
getToBeamRowFn().apply(beamSchema);
+          SerializableFunction<Row, T> fromBeamRow = 
getFromBeamRowFn().apply(beamSchema);

Review Comment:
   yes right! It relies on BQUtils to generate a converter function.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to