Disclaimer: this is just me playing around with SQLAlchemy's custom SQL feature.
Use at your own risk, etc.
This repository (aims to) add the MERGE INTO feature to SQLAlchemy/pybigquery.
The main class is pybigquery_merge_into.merge_clause.MergeInto()
.
>>> query = MergeInto(
target=target,
source=source, # this could also be a subquery
onclause=target.c.t1 == source.c.s1,
when_clauses=[
WhenMatched(update(target).values({target.c.t2: source.c.s2 + timedelta(days=1).days})),
WhenNotMatched(insert(target)),
WhenNotMatchedBySource(delete(target), condition=target.c.t2 > date.today()),
]
)
>>> print(str(query.compile(dialect=BigQueryDialect())))
MERGE INTO `target`
USING `source`
ON `target`.`t1` = `source`.`s1`
WHEN MATCHED THEN
UPDATE SET `t2`=(`source`.`s2` + :s2_1)
WHEN NOT MATCHED BY TARGET THEN
INSERT ROW
WHEN NOT MATCHED BY SOURCE AND `target`.`t2` > :t2_1 THEN
DELETE
See the tests for more examples.
Write some documentation.