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

chore: Add Apache Spark Jinja template processor #28335

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions superset/jinja_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,19 @@ class HiveTemplateProcessor(PrestoTemplateProcessor):
engine = "hive"


class SparkTemplateProcessor(HiveTemplateProcessor):
engine = "spark"

def process_template(self, sql: str, **kwargs: Any) -> str:
template = self.env.from_string(sql)
kwargs.update(self._context)

# Backwards compatibility if migrating from Hive.
context = validate_template_context(self.engine, kwargs)
context["hive"] = context["spark"]
return template.render(context)


class TrinoTemplateProcessor(PrestoTemplateProcessor):
engine = "trino"

Expand All @@ -657,6 +670,7 @@ def process_template(self, sql: str, **kwargs: Any) -> str:
DEFAULT_PROCESSORS = {
"presto": PrestoTemplateProcessor,
"hive": HiveTemplateProcessor,
"spark": SparkTemplateProcessor,
"trino": TrinoTemplateProcessor,
}

Expand Down
17 changes: 17 additions & 0 deletions tests/integration_tests/test_jinja_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,23 @@ def test_template_hive(app_context: AppContext, mocker: MockFixture) -> None:
assert tp.process_template(template) == "the_latest"


def test_template_spark(app_context: AppContext, mocker: MockFixture) -> None:
lp_mock = mocker.patch(
"superset.jinja_context.SparkTemplateProcessor.latest_partition"
)
lp_mock.return_value = "the_latest"
database = mock.Mock()
database.backend = "spark"
template = "{{ spark.latest_partition('my_table') }}"
tp = get_template_processor(database=database)
assert tp.process_template(template) == "the_latest"

# Backwards compatibility if migrating from Hive.
template = "{{ hive.latest_partition('my_table') }}"
tp = get_template_processor(database=database)
assert tp.process_template(template) == "the_latest"


def test_template_trino(app_context: AppContext, mocker: MockFixture) -> None:
lp_mock = mocker.patch(
"superset.jinja_context.TrinoTemplateProcessor.latest_partition"
Expand Down