Skip to content

Some visit_XXX compiler methods blow up when compile_kwargs are passed #8988

Closed
@evansd

Description

@evansd
Contributor

Describe the bug

Some visit_XXX compiler methods don't accept **kw which means they blow up when .compile() is called with compile_kwargs={...}.

I specifically noticed this with the visit_create_index() method on SQLiteCompiler but from grepping the codebase there seem to be other dialects and methods with this issue.

Obviously render_postcompile doesn't do anything in this case, but it would be good to be able to always pass this argument without having to worry which sorts of clause accept it.

Assuming this is accepted as a bug I'd be happy to submit a PR adding **kw to the relevant methods. But I wanted to check first.

To Reproduce

from sqlalchemy import Column, Index, MetaData, Table
from sqlalchemy.schema import CreateIndex

from sqlalchemy.dialects.mssql.pymssql import MSDialect_pymssql
from sqlalchemy.dialects.mysql.pymysql import MySQLDialect_pymysql
from sqlalchemy.dialects.sqlite.pysqlite import SQLiteDialect_pysqlite
from sqlalchemy.engine.default import DefaultDialect


table = Table("table", MetaData(), Column("col"))
index = Index(None, table.c.col)
create_index = CreateIndex(index)

for dialect in [
    DefaultDialect,
    MSDialect_pymssql,
    MySQLDialect_pymysql,
    SQLiteDialect_pysqlite,
]:
    print(f"Compiling with {dialect}")
    query = create_index.compile(
        dialect=dialect(), compile_kwargs={"render_postcompile": True}
    )
    print(query)
    print()

Error

Compiling with <class 'sqlalchemy.engine.default.DefaultDialect'>
CREATE INDEX ix_table_col ON "table" (col)

Compiling with <class 'sqlalchemy.dialects.mssql.pymssql.MSDialect_pymssql'>
CREATE INDEX ix_table_col ON [table] (col)

Compiling with <class 'sqlalchemy.dialects.mysql.pymysql.MySQLDialect_pymysql'>
CREATE INDEX ix_table_col ON `table` (col)

Compiling with <class 'sqlalchemy.dialects.sqlite.pysqlite.SQLiteDialect_pysqlite'>
Traceback (most recent call last):
  File "/home/dave/projects/ebmdatalab/databuilder/reproduce_error.py", line 14, in <module>
    query = create_index.compile(dialect=dialect(), compile_kwargs={"render_postcompile": True})
  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/ddl.py", line 32, in _compiler
    return dialect.ddl_compiler(dialect, self, **kw)
  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)
TypeError: visit_create_index() got an unexpected keyword argument 'render_postcompile'

Versions

  • Python: 3.9
  • SQLAlchemy: 1.4.39

Additional context

No response

Activity

added
bugSomething isn't working
PRs (with tests!) welcomea fix or feature which is appropriate to be implemented by volunteers
and removed
requires triageNew issue that requires categorization
on Dec 15, 2022
CaselIT

CaselIT commented on Dec 15, 2022

@CaselIT
Member

Hi,
thanks for reporting.

For the PR let's wait for Mike, since I don't know if there was a rationale for not having the kw in some methods

zzzeek

zzzeek commented on Dec 15, 2022

@zzzeek
Member

they've just not been needed for DDL compiler methods. "render_postcompile" is fairly recent, so kw can be added.

what would be nice though would be some way to assert all visit_ methods accept kw. I would propose gathering all SQLCompiler classes (using __subclasses__() ) and then running through all visit_XYZ methods with inspect.getfullargspec() to confirm.

added this to the 2.0 final milestone on Dec 15, 2022
CaselIT

CaselIT commented on Dec 15, 2022

@CaselIT
Member

I think we need to use the compuler as the superclass, since ddlcompiler does not inherit from sqlcompiler.

zzzeek

zzzeek commented on Dec 15, 2022

@zzzeek
Member

yes, Compiler

self-assigned this
on Dec 16, 2022
removed
PRs (with tests!) welcomea fix or feature which is appropriate to be implemented by volunteers
on Dec 16, 2022
modified the milestones: 2.0 final, 2.0beta5 on Dec 16, 2022
sqla-tester

sqla-tester commented on Dec 16, 2022

@sqla-tester
Collaborator

Mike Bayer has proposed a fix for this issue in the main branch:

**ensure all visit methods accept kw https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/4307

4 remaining items

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

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingsql

Type

No type

Projects

No projects

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @evansd@zzzeek@sqla-tester@CaselIT

      Issue actions

        Some `visit_XXX` compiler methods blow up when `compile_kwargs` are passed · Issue #8988 · sqlalchemy/sqlalchemy