Skip to content

bug: NVARCHAR(MAX) column fails on mssql connector #11191

Closed
@akanz1

Description

@akanz1

What happened?

After upgrading from 10.2.0 to 10.5.0 the dtype NVARCHAR(MAX) fails on mssql (likely also others are affected). previously this got resolved to a String() type.

The recently added handling of this type leads to a failure since in sql/datatypes.py:219 length.this.this yields 'MAX' which can not be places into an int() statement.

    @classmethod
    def _from_sqlglot_VARCHAR(
        cls, length: sge.DataTypeParam | None = None, nullable: bool | None = None
    ) -> dt.String:


        return dt.String(
            length=int(length.this.this) if length is not None else None,
            nullable=nullable,
        )

to make it work i've forked and fixed it like so:

    @classmethod
    def _from_sqlglot_VARCHAR(
        cls, length: sge.DataTypeParam | None = None, nullable: bool | None = None
    ) -> dt.String:
        if length is not None and length.this.this == "MAX":
            length = None
        return dt.String(
            length=int(length.this.this) if length is not None else None,
            nullable=nullable,
        )

i guess the following is even a bit nicer as it avoids the redundancy around None checks

@classmethod
def _from_sqlglot_VARCHAR(
    cls, length: sge.DataTypeParam | None = None, nullable: bool | None = None
) -> dt.String:
    length_value = None
    if length is not None and length.this.this != "MAX":
        length_value = int(length.this.this)
    return dt.String(
        length=length_value,
        nullable=nullable,
    )

What version of ibis are you using?

Ibis 10.5.0
python 3.11.11

What backend(s) are you using, if any?

No response

Relevant log output

File ".venv/lib/python3.11/site-packages/ibis/backends/sql/__init__.py", line 179, in sql
    schema = self._get_schema_using_query(query)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.11/site-packages/ibis/backends/mssql/__init__.py", line 369, in _get_schema_using_query
    newtyp = self.compiler.type_mapper.from_string(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.11/site-packages/ibis/backends/sql/datatypes.py", line 195, in from_string
    return cls.to_ibis(sgtype, nullable=nullable)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.11/site-packages/ibis/backends/sql/datatypes.py", line 161, in to_ibis
    dtype = method(*typ.expressions, nullable=nullable)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.11/site-packages/ibis/backends/sql/datatypes.py", line 219, in _from_sqlglot_VARCHAR
    length=int(length.this.this) if length is not None else None,
           ^^^^^^^^^^^^^^^^^^^^^
ValueError: invalid literal for int() with base 10: 'MAX'

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIncorrect behavior inside of ibismssqlThe Microsoft SQL Server backend

    Type

    No type

    Projects

    Status

    done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions