Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit eeb33a9

Browse files
authored
Merge pull request #214 from datafold/pr187
Fix for merging PR #187
2 parents 97c5e6a + d12b8b6 commit eeb33a9

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

data_diff/databases/presto.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,18 @@ class Presto(Database):
4040
def __init__(self, **kw):
4141
prestodb = import_presto()
4242

43-
self._conn = prestodb.dbapi.connect(**kw)
43+
if kw.get("schema"):
44+
self.default_schema = kw.get("schema")
45+
46+
if kw.get("auth") == "basic": # if auth=basic, add basic authenticator for Presto
47+
kw["auth"] = prestodb.auth.BasicAuthentication(kw.pop("user"), kw.pop("password"))
48+
49+
if "cert" in kw: # if a certificate was specified in URI, verify session with cert
50+
cert = kw.pop("cert")
51+
self._conn = prestodb.dbapi.connect(**kw)
52+
self._conn._http_session.verify = cert
53+
else:
54+
self._conn = prestodb.dbapi.connect(**kw)
4455

4556
def quote(self, s: str):
4657
return f'"{s}"'

data_diff/databases/snowflake.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import logging
22

33
from .database_types import *
4-
from .base import Database, import_helper, _query_conn, CHECKSUM_MASK
4+
from .base import ConnectError, Database, import_helper, _query_conn, CHECKSUM_MASK
55

66

77
@import_helper("snowflake")
88
def import_snowflake():
99
import snowflake.connector
10+
from cryptography.hazmat.primitives import serialization
11+
from cryptography.hazmat.backends import default_backend
1012

11-
return snowflake
13+
return snowflake, serialization, default_backend
1214

1315

1416
class Snowflake(Database):
@@ -26,7 +28,7 @@ class Snowflake(Database):
2628
ROUNDS_ON_PREC_LOSS = False
2729

2830
def __init__(self, *, schema: str, **kw):
29-
snowflake = import_snowflake()
31+
snowflake, serialization, default_backend = import_snowflake()
3032
logging.getLogger("snowflake.connector").setLevel(logging.WARNING)
3133

3234
# Got an error: snowflake.connector.network.RetryRequest: could not find io module state (interpreter shutdown?)
@@ -35,10 +37,25 @@ def __init__(self, *, schema: str, **kw):
3537
logging.getLogger("snowflake.connector.network").disabled = True
3638

3739
assert '"' not in schema, "Schema name should not contain quotes!"
38-
self._conn = snowflake.connector.connect(
39-
schema=f'"{schema}"',
40-
**kw,
41-
)
40+
if (
41+
"key" in kw
42+
): # if private keys are used for Snowflake connection, read in key from path specified and pass as "private_key" to connector.
43+
with open(kw.get("key"), "rb") as key:
44+
if 'password' in kw:
45+
raise ConnectError("Cannot use password and key at the same time")
46+
p_key = serialization.load_pem_private_key(
47+
key.read(),
48+
password=None,
49+
backend=default_backend(),
50+
)
51+
52+
kw["private_key"] = p_key.private_bytes(
53+
encoding=serialization.Encoding.DER,
54+
format=serialization.PrivateFormat.PKCS8,
55+
encryption_algorithm=serialization.NoEncryption(),
56+
)
57+
58+
self._conn = snowflake.connector.connect(schema=f'"{schema}"', **kw)
4259

4360
self.default_schema = schema
4461

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ preql = "^0.2.19"
3737
mysql-connector-python = "*"
3838
databricks-sql-connector = "*"
3939
snowflake-connector-python = "*"
40+
cryptography = "*"
4041
trino = "^0.314.0"
4142
psycopg2 = "*"
4243
presto-python-client = "*"
@@ -46,7 +47,7 @@ presto-python-client = "*"
4647
preql = ["preql"]
4748
mysql = ["mysql-connector-python"]
4849
postgresql = ["psycopg2"]
49-
snowflake = ["snowflake-connector-python"]
50+
snowflake = ["snowflake-connector-python", "cryptography"]
5051
presto = ["presto-python-client"]
5152
oracle = ["cx_Oracle"]
5253
databricks = ["databricks-sql-connector"]

0 commit comments

Comments
 (0)