[Jinja] Make Presto template functions backwards compatible (#7993)

This commit is contained in:
Erik Ritter
2019-08-07 09:35:16 -07:00
committed by michellethomas
parent b380879c41
commit cd6de3a1d8
2 changed files with 22 additions and 2 deletions

View File

@@ -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.

View File

@@ -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"