On Mon, Oct 23, 2017 at 6:40 AM, Антонио Антуан <a.ch....@gmail.com> wrote:
> Hello guys.
>
> I try to make my own GenericFunction with default compiler and custom
> compiler for particular dialect. As you see below, always used custom
> compiler:
>
> from sqlalchemy import func, create_engine, Integer
> from sqlalchemy.ext.compiler import compiles
> from sqlalchemy.orm import sessionmaker
> from sqlalchemy.sql.functions import GenericFunction
>
> engine_ch = create_engine('clickhouse+native://log:pas@127.0.0.1:9000/db')
> engine_pg = create_engine('postgresql://log:pas@127.0.0.1:9000/db')
>
> ses_pg = sessionmaker(bind=engine_pg)()
> ses_ch = sessionmaker(bind=engine_ch)()
>
>
> class IntDiv(GenericFunction):
>     type = Integer
>     package = 'custom'
>     name = 'div'
>     identifier = 'div'
>
>
> @compiles(IntDiv)
> def visit_div_default(element, compiler, **kwargs):
>     return 'div(%s)' % compiler.process(element.clause_expr.element)
>
>
> @compiles(IntDiv, 'clickhouse')
> def visit_div_ch(element, compiler, **kwargs):
>     return 'intDiv(%s)' % compiler.process(element.clause_expr.element)
>
>
> print(ses_ch.bind.dialect.name)
> print(ses_ch.query(func.custom.div(1, 2)))
>
> print(ses_pg.bind.dialect.name)
> print(ses_pg.query(func.custom.div(1, 2)))
>
>
> If I not make "@compiles(IntDiv)" (for default dialct), make only
> @compiles(IntDiv, 'clickhouse) I got this error:
>
> Traceback (most recent call last):
>   File "/home/anton/Projects/proj/core/run/stuff.py", line 31, in <module>
>     print(ses_ch.query(func.custom.div(1, 2)))
>   File
> "/home/anton/Projects/proj/.venv/lib/python2.7/site-packages/sqlalchemy/orm/query.py",
> line 3457, in __str__
>     return str(self._compile_context().statement)


Based on that line number, I can see you are on a 1.0.x version of
SQLAlchemy.   Query.__str__() takes into account the bound dialect
automatically as of 1.1:

http://docs.sqlalchemy.org/en/latest/changelog/migration_11.html#stringify-of-query-will-consult-the-session-for-the-correct-dialect

if you are on 1.0.x or earlier you need to compile from the statement:

query.statement.compile(my_engine)






>   File
> "/home/anton/Projects/proj/.venv/lib/python2.7/site-packages/sqlalchemy/sql/elements.py",
> line 506, in __str__
>     return unicode(self.compile()).encode('ascii', 'backslashreplace')
>   File "<string>", line 1, in <lambda>
>   File
> "/home/anton/Projects/proj/.venv/lib/python2.7/site-packages/sqlalchemy/sql/elements.py",
> line 494, in compile
>     return self._compiler(dialect, bind=bind, **kw)
>   File
> "/home/anton/Projects/proj/.venv/lib/python2.7/site-packages/sqlalchemy/sql/elements.py",
> line 500, in _compiler
>     return dialect.statement_compiler(dialect, self, **kw)
>   File
> "/home/anton/Projects/proj/.venv/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py",
> line 395, in __init__
>     Compiled.__init__(self, dialect, statement, **kwargs)
>   File
> "/home/anton/Projects/proj/.venv/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py",
> line 190, in __init__
>     self.string = self.process(self.statement, **compile_kwargs)
>   File
> "/home/anton/Projects/proj/.venv/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py",
> line 213, in process
>     return obj._compiler_dispatch(self, **kwargs)
>   File
> "/home/anton/Projects/proj/.venv/lib/python2.7/site-packages/sqlalchemy/sql/visitors.py",
> line 81, in _compiler_dispatch
>     return meth(self, **kw)
>   File
> "/home/anton/Projects/proj/.venv/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py",
> line 1584, in visit_select
>     for name, column in select._columns_plus_names
>   File
> "/home/anton/Projects/proj/.venv/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py",
> line 1357, in _label_select_column
>     **column_clause_args
>   File
> "/home/anton/Projects/proj/.venv/lib/python2.7/site-packages/sqlalchemy/sql/visitors.py",
> line 93, in _compiler_dispatch
>     return meth(self, **kw)
>   File
> "/home/anton/Projects/proj/.venv/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py",
> line 615, in visit_label
>     OPERATORS[operators.as_] + \
>   File
> "/home/anton/Projects/proj/.venv/lib/python2.7/site-packages/sqlalchemy/ext/compiler.py",
> line 423, in <lambda>
>     lambda *arg, **kw: existing(*arg, **kw))
>   File
> "/home/anton/Projects/proj/.venv/lib/python2.7/site-packages/sqlalchemy/ext/compiler.py",
> line 460, in __call__
>     "compilation handler." % type(element))
> sqlalchemy.exc.CompileError: <class '__main__.IntDiv'> construct has no
> default compilation handler.
>
>
> Is it possible to override default GenericFunction compiler?
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> 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+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

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+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to