Skip to content

Postcompiled parameters may be rendered with invalid/unescaped names #8708

Closed
@sm-Fifteen

Description

@sm-Fifteen

Describe the bug

On Oracle, when using literal tables and columns in conjunction with SQLA expressions, fully-qualified literal column names will have their parameter identifiers (seemingly) generated from the column clause key, which matches the literal column expression, something like :DUAL2.DUMMY_1. The dot is apparently invalid and needs to be escaped, so SQLA normally renders this as :"DUAL2.DUMMY_1" (with the quotes), but not for postcompiled parameters (e.g: lists).

This causes Oracle to raise an ORA-01036: illegal variable name/number error.

To Reproduce

bad_statement = select(literal_column("1")).select_from(
    sql_text("""DUAL CROSS JOIN DUAL DUAL2""")
).where(
    literal_column("DUAL2.DUMMY", VARCHAR(1)).in_(['X']),
)

good_statement = select(literal_column("1")).select_from(
    sql_text("""DUAL CROSS JOIN DUAL DUAL2""")
).where(
    literal_column("DUAL2.DUMMY", VARCHAR(1)) == 'X',
)

oracle_conn.execute(good_statement) # Works
oracle_conn.execute(bad_statement) # See error

Error

sqlalchemy.exc.DatabaseError: (cx_Oracle.DatabaseError) ORA-01036: illegal variable name/number
[SQL: SELECT 1 
FROM DUAL CROSS JOIN DUAL DUAL2 
WHERE DUAL2.DUMMY IN (:DUAL2.DUMMY_1_1)]
[parameters: {'DUAL2.DUMMY_1_1': 'X'}]
(Background on this error at: https://sqlalche.me/e/14/4xp6)

Versions

  • OS: Windows
  • Python: 3.10.7
  • SQLAlchemy: 1.4.42
  • Database: Oracle 19c
  • DBAPI: cx_oracle 8.3

Additional context

>>> print(bad_statement.compile(ora_engine, compile_kwargs={"render_postcompile": True}))
SELECT 1 
FROM DUAL CROSS JOIN DUAL DUAL2 
WHERE DUAL2.DUMMY IN (:DUAL2.DUMMY_1_1)

>>> print(good_statement.compile(ora_engine, compile_kwargs={"render_postcompile": True}))
SELECT 1 
FROM DUAL CROSS JOIN DUAL DUAL2 
WHERE DUAL2.DUMMY = :"DUAL2.DUMMY_1"

Activity

added
bugSomething isn't working
and removed
requires triageNew issue that requires categorization
on Oct 24, 2022
added this to the 1.4.x milestone on Oct 24, 2022
added
near-term releaseaddition to the milestone which indicates this should be in a near-term release
on Oct 24, 2022
zzzeek

zzzeek commented on Oct 24, 2022

@zzzeek
Member

the expanded parameters logic has very well integrated support for just escaping the characters, as opposed to quoting the bound name which is much more complicated and is specific to Oracle. it looks like Oracle dialect has an explicit skip on the usual quoting for the expanded parameters logic as it wasn't integrated there. so will just add "." to the escape characters.

sqla-tester

sqla-tester commented on Oct 24, 2022

@sqla-tester
Collaborator

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

add Oracle-specific parameter escapes for expanding params https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/4152

sqla-tester

sqla-tester commented on Oct 24, 2022

@sqla-tester
Collaborator

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

add Oracle-specific parameter escapes for expanding params https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/4153

zzzeek

zzzeek commented on Oct 24, 2022

@zzzeek
Member

I might look into using escaping entirely for Oracle rather than quoting but that would be 2.0 only

added a commit that references this issue on Oct 25, 2022
922f11f
zzzeek

zzzeek commented on Dec 2, 2022

@zzzeek
Member

I missed a bit of change here

reopened this on Dec 2, 2022
sqla-tester

sqla-tester commented on Dec 2, 2022

@sqla-tester
Collaborator

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

add spaces, leading underscore to oracle checks https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/4260

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

No one assigned

    Labels

    bugSomething isn't workingnear-term releaseaddition to the milestone which indicates this should be in a near-term releaseoracle

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @zzzeek@sm-Fifteen@sqla-tester

        Issue actions

          Postcompiled parameters may be rendered with invalid/unescaped names · Issue #8708 · sqlalchemy/sqlalchemy