Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PoC: Replace string templating with SQLAlchemy #54

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ include_package_data = true
package_dir = = src
install_requires =
snowflake-connector-python~=3.0
snowflake-sqlalchemy
pyyaml
test_requires =
pytest~=6.2

Expand Down
82 changes: 82 additions & 0 deletions src/diepvries/template_sql/hub_link_dml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
from sqlalchemy import create_engine
import yaml

with open("params.yaml", "r") as yamlfile:
parmas = yaml.load(yamlfile, Loader=yaml.FullLoader)

engine = create_engine(
"snowflake://{user}:{password}@{account_identifier}/diepvries_tutorial".format(
user=parmas["user"],
password=parmas["password"],
account_identifier=parmas["account"],

)
)
# try:
# connection = engine.connect()
# results = connection.execute("select current_version()").fetchone()
# print(results[0])
# finally:
# connection.close()
# engine.dispose()


# def set_minimum_timestamp(engine):
# min_timestamp = engine.execute(
# """SELECT
# DATEADD(HOUR, -4, COALESCE(MIN(target.{record_start_timestamp}), CURRENT_TIMESTAMP()))
# FROM {staging_schema}.{staging_table} AS staging
# INNER JOIN {target_schema}.{target_table} AS target
# ON (staging.{source_hashkey_field} = target.{target_hashkey_field})
# )"""
# )
# return min_timestamp


connection = engine.connect()

def fetch_timestamp(engine):
min_timestamp = engine.execute(
"""SELECT create_ts from dv_extract.order_customer"""
).fetchone()
return min_timestamp


def fetch_timestamp_placeholder(engine, params):
min_timestamp = engine.execute(
"""SELECT create_ts from dv_extract.{table}""".format(**params)
).fetchone()
return min_timestamp




params = {"table": "order_customer"}
print(fetch_timestamp_placeholder(engine=engine, params= params))




# def merge_SQL(min_timestamp):
# SQL = """MERGE INTO {target_schema}.{target_table} AS target
# USING (
# SELECT DISTINCT
# {source_hashkey_field},
# -- If multiple sources for the same hashkey are received, their values
# -- are concatenated using a comma.
# LISTAGG(DISTINCT {record_source_field}, ',')
# WITHIN GROUP (ORDER BY {record_source_field})
# OVER (PARTITION BY {source_hashkey_field}) AS {record_source_field},
# {source_fields}
# FROM {staging_schema}.{staging_table}
# ) AS staging ON (target.{target_hashkey_field} = staging.{source_hashkey_field}
# AND target.{record_start_timestamp} >= min_timestamp)
# WHEN NOT MATCHED THEN INSERT ({target_fields})
# VALUES ({staging_source_fields})"""


# sql_load_statement = (
# (TEMPLATES_DIR / "hub_link_dml.sql")
# .read_text()
# .format(**self.sql_placeholders)
# )