|
| 1 | +from typing import Union |
| 2 | + |
| 3 | +import ai.chronon.utils as c_utils |
| 4 | +from ai.chronon.api.common.ttypes import TableDependency, TableInfo, TimeUnit, Window |
| 5 | +from ai.chronon.api.ttypes import BootstrapPart, GroupBy, JoinPart, Source |
| 6 | + |
| 7 | +""" |
| 8 | +Given a node that represents an upstream, turn it into a TableDependency. |
| 9 | +""" |
| 10 | + |
| 11 | + |
| 12 | +def to_dependency(node: Union[Source, GroupBy, JoinPart, BootstrapPart], lag: int = 0) -> TableDependency: |
| 13 | + if isinstance(node, JoinPart): |
| 14 | + groupby_dep = to_dependency(node.groupBy, lag=lag) |
| 15 | + return groupby_dep |
| 16 | + elif isinstance(node, GroupBy): |
| 17 | + ti = node.metaData.executionInfo.outputTableInfo |
| 18 | + return TableDependency( |
| 19 | + tableInfo=ti, |
| 20 | + startOffset=Window(timeUnit=TimeUnit.DAYS, length=0), |
| 21 | + endOffset=Window(timeUnit=TimeUnit.DAYS, length=0), |
| 22 | + startCutOff=None, |
| 23 | + endCutOff=None, |
| 24 | + forceCompute=False, |
| 25 | + ) |
| 26 | + elif isinstance(node, BootstrapPart): |
| 27 | + partition_column = node.query.partitionColumn if node.query is not None else None |
| 28 | + table_info = TableInfo( |
| 29 | + table=node.table, |
| 30 | + partitionColumn=partition_column, |
| 31 | + ) |
| 32 | + start_cutoff = node.query.startPartition if node.query is not None else None |
| 33 | + end_cutoff = node.query.endPartition if node.query is not None else None |
| 34 | + return TableDependency( |
| 35 | + tableInfo=table_info, |
| 36 | + startOffset=Window(timeUnit=TimeUnit.DAYS, length=0), |
| 37 | + endOffset=Window(timeUnit=TimeUnit.DAYS, length=0), |
| 38 | + startCutOff=start_cutoff, |
| 39 | + endCutOff=end_cutoff, |
| 40 | + forceCompute=False, |
| 41 | + ) |
| 42 | + else: # When type of node is a Source |
| 43 | + table_name = c_utils.get_table(node) |
| 44 | + query = c_utils.get_query(node) |
| 45 | + partition_column = query.partitionColumn |
| 46 | + table_info = TableInfo( |
| 47 | + table=table_name, |
| 48 | + partitionColumn=partition_column, |
| 49 | + ) |
| 50 | + start_cutoff = query.startPartition if query is not None else None |
| 51 | + end_cutoff = query.endPartition if query is not None else None |
| 52 | + return TableDependency( |
| 53 | + tableInfo=table_info, |
| 54 | + startOffset=Window(timeUnit=TimeUnit.DAYS, length=0), |
| 55 | + endOffset=Window(timeUnit=TimeUnit.DAYS, length=0), |
| 56 | + startCutOff=start_cutoff, |
| 57 | + endCutOff=end_cutoff, |
| 58 | + forceCompute=False, |
| 59 | + ) |
0 commit comments