Skip to content

martin1keogh/pybigquery-merge-into

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

77 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pybigquery MERGE INTO support

Disclaimer: this is just me playing around with SQLAlchemy's custom SQL feature.
Use at your own risk, etc.

Presentation

This repository (aims to) add the MERGE INTO feature to SQLAlchemy/pybigquery.

Usage

The main class is pybigquery_merge_into.merge_clause.MergeInto().

Example

>>> 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.

TODO

Write some documentation.

About

Add MERGE INTO support to pybigquery

Resources

License

Stars

Watchers

Forks

Languages