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

SNOW-638839: MERGE INTO does not render argument correctly #322

Closed
luciengaitskell opened this issue Jul 21, 2022 · 3 comments · May be fixed by #321
Closed

SNOW-638839: MERGE INTO does not render argument correctly #322

luciengaitskell opened this issue Jul 21, 2022 · 3 comments · May be fixed by #321
Labels
bug Something isn't working needs triage Stale

Comments

@luciengaitskell
Copy link

Please answer these questions before submitting your issue. Thanks!

  1. What version of Python are you using?

    3.9.10

  2. What operating system and processor architecture are you using?

    Linux-5.10.0-16-cloud-amd64-x86_64-with-glibc2.31

  3. What are the component versions in the environment (pip freeze)?

    SQLAlchemy==1.4.31
    snowflake-connector-python==2.7.8
    snowflake-sqlalchemy==1.3.4
    
  4. What did you do?

from snowflake.sqlalchemy import MergeInto, dialect
from sqlalchemy import Column, Integer, MetaData, Table, select
from sqlalchemy.orm import aliased

meta = MetaData()

target_table = Table(
    "target_table",
    meta,
    Column("a", Integer),
)

source_table = aliased(
    Table(
        "some_table",
        meta,
        Column("a", Integer),
        Column("b", Integer),
    ),
    name="source_table",
)

# Example subquery if more processing needed ontop:
source = select(
    source_table.c["a"].label("a"),
    source_table.c["b"].label("b"),
).subquery("source")

op = MergeInto(target_table, source, target_table.c.a == source.c.a)

print(op.compile(dialect=dialect()))

Result:

MERGE INTO target_table USING SELECT source_table.a AS a, source_table.b AS b 
FROM some_table AS source_table ON target_table.a = source.a
  1. What did you expect to see?
MERGE INTO target_table USING (SELECT source_table.a AS a, source_table.b AS b 
FROM some_table AS source_table) AS source ON target_table.a = source.a

6. Can you set logging to DEBUG and collect the logs?

import logging
import os

for logger_name in ['snowflake.sqlalchemy', 'snowflake.connector']:
   logger = logging.getLogger(logger_name)
   logger.setLevel(logging.DEBUG)
   ch = logging.StreamHandler()
   ch.setLevel(logging.DEBUG)
   ch.setFormatter(logging.Formatter('%(asctime)s - %(threadName)s %(filename)s:%(lineno)d - %(funcName)s() - %(levelname)s - %(message)s'))
   logger.addHandler(ch)

The problem is already debugged and addrssed in #321

@sfc-gh-mkeller
Copy link
Collaborator

recreate jira

@github-actions github-actions bot changed the title MERGE INTO does not render argument correctly SNOW-638839: MERGE INTO does not render argument correctly Aug 2, 2022
@github-actions github-actions bot added the Stale label Apr 5, 2023
@github-actions
Copy link

github-actions bot commented Apr 5, 2023

To clean up and re-prioritize bugs and feature requests we are closing all issues older than 6 months as of Apr 1, 2023. If there are any issues or feature requests that you would like us to address, please re-create them. For urgent issues, opening a support case with this link Snowflake Community is the fastest way to get a response

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Apr 5, 2023
@ma0c
Copy link

ma0c commented May 2, 2024

For anybody having this issue, you can manually overwrite the behavior of visit_merge_into using a custom compiler, something like

from sqlalchemy.ext.compiler import compiles
from snowflake.sqlalchemy import MergeInto

@compiles(MergeInto, "snowflake")
def visit_merge_into(merge_into, compiler, **kw):
    clauses = " ".join(clause._compiler_dispatch(compiler, **kw) for clause in merge_into.clauses)
    source = merge_into.source._compiler_dispatch(compiler, **kw)
    on = merge_into.on._compiler_dispatch(compiler, **kw)
    return f"MERGE INTO {merge_into.target} USING ({source}) {merge_into.source.name} ON {on}" + (
        " " + clauses if clauses else ""
    )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs triage Stale
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants