Skip to content

Commit fd60bb1

Browse files
fix: Handle reserved words in db, schema and table names with the normalize_name dialect helper
Co-authored-by: Pat Nadolny <[email protected]>
1 parent 0365442 commit fd60bb1

File tree

5 files changed

+82
-10
lines changed

5 files changed

+82
-10
lines changed

Diff for: .github/workflows/test.yml

+8-5
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@ jobs:
2121
fail-fast: false
2222
matrix:
2323
python-version:
24-
- "3.8"
25-
- "3.9"
26-
- "3.10"
27-
- "3.11"
2824
- "3.12"
29-
os: ["ubuntu-latest", "macos-latest", "windows-latest"]
25+
# - "3.11"
26+
# - "3.10"
27+
# - "3.9"
28+
# - "3.8"
29+
os:
30+
- "ubuntu-latest"
31+
# - "macos-latest"
32+
# - "windows-latest"
3033
steps:
3134
- uses: actions/checkout@v4
3235
- name: Set up Python ${{ matrix.python-version }}

Diff for: target_snowflake/connector.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from snowflake.sqlalchemy import URL
1313
from snowflake.sqlalchemy.base import SnowflakeIdentifierPreparer
1414
from snowflake.sqlalchemy.snowdialect import SnowflakeDialect
15-
from sqlalchemy.sql import quoted_name, text
15+
from sqlalchemy.sql import text
1616

1717
from target_snowflake.snowflake_types import NUMBER, TIMESTAMP_NTZ, VARIANT
1818

@@ -61,6 +61,11 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
6161
self.schema_cache: dict = {}
6262
super().__init__(*args, **kwargs)
6363

64+
@property
65+
def dialect(self) -> SnowflakeDialect:
66+
"""Return a Snowflake dialect instance."""
67+
return self._engine.dialect
68+
6469
def get_table_columns(
6570
self,
6671
full_table_name: str,
@@ -388,7 +393,7 @@ def _get_merge_from_stage_statement( # noqa: ANN202
388393
dedup = f"QUALIFY ROW_NUMBER() OVER (PARTITION BY {dedup_cols} ORDER BY SEQ8() DESC) = 1"
389394
return (
390395
text(
391-
f"merge into {quoted_name(full_table_name, quote=True)} d using " # noqa: ISC003
396+
f"merge into {full_table_name} d using " # noqa: ISC003
392397
+ f"(select {json_casting_selects} from '@~/target-snowflake/{sync_id}'" # noqa: S608
393398
+ f"(file_format => {file_format}) {dedup}) s "
394399
+ f"on {join_expr} "

Diff for: target_snowflake/sinks.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
}
3030

3131

32-
class SnowflakeSink(SQLSink):
32+
class SnowflakeSink(SQLSink[SnowflakeConnector]):
3333
"""Snowflake target sink class."""
3434

3535
connector_class = SnowflakeConnector
@@ -64,7 +64,8 @@ def database_name(self) -> str | None:
6464

6565
@property
6666
def table_name(self) -> str:
67-
return super().table_name.upper()
67+
table = super().table_name.upper()
68+
return self.connector.dialect.identifier_preparer.quote_identifier(table)
6869

6970
def setup(self) -> None:
7071
"""Set up Sink.

Diff for: tests/core.py

+62-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import typing as t
34
from pathlib import Path
45

56
import pytest
@@ -25,6 +26,9 @@
2526
)
2627
from singer_sdk.testing.templates import TargetFileTestTemplate
2728

29+
if t.TYPE_CHECKING:
30+
from target_snowflake.connector import SnowflakeConnector
31+
2832

2933
class SnowflakeTargetArrayData(TargetArrayData):
3034
def validate(self) -> None:
@@ -65,7 +69,7 @@ def validate(self) -> None:
6569

6670
class SnowflakeTargetCamelcaseComplexSchema(TargetCamelcaseComplexSchema):
6771
def validate(self) -> None:
68-
connector = self.target.default_sink_class.connector_class(self.target.config)
72+
connector: SnowflakeConnector = self.target.default_sink_class.connector_class(self.target.config)
6973
table = f"{self.target.config['database']}.{self.target.config['default_target_schema']}.ForecastingTypeToCategory".upper() # noqa: E501
7074
table_schema = connector.get_table(table)
7175
expected_types = {
@@ -458,6 +462,61 @@ def setup(self) -> None:
458462
)
459463

460464

465+
class SnowflakeTargetExistingReservedNameTableAlter(TargetFileTestTemplate):
466+
name = "existing_reserved_name_table_alter"
467+
# This sends a schema that will request altering from TIMESTAMP_NTZ to VARCHAR
468+
469+
@property
470+
def singer_filepath(self) -> Path:
471+
current_dir = Path(__file__).resolve().parent
472+
return current_dir / "target_test_streams" / "reserved_words_in_table.singer"
473+
474+
def setup(self) -> None:
475+
connector = self.target.default_sink_class.connector_class(self.target.config)
476+
table = f"{self.target.config['database']}.{self.target.config['default_target_schema']}.\"order\"".upper()
477+
connector.connection.execute(
478+
f"""
479+
CREATE OR REPLACE TABLE {table} (
480+
ID VARCHAR(16777216),
481+
COL_STR VARCHAR(16777216),
482+
COL_TS TIMESTAMP_NTZ(9),
483+
COL_INT STRING,
484+
COL_BOOL BOOLEAN,
485+
COL_VARIANT VARIANT,
486+
_SDC_BATCHED_AT TIMESTAMP_NTZ(9),
487+
_SDC_DELETED_AT VARCHAR(16777216),
488+
_SDC_EXTRACTED_AT TIMESTAMP_NTZ(9),
489+
_SDC_RECEIVED_AT TIMESTAMP_NTZ(9),
490+
_SDC_SEQUENCE NUMBER(38,0),
491+
_SDC_TABLE_VERSION NUMBER(38,0),
492+
PRIMARY KEY (ID)
493+
)
494+
""",
495+
)
496+
497+
498+
class SnowflakeTargetReservedWordsInTable(TargetFileTestTemplate):
499+
# Contains reserved words from
500+
# https://docs.snowflake.com/en/sql-reference/reserved-keywords
501+
# Syncs records then alters schema by adding a non-reserved word column.
502+
name = "reserved_words_in_table"
503+
504+
@property
505+
def singer_filepath(self) -> Path:
506+
current_dir = Path(__file__).resolve().parent
507+
return current_dir / "target_test_streams" / "reserved_words_in_table.singer"
508+
509+
def validate(self) -> None:
510+
connector = self.target.default_sink_class.connector_class(self.target.config)
511+
table = f"{self.target.config['database']}.{self.target.config['default_target_schema']}.\"order\"".upper()
512+
result = connector.connection.execute(
513+
f"select * from {table}",
514+
)
515+
assert result.rowcount == 1
516+
row = result.first()
517+
assert len(row) == 13, f"Row has unexpected length {len(row)}"
518+
519+
461520
class SnowflakeTargetTypeEdgeCasesTest(TargetFileTestTemplate):
462521
name = "type_edge_cases"
463522

@@ -540,6 +599,8 @@ def singer_filepath(self) -> Path:
540599
SnowflakeTargetColonsInColName,
541600
SnowflakeTargetExistingTable,
542601
SnowflakeTargetExistingTableAlter,
602+
SnowflakeTargetExistingReservedNameTableAlter,
603+
SnowflakeTargetReservedWordsInTable,
543604
SnowflakeTargetTypeEdgeCasesTest,
544605
SnowflakeTargetColumnOrderMismatch,
545606
],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{ "type": "SCHEMA", "stream": "order", "schema": { "properties": { "id": { "type": [ "string", "null" ] }, "col_str": { "type": [ "string", "null" ] }, "col_ts": { "format": "date-time", "type": [ "string", "null" ] }, "col_int": { "type": "integer" }, "col_bool": { "type": [ "boolean", "null" ] }, "col_variant": {"type": "object"} }, "type": "object" }, "key_properties": [ "id" ], "bookmark_properties": [ "col_ts" ] }
2+
{ "type": "RECORD", "stream": "order", "record": { "id": "123", "col_str": "foo", "col_ts": "2023-06-13 11:50:04.072", "col_int": 5, "col_bool": true, "col_variant": {"key": "val"} }, "time_extracted": "2023-06-14T18:08:23.074716+00:00" }

0 commit comments

Comments
 (0)