@@ -1667,6 +1667,9 @@ def _table_database(con, server: Optional[Server]) -> Optional[str]:
16671667 - **SQL Server** (``mssql`` backend) has no ``schema`` kwarg on
16681668 ``do_connect()`` either, so it has the same limitation as Oracle: an
16691669 unqualified lookup falls back to the login's default schema.
1670+ - **Postgres** sets the ``search_path`` to the schema without quoting it,
1671+ so a mixed-case schema such as ``Reporting`` is read as ``reporting``
1672+ and an unqualified lookup raises ``TableNotFound``.
16701673 - **Redshift** has no dedicated ibis backend and goes through the Postgres
16711674 backend (``con.name == "postgres"``). When no schema is passed, ibis's
16721675 Postgres introspection resolves the active schema with ``SELECT
@@ -1685,9 +1688,8 @@ def _table_database(con, server: Optional[Server]) -> Optional[str]:
16851688 # so a table outside `main` has to be qualified at lookup.
16861689 if get_server_type (server ) == "duckdb" :
16871690 return server .schema_
1688- # Redshift rides the Postgres backend, so detect it by the contract's server
1689- # type rather than con.name.
1690- if get_server_type (server ) == "redshift" :
1691+ # Redshift rides the Postgres backend, so this covers both.
1692+ if getattr (con , "name" , None ) == "postgres" :
16911693 return server .schema_
16921694 return None
16931695
@@ -1721,10 +1723,14 @@ def _resolve_table(con, model: str, database: Optional[str] = None):
17211723 # duckdb reads `DESCRIBE "tables"` as `SHOW TABLES` however it is quoted, unless the name is qualified
17221724 if getattr (con , "name" , None ) == "duckdb" and not database and model .lower () in _DUCKDB_SHOW_NAMES :
17231725 database = "main"
1726+ from ibis .common .exceptions import TableNotFound
1727+
17241728 kwargs = {"database" : database } if database else {}
17251729 try :
17261730 return con .table (model , ** kwargs )
1727- except Exception :
1731+ except Exception as e :
1732+ if getattr (con , "name" , None ) == "postgres" and not isinstance (e , TableNotFound ):
1733+ return _postgres_table_unmappable_as_unknown (con , model , database )
17281734 try :
17291735 available = con .list_tables (** kwargs )
17301736 except Exception :
@@ -1735,6 +1741,47 @@ def _resolve_table(con, model: str, database: Optional[str] = None):
17351741 return con .table (match , ** kwargs )
17361742
17371743
1744+ def _postgres_table_unmappable_as_unknown (con , name : str , database : Optional [str ]):
1745+ """Reflect a postgres table, typing any column whose type ibis cannot map
1746+ (e.g. `regclass`) as `Unknown`, so the other columns can still be checked.
1747+ """
1748+ import ibis .expr .datatypes as dt
1749+ import ibis .expr .operations as ops
1750+ import ibis .expr .schema as sch
1751+ from ibis .common .exceptions import TableNotFound
1752+
1753+ # The query ibis's get_schema() runs, including its reading of enums
1754+ query = """
1755+ SELECT a.attname,
1756+ CASE WHEN t.typtype = 'e' THEN 'enum' ELSE pg_catalog.format_type(a.atttypid, a.atttypmod) END,
1757+ NOT a.attnotnull
1758+ FROM pg_catalog.pg_attribute a
1759+ JOIN pg_catalog.pg_class c ON a.attrelid = c.oid
1760+ JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid
1761+ JOIN pg_catalog.pg_type t ON t.oid = a.atttypid
1762+ WHERE a.attnum > 0 AND NOT a.attisdropped AND n.nspname = %(schema)s AND c.relname = %(name)s
1763+ ORDER BY a.attnum
1764+ """
1765+ with con .con .cursor () as cursor :
1766+ rows = cursor .execute (query , {"schema" : database or con .current_database , "name" : name }).fetchall ()
1767+ if not rows :
1768+ raise TableNotFound (name )
1769+ fields , unknown_columns = {}, []
1770+ for column , type_name , nullable in rows :
1771+ try :
1772+ fields [column ] = con .compiler .type_mapper .from_string (type_name , nullable = nullable )
1773+ except Exception :
1774+ fields [column ] = dt .unknown
1775+ unknown_columns .append (f"{ column } ({ type_name } )" )
1776+ logger .warning (
1777+ f"Model '{ name } ': column(s) { ', ' .join (unknown_columns )} have a type ibis cannot represent. "
1778+ f"Type checks for these columns will fail."
1779+ )
1780+ return ops .DatabaseTable (
1781+ name , schema = sch .Schema (fields ), source = con , namespace = ops .Namespace (database = database )
1782+ ).to_expr ()
1783+
1784+
17381785def _pyspark_table_unconvertible_as_unknown (con , name : str ):
17391786 """Reflect a pyspark table, typing any column whose Spark type Ibis cannot
17401787 convert (e.g. Databricks `VariantType`) as `Unknown`.
0 commit comments