|
17 | 17 | */ |
18 | 18 | package org.apache.beam.sdk.io.gcp.bigquery; |
19 | 19 |
|
| 20 | +import static org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions.checkArgument; |
| 21 | + |
20 | 22 | import com.google.auto.value.AutoValue; |
21 | 23 | import java.io.Serializable; |
| 24 | +import java.util.List; |
22 | 25 | import org.apache.beam.sdk.schemas.AutoValueSchema; |
23 | 26 | import org.apache.beam.sdk.schemas.annotations.DefaultSchema; |
24 | 27 | import org.apache.beam.sdk.schemas.annotations.SchemaCreate; |
25 | 28 | import org.apache.beam.sdk.schemas.annotations.SchemaFieldName; |
| 29 | +import org.apache.beam.sdk.schemas.annotations.SchemaFieldNumber; |
26 | 30 | import org.checkerframework.checker.nullness.qual.Nullable; |
27 | 31 | import org.checkerframework.dataflow.qual.Pure; |
28 | 32 |
|
|
31 | 35 | @AutoValue |
32 | 36 | public abstract class BigQueryDynamicReadDescriptor implements Serializable { |
33 | 37 | @SchemaFieldName("query") |
| 38 | + @SchemaFieldNumber("0") |
34 | 39 | @Pure |
35 | 40 | abstract @Nullable String getQuery(); |
36 | 41 |
|
37 | 42 | @SchemaFieldName("table") |
| 43 | + @SchemaFieldNumber("1") |
38 | 44 | @Pure |
39 | 45 | abstract @Nullable String getTable(); |
40 | 46 |
|
| 47 | + @SchemaFieldName("flattenResults") |
| 48 | + @SchemaFieldNumber("2") |
| 49 | + @Pure |
| 50 | + abstract @Nullable Boolean getFlattenResults(); |
| 51 | + |
| 52 | + @SchemaFieldName("legacySql") |
| 53 | + @SchemaFieldNumber("3") |
| 54 | + @Pure |
| 55 | + abstract @Nullable Boolean getLegacySql(); |
| 56 | + |
| 57 | + @SchemaFieldName("selectedFields") |
| 58 | + @SchemaFieldNumber("4") |
| 59 | + @Pure |
| 60 | + abstract @Nullable List<String> getSelectedFields(); |
| 61 | + |
| 62 | + @SchemaFieldName("rowRestriction") |
| 63 | + @SchemaFieldNumber("5") |
| 64 | + @Pure |
| 65 | + abstract @Nullable String getRowRestriction(); |
| 66 | + |
41 | 67 | @SchemaCreate |
42 | | - @SuppressWarnings("all") |
43 | 68 | public static BigQueryDynamicReadDescriptor create( |
44 | | - @Nullable String query, @Nullable String table) { |
45 | | - return new AutoValue_BigQueryDynamicReadDescriptor(query, table); |
| 69 | + @Nullable String query, |
| 70 | + @Nullable String table, |
| 71 | + @Nullable Boolean flattenResults, |
| 72 | + @Nullable Boolean legacySql, |
| 73 | + @Nullable List<String> selectedFields, |
| 74 | + @Nullable String rowRestriction) { |
| 75 | + checkArgument((query != null || table != null), "Either query or table has to be specified."); |
| 76 | + checkArgument( |
| 77 | + !(query != null && table != null), "Either query or table has to be specified not both."); |
| 78 | + checkArgument( |
| 79 | + !(table != null && (flattenResults != null || legacySql != null)), |
| 80 | + "Specifies a table with a result flattening preference or legacySql, which only applies to queries"); |
| 81 | + checkArgument( |
| 82 | + !(query != null && (selectedFields != null || rowRestriction != null)), |
| 83 | + "Selected fields and row restriction are only applicable for table reads"); |
| 84 | + checkArgument( |
| 85 | + !(query != null && (flattenResults == null || legacySql == null)), |
| 86 | + "If query is used, flattenResults and legacySql have to be set as well."); |
| 87 | + |
| 88 | + return new AutoValue_BigQueryDynamicReadDescriptor( |
| 89 | + query, table, flattenResults, legacySql, selectedFields, rowRestriction); |
| 90 | + } |
| 91 | + |
| 92 | + public static BigQueryDynamicReadDescriptor query( |
| 93 | + String query, Boolean flattenResults, Boolean legacySql) { |
| 94 | + return create(query, null, flattenResults, legacySql, null, null); |
| 95 | + } |
| 96 | + |
| 97 | + public static BigQueryDynamicReadDescriptor table( |
| 98 | + String table, @Nullable List<String> selectedFields, @Nullable String rowRestriction) { |
| 99 | + return create(null, table, null, null, selectedFields, rowRestriction); |
46 | 100 | } |
47 | 101 | } |
0 commit comments