Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(formats): turn TypeParser into a TypeMapper implementation for sqlglot #6876

Merged
merged 11 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions ibis/backends/base/sql/alchemy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,6 @@ def _handle_failed_column_type_inference(
self, table: sa.Table, nulltype_cols: Iterable[str]
) -> sa.Table:
"""Handle cases where SQLAlchemy cannot infer the column types of `table`."""

self.inspector.reflect_table(table, table.columns)

dialect = self.con.dialect
Expand All @@ -565,15 +564,15 @@ def _handle_failed_column_type_inference(
)
)

for colname, type in self._metadata(quoted_name):
for colname, dtype in self._metadata(quoted_name):
if colname in nulltype_cols:
# replace null types discovered by sqlalchemy with non null
# types
table.append_column(
sa.Column(
colname,
self.compiler.translator_class.get_sqla_type(type),
nullable=type.nullable,
self.compiler.translator_class.get_sqla_type(dtype),
nullable=dtype.nullable,
quote=self.compiler.translator_class._quote_column_names,
),
replace_existing=True,
Expand Down Expand Up @@ -925,7 +924,7 @@ def _get_sqla_table(
db = "".join(db) or database or current_db
ident = ".".join(map(self._quote, filter(None, (db, schema))))

pairs = self._metadata(f"SELECT * FROM {ident}.{self._quote(name)}")
pairs = self._metadata(f"SELECT * FROM {ident}.{self._quote(name)} LIMIT 0")
ibis_schema = ibis.schema(pairs)

with self._use_schema(ident, current_db, current_schema):
Expand Down
6 changes: 5 additions & 1 deletion ibis/backends/base/sql/alchemy/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import ibis.expr.datatypes as dt
from ibis.backends.base.sql.alchemy.geospatial import geospatial_supported
from ibis.backends.base.sqlglot.datatypes import SqlglotType
from ibis.common.collections import FrozenDict
from ibis.formats import TypeMapper

Expand Down Expand Up @@ -225,6 +226,10 @@ def to_string(cls, dtype: dt.DataType):
sa.types.to_instance(cls.from_ibis(dtype)).compile(dialect=dialect_class())
)

@classmethod
def from_string(cls, type_string, nullable=True):
return SqlglotType.from_string(type_string, nullable=nullable)

@classmethod
def from_ibis(cls, dtype: dt.DataType) -> sat.TypeEngine:
"""Convert an Ibis type to a SQLAlchemy type.
Expand Down Expand Up @@ -279,7 +284,6 @@ def to_ibis(cls, typ: sat.TypeEngine, nullable: bool = True) -> dt.DataType:
-------
Ibis type.
"""

if dtype := _from_sqlalchemy_types.get(type(typ)):
return dtype(nullable=nullable)
elif isinstance(typ, sat.Float):
Expand Down
Empty file.
Loading