Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enable partitioning by DATE column #1074

Merged
merged 2 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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