Skip to content

Commit

Permalink
Merge pull request #678 from dbt-labs/replace-redshift-sqlalchemy-wit…
Browse files Browse the repository at this point in the history
…h-dbt-redshift

Cut Redshift client over to dbt-redshift adapter
  • Loading branch information
tlento authored Jul 28, 2023
2 parents 12b11b1 + 6708cca commit 3c74d96
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 75 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/Features-20230726-084615.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Features
body: Enable support for Redshift queries in dbt-metricflow integration
time: 2023-07-26T08:46:15.952401-07:00
custom:
Author: tlento
Issue: "582"
6 changes: 6 additions & 0 deletions metricflow/cli/dbt_connectors/adapter_backed_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from metricflow.protocols.sql_client import SqlEngine
from metricflow.random_id import random_id
from metricflow.sql.render.postgres import PostgresSQLSqlQueryPlanRenderer
from metricflow.sql.render.redshift import RedshiftSqlQueryPlanRenderer
from metricflow.sql.render.snowflake import SnowflakeSqlQueryPlanRenderer
from metricflow.sql.render.sql_plan_renderer import SqlQueryPlanRenderer
from metricflow.sql.sql_bind_parameters import SqlBindParameters
Expand All @@ -28,12 +29,15 @@ class SupportedAdapterTypes(enum.Enum):

POSTGRES = "postgres"
SNOWFLAKE = "snowflake"
REDSHIFT = "redshift"

@property
def sql_engine_type(self) -> SqlEngine:
"""Return the SqlEngine corresponding to the supported adapter type."""
if self is SupportedAdapterTypes.POSTGRES:
return SqlEngine.POSTGRES
elif self is SupportedAdapterTypes.REDSHIFT:
return SqlEngine.REDSHIFT
elif self is SupportedAdapterTypes.SNOWFLAKE:
return SqlEngine.SNOWFLAKE
else:
Expand All @@ -44,6 +48,8 @@ def sql_query_plan_renderer(self) -> SqlQueryPlanRenderer:
"""Return the SqlQueryPlanRenderer corresponding to the supported adapter type."""
if self is SupportedAdapterTypes.POSTGRES:
return PostgresSQLSqlQueryPlanRenderer()
elif self is SupportedAdapterTypes.REDSHIFT:
return RedshiftSqlQueryPlanRenderer()
elif self is SupportedAdapterTypes.SNOWFLAKE:
return SnowflakeSqlQueryPlanRenderer()
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ postgres:
pass: "{{ env_var('MF_SQL_ENGINE_PASSWORD') }}"
dbname: "{{ env_var('MF_SQL_ENGINE_DATABASE') }}"
schema: "{{ env_var('MF_SQL_ENGINE_SCHEMA') }}"
redshift:
target: dev
outputs:
dev:
type: redshift
host: "{{ env_var('MF_SQL_ENGINE_HOST') }}"
port: "{{ env_var('MF_SQL_ENGINE_PORT') | int }}"
user: "{{ env_var('MF_SQL_ENGINE_USER') }}"
password: "{{ env_var('MF_SQL_ENGINE_PASSWORD') }}"
dbname: "{{ env_var('MF_SQL_ENGINE_DATABASE') }}"
schema: "{{ env_var('MF_SQL_ENGINE_SCHEMA') }}"
snowflake:
target: dev
outputs:
Expand Down
5 changes: 3 additions & 2 deletions metricflow/test/fixtures/sql_client_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from metricflow.test.fixtures.sql_clients.databricks import DatabricksSqlClient
from metricflow.test.fixtures.sql_clients.ddl_sql_client import SqlClientWithDDLMethods
from metricflow.test.fixtures.sql_clients.duckdb import DuckDbSqlClient
from metricflow.test.fixtures.sql_clients.redshift import RedshiftSqlClient

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -73,7 +72,9 @@ def make_test_sql_client(url: str, password: str, schema: str) -> SqlClientWithD
dialect = dialect_from_url(url=url)

if dialect == SqlDialect.REDSHIFT:
return RedshiftSqlClient.from_connection_details(url, password)
configure_test_env_from_url(url, schema)
__initialize_dbt()
return AdapterBackedDDLSqlClient(adapter=get_adapter_by_type("redshift"))
elif dialect == SqlDialect.SNOWFLAKE:
parsed_url = configure_test_env_from_url(url, schema)
assert "warehouse" in parsed_url.normalized_query, "Sql engine URL params did not include Snowflake warehouse!"
Expand Down
70 changes: 0 additions & 70 deletions metricflow/test/fixtures/sql_clients/redshift.py

This file was deleted.

2 changes: 1 addition & 1 deletion metricflow/test/integration/test_cases/itest_metrics.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ integration_test:
group_bys: ["metric_time"]
check_query: |
SELECT
(instant_bookings / CAST(bookings AS {{ double_data_type_name }})) + ((bookings - ref_bookings) / CAST(bookings AS {{ double_data_type_name }})) AS instant_plus_non_referred_bookings_pct
(instant_bookings * 1.0 / bookings) + ((bookings - ref_bookings) * 1.0 / bookings) AS instant_plus_non_referred_bookings_pct
, metric_time
FROM (
SELECT
Expand Down
2 changes: 0 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ sql-client-packages = [
"duckdb~=0.8",
"google-auth~=2.13.0",
"google-cloud-bigquery~=3.4.2",
"psycopg2-binary~=2.9.3",
"sqlalchemy-bigquery~=1.6.1",
"sqlalchemy-redshift==0.8.1",
"sqlalchemy2-stubs~=0.0.2a21",
]

Expand Down

0 comments on commit 3c74d96

Please sign in to comment.