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

SQL Alchemy and Big Query not Respecting Atomicity of Transactions #1114

Open
barmanroys opened this issue Sep 15, 2024 · 0 comments
Open

SQL Alchemy and Big Query not Respecting Atomicity of Transactions #1114

barmanroys opened this issue Sep 15, 2024 · 0 comments
Assignees
Labels
api: bigquery Issues related to the googleapis/python-bigquery-sqlalchemy API.

Comments

@barmanroys
Copy link

Code
#!/usr/bin/env python3
# encoding: utf-8
import polars as pl
from sqlalchemy import Table, MetaData, func
from sqlalchemy.engine import create_engine
from sqlalchemy.engine.base import Engine
from sqlalchemy.orm import Query, Session
...
frame=pl.DataFrame(data={'data':range(10)})
OUT_TABLE_NAME:str='transaction_test'

with EngineContext(uri=CONNECTION) as engine:
    out_table: Table = Table(OUT_TABLE_NAME, MetaData(bind=engine, schema=SCHEMA_NAME), autoload=True)
    with Session(bind=engine) as session, session.begin():
        for item in frame.to_dicts():
            # Insert values from 0 to 9 sequentially
            print(item['data'])
            if item['data']==5:
                # This to ensure premature termination
                # and making sure the session is rolled back
                raise ValueError 
            statement=out_table.insert(values=[item])
            print(statement)
            session.execute(statement=statement)
            sleep(1)
Description

Insert a sequence of records (each containing an integer) into big query one by one.

Pip Libraries
sqlalchemy==1.4.51 # ORM
sqlalchemy-bigquery==1.9.0 # Dialect for bigquery, need latest Ubuntu version
polars==1.2.1 # Tabular data manipulation
google-cloud-bigquery-storage==2.25.0 # Bigquery storage write client used for inserts
Technology Stack
  • Python 3.8
  • Ubuntu 20.04
Before

Before I run the above code, assume the table (in Google Bigquery) is empty.

After
Expected Outcome

The table remains empty, as the session should be rolled back by SQL Alchemy upon the error.

Observed Outcome

The table contains values from 0 to 4, both inclusive.

@product-auto-label product-auto-label bot added the api: bigquery Issues related to the googleapis/python-bigquery-sqlalchemy API. label Sep 15, 2024
@farhan0102 farhan0102 removed their assignment Sep 30, 2024
@Linchin Linchin assigned Linchin and unassigned Linchin Oct 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: bigquery Issues related to the googleapis/python-bigquery-sqlalchemy API.
Projects
None yet
Development

No branches or pull requests

4 participants