diff --git a/docs/sqllab.rst b/docs/sqllab.rst index c60123fa717..c69e5848167 100644 --- a/docs/sqllab.rst +++ b/docs/sqllab.rst @@ -56,7 +56,7 @@ Templating with Jinja SELECT * FROM some_table - WHERE partition_key = '{{ presto.latest_partition('some_table') }}' + WHERE partition_key = '{{ presto.first_latest_partition('some_table') }}' Templating unleashes the power and capabilities of a programming language within your SQL code. diff --git a/superset/jinja_context.py b/superset/jinja_context.py index 97b4dfe30cd..2fdcb1424d7 100644 --- a/superset/jinja_context.py +++ b/superset/jinja_context.py @@ -240,7 +240,25 @@ class PrestoTemplateProcessor(BaseTemplateProcessor): schema, table_name = table_name.split(".") return table_name, schema - def latest_partition(self, table_name: str): + def first_latest_partition(self, table_name: str) -> str: + """ + Gets the first value in the array of all latest partitions + + :param table_name: table name in the format `schema.table` + :return: the first (or only) value in the latest partition array + :raises IndexError: If no partition exists + """ + + return self.latest_partitions(table_name)[0] + + def latest_partitions(self, table_name: str) -> List[str]: + """ + Gets the array of all latest partitions + + :param table_name: table name in the format `schema.table` + :return: the latest partition array + """ + table_name, schema = self._schema_table(table_name, self.schema) return self.database.db_engine_spec.latest_partition( table_name, schema, self.database @@ -252,6 +270,8 @@ class PrestoTemplateProcessor(BaseTemplateProcessor): table_name=table_name, schema=schema, database=self.database, **kwargs ) + latest_partition = first_latest_partition + class HiveTemplateProcessor(PrestoTemplateProcessor): engine = "hive"