ibis-project / ibis

the portable Python dataframe library
https://ibis-project.org
Apache License 2.0
5.25k stars 592 forks source link

bug: not handle column non-existent in mssql #10331

Closed grieve54706 closed 1 week ago

grieve54706 commented 1 week ago

What happened?

When I use function sql() of mssql with a non-exist column like below

ibis.mssql.connect(...).sql('SELECT x from tpch.tiny.orders')

ibis will throw 'NoneType' object has no attribute 'lower' Because the function will get schema by _get_schema_using_query()

https://github.com/ibis-project/ibis/blob/ea1c179036b10b53e58156a0bfc3e31f3472a388/ibis/backends/mssql/__init__.py#L307-L335

But the rows will get [(None, None, None, None, None), (None, None, None, None, None)] and put the None into self.compiler.type_mapper.from_string()

Maybe we should check if the value is None or not.

What version of ibis are you using?

9.5.0

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

mssql

Relevant log output

../.venv/lib/python3.11/site-packages/ibis/backends/sql/__init__.py:176: in sql
    schema = self._get_schema_using_query(query)
../.venv/lib/python3.11/site-packages/ibis/backends/mssql/__init__.py:333: in _get_schema_using_query
    newtyp = self.compiler.type_mapper.from_string(
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

cls = <class 'ibis.backends.sql.datatypes.MSSQLType'>, text = None
nullable = None

    @classmethod
    def from_string(cls, text: str, nullable: bool | None = None) -> dt.DataType:
>       if dtype := cls.unknown_type_strings.get(text.lower()):
E       AttributeError: 'NoneType' object has no attribute 'lower'

../.venv/lib/python3.11/site-packages/ibis/backends/sql/datatypes.py:195: AttributeError

Code of Conduct

grieve54706 commented 1 week ago

I also found if the sql syntax is invalid, the dm_exec_describe_first_result_set will not fail and return None and error_message like Invalid object name 'X' when sql is select * from X a non-existent table.

gforsyth commented 1 week ago

Thanks for raising this, @grieve54706 ! I think this shouldn't be hard to handle better -- we can ask for error codes and error messages from dm_exec_describe_first_result_set and handle those accordingly.

grieve54706 commented 1 week ago

Hi @gforsyth, Thank you for helping me so quickly.