forked from airbytehq/airbyte
/
BigQuerySqlGeneratorIntegrationTest.java
475 lines (440 loc) · 20.4 KB
/
BigQuerySqlGeneratorIntegrationTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/*
* Copyright (c) 2023 Airbyte, Inc., all rights reserved.
*/
package io.airbyte.integrations.destination.bigquery.typing_deduping;
import static com.google.cloud.bigquery.LegacySQLTypeName.legacySQLTypeName;
import static java.util.stream.Collectors.joining;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryException;
import com.google.cloud.bigquery.Dataset;
import com.google.cloud.bigquery.DatasetId;
import com.google.cloud.bigquery.DatasetInfo;
import com.google.cloud.bigquery.Field;
import com.google.cloud.bigquery.FieldValue;
import com.google.cloud.bigquery.FieldValueList;
import com.google.cloud.bigquery.QueryJobConfiguration;
import com.google.cloud.bigquery.Schema;
import com.google.cloud.bigquery.StandardSQLTypeName;
import com.google.cloud.bigquery.Table;
import com.google.cloud.bigquery.TableDefinition;
import com.google.cloud.bigquery.TableResult;
import io.airbyte.cdk.integrations.base.JavaBaseConstants;
import io.airbyte.commons.json.Jsons;
import io.airbyte.integrations.base.destination.typing_deduping.AirbyteProtocolType;
import io.airbyte.integrations.base.destination.typing_deduping.BaseSqlGeneratorIntegrationTest;
import io.airbyte.integrations.base.destination.typing_deduping.StreamConfig;
import io.airbyte.integrations.base.destination.typing_deduping.StreamId;
import io.airbyte.integrations.destination.bigquery.BigQueryConsts;
import io.airbyte.integrations.destination.bigquery.BigQueryDestination;
import io.airbyte.protocol.models.v0.DestinationSyncMode;
import io.airbyte.protocol.models.v0.SyncMode;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.apache.commons.text.StringSubstitutor;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Execution(ExecutionMode.CONCURRENT)
public class BigQuerySqlGeneratorIntegrationTest extends BaseSqlGeneratorIntegrationTest<TableDefinition> {
private static final Logger LOGGER = LoggerFactory.getLogger(BigQuerySqlGeneratorIntegrationTest.class);
private static BigQuery bq;
private static String projectId;
private static String datasetLocation;
@BeforeAll
public static void setupBigquery() throws Exception {
final String rawConfig = Files.readString(Path.of("secrets/credentials-gcs-staging.json"));
final JsonNode config = Jsons.deserialize(rawConfig);
bq = BigQueryDestination.getBigQuery(config);
projectId = config.get(BigQueryConsts.CONFIG_PROJECT_ID).asText();
datasetLocation = config.get(BigQueryConsts.CONFIG_DATASET_LOCATION).asText();
}
@Override
protected BigQuerySqlGenerator getSqlGenerator() {
return new BigQuerySqlGenerator(projectId, datasetLocation);
}
@Override
protected BigQueryDestinationHandler getDestinationHandler() {
return new BigQueryDestinationHandler(bq, "US");
}
@Override
protected void createNamespace(final String namespace) {
bq.create(DatasetInfo.newBuilder(namespace)
// This unfortunately doesn't delete the actual dataset after 3 days, but at least we'll clear out
// old tables automatically
.setDefaultTableLifetime(Duration.ofDays(3).toMillis())
.build());
}
@Override
protected void createRawTable(final StreamId streamId) throws InterruptedException {
bq.query(QueryJobConfiguration.newBuilder(
new StringSubstitutor(Map.of(
"raw_table_id", streamId.rawTableId(BigQuerySqlGenerator.QUOTE))).replace(
"""
CREATE TABLE ${raw_table_id} (
_airbyte_raw_id STRING NOT NULL,
_airbyte_data STRING NOT NULL,
_airbyte_extracted_at TIMESTAMP NOT NULL,
_airbyte_loaded_at TIMESTAMP
) PARTITION BY (
DATE_TRUNC(_airbyte_extracted_at, DAY)
) CLUSTER BY _airbyte_loaded_at;
"""))
.build());
}
@Override
protected void createV1RawTable(final StreamId v1RawTable) throws Exception {
bq.query(
QueryJobConfiguration
.newBuilder(
new StringSubstitutor(Map.of(
"raw_table_id", v1RawTable.rawTableId(BigQuerySqlGenerator.QUOTE))).replace(
"""
CREATE TABLE ${raw_table_id} (
_airbyte_ab_id STRING NOT NULL,
_airbyte_data STRING NOT NULL,
_airbyte_emitted_at TIMESTAMP NOT NULL,
) PARTITION BY (
DATE_TRUNC(_airbyte_emitted_at, DAY)
) CLUSTER BY _airbyte_emitted_at;
"""))
.build());
}
@Override
protected void insertFinalTableRecords(final boolean includeCdcDeletedAt,
final StreamId streamId,
final String suffix,
final List<JsonNode> records)
throws InterruptedException {
final List<String> columnNames = includeCdcDeletedAt ? FINAL_TABLE_COLUMN_NAMES_CDC : FINAL_TABLE_COLUMN_NAMES;
final String cdcDeletedAtDecl = includeCdcDeletedAt ? ",`_ab_cdc_deleted_at` TIMESTAMP" : "";
final String cdcDeletedAtName = includeCdcDeletedAt ? ",`_ab_cdc_deleted_at`" : "";
final String recordsText = records.stream()
// For each record, convert it to a string like "(rawId, extractedAt, loadedAt, data)"
.map(record -> columnNames.stream()
.map(record::get)
.map(r -> {
if (r == null) {
return "NULL";
}
final String stringContents;
if (r.isTextual()) {
stringContents = r.asText();
} else {
stringContents = r.toString();
}
return '"' + stringContents
// Serialized json might contain backslashes and double quotes. Escape them.
.replace("\\", "\\\\")
.replace("\"", "\\\"") + '"';
})
.collect(joining(",")))
.map(row -> "(" + row + ")")
.collect(joining(","));
bq.query(QueryJobConfiguration.newBuilder(
new StringSubstitutor(Map.of(
"final_table_id", streamId.finalTableId(BigQuerySqlGenerator.QUOTE, suffix),
"cdc_deleted_at_name", cdcDeletedAtName,
"cdc_deleted_at_decl", cdcDeletedAtDecl,
"records", recordsText)).replace(
// Similar to insertRawTableRecords, some of these columns are declared as string and wrapped in
// parse_json().
// There's also a bunch of casting, because bigquery doesn't coerce strings to e.g. int
"""
insert into ${final_table_id} (
_airbyte_raw_id,
_airbyte_extracted_at,
_airbyte_meta,
`id1`,
`id2`,
`updated_at`,
`struct`,
`array`,
`string`,
`number`,
`integer`,
`boolean`,
`timestamp_with_timezone`,
`timestamp_without_timezone`,
`time_with_timezone`,
`time_without_timezone`,
`date`,
`unknown`
${cdc_deleted_at_name}
)
select
_airbyte_raw_id,
_airbyte_extracted_at,
parse_json(_airbyte_meta),
cast(`id1` as int64),
cast(`id2` as int64),
`updated_at`,
parse_json(`struct`),
parse_json(`array`),
`string`,
cast(`number` as numeric),
cast(`integer` as int64),
cast(`boolean` as boolean),
`timestamp_with_timezone`,
`timestamp_without_timezone`,
`time_with_timezone`,
`time_without_timezone`,
`date`,
parse_json(`unknown`)
${cdc_deleted_at_name}
from unnest([
STRUCT<
_airbyte_raw_id STRING,
_airbyte_extracted_at TIMESTAMP,
_airbyte_meta STRING,
`id1` STRING,
`id2` STRING,
`updated_at` TIMESTAMP,
`struct` STRING,
`array` STRING,
`string` STRING,
`number` STRING,
`integer` STRING,
`boolean` STRING,
`timestamp_with_timezone` TIMESTAMP,
`timestamp_without_timezone` DATETIME,
`time_with_timezone` STRING,
`time_without_timezone` TIME,
`date` DATE,
`unknown` STRING
${cdc_deleted_at_decl}
>
${records}
])
"""))
.build());
}
private String stringifyRecords(final List<JsonNode> records, final List<String> columnNames) {
return records.stream()
// For each record, convert it to a string like "(rawId, extractedAt, loadedAt, data)"
.map(record -> columnNames.stream()
.map(record::get)
.map(r -> {
if (r == null) {
return "NULL";
}
final String stringContents;
if (r.isTextual()) {
stringContents = r.asText();
} else {
stringContents = r.toString();
}
return '"' + stringContents
// Serialized json might contain backslashes and double quotes. Escape them.
.replace("\\", "\\\\")
.replace("\"", "\\\"") + '"';
})
.collect(joining(",")))
.map(row -> "(" + row + ")")
.collect(joining(","));
}
@Override
protected void insertRawTableRecords(final StreamId streamId, final List<JsonNode> records) throws InterruptedException {
final String recordsText = stringifyRecords(records, JavaBaseConstants.V2_RAW_TABLE_COLUMN_NAMES);
bq.query(QueryJobConfiguration.newBuilder(
new StringSubstitutor(Map.of(
"raw_table_id", streamId.rawTableId(BigQuerySqlGenerator.QUOTE),
"records", recordsText)).replace(
// TODO: Perform a normal insert - edward
"""
INSERT INTO ${raw_table_id} (_airbyte_raw_id, _airbyte_extracted_at, _airbyte_loaded_at, _airbyte_data)
SELECT _airbyte_raw_id, _airbyte_extracted_at, _airbyte_loaded_at, _airbyte_data FROM UNNEST([
STRUCT<`_airbyte_raw_id` STRING, `_airbyte_extracted_at` TIMESTAMP, `_airbyte_loaded_at` TIMESTAMP, _airbyte_data STRING>
${records}
])
"""))
.build());
}
@Override
protected void insertV1RawTableRecords(final StreamId streamId, final List<JsonNode> records) throws Exception {
final String recordsText = stringifyRecords(records, JavaBaseConstants.LEGACY_RAW_TABLE_COLUMNS);
bq.query(
QueryJobConfiguration
.newBuilder(
new StringSubstitutor(Map.of(
"v1_raw_table_id", streamId.rawTableId(BigQuerySqlGenerator.QUOTE),
"records", recordsText)).replace(
"""
INSERT INTO ${v1_raw_table_id} (_airbyte_ab_id, _airbyte_data, _airbyte_emitted_at)
SELECT _airbyte_ab_id, _airbyte_data, _airbyte_emitted_at FROM UNNEST([
STRUCT<`_airbyte_ab_id` STRING, _airbyte_data STRING, `_airbyte_emitted_at` TIMESTAMP>
${records}
])
"""))
.build());
}
@Override
protected List<JsonNode> dumpRawTableRecords(final StreamId streamId) throws Exception {
final TableResult result = bq.query(QueryJobConfiguration.of("SELECT * FROM " + streamId.rawTableId(BigQuerySqlGenerator.QUOTE)));
return BigQuerySqlGeneratorIntegrationTest.toJsonRecords(result).stream().peek(record -> {
final JsonNode deserializedData = Jsons.deserializeExact(record.get("_airbyte_data").asText());
((ObjectNode) record).set("_airbyte_data", deserializedData);
}).toList();
}
@Override
protected List<JsonNode> dumpFinalTableRecords(final StreamId streamId, final String suffix) throws Exception {
final TableResult result = bq.query(QueryJobConfiguration.of("SELECT * FROM " + streamId.finalTableId(BigQuerySqlGenerator.QUOTE, suffix)));
return BigQuerySqlGeneratorIntegrationTest.toJsonRecords(result);
}
@Override
protected void teardownNamespace(final String namespace) {
bq.delete(namespace, BigQuery.DatasetDeleteOption.deleteContents());
}
@Override
@Test
public void testCreateTableIncremental() throws Exception {
destinationHandler.execute(generator.createTable(incrementalDedupStream, "", false));
final Table table = bq.getTable(namespace, "users_final");
// The table should exist
assertNotNull(table);
final Schema schema = table.getDefinition().getSchema();
// And we should know exactly what columns it contains
assertEquals(
// Would be nice to assert directly against StandardSQLTypeName, but bigquery returns schemas of
// LegacySQLTypeName. So we have to translate.
Schema.of(
Field.newBuilder("_airbyte_raw_id", legacySQLTypeName(StandardSQLTypeName.STRING)).setMode(Field.Mode.REQUIRED).build(),
Field.newBuilder("_airbyte_extracted_at", legacySQLTypeName(StandardSQLTypeName.TIMESTAMP)).setMode(Field.Mode.REQUIRED).build(),
Field.newBuilder("_airbyte_meta", legacySQLTypeName(StandardSQLTypeName.JSON)).setMode(Field.Mode.REQUIRED).build(),
Field.of("id1", legacySQLTypeName(StandardSQLTypeName.INT64)),
Field.of("id2", legacySQLTypeName(StandardSQLTypeName.INT64)),
Field.of("updated_at", legacySQLTypeName(StandardSQLTypeName.TIMESTAMP)),
Field.of("struct", legacySQLTypeName(StandardSQLTypeName.JSON)),
Field.of("array", legacySQLTypeName(StandardSQLTypeName.JSON)),
Field.of("string", legacySQLTypeName(StandardSQLTypeName.STRING)),
Field.of("number", legacySQLTypeName(StandardSQLTypeName.NUMERIC)),
Field.of("integer", legacySQLTypeName(StandardSQLTypeName.INT64)),
Field.of("boolean", legacySQLTypeName(StandardSQLTypeName.BOOL)),
Field.of("timestamp_with_timezone", legacySQLTypeName(StandardSQLTypeName.TIMESTAMP)),
Field.of("timestamp_without_timezone", legacySQLTypeName(StandardSQLTypeName.DATETIME)),
Field.of("time_with_timezone", legacySQLTypeName(StandardSQLTypeName.STRING)),
Field.of("time_without_timezone", legacySQLTypeName(StandardSQLTypeName.TIME)),
Field.of("date", legacySQLTypeName(StandardSQLTypeName.DATE)),
Field.of("unknown", legacySQLTypeName(StandardSQLTypeName.JSON))),
schema);
// TODO this should assert partitioning/clustering configs
}
@Test
public void testCreateTableInOtherRegion() throws InterruptedException {
final BigQueryDestinationHandler destinationHandler = new BigQueryDestinationHandler(bq, "asia-east1");
// We're creating the dataset in the wrong location in the @BeforeEach block. Explicitly delete it.
bq.getDataset(namespace).delete();
var sqlGenerator = new BigQuerySqlGenerator(projectId, "asia-east1");
destinationHandler.execute(sqlGenerator.createSchema(namespace));
destinationHandler.execute(sqlGenerator.createTable(incrementalDedupStream, "", false));
// Empirically, it sometimes takes Bigquery nearly 30 seconds to propagate the dataset's existence.
// Give ourselves 2 minutes just in case.
for (int i = 0; i < 120; i++) {
final Dataset dataset = bq.getDataset(DatasetId.of(bq.getOptions().getProjectId(), namespace));
if (dataset == null) {
LOGGER.info("Sleeping and trying again... ({})", i);
Thread.sleep(1000);
} else {
assertEquals("asia-east1", dataset.getLocation());
return;
}
}
fail("Dataset does not exist");
}
/**
* Bigquery column names aren't allowed to start with certain prefixes. Verify that we throw an
* error in these cases.
*/
@ParameterizedTest
@ValueSource(strings = {
"_table_",
"_file_",
"_partition_",
"_row_timestamp_",
"__root__",
"_colidentifier_"
})
public void testFailureOnReservedColumnNamePrefix(final String prefix) {
final StreamConfig stream = new StreamConfig(
streamId,
SyncMode.INCREMENTAL,
DestinationSyncMode.APPEND,
null,
Optional.empty(),
new LinkedHashMap<>() {
{
put(generator.buildColumnId(prefix + "the_column_name"), AirbyteProtocolType.STRING);
}
});
final String createTable = generator.createTable(stream, "", false);
assertThrows(
BigQueryException.class,
() -> destinationHandler.execute(createTable));
}
/**
* Something about this test is borked on bigquery. It fails because the raw table doesn't exist,
* but you can go into the UI and see that it does exist.
*/
@Override
@Disabled
public void noCrashOnSpecialCharacters(final String specialChars) throws Exception {
super.noCrashOnSpecialCharacters(specialChars);
}
/**
* TableResult contains records in a somewhat nonintuitive format (and it avoids loading them all
* into memory). That's annoying for us since we're working with small test data, so just pull
* everything into a list.
*/
public static List<JsonNode> toJsonRecords(final TableResult result) {
return result.streamAll().map(row -> toJson(result.getSchema(), row)).toList();
}
/**
* FieldValueList stores everything internally as string (I think?) but provides conversions to more
* useful types. This method does that conversion, using the schema to determine which type is most
* appropriate. Then we just dump everything into a jsonnode for interop with RecordDiffer.
*/
private static JsonNode toJson(final Schema schema, final FieldValueList row) {
final ObjectNode json = (ObjectNode) Jsons.emptyObject();
for (int i = 0; i < schema.getFields().size(); i++) {
final Field field = schema.getFields().get(i);
final FieldValue value = row.get(i);
final JsonNode typedValue;
if (!value.isNull()) {
typedValue = switch (field.getType().getStandardType()) {
case BOOL -> Jsons.jsonNode(value.getBooleanValue());
case INT64 -> Jsons.jsonNode(value.getLongValue());
case FLOAT64 -> Jsons.jsonNode(value.getDoubleValue());
case NUMERIC, BIGNUMERIC -> Jsons.jsonNode(value.getNumericValue());
case STRING -> Jsons.jsonNode(value.getStringValue());
// naively converting an Instant returns a DecimalNode with the unix epoch, so instead we manually
// stringify it
case TIMESTAMP -> Jsons.jsonNode(value.getTimestampInstant().toString());
// value.getTimestampInstant() fails to parse these types
case DATE, DATETIME, TIME -> Jsons.jsonNode(value.getStringValue());
// bigquery returns JSON columns as string; manually parse it into a JsonNode
case JSON -> Jsons.jsonNode(Jsons.deserializeExact(value.getStringValue()));
// Default case for weird types (struct, array, geography, interval, bytes)
default -> Jsons.jsonNode(value.getStringValue());
};
json.set(field.getName(), typedValue);
}
}
return json;
}
}