From 1bf1244a4ae0a1f65e780cf052e0b451d3b025b1 Mon Sep 17 00:00:00 2001 From: Paul Yang Date: Mon, 14 Oct 2024 13:48:02 -0700 Subject: [PATCH] /* PR_START p--cte 00 */ Make `SqlTable.schema_name` optional. --- .../metricflow_semantics/sql/sql_table.py | 28 ++++++++++--------- .../time/time_spine_source.py | 2 +- .../sql/test_sql_table.py | 9 ++++++ metricflow/sql/sql_column.py | 2 +- 4 files changed, 26 insertions(+), 15 deletions(-) create mode 100644 metricflow-semantics/tests_metricflow_semantics/sql/test_sql_table.py diff --git a/metricflow-semantics/metricflow_semantics/sql/sql_table.py b/metricflow-semantics/metricflow_semantics/sql/sql_table.py index 78e2a66d1a..e5bebf6620 100644 --- a/metricflow-semantics/metricflow_semantics/sql/sql_table.py +++ b/metricflow-semantics/metricflow_semantics/sql/sql_table.py @@ -2,23 +2,28 @@ from dataclasses import dataclass from enum import Enum -from typing import Optional, Tuple, Union +from typing import Optional class SqlTableType(Enum): # noqa: D101 TABLE = "table" VIEW = "view" + # CTE type may be added later. @dataclass(frozen=True, order=True) class SqlTable: """Represents a reference to a SQL table.""" - schema_name: str + schema_name: Optional[str] table_name: str db_name: Optional[str] = None table_type: SqlTableType = SqlTableType.TABLE + def __post_init__(self) -> None: # noqa: D105 + if self.db_name is not None: + assert self.schema_name is not None, "`schema_name` is required with `db_name`" + @staticmethod def from_string(sql_str: str) -> SqlTable: # noqa: D102 sql_str_split = sql_str.split(".") @@ -34,14 +39,11 @@ def from_string(sql_str: str) -> SqlTable: # noqa: D102 @property def sql(self) -> str: """Return the snippet that can be used for use in SQL queries.""" - if self.db_name: - return f"{self.db_name}.{self.schema_name}.{self.table_name}" - return f"{self.schema_name}.{self.table_name}" - - @property - def parts_tuple(self) -> Union[Tuple[str, str], Tuple[str, str, str]]: - """Return a tuple of the sql table parts.""" - if self.db_name: - return (self.db_name, self.schema_name, self.table_name) - else: - return (self.schema_name, self.table_name) + items = [] + if self.db_name is not None: + items.append(self.db_name) + if self.schema_name is not None: + items.append(self.schema_name) + items.append(self.table_name) + + return ".".join(items) diff --git a/metricflow-semantics/metricflow_semantics/time/time_spine_source.py b/metricflow-semantics/metricflow_semantics/time/time_spine_source.py index ddc6e17534..f55967d3eb 100644 --- a/metricflow-semantics/metricflow_semantics/time/time_spine_source.py +++ b/metricflow-semantics/metricflow_semantics/time/time_spine_source.py @@ -24,7 +24,7 @@ class TimeSpineSource: Dates should be contiguous. May also contain custom granularity columns. """ - schema_name: str + schema_name: Optional[str] table_name: str = "mf_time_spine" # Name of the column in the table that contains date/time values that map to a standard granularity. base_column: str = "ds" diff --git a/metricflow-semantics/tests_metricflow_semantics/sql/test_sql_table.py b/metricflow-semantics/tests_metricflow_semantics/sql/test_sql_table.py new file mode 100644 index 0000000000..5d884b2ce6 --- /dev/null +++ b/metricflow-semantics/tests_metricflow_semantics/sql/test_sql_table.py @@ -0,0 +1,9 @@ +from __future__ import annotations + +from metricflow_semantics.sql.sql_table import SqlTable + + +def test_sql_table() -> None: # noqa: D102 + assert SqlTable(schema_name=None, table_name="table", db_name=None).sql == "table" + assert SqlTable(schema_name="schema", table_name="table", db_name=None).sql == "schema.table" + assert SqlTable(schema_name="schema", table_name="table", db_name="db").sql == "db.schema.table" diff --git a/metricflow/sql/sql_column.py b/metricflow/sql/sql_column.py index 2765d36a7d..ff515bbcb5 100644 --- a/metricflow/sql/sql_column.py +++ b/metricflow/sql/sql_column.py @@ -35,7 +35,7 @@ def db_name(self) -> Optional[str]: # noqa: D102 return self.table.db_name @property - def schema_name(self) -> str: # noqa: D102 + def schema_name(self) -> Optional[str]: # noqa: D102 return self.table.schema_name @property