Skip to content

Commit

Permalink
/* PR_START p--cte 07 */ Replace is_table with as_sql_table_node.
Browse files Browse the repository at this point in the history
  • Loading branch information
plypaul committed Nov 4, 2024
1 parent d01cbea commit c7c7b6b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
4 changes: 2 additions & 2 deletions metricflow/sql/render/sql_plan_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def _render_from_section(self, from_source: SqlQueryPlanNode, from_source_alias:
from_render_result = self._render_node(from_source)

from_section_lines = []
if from_source.is_table:
if from_source.as_sql_table_node is not None:
from_section_lines.append(f"FROM {from_render_result.sql} {from_source_alias}")
else:
from_section_lines.append("FROM (")
Expand Down Expand Up @@ -228,7 +228,7 @@ def _render_joins_section(self, join_descriptions: Sequence[SqlJoinDescription])
on_condition_rendered = self.EXPR_RENDERER.render_sql_expr(join_description.on_condition)
params = params.merge(on_condition_rendered.bind_parameter_set)

if join_description.right_source.is_table:
if join_description.right_source.as_sql_table_node is not None:
join_section_lines.append(join_description.join_type.value)
join_section_lines.append(
textwrap.indent(
Expand Down
50 changes: 26 additions & 24 deletions metricflow/sql/sql_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ def accept(self, visitor: SqlQueryPlanNodeVisitor[VisitorOutputT]) -> VisitorOut

@property
@abstractmethod
def is_table(self) -> bool:
"""Returns whether this node resolves to a table (vs. a query)."""
def as_select_node(self) -> Optional[SqlSelectStatementNode]:
"""If possible, return this as a select statement node."""
raise NotImplementedError

@property
@abstractmethod
def as_select_node(self) -> Optional[SqlSelectStatementNode]:
"""If possible, return this as a select statement node."""
def as_sql_table_node(self) -> Optional[SqlTableNode]:
"""If possible, return this as SQL table node."""
raise NotImplementedError


Expand Down Expand Up @@ -192,14 +192,15 @@ def displayed_properties(self) -> Sequence[DisplayedProperty]: # noqa: D102
def accept(self, visitor: SqlQueryPlanNodeVisitor[VisitorOutputT]) -> VisitorOutputT: # noqa: D102
return visitor.visit_select_statement_node(self)

@property
def is_table(self) -> bool: # noqa: D102
return False

@property
def as_select_node(self) -> Optional[SqlSelectStatementNode]: # noqa: D102
return self

@property
@override
def as_sql_table_node(self) -> Optional[SqlTableNode]:
return None

@property
@override
def description(self) -> str:
Expand Down Expand Up @@ -234,14 +235,15 @@ def displayed_properties(self) -> Sequence[DisplayedProperty]: # noqa: D102
def accept(self, visitor: SqlQueryPlanNodeVisitor[VisitorOutputT]) -> VisitorOutputT: # noqa: D102
return visitor.visit_table_node(self)

@property
def is_table(self) -> bool: # noqa: D102
return True

@property
def as_select_node(self) -> Optional[SqlSelectStatementNode]: # noqa: D102
return None

@property
@override
def as_sql_table_node(self) -> Optional[SqlTableNode]:
return self


@dataclass(frozen=True, eq=False)
class SqlSelectQueryFromClauseNode(SqlQueryPlanNode):
Expand Down Expand Up @@ -272,11 +274,12 @@ def accept(self, visitor: SqlQueryPlanNodeVisitor[VisitorOutputT]) -> VisitorOut
return visitor.visit_query_from_clause_node(self)

@property
def is_table(self) -> bool: # noqa: D102
return False
def as_select_node(self) -> Optional[SqlSelectStatementNode]: # noqa: D102
return None

@override
@property
def as_select_node(self) -> Optional[SqlSelectStatementNode]: # noqa: D102
def as_sql_table_node(self) -> Optional[SqlTableNode]:
return None


Expand All @@ -286,7 +289,6 @@ class SqlCreateTableAsNode(SqlQueryPlanNode):
Attributes:
sql_table: The SQL table to create.
parent_node: The parent query plan node.
"""

sql_table: SqlTable
Expand All @@ -308,12 +310,12 @@ def accept(self, visitor: SqlQueryPlanNodeVisitor[VisitorOutputT]) -> VisitorOut

@property
@override
def is_table(self) -> bool:
return False
def as_select_node(self) -> Optional[SqlSelectStatementNode]:
return None

@property
@override
def as_select_node(self) -> Optional[SqlSelectStatementNode]:
def as_sql_table_node(self) -> Optional[SqlTableNode]:
return None

@property
Expand Down Expand Up @@ -356,11 +358,11 @@ def render_node(self) -> SqlQueryPlanNode: # noqa: D102
class SqlCteNode(SqlQueryPlanNode):
"""Represents a single common table expression."""

select_statement: SqlSelectStatementNode
select_statement: SqlQueryPlanNode
cte_alias: str

@staticmethod
def create(select_statement: SqlSelectStatementNode, cte_alias: str) -> SqlCteNode: # noqa: D102
def create(select_statement: SqlQueryPlanNode, cte_alias: str) -> SqlCteNode: # noqa: D102
return SqlCteNode(
parent_nodes=(select_statement,),
select_statement=select_statement,
Expand All @@ -373,12 +375,12 @@ def accept(self, visitor: SqlQueryPlanNodeVisitor[VisitorOutputT]) -> VisitorOut

@property
@override
def is_table(self) -> bool:
return False
def as_select_node(self) -> Optional[SqlSelectStatementNode]:
return None

@property
@override
def as_select_node(self) -> Optional[SqlSelectStatementNode]:
def as_sql_table_node(self) -> Optional[SqlTableNode]:
return None

@property
Expand Down

0 comments on commit c7c7b6b

Please sign in to comment.