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

Fix validation for BQ label values for BigQueryInsertJobOperator #39568

Merged
merged 2 commits into from
May 12, 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
2 changes: 1 addition & 1 deletion airflow/providers/google/cloud/operators/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@

BIGQUERY_JOB_DETAILS_LINK_FMT = "https://console.cloud.google.com/bigquery?j={job_id}"

LABEL_REGEX = re.compile(r"^[a-z][\w-]{0,63}$")
LABEL_REGEX = re.compile(r"^[\w-]{0,63}$")


class BigQueryUIColors(enum.Enum):
Expand Down
58 changes: 57 additions & 1 deletion tests/providers/google/cloud/operators/test_bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -1921,6 +1921,62 @@ def test_labels_lowercase(self, dag_maker):
assert configuration["labels"]["airflow-dag"] == "yelling_dag_name"
assert configuration["labels"]["airflow-task"] == "yelling_task_id"

def test_labels_starting_with_numbers(self, dag_maker):
configuration = {
"query": {
"query": "SELECT * FROM any",
"useLegacySql": False,
},
}
with dag_maker("123_dag"):
op = BigQueryInsertJobOperator(
task_id="123_task",
configuration=configuration,
location=TEST_DATASET_LOCATION,
project_id=TEST_GCP_PROJECT_ID,
)
op._add_job_labels()
assert configuration["labels"]["airflow-dag"] == "123_dag"
assert configuration["labels"]["airflow-task"] == "123_task"

def test_labels_starting_with_underscore(self, dag_maker):
configuration = {
"query": {
"query": "SELECT * FROM any",
"useLegacySql": False,
},
}
with dag_maker("_dag_starting_with_underscore"):
op = BigQueryInsertJobOperator(
task_id="_task_starting_with_underscore",
configuration=configuration,
location=TEST_DATASET_LOCATION,
project_id=TEST_GCP_PROJECT_ID,
)
op._add_job_labels()
assert "labels" in configuration
assert configuration["labels"]["airflow-dag"] == "_dag_starting_with_underscore"
assert configuration["labels"]["airflow-task"] == "_task_starting_with_underscore"

def test_labels_starting_with_hyphen(self, dag_maker):
configuration = {
"query": {
"query": "SELECT * FROM any",
"useLegacySql": False,
},
}
with dag_maker("-dag-starting-with-hyphen"):
op = BigQueryInsertJobOperator(
task_id="-task-starting-with-hyphen",
configuration=configuration,
location=TEST_DATASET_LOCATION,
project_id=TEST_GCP_PROJECT_ID,
)
op._add_job_labels()
assert "labels" in configuration
assert configuration["labels"]["airflow-dag"] == "-dag-starting-with-hyphen"
assert configuration["labels"]["airflow-task"] == "-task-starting-with-hyphen"

def test_labels_invalid_names(self, dag_maker):
configuration = {
"query": {
Expand All @@ -1938,7 +1994,7 @@ def test_labels_invalid_names(self, dag_maker):
assert "labels" not in configuration

op = BigQueryInsertJobOperator(
task_id="123_task",
task_id="task_id_with_exactly_64_characters_00000000000000000000000000000",
configuration=configuration,
location=TEST_DATASET_LOCATION,
project_id=TEST_GCP_PROJECT_ID,
Expand Down