Skip to content

Getting "cx_Oracle.DatabaseError: DPI-1037" when trying to query a long view definition as STRING #642

@recognosco

Description

@recognosco
  1. What versions are you using?
Database: 19.16.0.0.0
platform.platform: Linux-3.10.0-1160.76.1-el7.x86_64-x86_64-with-glibc2.17
sys.maxsize > 2**32: True
platform.python_version: 3.9.13
cx_Oracle.version: 8.1.0
cx_Oracle.clientversion: (18, 3, 0, 0, 0)
  1. Is it an error or a hang or a crash?
    It's an error that is returned from the Database.

  2. What error(s) or behavior you are seeing?
    cx_Oracle.DatabaseError: DPI-1037: column at array position 0 fetched with error 1406
    We have noticed a case where cx_Oracle is raising an error when the cursor.outputtypehandler is set with defaultType as cx_Oracle.STRING and works when it is set with cx_Oracle.LONG_STRING.
    The issue manifested in our codebase from a SQLAlchemy change, where setting coerce_to_unicode was causing the defaultType to change. Despite the change in defaultType, should this error manifest?
    We would expect cx_Oracle to handle it gracefully and show us the query result.

  3. Include a runnable Python script that shows the problem.

def cx_conn():
    import cx_Oracle
    cx_dsn= cx_Oracle.makedsn("hostname", "12345", service_name="SERVICE_NAME")
    connection = cx_Oracle.connect(user="username", password="password1234", dsn=cx_dsn)
    cursor = connection.cursor()
    cursor.arraysize = 500
    def OutputTypeHandler(cursor, name, defaultType, size, precision, scale):
        # return cursor.var(cx_Oracle.LONG_STRING, arraysize=cursor.arraysize) # works
        return cursor.var(cx_Oracle.STRING, arraysize=cursor.arraysize)        # doesn't work
    cursor.outputtypehandler = OutputTypeHandler
    view_name = 'VW_TEST_CX_ORACLE'
    view_def = f"CREATE OR REPLACE VIEW {view_name} AS SELECT '{'A' * 4000}' AS a, '{'B' * 4000}' AS b, '{'C' * 4000}' AS c, '{'D' * 3950}' AS d FROM DUAL"
    cursor.execute(view_def)
    print(f'''This creates a long view definition. It is {len(view_def)} characters long.
        This is the threshold where "sqlalchemy.exc.DatabaseError: (cx_Oracle.DatabaseError) DPI-1037: column at array position 0 fetched with error 1406" error starts.
        Our production view definitions are often much longer''')
    test_cmd = f"SELECT text FROM all_views WHERE view_name='{view_name}'"
    result = cursor.execute(test_cmd)
    x = result.fetchone()
    print(x)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions