Skip to content

Commit

Permalink
Comply with SQL Alchemy implementation style for partition by clause
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-lcalderonachio committed Nov 19, 2024
1 parent 0244b2c commit c9f90d9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/snowflake/sqlalchemy/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,9 @@ def visit_copy_into(self, copy_into, **kw):
from_ = f"({copy_into.from_._compiler_dispatch(self, **kw)})"

partition_by = ""
if copy_into.partition_by is not None:
if isinstance(copy_into.partition_by, str):
partition_by = f"PARTITION BY '{copy_into.partition_by}'"
elif copy_into.partition_by is not None:
partition_by = f"PARTITION BY {copy_into.partition_by}"

credentials, encryption = "", ""
Expand Down
16 changes: 14 additions & 2 deletions tests/test_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest
from sqlalchemy import Column, Integer, MetaData, Sequence, String, Table
from sqlalchemy.sql import select, text
from sqlalchemy.sql import functions, select, text

from snowflake.sqlalchemy import (
AWSBucket,
Expand Down Expand Up @@ -154,13 +154,25 @@ def test_copy_into_location(engine_testaccount, sql_compiler):
copy_stmt_8 = CopyIntoStorage(
from_=food_items,
into=ExternalStage(name="stage_name"),
partition_by="('YEAR=' || year)",
partition_by=text("('YEAR=' || year)"),
)
assert (
sql_compiler(copy_stmt_8)
== "COPY INTO @stage_name FROM python_tests_foods PARTITION BY ('YEAR=' || year) "
)

copy_stmt_9 = CopyIntoStorage(
from_=food_items,
into=ExternalStage(name="stage_name"),
partition_by=functions.concat(
text("'YEAR='"), text(food_items.columns["name"].name)
),
)
assert (
sql_compiler(copy_stmt_9)
== "COPY INTO @stage_name FROM python_tests_foods PARTITION BY concat('YEAR=', name) "
)

# NOTE Other than expect known compiled text, submit it to RegressionTests environment and expect them to fail, but
# because of the right reasons
acceptable_exc_reasons = {
Expand Down

0 comments on commit c9f90d9

Please sign in to comment.