Skip to content

Error when combining literal_binds with bind_expression() on an "expanding" BindParameter #8989

Description

@evansd

Describe the bug

Compiling a clause using literal_binds=True throws an error when the clause contains an "expanding" BindParameter on a type with a bind_expression() method.

I understand that this is a bug in an obscure corner of a feature whose use is discouraged in any case (for good reasons). It would be quite reasonable not to fix this, but I thought it was worth at least making a record of it in case others hit the same problem.

For our own use case we have been able to work around this by doing the parameter substitution ourselves, having run all the parameter values through literal_processor. In our particular context the usual considerations around untrusted inputs don't apply, so this is an acceptable solution.

To Reproduce

import sqlalchemy


class CustomType(sqlalchemy.types.TypeDecorator):
    impl = sqlalchemy.types.String

    def bind_expression(self, bindvalue):
        return sqlalchemy.func.lower(bindvalue)


table = sqlalchemy.Table(
    "t", sqlalchemy.MetaData(), sqlalchemy.Column("col", CustomType())
)

clause = table.c.col.in_(["ABC", "DEF"])

for compile_kwargs in [{"render_postcompile": True}, {"literal_binds": True}]:
    print(f"{compile_kwargs=}")
    print(clause.compile(compile_kwargs=compile_kwargs))
    print()

Error

compile_kwargs={'render_postcompile': True}
t.col IN (lower(:col_1_1), lower(:col_1_2))

compile_kwargs={'literal_binds': True}
Traceback (most recent call last):
  File "/home/dave/projects/ebmdatalab/databuilder/reproduce_error_2.py", line 19, in <module>
    print(clause.compile(compile_kwargs=compile_kwargs))
  File "/home/dave/.virtualenvs/ebm-databuilder/lib/python3.9/site-packages/sqlalchemy/sql/elements.py", line 501, in compile
    return self._compiler(dialect, **kw)
  File "/home/dave/.virtualenvs/ebm-databuilder/lib/python3.9/site-packages/sqlalchemy/sql/elements.py", line 565, in _compiler
    return dialect.statement_compiler(dialect, self, **kw)
  File "/home/dave/.virtualenvs/ebm-databuilder/lib/python3.9/site-packages/sqlalchemy/sql/compiler.py", line 778, in __init__
    Compiled.__init__(self, dialect, statement, **kwargs)
  File "/home/dave/.virtualenvs/ebm-databuilder/lib/python3.9/site-packages/sqlalchemy/sql/compiler.py", line 451, in __init__
    self.string = self.process(self.statement, **compile_kwargs)
  File "/home/dave/.virtualenvs/ebm-databuilder/lib/python3.9/site-packages/sqlalchemy/sql/compiler.py", line 486, in process
    return obj._compiler_dispatch(self, **kwargs)
  File "/home/dave/.virtualenvs/ebm-databuilder/lib/python3.9/site-packages/sqlalchemy/sql/visitors.py", line 82, in _compiler_dispatch
    return meth(self, **kw)
  File "/home/dave/.virtualenvs/ebm-databuilder/lib/python3.9/site-packages/sqlalchemy/sql/compiler.py", line 2208, in visit_binary
    return self._generate_generic_binary(
  File "/home/dave/.virtualenvs/ebm-databuilder/lib/python3.9/site-packages/sqlalchemy/sql/compiler.py", line 2264, in _generate_generic_binary
    + binary.right._compiler_dispatch(
  File "/home/dave/.virtualenvs/ebm-databuilder/lib/python3.9/site-packages/sqlalchemy/sql/visitors.py", line 82, in _compiler_dispatch
    return meth(self, **kw)
  File "/home/dave/.virtualenvs/ebm-databuilder/lib/python3.9/site-packages/sqlalchemy/sql/compiler.py", line 2427, in visit_bindparam
    m.group(2),
AttributeError: 'NoneType' object has no attribute 'group'

Versions

  • Python: 3.9
  • SQLAlchemy: 1.4.39

Additional context

No response

Activity

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

    bugSomething isn't workinggreat mcveAn issue with a great mcvesql

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions