Apply a function to every query

19 views
Skip to first unread message

Berislav Lopac

unread,
Jun 14, 2019, 6:22:35 AM6/14/19
to sqlalchemy
Hi, does anyone know of a simple way to apply a (SQL) function to each and every query in an ORM model? For example, this article[0] suggests building a lower()-based index for emails in order to normalise them, and then using lower() every time you search for an email: select * from users where lower(email) = lower('PERSO...@example.com').

Is there a way to define a SA model to be able to do that automatically, i.e. to wrap both sides in a function when doing basic queries like get, filter and filter_by? Thanks!

Berislav


Mike Bayer

unread,
Jun 14, 2019, 9:33:49 AM6/14/19
to sqlal...@googlegroups.com


On Fri, Jun 14, 2019, at 6:22 AM, Berislav Lopac wrote:
Hi, does anyone know of a simple way to apply a (SQL) function to each and every query in an ORM model? For example, this article[0] suggests building a lower()-based index for emails in order to normalise them, and then using lower() every time you search for an email: select * from users where lower(email) = lower('PERSO...@example.com').

Is there a way to define a SA model to be able to do that automatically, i.e. to wrap both sides in a function when doing basic queries like get, filter and filter_by? Thanks!

So you are describing two different things.   Apply a SQL function to every *query* is at the query level, and you would use the before_compile hook to add this criteria:




However, if you want a particular column to use lower() on both sides, that's a different question, as it applies only to specific columns using specific datatypes (strings).  The easiest way to get that is to define a custom type, such as:

from sqlalchemy import Column
from sqlalchemy import func
from sqlalchemy import Integer
from sqlalchemy import String
from sqlalchemy import TypeDecorator
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()


class LowerCaseString(TypeDecorator):
    impl = String

    class comparator_factory(TypeDecorator.Comparator):
        def __eq__(self, other):
            return func.lower(self) == func.lower(other)

        def __ne__(self, other):
            return func.lower(self) != func.lower(other)

class A(Base):
    __tablename__ = "a"

    id = Column(Integer, primary_key=True)
    x = Column(LowerCaseString)


print(A.x == 'hi')
print(A.x != 'hi')


docs for this general idea are at https://docs.sqlalchemy.org/en/13/core/custom_types.html#redefining-and-creating-new-operators but this is likely a good example to add




--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
 
 
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jonathan Vanasco

unread,
Jun 14, 2019, 12:30:09 PM6/14/19
to sqlalchemy


On Friday, June 14, 2019 at 9:33:49 AM UTC-4, Mike Bayer wrote:
docs for this general idea are at https://docs.sqlalchemy.org/en/13/core/custom_types.html#redefining-and-creating-new-operators but this is likely a good example to add

Mike- This is actually a great example to add, because in Postgres (and likely other databases) the query planner will only consult a function index if that function is used in the query.

Sorry to sidetrack this for a moment, but can the TypeDecorator support multiple comparisons or would you recommend another SqlAlchemy internal?

The use-case I am concerned with a table that has an index on a 32 char md5 value and function index that is a 6char substring of that md5 value.   Every time I want to query:

     query.filter(Foo.md5 == md5)

I have to write something like the following, so the query planner will use the function index:

     query.filter((Foo.md5 == md5, func.substring(Foo.md5, 0, 6) == md5[:6])

With a few billion records in a table, the difference is a few seconds of processing vs instant results.  Right now I'm automating this with a function, but a TypeDecorator looks to be a much better option.

Mike Bayer

unread,
Jun 14, 2019, 12:49:07 PM6/14/19
to sqlal...@googlegroups.com
you can do that, sure, have the __eq__() method return that whole function.    Hybrid property can do this too w/ a custom comparator as can column_property() with the same idea.    There are Comparator objects all over the place and they are all pluggable.   makes for very difficult stack traces.




--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
 
 
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.

Berislav Lopac

unread,
Jun 14, 2019, 8:11:49 PM6/14/19
to sqlalchemy
Thank you Mike! You're right -- I had in mind the latter but used the terminology more suited to the former.

Berislav
Reply all
Reply to author
Forward
0 new messages