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

Exclude partition from BigQuery table name #42130

Merged
merged 1 commit into from
Sep 11, 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
13 changes: 12 additions & 1 deletion airflow/providers/google/cloud/hooks/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -2415,10 +2415,13 @@ def var_print(var_name):
table_id = cmpt[1]
else:
raise ValueError(
f"{var_print(var_name)} Expect format of (<project.|<project:)<dataset>.<table>, "
f"{var_print(var_name)}Expect format of (<project.|<project:)<dataset>.<table>, "
f"got {table_input}"
)

# Exclude partition from the table name
table_id = table_id.split("$")[0]

if project_id is None:
if var_name is not None:
self.log.info(
Expand Down Expand Up @@ -3282,6 +3285,11 @@ def _escape(s: str) -> str:
return e


@deprecated(
planned_removal_date="April 01, 2025",
use_instead="airflow.providers.google.cloud.hooks.bigquery.BigQueryHook.split_tablename",
category=AirflowProviderDeprecationWarning,
)
def split_tablename(
table_input: str, default_project_id: str, var_name: str | None = None
) -> tuple[str, str, str]:
Expand Down Expand Up @@ -3330,6 +3338,9 @@ def var_print(var_name):
f"{var_print(var_name)}Expect format of (<project.|<project:)<dataset>.<table>, got {table_input}"
)

# Exclude partition from the table name
table_id = table_id.split("$")[0]

if project_id is None:
if var_name is not None:
log.info(
Expand Down
71 changes: 64 additions & 7 deletions tests/providers/google/cloud/hooks/test_bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -1030,12 +1030,11 @@ def test_query_results(self, _, selected_fields, result):
== result
)


class TestBigQueryTableSplitter:
def test_internal_need_default_project(self):
def test_split_tablename_internal_need_default_project(self):
with pytest.raises(ValueError, match="INTERNAL: No default project is specified"):
split_tablename("dataset.table", None)
self.hook.split_tablename("dataset.table", None)

@pytest.mark.parametrize("partition", ["$partition", ""])
@pytest.mark.parametrize(
"project_expected, dataset_expected, table_expected, table_input",
[
Expand All @@ -1046,9 +1045,11 @@ def test_internal_need_default_project(self):
("alt1:alt", "dataset", "table", "alt1:alt:dataset.table"),
],
)
def test_split_tablename(self, project_expected, dataset_expected, table_expected, table_input):
def test_split_tablename(
self, project_expected, dataset_expected, table_expected, table_input, partition
):
default_project_id = "project"
project, dataset, table = split_tablename(table_input, default_project_id)
project, dataset, table = self.hook.split_tablename(table_input + partition, default_project_id)
assert project_expected == project
assert dataset_expected == dataset
assert table_expected == table
Expand Down Expand Up @@ -1080,9 +1081,65 @@ def test_split_tablename(self, project_expected, dataset_expected, table_expecte
),
],
)
def test_invalid_syntax(self, table_input, var_name, exception_message):
def test_split_tablename_invalid_syntax(self, table_input, var_name, exception_message):
default_project_id = "project"
with pytest.raises(ValueError, match=exception_message.format(table_input)):
self.hook.split_tablename(table_input, default_project_id, var_name)


class TestBigQueryTableSplitter:
def test_internal_need_default_project(self):
with pytest.raises(AirflowProviderDeprecationWarning):
split_tablename("dataset.table", None)

@pytest.mark.parametrize("partition", ["$partition", ""])
@pytest.mark.parametrize(
"project_expected, dataset_expected, table_expected, table_input",
[
("project", "dataset", "table", "dataset.table"),
("alternative", "dataset", "table", "alternative:dataset.table"),
("alternative", "dataset", "table", "alternative.dataset.table"),
("alt1:alt", "dataset", "table", "alt1:alt.dataset.table"),
("alt1:alt", "dataset", "table", "alt1:alt:dataset.table"),
],
)
def test_split_tablename(
self, project_expected, dataset_expected, table_expected, table_input, partition
):
default_project_id = "project"
with pytest.raises(AirflowProviderDeprecationWarning):
split_tablename(table_input + partition, default_project_id)

@pytest.mark.parametrize(
"table_input, var_name, exception_message",
[
("alt1:alt2:alt3:dataset.table", None, "Use either : or . to specify project got {}"),
(
"alt1.alt.dataset.table",
None,
r"Expect format of \(<project\.\|<project\:\)<dataset>\.<table>, got {}",
),
(
"alt1:alt2:alt.dataset.table",
"var_x",
"Format exception for var_x: Use either : or . to specify project got {}",
),
(
"alt1:alt2:alt:dataset.table",
"var_x",
"Format exception for var_x: Use either : or . to specify project got {}",
),
(
"alt1.alt.dataset.table",
"var_x",
r"Format exception for var_x: Expect format of "
r"\(<project\.\|<project:\)<dataset>.<table>, got {}",
),
],
)
def test_invalid_syntax(self, table_input, var_name, exception_message):
default_project_id = "project"
with pytest.raises(AirflowProviderDeprecationWarning):
split_tablename(table_input, default_project_id, var_name)


Expand Down