Skip to content

Commit 3b1c23e

Browse files
committed
Fix incorrect result when aggregating count BigQuery view
1 parent 320a9bd commit 3b1c23e

2 files changed

Lines changed: 44 additions & 2 deletions

File tree

presto-bigquery/src/main/java/io/prestosql/plugin/bigquery/BigQuerySplitManager.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import com.google.cloud.bigquery.BigQueryException;
1717
import com.google.cloud.bigquery.TableId;
18+
import com.google.cloud.bigquery.TableInfo;
1819
import com.google.cloud.bigquery.TableResult;
1920
import com.google.cloud.bigquery.storage.v1beta1.Storage.ReadSession;
2021
import com.google.common.collect.ImmutableList;
@@ -37,8 +38,11 @@
3738
import java.util.Optional;
3839
import java.util.OptionalInt;
3940

41+
import static com.google.cloud.bigquery.TableDefinition.Type.TABLE;
42+
import static com.google.cloud.bigquery.TableDefinition.Type.VIEW;
4043
import static com.google.common.collect.ImmutableList.toImmutableList;
4144
import static io.prestosql.plugin.bigquery.BigQueryErrorCode.BIGQUERY_FAILED_TO_EXECUTE_QUERY;
45+
import static io.prestosql.spi.StandardErrorCode.NOT_SUPPORTED;
4246
import static java.util.Objects.requireNonNull;
4347
import static java.util.stream.Collectors.toList;
4448
import static java.util.stream.IntStream.range;
@@ -124,8 +128,19 @@ private List<BigQuerySplit> createEmptyProjection(TableId tableId, int actualPar
124128
numberOfRows = result.iterateAll().iterator().next().get(0).getLongValue();
125129
}
126130
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+
}
129144
}
130145

131146
long rowsPerSplit = numberOfRows / actualParallelism;

presto-bigquery/src/test/java/io/prestosql/plugin/bigquery/TestBigQueryIntegrationSmokeTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,33 @@ public void testPredicatePushdownPrunnedColumns()
105105
"VALUES (1)");
106106
}
107107

108+
@Test(description = "regression test for https://github.com/prestosql/presto/issues/5635")
109+
public void testCountAggregationView()
110+
{
111+
BigQuery client = createBigQueryClient();
112+
113+
String tableName = "test.count_aggregation_table";
114+
String viewName = "test.count_aggregation_view";
115+
116+
executeBigQuerySql(client, "DROP TABLE IF EXISTS " + tableName);
117+
executeBigQuerySql(client, "DROP VIEW IF EXISTS " + viewName);
118+
executeBigQuerySql(client, "CREATE TABLE " + tableName + " (a INT64, b INT64, c INT64)");
119+
executeBigQuerySql(client, "INSERT INTO " + tableName + " VALUES (1, 2, 3), (4, 5, 6)");
120+
executeBigQuerySql(client, "CREATE VIEW " + viewName + " AS SELECT * FROM " + tableName);
121+
122+
assertQuery(
123+
"SELECT count(*) FROM " + viewName,
124+
"VALUES (2)");
125+
126+
assertQuery(
127+
"SELECT count(*) FROM " + viewName + " WHERE a = 1",
128+
"VALUES (1)");
129+
130+
assertQuery(
131+
"SELECT count(a) FROM " + viewName + " WHERE b = 2",
132+
"VALUES (1)");
133+
}
134+
108135
private static void executeBigQuerySql(BigQuery bigquery, String query)
109136
{
110137
QueryJobConfiguration queryConfig = QueryJobConfiguration.newBuilder(query)

0 commit comments

Comments
 (0)