Skip to content

Commit ad69c63

Browse files
bnaulchalmerlowe
andauthored
fix: Fix partitioning by DATE column (googleapis#1074)
Fixes googleapis#1056. Co-authored-by: Chalmer Lowe <chalmerlowe@google.com>
1 parent 1fc3d74 commit ad69c63

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

sqlalchemy_bigquery/base.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,11 @@ def _process_time_partitioning(
831831
if time_partitioning.field is not None:
832832
field = time_partitioning.field
833833
if isinstance(
834+
table.columns[time_partitioning.field].type,
835+
sqlalchemy.sql.sqltypes.DATE,
836+
):
837+
return f"PARTITION BY {field}"
838+
elif isinstance(
834839
table.columns[time_partitioning.field].type,
835840
sqlalchemy.sql.sqltypes.TIMESTAMP,
836841
):

tests/system/test_sqlalchemy_bigquery.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,8 @@ def test_dml(engine, session, table_dml):
561561
assert len(result) == 0
562562

563563

564-
def test_create_table(engine, bigquery_dataset):
564+
@pytest.mark.parametrize("time_partitioning_field", ["timestamp_c", "date_c"])
565+
def test_create_table(engine, bigquery_dataset, time_partitioning_field):
565566
meta = MetaData()
566567
Table(
567568
f"{bigquery_dataset}.test_table_create",
@@ -581,7 +582,7 @@ def test_create_table(engine, bigquery_dataset):
581582
bigquery_friendly_name="test table name",
582583
bigquery_expiration_timestamp=datetime.datetime(2183, 3, 26, 8, 30, 0),
583584
bigquery_time_partitioning=TimePartitioning(
584-
field="timestamp_c",
585+
field=time_partitioning_field,
585586
expiration_ms=1000 * 60 * 60 * 24 * 30, # 30 days
586587
),
587588
bigquery_require_partition_filter=True,

tests/unit/test_table_options.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,24 @@ def test_table_time_partitioning_with_timestamp_dialect_option(faux_conn):
193193
)
194194

195195

196+
def test_table_time_partitioning_with_date_dialect_option(faux_conn):
197+
# expect table creation to fail as SQLite does not support partitioned tables
198+
with pytest.raises(sqlite3.OperationalError):
199+
setup_table(
200+
faux_conn,
201+
"some_table_2",
202+
sqlalchemy.Column("id", sqlalchemy.Integer),
203+
sqlalchemy.Column("createdAt", sqlalchemy.DATE),
204+
bigquery_time_partitioning=TimePartitioning(field="createdAt"),
205+
)
206+
207+
# confirm that the following code creates the correct SQL string
208+
assert " ".join(faux_conn.test_data["execute"][-1][0].strip().split()) == (
209+
"CREATE TABLE `some_table_2` ( `id` INT64, `createdAt` DATE )"
210+
" PARTITION BY createdAt"
211+
)
212+
213+
196214
def test_table_time_partitioning_dialect_option_partition_expiration_days(faux_conn):
197215
# expect table creation to fail as SQLite does not support partitioned tables
198216
with pytest.raises(sqlite3.OperationalError):

0 commit comments

Comments
 (0)