|
23 | 23 | import sqlalchemy |
24 | 24 |
|
25 | 25 | from google.cloud.bigquery import ( |
| 26 | + AvroOptions, |
| 27 | + CSVOptions, |
| 28 | + ExternalConfig, |
| 29 | + ExternalSourceFormat, |
| 30 | + HivePartitioningOptions, |
| 31 | + ParquetOptions, |
26 | 32 | PartitionRange, |
27 | 33 | RangePartitioning, |
28 | 34 | TimePartitioning, |
@@ -461,6 +467,142 @@ def test_table_all_dialect_option(faux_conn): |
461 | 467 | assert result == expected |
462 | 468 |
|
463 | 469 |
|
| 470 | +def test_create_external_table(faux_conn): |
| 471 | + external_config = ExternalConfig(ExternalSourceFormat.AVRO) |
| 472 | + external_config.source_uris = ["gs://bucket-name/prefix/file.avro"] |
| 473 | + |
| 474 | + with pytest.raises(sqlite3.OperationalError): |
| 475 | + # expect table creation to fail as SQLite does not support external tables |
| 476 | + setup_table( |
| 477 | + faux_conn, |
| 478 | + "some_table", |
| 479 | + sqlalchemy.Column("string_col", sqlalchemy.String), |
| 480 | + sqlalchemy.Column("int_col", sqlalchemy.Integer), |
| 481 | + prefixes=["EXTERNAL"], |
| 482 | + bigquery_external_data_configuration=external_config, |
| 483 | + ) |
| 484 | + |
| 485 | + result = " ".join(faux_conn.test_data["execute"][-1][0].strip().split()) |
| 486 | + expected = ( |
| 487 | + "CREATE EXTERNAL TABLE `some_table` ( `string_col` STRING, `int_col` INT64 )" |
| 488 | + " OPTIONS(format='AVRO', uris=['gs://bucket-name/prefix/file.avro'])" |
| 489 | + ) |
| 490 | + |
| 491 | + assert result == expected |
| 492 | + |
| 493 | + |
| 494 | +def test_create_external_table_hive_partitioning(faux_conn): |
| 495 | + hive_partitioning = HivePartitioningOptions() |
| 496 | + hive_partitioning.source_uri_prefix = "gs://bucket-name/prefix" |
| 497 | + hive_partitioning.require_partition_filter = False |
| 498 | + |
| 499 | + external_config = ExternalConfig(ExternalSourceFormat.PARQUET) |
| 500 | + external_config.source_uris = [ |
| 501 | + "gs://bucket-name/prefix/string_col=A/*", |
| 502 | + "gs://bucket-name/prefix/string_col=B/*", |
| 503 | + ] |
| 504 | + external_config.hive_partitioning = hive_partitioning |
| 505 | + |
| 506 | + with pytest.raises(sqlite3.OperationalError): |
| 507 | + # expect table creation to fail as SQLite does not support external tables |
| 508 | + setup_table( |
| 509 | + faux_conn, |
| 510 | + "some_table", |
| 511 | + sqlalchemy.Column("string_col", sqlalchemy.String), |
| 512 | + sqlalchemy.Column("int_col", sqlalchemy.Integer), |
| 513 | + prefixes=["EXTERNAL"], |
| 514 | + bigquery_external_data_configuration=external_config, |
| 515 | + ) |
| 516 | + |
| 517 | + result = " ".join(faux_conn.test_data["execute"][-1][0].strip().split()) |
| 518 | + expected = ( |
| 519 | + "CREATE EXTERNAL TABLE `some_table` WITH PARTITION COLUMNS ( `string_col` STRING, `int_col` INT64 )" |
| 520 | + " OPTIONS(format='PARQUET', uris=['gs://bucket-name/prefix/string_col=A/*', 'gs://bucket-name/prefix/string_col=B/*'], hive_partition_uri_prefix='gs://bucket-name/prefix', require_hive_partition_filter=false)" |
| 521 | + ) |
| 522 | + |
| 523 | + assert result == expected |
| 524 | + |
| 525 | + |
| 526 | +def test_create_external_table_format_csv_options(faux_conn): |
| 527 | + external_config = ExternalConfig(ExternalSourceFormat.CSV) |
| 528 | + external_config.source_uris = ["gs://bucket-name/prefix/string_col=A/file.csv"] |
| 529 | + |
| 530 | + external_config.csv_options = CSVOptions() |
| 531 | + external_config.csv_options.skip_leading_rows = 1 |
| 532 | + external_config.csv_options.preserve_ascii_control_characters = True |
| 533 | + |
| 534 | + with pytest.raises(sqlite3.OperationalError): |
| 535 | + # expect table creation to fail as SQLite does not support external tables |
| 536 | + setup_table( |
| 537 | + faux_conn, |
| 538 | + "some_table", |
| 539 | + sqlalchemy.Column("string_col", sqlalchemy.String), |
| 540 | + prefixes=["EXTERNAL"], |
| 541 | + bigquery_external_data_configuration=external_config, |
| 542 | + ) |
| 543 | + |
| 544 | + result = " ".join(faux_conn.test_data["execute"][-1][0].strip().split()) |
| 545 | + expected = ( |
| 546 | + "CREATE EXTERNAL TABLE `some_table` ( `string_col` STRING )" |
| 547 | + " OPTIONS(format='CSV', uris=['gs://bucket-name/prefix/string_col=A/file.csv'], preserve_ascii_control_characters=true, skip_leading_rows=1)" |
| 548 | + ) |
| 549 | + |
| 550 | + assert result == expected |
| 551 | + |
| 552 | + |
| 553 | +def test_create_external_table_format_parquet_options(faux_conn): |
| 554 | + external_config = ExternalConfig(ExternalSourceFormat.PARQUET) |
| 555 | + external_config.source_uris = ["gs://bucket-name/prefix/string_col=A/file.parquet"] |
| 556 | + |
| 557 | + external_config.parquet_options = ParquetOptions() |
| 558 | + external_config.parquet_options.enable_list_inference = True |
| 559 | + external_config.parquet_options.enum_as_string = False |
| 560 | + |
| 561 | + with pytest.raises(sqlite3.OperationalError): |
| 562 | + # expect table creation to fail as SQLite does not support external tables |
| 563 | + setup_table( |
| 564 | + faux_conn, |
| 565 | + "some_table", |
| 566 | + sqlalchemy.Column("string_col", sqlalchemy.String), |
| 567 | + prefixes=["EXTERNAL"], |
| 568 | + bigquery_external_data_configuration=external_config, |
| 569 | + ) |
| 570 | + |
| 571 | + result = " ".join(faux_conn.test_data["execute"][-1][0].strip().split()) |
| 572 | + expected = ( |
| 573 | + "CREATE EXTERNAL TABLE `some_table` ( `string_col` STRING )" |
| 574 | + " OPTIONS(format='PARQUET', uris=['gs://bucket-name/prefix/string_col=A/file.parquet'], enable_list_inference=true, enum_as_string=false)" |
| 575 | + ) |
| 576 | + |
| 577 | + assert result == expected |
| 578 | + |
| 579 | + |
| 580 | +def test_create_external_table_format_avro_options(faux_conn): |
| 581 | + external_config = ExternalConfig(ExternalSourceFormat.AVRO) |
| 582 | + external_config.source_uris = ["gs://bucket-name/prefix/string_col=A/file.avro"] |
| 583 | + |
| 584 | + external_config.avro_options = AvroOptions() |
| 585 | + external_config.avro_options.use_avro_logical_types = True |
| 586 | + |
| 587 | + with pytest.raises(sqlite3.OperationalError): |
| 588 | + # expect table creation to fail as SQLite does not support external tables |
| 589 | + setup_table( |
| 590 | + faux_conn, |
| 591 | + "some_table", |
| 592 | + sqlalchemy.Column("string_col", sqlalchemy.String), |
| 593 | + prefixes=["EXTERNAL"], |
| 594 | + bigquery_external_data_configuration=external_config, |
| 595 | + ) |
| 596 | + |
| 597 | + result = " ".join(faux_conn.test_data["execute"][-1][0].strip().split()) |
| 598 | + expected = ( |
| 599 | + "CREATE EXTERNAL TABLE `some_table` ( `string_col` STRING )" |
| 600 | + " OPTIONS(format='AVRO', uris=['gs://bucket-name/prefix/string_col=A/file.avro'], use_avro_logical_types=true)" |
| 601 | + ) |
| 602 | + |
| 603 | + assert result == expected |
| 604 | + |
| 605 | + |
464 | 606 | def test_validate_friendly_name_value_type(ddl_compiler): |
465 | 607 | # expect option value to be transformed as a string expression |
466 | 608 |
|
|
0 commit comments