Skip to content

Commit

Permalink
/* PR_START p--cte 00 */ Make SqlTable.schema_name optional.
Browse files Browse the repository at this point in the history
  • Loading branch information
plypaul committed Oct 15, 2024
1 parent 15a6ae7 commit 1bf1244
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
28 changes: 15 additions & 13 deletions metricflow-semantics/metricflow_semantics/sql/sql_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(".")
Expand All @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 1 addition & 1 deletion metricflow/sql/sql_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1bf1244

Please sign in to comment.