amoghrajesh commented on code in PR #75429:
URL: https://github.com/apache/airflow/pull/75429#discussion_r4716028241
##########
providers/databricks/src/airflow/providers/databricks/operators/databricks.py:
##########
@@ -2063,7 +2063,18 @@ def _convert_to_databricks_workflow_task(
context: Context | None = None,
) -> dict[str, object]:
"""Convert the operator to a Databricks workflow task that can be a
task in a workflow."""
- base_task_json = self._get_task_base_json()
+ if context is not None:
+ fields_to_render = [f for f in self.template_fields if f !=
"workflow_run_metadata"]
+ if fields_to_render:
+ operator_proxy = copy.copy(self)
+ operator_proxy._do_render_template_fields(
Review Comment:
Plain Databricks refs like `{{ job.parameters.x }}` now crash the launch
task, so the whole Databricks job never starts. Today, that job runs and only
the member's airflow task fails, and before `7.14` it worked fully. I suggest
to fix this by - adding the auto-escape to
`DatabricksTaskOperator.render_template_fields` and to `notebook_params` on
`DatabricksNotebookOperator`, then call render_template_fields here instead of
`_do_render_template_fields`
##########
providers/databricks/src/airflow/providers/databricks/operators/databricks.py:
##########
@@ -2370,6 +2381,8 @@ class
DatabricksNotebookOperator(DatabricksTaskBaseOperator):
"""
template_fields = (
+ "notebook_path",
Review Comment:
`notebook_path` and source become templated for standalone tasks too. A path
containing a literal `{{` or `{%` would now crash on single tasks in every era.
These fields aren't needed for #46563, so drop them from this PR.
##########
providers/databricks/tests/unit/databricks/operators/test_databricks_workflow.py:
##########
@@ -647,3 +651,269 @@ def test_downstream_set_after_with_block(self):
tg >> end
nb = dag.get_task("tg.nb")
assert "end" in nb.downstream_task_ids
+
+
+class CustomWorkflowTaskOperator(DatabricksTaskBaseOperator):
+ """Custom operator subclassing DatabricksTaskBaseOperator for GH-46563
reproduction."""
+
+ template_fields = ("application_args", "spark_conf")
+
+ def __init__(self, application_args=None, spark_conf=None, **kwargs):
+ self.application_args = application_args or []
+ self.spark_conf = spark_conf or {}
+ super().__init__(**kwargs)
+
+ def _get_task_base_json(self):
+ return {
+ "spark_jar_task": {
+ "parameters": self.application_args,
+ "spark_conf": self.spark_conf,
+ }
+ }
+
+
+class TestWorkflowTemplateFields:
+ """Tests for template field rendering in DatabricksWorkflowTaskGroup
members (GH-46563)."""
+
+ def test_create_workflow_json_renders_task_operator_template_fields(self):
+ """Test that DatabricksTaskOperator task_config is rendered with
execution context."""
+ with DAG(dag_id="test_dag_task_op", start_date=DEFAULT_DATE,
schedule=None) as dag:
+ with DatabricksWorkflowTaskGroup(
+ group_id="test_tg",
+ databricks_conn_id="databricks_conn",
+ ):
+ DatabricksTaskOperator(
+ task_id="spark_task",
+ task_config={
+ "spark_python_task": {
+ "python_file": "dbfs:/script.py",
+ "parameters": ["--date", "{{ ds }}", "--cluster",
"{{ params.cluster_name }}"],
+ }
+ },
+ )
+
+ launch_op = dag.get_task("test_tg.launch")
+ context = {
+ "ds": "2025-06-15",
+ "params": {"cluster_name": "prod-cluster"},
+ }
+ workflow_json = launch_op.create_workflow_json(context=context)
+ tasks = workflow_json["tasks"]
+ assert len(tasks) == 1
+ assert tasks[0]["spark_python_task"]["parameters"] == [
+ "--date",
+ "2025-06-15",
+ "--cluster",
+ "prod-cluster",
+ ]
+
+ def
test_create_workflow_json_renders_notebook_operator_template_fields(self):
+ """Test that DatabricksNotebookOperator path, source, and params are
rendered."""
+ with DAG(dag_id="test_dag_nb_op", start_date=DEFAULT_DATE,
schedule=None) as dag:
+ with DatabricksWorkflowTaskGroup(
+ group_id="test_tg",
+ databricks_conn_id="databricks_conn",
+ notebook_params={"group_param": "group_{{ ds }}"},
+ ):
+ DatabricksNotebookOperator(
+ task_id="notebook_task",
+ notebook_path="/notebooks/{{ ds }}/etl",
+ source="{{ params.source_type }}",
+ notebook_params={"task_param": "task_{{ ds }}"},
+ )
+
+ launch_op = dag.get_task("test_tg.launch")
+ context = {
+ "ds": "2025-06-15",
+ "params": {"source_type": "WORKSPACE"},
+ }
+ workflow_json = launch_op.create_workflow_json(context=context)
+ tasks = workflow_json["tasks"]
+ assert len(tasks) == 1
+ nb_task = tasks[0]["notebook_task"]
+ assert nb_task["notebook_path"] == "/notebooks/2025-06-15/etl"
+ assert nb_task["source"] == "WORKSPACE"
+ assert nb_task["base_parameters"] == {
+ "task_param": "task_2025-06-15",
+ "group_param": "group_2025-06-15",
+ }
+
+ def
test_create_workflow_json_renders_custom_operator_template_fields(self):
+ """Test that custom DatabricksTaskBaseOperator subclasses render
declared template_fields (GH-46563)."""
+ with DAG(dag_id="test_dag_custom_op", start_date=DEFAULT_DATE,
schedule=None) as dag:
+ with DatabricksWorkflowTaskGroup(
+ group_id="test_tg",
+ databricks_conn_id="databricks_conn",
+ ):
+ CustomWorkflowTaskOperator(
+ task_id="custom_task",
+ application_args=["main.jar", "--date", "{{ ds }}"],
+ spark_conf={"spark.app.name": "app_{{ ds }}"},
+ )
+
+ launch_op = dag.get_task("test_tg.launch")
+ context = {"ds": "2025-06-15"}
+ workflow_json = launch_op.create_workflow_json(context=context)
+ tasks = workflow_json["tasks"]
+ assert len(tasks) == 1
+ assert tasks[0]["spark_jar_task"]["parameters"] == ["main.jar",
"--date", "2025-06-15"]
+ assert tasks[0]["spark_jar_task"]["spark_conf"] == {"spark.app.name":
"app_2025-06-15"}
+
+ def
test_create_workflow_json_without_context_preserves_unrendered_templates(self):
+ """Test that create_workflow_json without context leaves raw templates
intact."""
+ with DAG(dag_id="test_dag_no_ctx", start_date=DEFAULT_DATE,
schedule=None) as dag:
+ with DatabricksWorkflowTaskGroup(
+ group_id="test_tg",
+ databricks_conn_id="databricks_conn",
+ ):
+ DatabricksTaskOperator(
+ task_id="spark_task",
+ task_config={
+ "spark_python_task": {
+ "python_file": "dbfs:/script.py",
+ "parameters": ["--date", "{{ ds }}"],
+ }
+ },
+ )
+
+ launch_op = dag.get_task("test_tg.launch")
+ workflow_json = launch_op.create_workflow_json(context=None)
+ tasks = workflow_json["tasks"]
+ assert tasks[0]["spark_python_task"]["parameters"] == ["--date", "{{
ds }}"]
+
+ def
test_create_workflow_json_does_not_mutate_member_task_attributes_across_runs(self):
+ """Test that multiple workflow JSON generations with different
contexts do not mutate operator state."""
+ with DAG(dag_id="test_dag_mutation_guard", start_date=DEFAULT_DATE,
schedule=None) as dag:
+ with DatabricksWorkflowTaskGroup(
+ group_id="test_tg",
+ databricks_conn_id="databricks_conn",
+ notebook_params={"group_param": "group_{{ ds }}"},
+ ):
+ spark_task = DatabricksTaskOperator(
+ task_id="spark_task",
+ task_config={
+ "spark_python_task": {
+ "python_file": "dbfs:/script.py",
+ "parameters": ["--date", "{{ ds }}"],
+ }
+ },
+ )
+ nb_task = DatabricksNotebookOperator(
+ task_id="notebook_task",
+ notebook_path="/notebooks/{{ ds }}/etl",
+ source="WORKSPACE",
+ notebook_params={"task_param": "task_{{ ds }}"},
+ )
+
+ launch_op = dag.get_task("test_tg.launch")
+
+ # First call with date 1
Review Comment:
Please remove the step comments.
##########
providers/databricks/docs/operators/workflow.rst:
##########
Review Comment:
Add a changelog entry please to say -
- Databricks refs work as-is.
- `{% raw %}` still works.
- `{{run_id}}` means Databricks in groups and Airflow in single tasks.
- The render context is the launch task's.
##########
providers/databricks/src/airflow/providers/databricks/operators/databricks.py:
##########
@@ -2063,7 +2063,18 @@ def _convert_to_databricks_workflow_task(
context: Context | None = None,
) -> dict[str, object]:
"""Convert the operator to a Databricks workflow task that can be a
task in a workflow."""
- base_task_json = self._get_task_base_json()
+ if context is not None:
+ fields_to_render = [f for f in self.template_fields if f !=
"workflow_run_metadata"]
+ if fields_to_render:
+ operator_proxy = copy.copy(self)
+ operator_proxy._do_render_template_fields(
Review Comment:
Also, the legacy `{{run_id}}` in a group member used to get the Databricks
run id. Now it silently gets airflow `run_id`, which matches neither `7.25` nor
pre-`7.14` group behavior. The fix would be to escape `run_id` in group
renders and log a warning that points to `{{ job.run_id }}` or `{{
dag_run.run_id }}`
##########
providers/databricks/src/airflow/providers/databricks/operators/databricks.py:
##########
@@ -2063,7 +2063,18 @@ def _convert_to_databricks_workflow_task(
context: Context | None = None,
) -> dict[str, object]:
"""Convert the operator to a Databricks workflow task that can be a
task in a workflow."""
- base_task_json = self._get_task_base_json()
+ if context is not None:
+ fields_to_render = [f for f in self.template_fields if f !=
"workflow_run_metadata"]
+ if fields_to_render:
+ operator_proxy = copy.copy(self)
Review Comment:
Here and line 2500, `copy.copy(self)` isn't locked, so `__setattr__` writes
rendered values back into the original's `__init_kwargs`. Use
`self.prepare_for_execution()` instead.
##########
providers/databricks/tests/unit/databricks/operators/test_databricks_workflow.py:
##########
@@ -647,3 +651,269 @@ def test_downstream_set_after_with_block(self):
tg >> end
nb = dag.get_task("tg.nb")
assert "end" in nb.downstream_task_ids
+
+
+class CustomWorkflowTaskOperator(DatabricksTaskBaseOperator):
+ """Custom operator subclassing DatabricksTaskBaseOperator for GH-46563
reproduction."""
Review Comment:
+1 to this.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]