Snowflake - Failed to rewrite multi-row insert -(insert into select)

会有一股神秘感。 提交于 2019-12-24 15:19:38

问题


I got below error when use the Sqlalchemy to insert the data into Snowflake warehouse, any idea?

Error:

Failed to rewrite multi-row insert [SQL: 'INSERT INTO widgets (id, name, type)

SELECT %(id)s AS anon_1, %(name)s AS anon_2, widgets.id \nFROM widgets \nWHERE widgets.type = %(card_id)s']

[parameters: ({'id': 2, 'name': 'Lychee', 'card_id': 1}, {'id': 3, 'name': 'testing', 'card_id': 2})]

Code:

from sqlalchemy import *

from snowflake.sqlalchemy import URL

# Helper function for local debugging
def createSQLAlchemyEngine():
    url = URL(
        account='',
        user='',
        password='',
        database='db',
        schema='',
        warehouse='',
        role='',
        proxy_host='',
        proxy_port=8099
    )
    engine = create_engine(url)
    return engine
conn = createSQLAlchemyEngine()
# Construct database
metadata = MetaData()
widgetTypes = Table('widgetTypes', metadata,
                    Column('id', INTEGER(), nullable=True),
                    Column('type', VARCHAR(), nullable=True))
widgets = Table('widgets', metadata,
                Column('id', INTEGER(), nullable=True),
                Column('name', VARCHAR(), nullable=True),
                Column('type', INTEGER(), nullable=True))
engine = conn
metadata.create_all(engine)
# Connect and populate db for testing
conn = engine.connect()
sel = select([bindparam('id'), bindparam('name'), widgets.c.id]).where(widgets.c.type == bindparam('card_id'))
ins = widgets.insert().from_select(['id', 'name', 'type'], sel)
conn.execute(ins, [
    # {'name': 'Melon',  'type_name': 'Squidgy'},
    {'id': 2, 'name': 'Lychee', 'card_id' : 1 },
    {'id': 3, 'name': 'testing', 'card_id': 2}
])
conn.close()

Basically, what Im trying to do this like below, but the Snowflake doesn't support this syntax.

insert into  tableXX

values('Z', 

(select max(b) +1 from tableXX a where a.CD = 'I'), 'val1', 'val2')

So, I have to do with something like this, but got the above error.

insert into tableXX
select 'val1', 'val2', ifnull(max(c), 0) + 1 from tableXX where a.CD = 'I',
select 'val1', 'val2', ifnull(max(c), 0) + 1 from tableXX where a.CD = 'I',

Target:

The logic behind the code is I want to update the sequence_id base on the max of existing sequence_id of record which has the same 'CD' with the new record.

来源:https://stackoverflow.com/questions/55367597/snowflake-failed-to-rewrite-multi-row-insert-insert-into-select

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!