Skip to content

Fail to unpickle PickleType #654

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

Closed
baely opened this issue May 4, 2025 · 0 comments · Fixed by #655 or #651
Closed

Fail to unpickle PickleType #654

baely opened this issue May 4, 2025 · 0 comments · Fixed by #655 or #651
Assignees
Labels
api: spanner Issues related to the googleapis/python-spanner-sqlalchemy API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@baely
Copy link

baely commented May 4, 2025

Thanks for stopping by to let us know something could be better!

PLEASE READ: If you have a support contract with Google, please create an issue in the support console instead of filing on GitHub. This will ensure a timely response.

Is this a client library issue or a product issue? We will only be able to assist with issues that pertain to the behaviors of this library. If the issue you're experiencing is due to the behavior of the product itself, please visit the Support page to reach the most relevant engineers.

If the support paths suggested above still do not result in a resolution, please provide the following details.

Environment details

  • Programming language: Python 3.13
  • OS: MacOS 15
  • Language runtime version:
  • Package version: 1.10.0

Steps to reproduce

  1. Define class with column of type sqlalchemy PickleType
  2. Add instance of class to Spanner
  3. Attempt to read the row

Sample:

import os

from sqlalchemy import Column, Integer, String, PickleType, create_engine
from sqlalchemy.orm import DeclarativeBase, Session

class Base(DeclarativeBase):
    pass

class UserPreferences(Base):
    __tablename__ = 'user_preferences'
    
    user_id = Column(Integer, primary_key=True)
    username = Column(String(50), nullable=False)
    preferences = Column(PickleType, nullable=True)
    created_at = Column(String(30), nullable=False)


os.environ["SPANNER_EMULATOR_HOST"] = "localhost:9010"
engine = create_engine("spanner+spanner:///projects/test-project/instances/test-instance/databases/test-database")

with Session(engine) as session:
    new_user = UserPreferences(
        user_id=1,
        username='test_user',
        preferences={
            'setting': 'true'
        },
        created_at="2025-05-04T00:00:00.000000"
    )

    session.add(new_user)
    session.commit()

    user = session.query(UserPreferences).filter_by(user_id=1).first()

Will raise an exception on the last line: _pickle.UnpicklingError: pickle data was truncated

The data is being base64 encoded on the way out from Spanner where SQLAlchemy is expecting raw bytes for pickled data. The data is stored as raw bytes in Spanner but being encoded on the way out.

Image Image

Making sure to follow these steps will guarantee the quickest resolution possible.

Thanks!

@baely baely added priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. labels May 4, 2025
@product-auto-label product-auto-label bot added the api: spanner Issues related to the googleapis/python-spanner-sqlalchemy API. label May 4, 2025
@olavloite olavloite assigned olavloite and unassigned harshachinta May 5, 2025
olavloite added a commit that referenced this issue May 5, 2025
Adds a SpannerPickleType that can be used as the implementation for
the standard SQLAlchemy PickleType. The SpannerPickleType ensures that
the binary values are encoded/decoded to/from base64 strings, which is
how Spanner stores binary values.

Fixes #654
olavloite added a commit that referenced this issue May 7, 2025
Adds a SpannerPickleType that can be used as the implementation for
the standard SQLAlchemy PickleType. The SpannerPickleType ensures that
the binary values are encoded/decoded to/from base64 strings, which is
how Spanner stores binary values.

Fixes #654
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: spanner Issues related to the googleapis/python-spanner-sqlalchemy API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
3 participants