-
Notifications
You must be signed in to change notification settings - Fork 2.2k
add configurable write_mode to BigQueryIOManager #32998
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
Merged
smackesey
merged 14 commits into
dagster-io:master
from
michalcabir-ui:feat/bigquery-write-modes
Mar 10, 2026
Merged
Changes from 2 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1fdbb1e
add configurable write_mode to BigQueryIOManager
michalcabir-ui a5d6cea
add test test_default_write_mode_in_factory
michalcabir-ui 85722af
fix remarks
michalcabir-ui 003c149
fix ci errors
michalcabir-ui 262b24c
Update docs/docs/integrations/libraries/gcp/bigquery/reference.md
michalcabir-ui 2e044f1
Update docs/docs/integrations/libraries/gcp/bigquery/using-bigquery-w…
michalcabir-ui 4eefb2f
fix ci remarks
michalcabir-ui 939bc32
Fix Pyright errors, pass credentials to Spark client, and fix prettie…
michalcabir-ui 23a2484
add test for pyspark error
michalcabir-ui 45fa0d2
ruff fix
michalcabir-ui cc221c2
Merge branch 'master' into feat/bigquery-write-modes
michalcabir-ui 3ec638d
CI erros
michalcabir-ui 99dc34f
Merge branch 'feat/bigquery-write-modes' of https://github.com/michal…
michalcabir-ui b8fbcf3
CI error
michalcabir-ui File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
...dules/libraries/dagster-gcp/dagster_gcp_tests/bigquery_tests/test_bigquery_write_modes.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| from dagster import OutputContext, build_init_resource_context | ||
| from dagster._core.storage.db_io_manager import TableSlice | ||
| from dagster_gcp.bigquery.io_manager import BigQueryClient, BigQueryIOManager, BigQueryWriteMode | ||
|
|
||
|
|
||
| def test_delete_table_slice_truncate(): | ||
| """Test TRUNCATE mode: Should execute TRUNCATE TABLE statement.""" | ||
| client = BigQueryClient(write_mode=BigQueryWriteMode.TRUNCATE) | ||
| mock_conn = MagicMock() | ||
| table_slice = TableSlice(database="my_project", schema="my_dataset", table="my_table") | ||
| context = MagicMock(spec=OutputContext) | ||
|
|
||
| client.delete_table_slice(context, table_slice, mock_conn) | ||
|
|
||
| mock_conn.query.assert_called_once_with( | ||
| "TRUNCATE TABLE `my_project.my_dataset.my_table`" | ||
| ) | ||
|
|
||
| def test_delete_table_slice_replace(): | ||
| """Test REPLACE mode: Should execute DROP TABLE statement.""" | ||
| client = BigQueryClient(write_mode=BigQueryWriteMode.REPLACE) | ||
| mock_conn = MagicMock() | ||
| table_slice = TableSlice(database="my_project", schema="my_dataset", table="my_table") | ||
| context = MagicMock(spec=OutputContext) | ||
|
|
||
| client.delete_table_slice(context, table_slice, mock_conn) | ||
|
|
||
| mock_conn.query.assert_called_once_with( | ||
| "DROP TABLE IF EXISTS `my_project.my_dataset.my_table`" | ||
| ) | ||
|
|
||
| def test_delete_table_slice_append(): | ||
| """Test APPEND mode: Should do NOTHING (no deletion).""" | ||
| client = BigQueryClient(write_mode=BigQueryWriteMode.APPEND) | ||
| mock_conn = MagicMock() | ||
| table_slice = TableSlice(database="my_project", schema="my_dataset", table="my_table") | ||
| context = MagicMock(spec=OutputContext) | ||
|
|
||
| client.delete_table_slice(context, table_slice, mock_conn) | ||
|
|
||
| mock_conn.query.assert_not_called() | ||
|
|
||
| def test_partitioned_table_ignores_write_mode(): | ||
| """Test that partitioned tables ignore the write mode and use legacy cleanup logic.""" | ||
| client = BigQueryClient(write_mode=BigQueryWriteMode.REPLACE) | ||
| mock_conn = MagicMock() | ||
|
|
||
| mock_partition = MagicMock() | ||
| mock_partition.partitions = ["some_value"] | ||
| mock_partition.partition_expr = "my_partition_col" | ||
|
|
||
| table_slice = TableSlice( | ||
| database="my_project", | ||
| schema="my_dataset", | ||
| table="my_table", | ||
| partition_dimensions=[mock_partition] | ||
| ) | ||
| context = MagicMock(spec=OutputContext) | ||
|
|
||
| client.delete_table_slice(context, table_slice, mock_conn) | ||
|
|
||
| args, _ = mock_conn.query.call_args | ||
| query_str = args[0] | ||
|
|
||
| assert "DROP TABLE" not in query_str | ||
| assert "DELETE FROM" in query_str | ||
|
|
||
|
|
||
| class TestBigQueryIOManager(BigQueryIOManager): | ||
| @staticmethod | ||
| def type_handlers(): | ||
| return [] | ||
|
|
||
| @patch("dagster_gcp.bigquery.io_manager.DbIOManager") | ||
| def test_default_write_mode_in_factory(MockDbIOManager): | ||
| """Test that the default write mode propagates correctly from config to client.""" | ||
| context = build_init_resource_context(config={"project": "test-project"}) | ||
|
|
||
| manager_factory = TestBigQueryIOManager(project="test-project") | ||
| iterator = manager_factory.create_io_manager(context) | ||
| next(iterator) | ||
|
|
||
| assert MockDbIOManager.called | ||
| _, kwargs = MockDbIOManager.call_args | ||
| client = kwargs.get("db_client") | ||
|
|
||
| assert client is not None | ||
| assert client.write_mode == BigQueryWriteMode.TRUNCATE | ||
|
|
||
| @patch("dagster_gcp.bigquery.io_manager.DbIOManager") | ||
| def test_explicit_write_mode_in_factory(MockDbIOManager): | ||
| """Test that explicit write mode propagates correctly.""" | ||
| context = build_init_resource_context( | ||
| config={"project": "test-project", "write_mode": "append"} | ||
| ) | ||
|
|
||
| manager_factory = TestBigQueryIOManager(project="test-project", write_mode="append") | ||
| iterator = manager_factory.create_io_manager(context) | ||
| next(iterator) | ||
|
|
||
| _, kwargs = MockDbIOManager.call_args | ||
| client = kwargs.get("db_client") | ||
|
|
||
| assert client is not None | ||
| assert client.write_mode == BigQueryWriteMode.APPEND |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.