Skip to content

Commit

Permalink
added renderer for SqlGenerateUuidExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamDee committed Dec 7, 2022
1 parent 485fbec commit b677a2b
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 4 deletions.
8 changes: 8 additions & 0 deletions metricflow/sql/render/big_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
SqlExpressionRenderResult,
)
from metricflow.sql.render.sql_plan_renderer import DefaultSqlQueryPlanRenderer
from metricflow.sql.sql_bind_parameters import SqlBindParameters
from metricflow.sql.sql_exprs import (
SqlCastToTimestampExpression,
SqlDateTruncExpression,
SqlGenerateUuidExpression,
SqlTimeDeltaExpression,
)
from metricflow.time.time_granularity import TimeGranularity
Expand Down Expand Up @@ -57,6 +59,12 @@ def visit_time_delta_expr(self, node: SqlTimeDeltaExpression) -> SqlExpressionRe
execution_parameters=column.execution_parameters,
)

def visit_generate_uuid_expr(self, node: SqlGenerateUuidExpression) -> SqlExpressionRenderResult: # noqa: D
return SqlExpressionRenderResult(
sql="GENERATE_UUID()",
execution_parameters=SqlBindParameters(),
)


class BigQuerySqlQueryPlanRenderer(DefaultSqlQueryPlanRenderer):
"""Plan renderer for the BigQuery engine."""
Expand Down
8 changes: 8 additions & 0 deletions metricflow/sql/render/duckdb_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
SqlExpressionRenderResult,
)
from metricflow.sql.render.sql_plan_renderer import DefaultSqlQueryPlanRenderer
from metricflow.sql.sql_bind_parameters import SqlBindParameters
from metricflow.sql.sql_exprs import (
SqlGenerateUuidExpression,
SqlTimeDeltaExpression,
)
from metricflow.time.time_granularity import TimeGranularity
Expand Down Expand Up @@ -32,6 +34,12 @@ def visit_time_delta_expr(self, node: SqlTimeDeltaExpression) -> SqlExpressionRe
execution_parameters=arg_rendered.execution_parameters,
)

def visit_generate_uuid_expr(self, node: SqlGenerateUuidExpression) -> SqlExpressionRenderResult: # noqa: D
return SqlExpressionRenderResult(
sql="GEN_RANDOM_UUID()",
execution_parameters=SqlBindParameters(),
)


class DuckDbSqlQueryPlanRenderer(DefaultSqlQueryPlanRenderer):
"""Plan renderer for the DuckDB engine."""
Expand Down
7 changes: 7 additions & 0 deletions metricflow/sql/render/expr_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
SqlComparisonExpression,
SqlExpressionNode,
SqlFunction,
SqlGenerateUuidExpression,
SqlAggregateFunctionExpression,
SqlNullExpression,
SqlLogicalExpression,
Expand Down Expand Up @@ -314,3 +315,9 @@ def visit_window_function_expr(self, node: SqlWindowFunctionExpression) -> SqlEx
sql=f"{node.sql_function.value}({sql_function_args_string}) OVER ({window_string})",
execution_parameters=combined_params,
)

def visit_generate_uuid_expr(self, node: SqlGenerateUuidExpression) -> SqlExpressionRenderResult: # noqa: D
return SqlExpressionRenderResult(
sql="UUID()",
execution_parameters=SqlBindParameters(),
)
12 changes: 11 additions & 1 deletion metricflow/sql/render/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
SqlExpressionRenderResult,
)
from metricflow.sql.render.sql_plan_renderer import DefaultSqlQueryPlanRenderer
from metricflow.sql.sql_exprs import SqlTimeDeltaExpression
from metricflow.sql.sql_bind_parameters import SqlBindParameters
from metricflow.sql.sql_exprs import (
SqlGenerateUuidExpression,
SqlTimeDeltaExpression,
)
from metricflow.time.time_granularity import TimeGranularity


Expand Down Expand Up @@ -34,6 +38,12 @@ def visit_time_delta_expr(self, node: SqlTimeDeltaExpression) -> SqlExpressionRe
execution_parameters=arg_rendered.execution_parameters,
)

def visit_generate_uuid_expr(self, node: SqlGenerateUuidExpression) -> SqlExpressionRenderResult: # noqa: D
return SqlExpressionRenderResult(
sql="GEN_RANDOM_UUID()",
execution_parameters=SqlBindParameters(),
)


class PostgresSQLSqlQueryPlanRenderer(DefaultSqlQueryPlanRenderer):
"""Plan renderer for the PostgreSQL engine."""
Expand Down
21 changes: 20 additions & 1 deletion metricflow/sql/render/redshift.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from metricflow.sql.render.expr_renderer import DefaultSqlExpressionRenderer, SqlExpressionRenderer
from metricflow.sql.render.expr_renderer import (
DefaultSqlExpressionRenderer,
SqlExpressionRenderer,
SqlExpressionRenderResult,
)
from metricflow.sql.render.sql_plan_renderer import DefaultSqlQueryPlanRenderer
from metricflow.sql.sql_bind_parameters import SqlBindParameters
from metricflow.sql.sql_exprs import SqlGenerateUuidExpression


class RedshiftSqlExpressionRenderer(DefaultSqlExpressionRenderer):
Expand All @@ -10,6 +16,19 @@ def double_data_type(self) -> str:
"""Custom double data type for the Redshift engine"""
return "DOUBLE PRECISION"

def visit_generate_uuid_expr(self, node: SqlGenerateUuidExpression) -> SqlExpressionRenderResult: # noqa: D
"""Generates a "good enough" random key to simulate a UUID.
NOTE: This is a temporary hacky solution as redshift does not have any UUID generation function.
Proposed solutions that requires more thinking:
- create a python UDF (Could we insert this without needing additional permissions?)
"""
return SqlExpressionRenderResult(
sql="CONCAT(CAST(RANDOM()*100000000 AS INT)::VARCHAR,CAST(RANDOM()*100000000 AS INT)::VARCHAR)",
execution_parameters=SqlBindParameters(),
)


class RedshiftSqlQueryPlanRenderer(DefaultSqlQueryPlanRenderer):
"""Plan renderer for the Redshift engine."""
Expand Down
28 changes: 28 additions & 0 deletions metricflow/sql/render/snowflake.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from metricflow.sql.render.expr_renderer import (
DefaultSqlExpressionRenderer,
SqlExpressionRenderer,
SqlExpressionRenderResult,
)
from metricflow.sql.render.sql_plan_renderer import DefaultSqlQueryPlanRenderer
from metricflow.sql.sql_bind_parameters import SqlBindParameters
from metricflow.sql.sql_exprs import SqlGenerateUuidExpression


class SnowflakeSqlExpressionRenderer(DefaultSqlExpressionRenderer):
"""Expression renderer for the Snowflake engine."""

def visit_generate_uuid_expr(self, node: SqlGenerateUuidExpression) -> SqlExpressionRenderResult: # noqa: D
return SqlExpressionRenderResult(
sql="UUID_STRING()",
execution_parameters=SqlBindParameters(),
)


class SnowflakeSqlQueryPlanRenderer(DefaultSqlQueryPlanRenderer):
"""Plan renderer for the Snowflake engine."""

EXPR_RENDERER = SnowflakeSqlExpressionRenderer()

@property
def expr_renderer(self) -> SqlExpressionRenderer: # noqa :D
return self.EXPR_RENDERER
4 changes: 2 additions & 2 deletions metricflow/sql_clients/snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import sqlalchemy
from sqlalchemy.exc import ProgrammingError

from metricflow.sql.render.snowflake import SnowflakeSqlQueryPlanRenderer
from metricflow.protocols.sql_client import SqlEngine, SqlIsolationLevel
from metricflow.protocols.sql_client import SqlEngineAttributes
from metricflow.protocols.sql_request import (
Expand All @@ -21,7 +22,6 @@
MF_EXTRA_TAGS_KEY,
SqlJsonTag,
)
from metricflow.sql.render.sql_plan_renderer import DefaultSqlQueryPlanRenderer
from metricflow.sql.render.sql_plan_renderer import SqlQueryPlanRenderer
from metricflow.sql.sql_bind_parameters import SqlBindParameters
from metricflow.sql_clients.async_request import SqlStatementCommentMetadata, CombinedSqlTags
Expand Down Expand Up @@ -56,7 +56,7 @@ class SnowflakeEngineAttributes:
random_function_name: ClassVar[str] = "RANDOM"

# MetricFlow attributes
sql_query_plan_renderer: ClassVar[SqlQueryPlanRenderer] = DefaultSqlQueryPlanRenderer()
sql_query_plan_renderer: ClassVar[SqlQueryPlanRenderer] = SnowflakeSqlQueryPlanRenderer()


class SnowflakeSqlClient(SqlAlchemySqlClient):
Expand Down

0 comments on commit b677a2b

Please sign in to comment.