Search code examples
pythonsqlpostgresqlsqlalchemyblaze

calling SQL functions from Blaze


In particular I would like to call the Postgres levenshtein function. I would like to write the blaze query to return words similar to the word 'similar', ie the equivalent of:

select word from wordtable where levenshtein(word, 'similar') < 3;

In Blaze this should look something like

db.wordtable.word[levenshtein(db.wordtable.word, 'similar') < 3]

but levenshtein is not defined in any module I am importing on the python side. Where/how do I get a levenshtein definition for use in Blaze expressions on the Python side?


I have found the sqlalchemy.sql.func package which provides Python handles for SQL functions for use with SqlAlchemy, but these do not work in Blaze expressions. Is there an equivalent Blaze package, or how can I use sqlalchemy.sql.func.levenshtein inside a Blaze expression?


Solution

  • Blaze is only a Frontend; and restructures and then gives the query to the SQL Backend, which collects the data from your DB.

    From the doku:

    What operations work on SQL databases? Most tabular operations, but not all. SQLAlchemy translation is a high priority. Failures include array operations like slicing and dot products don’t make sense in SQL. Additionally some operations like datetime access are not yet well supported through SQLAlchemy. Finally some databases, like SQLite, have limited support for common mathematical functions like sin.

    Blaze is intended to forget about the backend and have similar / same syntax for all backends. Therefore it only supports common operations. In my opinion, there is no possibility to specify sqlalchemy or postgresql functions for blaze to use, therefore I think this is not possible by now.

    ... BUT blaze has version 0.10, it's kind of beta with active contribution in the last year. I am confident, this will be implemented in some time. You always can track the changes in the release notes.

    Addition: For further explanation on UDFs (User defined functions) in SQL and working with them in blaze, compare the link provided by beldaz (very similar to this question).