|
15 | 15 |
|
16 | 16 | import com.google.cloud.bigquery.BigQueryException; |
17 | 17 | import com.google.cloud.bigquery.TableId; |
| 18 | +import com.google.cloud.bigquery.TableInfo; |
18 | 19 | import com.google.cloud.bigquery.TableResult; |
19 | 20 | import com.google.cloud.bigquery.storage.v1beta1.Storage.ReadSession; |
20 | 21 | import com.google.common.collect.ImmutableList; |
|
37 | 38 | import java.util.Optional; |
38 | 39 | import java.util.OptionalInt; |
39 | 40 |
|
| 41 | +import static com.google.cloud.bigquery.TableDefinition.Type.TABLE; |
| 42 | +import static com.google.cloud.bigquery.TableDefinition.Type.VIEW; |
40 | 43 | import static com.google.common.collect.ImmutableList.toImmutableList; |
41 | 44 | import static io.prestosql.plugin.bigquery.BigQueryErrorCode.BIGQUERY_FAILED_TO_EXECUTE_QUERY; |
| 45 | +import static io.prestosql.spi.StandardErrorCode.NOT_SUPPORTED; |
42 | 46 | import static java.util.Objects.requireNonNull; |
43 | 47 | import static java.util.stream.Collectors.toList; |
44 | 48 | import static java.util.stream.IntStream.range; |
@@ -124,8 +128,19 @@ private List<BigQuerySplit> createEmptyProjection(TableId tableId, int actualPar |
124 | 128 | numberOfRows = result.iterateAll().iterator().next().get(0).getLongValue(); |
125 | 129 | } |
126 | 130 | else { |
127 | | - // no filters, so we can take the value from the table info |
128 | | - numberOfRows = bigQueryClient.getTable(tableId).getNumRows().longValue(); |
| 131 | + // no filters, so we can take the value from the table info when the object is TABLE |
| 132 | + TableInfo tableInfo = bigQueryClient.getTable(tableId); |
| 133 | + if (tableInfo.getDefinition().getType() == TABLE) { |
| 134 | + numberOfRows = tableInfo.getNumRows().longValue(); |
| 135 | + } |
| 136 | + else if (tableInfo.getDefinition().getType() == VIEW) { |
| 137 | + String sql = bigQueryClient.selectSql(tableId, "COUNT(*)"); |
| 138 | + TableResult result = bigQueryClient.query(sql); |
| 139 | + numberOfRows = result.iterateAll().iterator().next().get(0).getLongValue(); |
| 140 | + } |
| 141 | + else { |
| 142 | + throw new PrestoException(NOT_SUPPORTED, "Unsupported table type: " + tableInfo.getDefinition().getType()); |
| 143 | + } |
129 | 144 | } |
130 | 145 |
|
131 | 146 | long rowsPerSplit = numberOfRows / actualParallelism; |
|
0 commit comments