Skip to content

Support MariaDB JSON Functions #5156

Closed
Closed

Activity

  1. added
    question
    on Feb 16, 2020
  2. zzzeek commented on Feb 16, 2020

    @zzzeek
    Member

    hi there -

    please use func.

    eg. array_agg: https://docs.sqlalchemy.org/en/13/core/functions.html?highlight=array_agg#sqlalchemy.sql.functions.array_agg

    make sure also you specify return type as JSON for the above as it defaults to ARRAY which MariaDB does not seem to have:

    func.array_agg(column, type_=JSON)

    feel free to indicate which specific patterns you are seeking and I can illustrate how to do them and/or locate specific language features which need support. "add all JSON functions" is too general. thanks.

  3. honglei commented on Feb 27, 2020

    @honglei
    Author

    Thanks! I test under sqlite3 3.31.1

    class Resource(Base):
        __tablename__ = 'resource'
        id = Column(Integer, primary_key=True)
        data = Column(JSON)
    
    #Base.metadata.drop_all(engine)
    Base.metadata.create_all(engine)
    
    Session = sessionmaker(bind=engine)
    db_session = Session()
    db_session.execute(Resource.__table__.insert(), [
        {'id': 4, 'data': {"age": 54, "tel": 15046464563, 'name': 'Doe', "passcode": "654567"}},
        {'id': 5, 'data': {"age": "33", "tel": 189776542, "name": "David", "olds": "12", "address": "Hangzhou"}},
        {'id': 3, 'data': ["a", ["b", "c"], "d"]},
    ]
    )
    
    stmt = update(Resource).where(Resource.id == 3).\
        values(data=func.json_replace(Resource.data, '$[2]', 7))
    db_session.execute(stmt)
    
    id3data = db_session.query(Resource.data).filter(Resource.id == 3).scalar()
    print(id3data)
    
    records = db_session.query(Resource.id, func.json_array_length(Resource.data)).filter(
        cast(Resource.data["name"], String) == '"Doe"'
    ).all()
    
    print (records)
  4. zzzeek commented on Feb 27, 2020

    @zzzeek
    Member

    OK, there is no "json_array_length" function on MariaDB that I can find, did you mean "json_length" ? https://mariadb.com/kb/en/json_length/

    your test script works if I use that:

    records = db_session.query(Resource.id, func.json_length(Resource.data)).filter(
        cast(Resource.data["name"], String) == '"Doe"'
    ).all()
    
    

    SQL output:

    SELECT resource.id AS resource_id, json_length(resource.data) AS json_length_1 
    FROM resource 
    WHERE CAST(JSON_EXTRACT(resource.data, %s) AS CHAR) = %s
    2020-02-27 10:31:36,379 INFO sqlalchemy.engine.base.Engine ('$."name"', '"Doe"')
    2020-02-27 10:31:36,384 DEBUG sqlalchemy.engine.base.Engine Col ('resource_id', 'json_length_1')
    2020-02-27 10:31:36,385 DEBUG sqlalchemy.engine.base.Engine Row (4, 4)
    [(4, 4)]
    
    
  5. zzzeek commented on Feb 27, 2020

    @zzzeek
    Member

    just noticed you changed to sqlite all the sudden. json_array_length also renders correctly. Gets a different result, I don't know why that is, but the SQL you ask for is rendered:

    SELECT resource.id AS resource_id, json_array_length(resource.data) AS json_array_length_1 
    FROM resource 
    WHERE CAST(JSON_QUOTE(JSON_EXTRACT(resource.data, ?)) AS VARCHAR) = ?
    2020-02-27 10:35:41,533 INFO sqlalchemy.engine.base.Engine ('$."name"', '"Doe"')
    2020-02-27 10:35:41,533 DEBUG sqlalchemy.engine.base.Engine Col ('resource_id', 'json_array_length_1')
    2020-02-27 10:35:41,533 DEBUG sqlalchemy.engine.base.Engine Row (4, 0)
    [(4, 0)]
    
    
  6. zzzeek commented on Feb 27, 2020

    @zzzeek
    Member

    this is not an ARRAY, it's a mapping:

    https://www.sqlite.org/json1.html#jarraylen

    The json_array_length(X) function returns the number of elements in the JSON array X, or 0 if X is some kind of JSON value other than an array.

    that these functions all work dramatically differently on different backends is why SQLAlchemy can't provide any kind of abstraction layer as far as their features. SQLite does not appear to have a "count how many elements in a json mapping" function.

  7. zzzeek commented on Mar 3, 2020

    @zzzeek
    Member

    no bug illustrated here for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    question

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions