1313 */
1414package io .trino .plugin .bigquery ;
1515
16- import com .google .cloud .bigquery .BigQuery ;
17- import com .google .cloud .bigquery .Job ;
18- import com .google .cloud .bigquery .JobId ;
19- import com .google .cloud .bigquery .JobInfo ;
20- import com .google .cloud .bigquery .QueryJobConfiguration ;
2116import com .google .common .collect .ImmutableMap ;
2217import io .trino .testing .AbstractTestIntegrationSmokeTest ;
2318import io .trino .testing .MaterializedResult ;
2419import io .trino .testing .QueryRunner ;
2520import org .testng .annotations .Test ;
2621
27- import static io .trino .plugin .bigquery .BigQueryQueryRunner .createBigQueryClient ;
22+ import static io .trino .plugin .bigquery .BigQueryQueryRunner .BigQuerySqlExecutor ;
2823import static io .trino .spi .type .VarcharType .VARCHAR ;
2924import static io .trino .testing .MaterializedResult .resultBuilder ;
3025import static io .trino .testing .assertions .Assert .assertEquals ;
3631public class TestBigQueryIntegrationSmokeTest
3732 extends AbstractTestIntegrationSmokeTest
3833{
34+ private BigQuerySqlExecutor bigQuerySqlExecutor ;
35+
3936 @ Override
4037 protected QueryRunner createQueryRunner ()
4138 throws Exception
4239 {
40+ this .bigQuerySqlExecutor = new BigQuerySqlExecutor ();
4341 return BigQueryQueryRunner .createQueryRunner (ImmutableMap .of ());
4442 }
4543
@@ -64,11 +62,9 @@ public void testDescribeTable()
6462 @ Test (enabled = false )
6563 public void testSelectFromHourlyPartitionedTable ()
6664 {
67- BigQuery client = createBigQueryClient ();
68-
69- executeBigQuerySql (client , "DROP TABLE IF EXISTS test.hourly_partitioned" );
70- executeBigQuerySql (client , "CREATE TABLE test.hourly_partitioned (value INT64, ts TIMESTAMP) PARTITION BY TIMESTAMP_TRUNC(ts, HOUR)" );
71- executeBigQuerySql (client , "INSERT INTO test.hourly_partitioned (value, ts) VALUES (1000, '2018-01-01 10:00:00')" );
65+ onBigQuery ("DROP TABLE IF EXISTS test.hourly_partitioned" );
66+ onBigQuery ("CREATE TABLE test.hourly_partitioned (value INT64, ts TIMESTAMP) PARTITION BY TIMESTAMP_TRUNC(ts, HOUR)" );
67+ onBigQuery ("INSERT INTO test.hourly_partitioned (value, ts) VALUES (1000, '2018-01-01 10:00:00')" );
7268
7369 MaterializedResult actualValues = computeActual ("SELECT COUNT(1) FROM test.hourly_partitioned" );
7470
@@ -78,11 +74,9 @@ public void testSelectFromHourlyPartitionedTable()
7874 @ Test (enabled = false )
7975 public void testSelectFromYearlyPartitionedTable ()
8076 {
81- BigQuery client = createBigQueryClient ();
82-
83- executeBigQuerySql (client , "DROP TABLE IF EXISTS test.yearly_partitioned" );
84- executeBigQuerySql (client , "CREATE TABLE test.yearly_partitioned (value INT64, ts TIMESTAMP) PARTITION BY TIMESTAMP_TRUNC(ts, YEAR)" );
85- executeBigQuerySql (client , "INSERT INTO test.yearly_partitioned (value, ts) VALUES (1000, '2018-01-01 10:00:00')" );
77+ onBigQuery ("DROP TABLE IF EXISTS test.yearly_partitioned" );
78+ onBigQuery ("CREATE TABLE test.yearly_partitioned (value INT64, ts TIMESTAMP) PARTITION BY TIMESTAMP_TRUNC(ts, YEAR)" );
79+ onBigQuery ("INSERT INTO test.yearly_partitioned (value, ts) VALUES (1000, '2018-01-01 10:00:00')" );
8680
8781 MaterializedResult actualValues = computeActual ("SELECT COUNT(1) FROM test.yearly_partitioned" );
8882
@@ -92,13 +86,11 @@ public void testSelectFromYearlyPartitionedTable()
9286 @ Test (description = "regression test for https://github.com/trinodb/trino/issues/5618" )
9387 public void testPredicatePushdownPrunnedColumns ()
9488 {
95- BigQuery client = createBigQueryClient ();
96-
9789 String tableName = "test.predicate_pushdown_prunned_columns" ;
9890
99- executeBigQuerySql ( client , "DROP TABLE IF EXISTS " + tableName );
100- executeBigQuerySql ( client , "CREATE TABLE " + tableName + " (a INT64, b INT64, c INT64)" );
101- executeBigQuerySql ( client , "INSERT INTO " + tableName + " VALUES (1,2,3)" );
91+ onBigQuery ( "DROP TABLE IF EXISTS " + tableName );
92+ onBigQuery ( "CREATE TABLE " + tableName + " (a INT64, b INT64, c INT64)" );
93+ onBigQuery ( "INSERT INTO " + tableName + " VALUES (1,2,3)" );
10294
10395 assertQuery (
10496 "SELECT 1 FROM " + tableName + " WHERE " +
@@ -110,16 +102,14 @@ public void testPredicatePushdownPrunnedColumns()
110102 @ Test (description = "regression test for https://github.com/trinodb/trino/issues/5635" )
111103 public void testCountAggregationView ()
112104 {
113- BigQuery client = createBigQueryClient ();
114-
115105 String tableName = "test.count_aggregation_table" ;
116106 String viewName = "test.count_aggregation_view" ;
117107
118- executeBigQuerySql ( client , "DROP TABLE IF EXISTS " + tableName );
119- executeBigQuerySql ( client , "DROP VIEW IF EXISTS " + viewName );
120- executeBigQuerySql ( client , "CREATE TABLE " + tableName + " (a INT64, b INT64, c INT64)" );
121- executeBigQuerySql ( client , "INSERT INTO " + tableName + " VALUES (1, 2, 3), (4, 5, 6)" );
122- executeBigQuerySql ( client , "CREATE VIEW " + viewName + " AS SELECT * FROM " + tableName );
108+ onBigQuery ( "DROP TABLE IF EXISTS " + tableName );
109+ onBigQuery ( "DROP VIEW IF EXISTS " + viewName );
110+ onBigQuery ( "CREATE TABLE " + tableName + " (a INT64, b INT64, c INT64)" );
111+ onBigQuery ( "INSERT INTO " + tableName + " VALUES (1, 2, 3), (4, 5, 6)" );
112+ onBigQuery ( "CREATE VIEW " + viewName + " AS SELECT * FROM " + tableName );
123113
124114 assertQuery (
125115 "SELECT count(*) FROM " + viewName ,
@@ -140,32 +130,28 @@ public void testCountAggregationView()
140130 @ Test
141131 public void testRepeatCountAggregationView ()
142132 {
143- BigQuery client = createBigQueryClient ();
144-
145133 String viewName = "test.repeat_count_aggregation_view_" + randomTableSuffix ();
146134
147- executeBigQuerySql ( client , "DROP VIEW IF EXISTS " + viewName );
148- executeBigQuerySql ( client , "CREATE VIEW " + viewName + " AS SELECT 1 AS col1" );
135+ onBigQuery ( "DROP VIEW IF EXISTS " + viewName );
136+ onBigQuery ( "CREATE VIEW " + viewName + " AS SELECT 1 AS col1" );
149137
150138 assertQuery ("SELECT count(*) FROM " + viewName , "VALUES (1)" );
151139 assertQuery ("SELECT count(*) FROM " + viewName , "VALUES (1)" );
152140
153- executeBigQuerySql ( client , "DROP VIEW " + viewName );
141+ onBigQuery ( "DROP VIEW " + viewName );
154142 }
155143
156144 @ Test
157145 public void testViewDefinitionSystemTable ()
158146 {
159- BigQuery client = createBigQueryClient ();
160-
161147 String schemaName = "test" ;
162148 String tableName = "views_system_table_base_" + randomTableSuffix ();
163149 String viewName = "views_system_table_view_" + randomTableSuffix ();
164150
165- executeBigQuerySql ( client , format ("DROP TABLE IF EXISTS %s.%s" , schemaName , tableName ));
166- executeBigQuerySql ( client , format ("DROP VIEW IF EXISTS %s.%s" , schemaName , viewName ));
167- executeBigQuerySql ( client , format ("CREATE TABLE %s.%s (a INT64, b INT64, c INT64)" , schemaName , tableName ));
168- executeBigQuerySql ( client , format ("CREATE VIEW %s.%s AS SELECT * FROM %s.%s" , schemaName , viewName , schemaName , tableName ));
151+ onBigQuery ( format ("DROP TABLE IF EXISTS %s.%s" , schemaName , tableName ));
152+ onBigQuery ( format ("DROP VIEW IF EXISTS %s.%s" , schemaName , viewName ));
153+ onBigQuery ( format ("CREATE TABLE %s.%s (a INT64, b INT64, c INT64)" , schemaName , tableName ));
154+ onBigQuery ( format ("CREATE VIEW %s.%s AS SELECT * FROM %s.%s" , schemaName , viewName , schemaName , tableName ));
169155
170156 assertEquals (
171157 computeScalar (format ("SELECT * FROM %s.\" %s$view_definition\" " , schemaName , viewName )),
@@ -175,34 +161,8 @@ public void testViewDefinitionSystemTable()
175161 format ("SELECT * FROM %s.\" %s$view_definition\" " , schemaName , tableName ),
176162 format ("Table '%s.%s\\ $view_definition' not found" , schemaName , tableName ));
177163
178- executeBigQuerySql (client , format ("DROP TABLE %s.%s" , schemaName , tableName ));
179- executeBigQuerySql (client , format ("DROP VIEW %s.%s" , schemaName , viewName ));
180- }
181-
182- private static void executeBigQuerySql (BigQuery bigquery , String query )
183- {
184- QueryJobConfiguration queryConfig = QueryJobConfiguration .newBuilder (query )
185- .setUseLegacySql (false )
186- .build ();
187-
188- JobId jobId = JobId .of ();
189- Job queryJob = bigquery .create (JobInfo .newBuilder (queryConfig ).setJobId (jobId ).build ());
190-
191- try {
192- queryJob = queryJob .waitFor ();
193-
194- if (queryJob == null ) {
195- throw new RuntimeException (format ("Job with uuid %s does not longer exists" , jobId .getJob ()));
196- }
197-
198- if (queryJob .getStatus ().getError () != null ) {
199- throw new RuntimeException (format ("Query '%s' failed: %s" , query , queryJob .getStatus ().getError ()));
200- }
201- }
202- catch (InterruptedException e ) {
203- Thread .currentThread ().interrupt ();
204- throw new RuntimeException (e );
205- }
164+ onBigQuery (format ("DROP TABLE %s.%s" , schemaName , tableName ));
165+ onBigQuery (format ("DROP VIEW %s.%s" , schemaName , viewName ));
206166 }
207167
208168 @ Override
@@ -221,4 +181,9 @@ public void testShowCreateTable()
221181 " comment varchar NOT NULL\n " +
222182 ")" );
223183 }
184+
185+ private void onBigQuery (String sql )
186+ {
187+ bigQuerySqlExecutor .execute (sql );
188+ }
224189}
0 commit comments