Skip to content

Commit

Permalink
fix: Fix partitioning by DATE column (#1074)
Browse files Browse the repository at this point in the history
Fixes #1056.

Co-authored-by: Chalmer Lowe <chalmerlowe@google.com>
  • Loading branch information
bnaul and chalmerlowe authored May 13, 2024
1 parent 1fc3d74 commit ad69c63
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
5 changes: 5 additions & 0 deletions sqlalchemy_bigquery/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,11 @@ def _process_time_partitioning(
if time_partitioning.field is not None:
field = time_partitioning.field
if isinstance(
table.columns[time_partitioning.field].type,
sqlalchemy.sql.sqltypes.DATE,
):
return f"PARTITION BY {field}"
elif isinstance(
table.columns[time_partitioning.field].type,
sqlalchemy.sql.sqltypes.TIMESTAMP,
):
Expand Down
5 changes: 3 additions & 2 deletions tests/system/test_sqlalchemy_bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,8 @@ def test_dml(engine, session, table_dml):
assert len(result) == 0


def test_create_table(engine, bigquery_dataset):
@pytest.mark.parametrize("time_partitioning_field", ["timestamp_c", "date_c"])
def test_create_table(engine, bigquery_dataset, time_partitioning_field):
meta = MetaData()
Table(
f"{bigquery_dataset}.test_table_create",
Expand All @@ -581,7 +582,7 @@ def test_create_table(engine, bigquery_dataset):
bigquery_friendly_name="test table name",
bigquery_expiration_timestamp=datetime.datetime(2183, 3, 26, 8, 30, 0),
bigquery_time_partitioning=TimePartitioning(
field="timestamp_c",
field=time_partitioning_field,
expiration_ms=1000 * 60 * 60 * 24 * 30, # 30 days
),
bigquery_require_partition_filter=True,
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/test_table_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,24 @@ def test_table_time_partitioning_with_timestamp_dialect_option(faux_conn):
)


def test_table_time_partitioning_with_date_dialect_option(faux_conn):
# expect table creation to fail as SQLite does not support partitioned tables
with pytest.raises(sqlite3.OperationalError):
setup_table(
faux_conn,
"some_table_2",
sqlalchemy.Column("id", sqlalchemy.Integer),
sqlalchemy.Column("createdAt", sqlalchemy.DATE),
bigquery_time_partitioning=TimePartitioning(field="createdAt"),
)

# confirm that the following code creates the correct SQL string
assert " ".join(faux_conn.test_data["execute"][-1][0].strip().split()) == (
"CREATE TABLE `some_table_2` ( `id` INT64, `createdAt` DATE )"
" PARTITION BY createdAt"
)


def test_table_time_partitioning_dialect_option_partition_expiration_days(faux_conn):
# expect table creation to fail as SQLite does not support partitioned tables
with pytest.raises(sqlite3.OperationalError):
Expand Down

0 comments on commit ad69c63

Please sign in to comment.