13
13
*/
14
14
package io .trino .plugin .bigquery ;
15
15
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 ;
21
16
import com .google .common .collect .ImmutableMap ;
22
17
import io .trino .testing .AbstractTestIntegrationSmokeTest ;
23
18
import io .trino .testing .MaterializedResult ;
24
19
import io .trino .testing .QueryRunner ;
25
20
import org .testng .annotations .Test ;
26
21
27
- import static io .trino .plugin .bigquery .BigQueryQueryRunner .createBigQueryClient ;
22
+ import static io .trino .plugin .bigquery .BigQueryQueryRunner .BigQuerySqlExecutor ;
28
23
import static io .trino .spi .type .VarcharType .VARCHAR ;
29
24
import static io .trino .testing .MaterializedResult .resultBuilder ;
30
25
import static io .trino .testing .assertions .Assert .assertEquals ;
36
31
public class TestBigQueryIntegrationSmokeTest
37
32
extends AbstractTestIntegrationSmokeTest
38
33
{
34
+ private BigQuerySqlExecutor bigQuerySqlExecutor ;
35
+
39
36
@ Override
40
37
protected QueryRunner createQueryRunner ()
41
38
throws Exception
42
39
{
40
+ this .bigQuerySqlExecutor = new BigQuerySqlExecutor ();
43
41
return BigQueryQueryRunner .createQueryRunner (ImmutableMap .of ());
44
42
}
45
43
@@ -64,11 +62,9 @@ public void testDescribeTable()
64
62
@ Test (enabled = false )
65
63
public void testSelectFromHourlyPartitionedTable ()
66
64
{
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')" );
72
68
73
69
MaterializedResult actualValues = computeActual ("SELECT COUNT(1) FROM test.hourly_partitioned" );
74
70
@@ -78,11 +74,9 @@ public void testSelectFromHourlyPartitionedTable()
78
74
@ Test (enabled = false )
79
75
public void testSelectFromYearlyPartitionedTable ()
80
76
{
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')" );
86
80
87
81
MaterializedResult actualValues = computeActual ("SELECT COUNT(1) FROM test.yearly_partitioned" );
88
82
@@ -92,13 +86,11 @@ public void testSelectFromYearlyPartitionedTable()
92
86
@ Test (description = "regression test for https://github.com/trinodb/trino/issues/5618" )
93
87
public void testPredicatePushdownPrunnedColumns ()
94
88
{
95
- BigQuery client = createBigQueryClient ();
96
-
97
89
String tableName = "test.predicate_pushdown_prunned_columns" ;
98
90
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)" );
102
94
103
95
assertQuery (
104
96
"SELECT 1 FROM " + tableName + " WHERE " +
@@ -110,16 +102,14 @@ public void testPredicatePushdownPrunnedColumns()
110
102
@ Test (description = "regression test for https://github.com/trinodb/trino/issues/5635" )
111
103
public void testCountAggregationView ()
112
104
{
113
- BigQuery client = createBigQueryClient ();
114
-
115
105
String tableName = "test.count_aggregation_table" ;
116
106
String viewName = "test.count_aggregation_view" ;
117
107
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 );
123
113
124
114
assertQuery (
125
115
"SELECT count(*) FROM " + viewName ,
@@ -140,32 +130,28 @@ public void testCountAggregationView()
140
130
@ Test
141
131
public void testRepeatCountAggregationView ()
142
132
{
143
- BigQuery client = createBigQueryClient ();
144
-
145
133
String viewName = "test.repeat_count_aggregation_view_" + randomTableSuffix ();
146
134
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" );
149
137
150
138
assertQuery ("SELECT count(*) FROM " + viewName , "VALUES (1)" );
151
139
assertQuery ("SELECT count(*) FROM " + viewName , "VALUES (1)" );
152
140
153
- executeBigQuerySql ( client , "DROP VIEW " + viewName );
141
+ onBigQuery ( "DROP VIEW " + viewName );
154
142
}
155
143
156
144
@ Test
157
145
public void testViewDefinitionSystemTable ()
158
146
{
159
- BigQuery client = createBigQueryClient ();
160
-
161
147
String schemaName = "test" ;
162
148
String tableName = "views_system_table_base_" + randomTableSuffix ();
163
149
String viewName = "views_system_table_view_" + randomTableSuffix ();
164
150
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 ));
169
155
170
156
assertEquals (
171
157
computeScalar (format ("SELECT * FROM %s.\" %s$view_definition\" " , schemaName , viewName )),
@@ -175,34 +161,8 @@ public void testViewDefinitionSystemTable()
175
161
format ("SELECT * FROM %s.\" %s$view_definition\" " , schemaName , tableName ),
176
162
format ("Table '%s.%s\\ $view_definition' not found" , schemaName , tableName ));
177
163
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 ));
206
166
}
207
167
208
168
@ Override
@@ -221,4 +181,9 @@ public void testShowCreateTable()
221
181
" comment varchar NOT NULL\n " +
222
182
")" );
223
183
}
184
+
185
+ private void onBigQuery (String sql )
186
+ {
187
+ bigQuerySqlExecutor .execute (sql );
188
+ }
224
189
}
0 commit comments