Repository: ibis-project/ibis

Issue:
bug: Specifying the database and schema for a table does not work in Trino
### What happened?

Creating a dataframe from a "fully quallified" table name caused an exception:
```
connection = ibis.trino.connect(user='trino', host='localhost', port=8080)
countries = connection.table(name='nation', database='tpch', schema='tiny')
```


### What version of ibis are you using?

7.0.0

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

Trino

### Relevant log output

```sh
/Users/alex.albu/Library/Caches/pypoetry/virtualenvs/pydantic-demo-0yMeIGuA-py3.11/lib/python3.11/site-packages/ibis/backends/base/sql/alchemy/__init__.py:623: FutureWarning: `BaseBackend.database` is deprecated; use equivalent methods in the backend
  return self.database(name=database).table(name=name, schema=schema)
Traceback (most recent call last):
  File "/Users/alex.albu/Library/Application Support/JetBrains/Toolbox/apps/IDEA-U/ch-0/232.9921.47/IntelliJ IDEA.app.plugins/python/helpers/pydev/pydevd.py", line 1500, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alex.albu/Library/Application Support/JetBrains/Toolbox/apps/IDEA-U/ch-0/232.9921.47/IntelliJ IDEA.app.plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/Users/alex.albu/tmp/pydantic-demo/pydantic_demo/ibis_test.py", line 6, in <module>
    countries = connection.table(name='nation', database='tpch', schema='tiny')
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alex.albu/Library/Caches/pypoetry/virtualenvs/pydantic-demo-0yMeIGuA-py3.11/lib/python3.11/site-packages/ibis/backends/base/sql/alchemy/__init__.py", line 623, in table
    return self.database(name=database).table(name=name, schema=schema)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Database.table() got an unexpected keyword argument 'schema'
python-BaseException
```


### Code of Conduct

- [X] I agree to follow this project's Code of Conduct


Discussion:
Hi @aalbu!

Thanks for the issue.

Can you try this instead?

```
connection = ibis.trino.connect(database=..., schema=..., user='trino', host='localhost', port=8080)
countries = connection.table(name='nation', schema='tpch.tiny')
```

I found another issue while testing this 😅. In short, you need to specify the catalog and schema in the connection.

We're working on making the database/schema parameters make a bit more sense. They are kind of all over the place at the moment.
Yes, that works:
```
connection = ibis.trino.connect(user='sql', host='localhost', port=8080, database='tpch', schema='sf100')
countries = connection.table(name='nation', schema='tiny')
```
However this workaround does not work when trying to query across catalogs (databases) because as soon as you specify a database that is different that the connection one, you get the error above.  And federating datasources is something Trino shines at.
You should be able to use `schema` for cross-catalog access:

```
In [1]: from ibis.interactive import *

In [2]: con = ibis.trino.connect(database="tpch", schema="sf1")

In [3]: t = con.table("nation", schema="tpch.sf10")

In [4]: s = con.table("tables", schema="system.information_schema")

In [5]: t.head()
Out[5]:
┏━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ nationkey ┃ name      ┃ regionkey ┃ comment                                                                          ┃
┡━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ int64     │ string    │ int64     │ string                                                                           │
├───────────┼───────────┼───────────┼──────────────────────────────────────────────────────────────────────────────────┤
│         0 │ ALGERIA   │         0 │  haggle. carefully final deposits detect slyly agai                              │
│         1 │ ARGENTINA │         1 │ al foxes promise slyly according to the regular accounts. bold requests alon     │
│         2 │ BRAZIL    │         1 │ y alongside of the pending deposits. carefully special packages are about the i… │
│         3 │ CANADA    │         1 │ eas hang ironic, silent packages. slyly regular packages are furiously over the… │
│         4 │ EGYPT     │         4 │ y above the carefully unusual theodolites. final dugouts are quickly across the… │
└───────────┴───────────┴───────────┴──────────────────────────────────────────────────────────────────────────────────┘

In [6]: s.head()
Out[6]:
┏━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┓
┃ table_catalog ┃ table_schema       ┃ table_name       ┃ table_type ┃
┡━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━┩
│ string        │ string             │ string           │ string     │
├───────────────┼────────────────────┼──────────────────┼────────────┤
│ system        │ information_schema │ columns          │ BASE TABLE │
│ system        │ information_schema │ tables           │ BASE TABLE │
│ system        │ information_schema │ views            │ BASE TABLE │
│ system        │ information_schema │ schemata         │ BASE TABLE │
│ system        │ information_schema │ table_privileges │ BASE TABLE │
└───────────────┴────────────────────┴──────────────────┴────────────┘
```

Let me know if that still doesn't work for you.
Sorry, I didn't notice you had a qualified schema name.  That works, indeed.

Resolving patch:
diff --git a/ibis/backends/base/sql/alchemy/__init__.py b/ibis/backends/base/sql/alchemy/__init__.py
--- a/ibis/backends/base/sql/alchemy/__init__.py
+++ b/ibis/backends/base/sql/alchemy/__init__.py
@@ -9,6 +9,7 @@
 from typing import TYPE_CHECKING, Any
 
 import sqlalchemy as sa
+import sqlglot as sg
 from sqlalchemy.ext.compiler import compiles
 from sqlalchemy.sql import quoted_name
 from sqlalchemy.sql.expression import ClauseElement, Executable
@@ -38,6 +39,7 @@
     AlchemyContext,
     AlchemyExprTranslator,
 )
+from ibis.backends.base.sqlglot import STAR
 from ibis.formats.pandas import PandasData
 
 if TYPE_CHECKING:
@@ -902,28 +904,6 @@ class AlchemyCrossSchemaBackend(BaseAlchemyBackend):
     currently active one.
     """
 
-    @property
-    @abc.abstractmethod
-    def use_stmt_prefix(self) -> str:
-        """The prefix to use for switching schemas.
-
-        Common examples are `USE` and `USE SCHEMA`.
-        """
-
-    @contextlib.contextmanager
-    def _use_schema(self, ident: str, current_db: str, current_schema: str) -> None:
-        use_prefix = self.use_stmt_prefix
-
-        try:
-            with self.begin() as c:
-                c.exec_driver_sql(f"{use_prefix} {ident}")
-            yield
-        finally:
-            with self.begin() as c:
-                c.exec_driver_sql(
-                    f"{use_prefix} {self._quote(current_db)}.{self._quote(current_schema)}"
-                )
-
     def _get_sqla_table(
         self,
         name: str,
@@ -937,27 +917,51 @@ def _get_sqla_table(
             schema = current_schema
         *db, schema = schema.split(".")
         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)} LIMIT 0")
+        table = sg.table(
+            name,
+            db=schema,
+            catalog=db,
+            quoted=self.compiler.translator_class._quote_table_names,
+        )
+        metadata_query = sg.select(STAR).from_(table).limit(0).sql(dialect=self.name)
+        pairs = self._metadata(metadata_query)
         ibis_schema = ibis.schema(pairs)
 
-        with self._use_schema(ident, current_db, current_schema):
-            result = self._table_from_schema(name, schema=ibis_schema)
-        result.schema = self._get_schema_for_table(qualname=ident, schema=schema)
+        columns = self._columns_from_schema(name, ibis_schema)
+        result = sa.Table(
+            name,
+            sa.MetaData(),
+            *columns,
+            quote=self.compiler.translator_class._quote_table_names,
+        )
+        result.fullname = table.sql(dialect=self.name)
         return result
 
-    @abc.abstractmethod
-    def _get_schema_for_table(self, *, qualname: str, schema: str) -> str:
-        """Choose whether to prefix a table with its fully qualified path or schema."""
-
     def drop_table(
         self, name: str, database: str | None = None, force: bool = False
     ) -> None:
-        name = self._quote(name)
-        # TODO: handle database quoting
-        if database is not None:
-            name = f"{database}.{name}"
-        drop_stmt = "DROP TABLE" + (" IF EXISTS" * force) + f" {name}"
+        table = sg.table(name, db=database)
+        drop_table = sg.exp.Drop(kind="TABLE", exists=force, this=table)
+        drop_table_sql = drop_table.sql(dialect=self.name)
         with self.begin() as con:
-            con.exec_driver_sql(drop_stmt)
+            con.exec_driver_sql(drop_table_sql)
+
+
+@compiles(sa.Table, "trino")
+def compile_trino_table(element, compiler, **kw):
+    return element.fullname
+
+
+@compiles(sa.Table, "snowflake")
+def compile_snowflake_table(element, compiler, **kw):
+    dialect = compiler.dialect.name
+    return (
+        sg.parse_one(element.fullname, into=sg.exp.Table, read=dialect)
+        .transform(
+            lambda node: node.__class__(this=node.this, quoted=True)
+            if isinstance(node, sg.exp.Identifier)
+            else node
+        )
+        .sql(dialect)
+    )
diff --git a/ibis/backends/snowflake/__init__.py b/ibis/backends/snowflake/__init__.py
--- a/ibis/backends/snowflake/__init__.py
+++ b/ibis/backends/snowflake/__init__.py
@@ -128,7 +128,6 @@ class Backend(AlchemyCrossSchemaBackend, CanCreateDatabase, AlchemyCanCreateSche
     compiler = SnowflakeCompiler
     supports_create_or_replace = True
     supports_python_udfs = True
-    use_stmt_prefix = "USE SCHEMA"
 
     _latest_udf_python_version = (3, 10)
 
@@ -813,9 +812,6 @@ def read_json(
 
         return self.table(table)
 
-    def _get_schema_for_table(self, *, qualname: str, schema: str) -> str:
-        return qualname
-
     def read_parquet(
         self, path: str | Path, table_name: str | None = None, **kwargs: Any
     ) -> ir.Table:
@@ -888,20 +884,6 @@ def read_parquet(
         return self.table(table)
 
 
-@compiles(sa.Table, "snowflake")
-def compile_table(element, compiler, **kw):
-    """Override compilation of leaf tables.
-
-    The override is necessary because snowflake-sqlalchemy does not handle
-    quoting databases and schemas correctly.
-    """
-    schema = element.schema
-    name = compiler.preparer.quote_identifier(element.name)
-    if schema is not None:
-        return f"{schema}.{name}"
-    return name
-
-
 @compiles(sa.sql.Join, "snowflake")
 def compile_join(element, compiler, **kw):
     """Override compilation of LATERAL joins.
diff --git a/ibis/backends/trino/__init__.py b/ibis/backends/trino/__init__.py
--- a/ibis/backends/trino/__init__.py
+++ b/ibis/backends/trino/__init__.py
@@ -41,7 +41,6 @@ class Backend(AlchemyCrossSchemaBackend, AlchemyCanCreateSchema, CanListDatabase
     compiler = TrinoSQLCompiler
     supports_create_or_replace = False
     supports_temporary_tables = False
-    use_stmt_prefix = "USE"
 
     @cached_property
     def version(self) -> str:
@@ -373,11 +372,3 @@ def _table_from_schema(
             trino_catalog=database or self.current_database,
             **kwargs,
         )
-
-    def _get_schema_for_table(self, *, qualname: str, schema: str) -> str:
-        """Trino compiles the `trino_catalog` argument into `sa.Table`.
-
-        This means we only need the schema and not the fully qualified
-        $catalog.$schema identifier.
-        """
-        return schema


Test patch:
diff --git a/ibis/backends/tests/test_export.py b/ibis/backends/tests/test_export.py
--- a/ibis/backends/tests/test_export.py
+++ b/ibis/backends/tests/test_export.py
@@ -335,14 +335,15 @@ def test_to_pyarrow_decimal(backend, dtype, pyarrow_dtype):
         "postgres",
         "snowflake",
         "sqlite",
+        "bigquery",
+        "dask",
         "trino",
     ],
-    raises=AttributeError,
+    raises=NotImplementedError,
     reason="read_delta not yet implemented",
 )
 @pytest.mark.notyet(["clickhouse"], raises=Exception)
 @pytest.mark.notyet(["mssql", "pandas"], raises=PyDeltaTableError)
-@pytest.mark.notyet(["bigquery", "dask"], raises=NotImplementedError)
 @pytest.mark.notyet(
     ["druid"],
     raises=pa.lib.ArrowTypeError,
diff --git a/ibis/backends/tests/tpch/snapshots/test_h01/test_tpc_h01/trino/h01.sql b/ibis/backends/tests/tpch/snapshots/test_h01/test_tpc_h01/trino/h01.sql
--- a/ibis/backends/tests/tpch/snapshots/test_h01/test_tpc_h01/trino/h01.sql
+++ b/ibis/backends/tests/tpch/snapshots/test_h01/test_tpc_h01/trino/h01.sql
@@ -27,7 +27,7 @@ FROM (
     AVG(t1.l_extendedprice) AS avg_price,
     AVG(t1.l_discount) AS avg_disc,
     COUNT(*) AS count_order
-  FROM "hive".ibis_sf1.lineitem AS t1
+  FROM hive.ibis_sf1.lineitem AS t1
   WHERE
     t1.l_shipdate <= FROM_ISO8601_DATE('1998-09-02')
   GROUP BY
diff --git a/ibis/backends/tests/tpch/snapshots/test_h02/test_tpc_h02/trino/h02.sql b/ibis/backends/tests/tpch/snapshots/test_h02/test_tpc_h02/trino/h02.sql
--- a/ibis/backends/tests/tpch/snapshots/test_h02/test_tpc_h02/trino/h02.sql
+++ b/ibis/backends/tests/tpch/snapshots/test_h02/test_tpc_h02/trino/h02.sql
@@ -28,14 +28,14 @@ WITH t0 AS (
     t6.r_regionkey AS r_regionkey,
     t6.r_name AS r_name,
     t6.r_comment AS r_comment
-  FROM "hive".ibis_sf1.part AS t2
-  JOIN "hive".ibis_sf1.partsupp AS t3
+  FROM hive.ibis_sf1.part AS t2
+  JOIN hive.ibis_sf1.partsupp AS t3
     ON t2.p_partkey = t3.ps_partkey
-  JOIN "hive".ibis_sf1.supplier AS t4
+  JOIN hive.ibis_sf1.supplier AS t4
     ON t4.s_suppkey = t3.ps_suppkey
-  JOIN "hive".ibis_sf1.nation AS t5
+  JOIN hive.ibis_sf1.nation AS t5
     ON t4.s_nationkey = t5.n_nationkey
-  JOIN "hive".ibis_sf1.region AS t6
+  JOIN hive.ibis_sf1.region AS t6
     ON t5.n_regionkey = t6.r_regionkey
   WHERE
     t2.p_size = 15
@@ -44,12 +44,12 @@ WITH t0 AS (
     AND t3.ps_supplycost = (
       SELECT
         MIN(t3.ps_supplycost) AS "Min(ps_supplycost)"
-      FROM "hive".ibis_sf1.partsupp AS t3
-      JOIN "hive".ibis_sf1.supplier AS t4
+      FROM hive.ibis_sf1.partsupp AS t3
+      JOIN hive.ibis_sf1.supplier AS t4
         ON t4.s_suppkey = t3.ps_suppkey
-      JOIN "hive".ibis_sf1.nation AS t5
+      JOIN hive.ibis_sf1.nation AS t5
         ON t4.s_nationkey = t5.n_nationkey
-      JOIN "hive".ibis_sf1.region AS t6
+      JOIN hive.ibis_sf1.region AS t6
         ON t5.n_regionkey = t6.r_regionkey
       WHERE
         t6.r_name = 'EUROPE' AND t2.p_partkey = t3.ps_partkey
diff --git a/ibis/backends/tests/tpch/snapshots/test_h03/test_tpc_h03/trino/h03.sql b/ibis/backends/tests/tpch/snapshots/test_h03/test_tpc_h03/trino/h03.sql
--- a/ibis/backends/tests/tpch/snapshots/test_h03/test_tpc_h03/trino/h03.sql
+++ b/ibis/backends/tests/tpch/snapshots/test_h03/test_tpc_h03/trino/h03.sql
@@ -6,10 +6,10 @@ WITH t0 AS (
     SUM(t4.l_extendedprice * (
       1 - t4.l_discount
     )) AS revenue
-  FROM "hive".ibis_sf1.customer AS t2
-  JOIN "hive".ibis_sf1.orders AS t3
+  FROM hive.ibis_sf1.customer AS t2
+  JOIN hive.ibis_sf1.orders AS t3
     ON t2.c_custkey = t3.o_custkey
-  JOIN "hive".ibis_sf1.lineitem AS t4
+  JOIN hive.ibis_sf1.lineitem AS t4
     ON t4.l_orderkey = t3.o_orderkey
   WHERE
     t2.c_mktsegment = 'BUILDING'
diff --git a/ibis/backends/tests/tpch/snapshots/test_h04/test_tpc_h04/trino/h04.sql b/ibis/backends/tests/tpch/snapshots/test_h04/test_tpc_h04/trino/h04.sql
--- a/ibis/backends/tests/tpch/snapshots/test_h04/test_tpc_h04/trino/h04.sql
+++ b/ibis/backends/tests/tpch/snapshots/test_h04/test_tpc_h04/trino/h04.sql
@@ -1,13 +1,13 @@
 SELECT
   t0.o_orderpriority,
   COUNT(*) AS order_count
-FROM "hive".ibis_sf1.orders AS t0
+FROM hive.ibis_sf1.orders AS t0
 WHERE
   (
     EXISTS(
       SELECT
         1 AS anon_1
-      FROM "hive".ibis_sf1.lineitem AS t1
+      FROM hive.ibis_sf1.lineitem AS t1
       WHERE
         t1.l_orderkey = t0.o_orderkey AND t1.l_commitdate < t1.l_receiptdate
     )
diff --git a/ibis/backends/tests/tpch/snapshots/test_h05/test_tpc_h05/trino/h05.sql b/ibis/backends/tests/tpch/snapshots/test_h05/test_tpc_h05/trino/h05.sql
--- a/ibis/backends/tests/tpch/snapshots/test_h05/test_tpc_h05/trino/h05.sql
+++ b/ibis/backends/tests/tpch/snapshots/test_h05/test_tpc_h05/trino/h05.sql
@@ -7,16 +7,16 @@ FROM (
     SUM(t3.l_extendedprice * (
       1 - t3.l_discount
     )) AS revenue
-  FROM "hive".ibis_sf1.customer AS t1
-  JOIN "hive".ibis_sf1.orders AS t2
+  FROM hive.ibis_sf1.customer AS t1
+  JOIN hive.ibis_sf1.orders AS t2
     ON t1.c_custkey = t2.o_custkey
-  JOIN "hive".ibis_sf1.lineitem AS t3
+  JOIN hive.ibis_sf1.lineitem AS t3
     ON t3.l_orderkey = t2.o_orderkey
-  JOIN "hive".ibis_sf1.supplier AS t4
+  JOIN hive.ibis_sf1.supplier AS t4
     ON t3.l_suppkey = t4.s_suppkey
-  JOIN "hive".ibis_sf1.nation AS t5
+  JOIN hive.ibis_sf1.nation AS t5
     ON t1.c_nationkey = t4.s_nationkey AND t4.s_nationkey = t5.n_nationkey
-  JOIN "hive".ibis_sf1.region AS t6
+  JOIN hive.ibis_sf1.region AS t6
     ON t5.n_regionkey = t6.r_regionkey
   WHERE
     t6.r_name = 'ASIA'
diff --git a/ibis/backends/tests/tpch/snapshots/test_h06/test_tpc_h06/trino/h06.sql b/ibis/backends/tests/tpch/snapshots/test_h06/test_tpc_h06/trino/h06.sql
--- a/ibis/backends/tests/tpch/snapshots/test_h06/test_tpc_h06/trino/h06.sql
+++ b/ibis/backends/tests/tpch/snapshots/test_h06/test_tpc_h06/trino/h06.sql
@@ -1,6 +1,6 @@
 SELECT
   SUM(t0.l_extendedprice * t0.l_discount) AS revenue
-FROM "hive".ibis_sf1.lineitem AS t0
+FROM hive.ibis_sf1.lineitem AS t0
 WHERE
   t0.l_shipdate >= FROM_ISO8601_DATE('1994-01-01')
   AND t0.l_shipdate < FROM_ISO8601_DATE('1995-01-01')
diff --git a/ibis/backends/tests/tpch/snapshots/test_h07/test_tpc_h07/trino/h07.sql b/ibis/backends/tests/tpch/snapshots/test_h07/test_tpc_h07/trino/h07.sql
--- a/ibis/backends/tests/tpch/snapshots/test_h07/test_tpc_h07/trino/h07.sql
+++ b/ibis/backends/tests/tpch/snapshots/test_h07/test_tpc_h07/trino/h07.sql
@@ -9,16 +9,16 @@ WITH t0 AS (
     t3.l_extendedprice * (
       1 - t3.l_discount
     ) AS volume
-  FROM "hive".ibis_sf1.supplier AS t2
-  JOIN "hive".ibis_sf1.lineitem AS t3
+  FROM hive.ibis_sf1.supplier AS t2
+  JOIN hive.ibis_sf1.lineitem AS t3
     ON t2.s_suppkey = t3.l_suppkey
-  JOIN "hive".ibis_sf1.orders AS t4
+  JOIN hive.ibis_sf1.orders AS t4
     ON t4.o_orderkey = t3.l_orderkey
-  JOIN "hive".ibis_sf1.customer AS t5
+  JOIN hive.ibis_sf1.customer AS t5
     ON t5.c_custkey = t4.o_custkey
-  JOIN "hive".ibis_sf1.nation AS t6
+  JOIN hive.ibis_sf1.nation AS t6
     ON t2.s_nationkey = t6.n_nationkey
-  JOIN "hive".ibis_sf1.nation AS t7
+  JOIN hive.ibis_sf1.nation AS t7
     ON t5.c_nationkey = t7.n_nationkey
 )
 SELECT
diff --git a/ibis/backends/tests/tpch/snapshots/test_h08/test_tpc_h08/trino/h08.sql b/ibis/backends/tests/tpch/snapshots/test_h08/test_tpc_h08/trino/h08.sql
--- a/ibis/backends/tests/tpch/snapshots/test_h08/test_tpc_h08/trino/h08.sql
+++ b/ibis/backends/tests/tpch/snapshots/test_h08/test_tpc_h08/trino/h08.sql
@@ -8,20 +8,20 @@ WITH t0 AS (
     t10.r_name AS r_name,
     t7.o_orderdate AS o_orderdate,
     t4.p_type AS p_type
-  FROM "hive".ibis_sf1.part AS t4
-  JOIN "hive".ibis_sf1.lineitem AS t5
+  FROM hive.ibis_sf1.part AS t4
+  JOIN hive.ibis_sf1.lineitem AS t5
     ON t4.p_partkey = t5.l_partkey
-  JOIN "hive".ibis_sf1.supplier AS t6
+  JOIN hive.ibis_sf1.supplier AS t6
     ON t6.s_suppkey = t5.l_suppkey
-  JOIN "hive".ibis_sf1.orders AS t7
+  JOIN hive.ibis_sf1.orders AS t7
     ON t5.l_orderkey = t7.o_orderkey
-  JOIN "hive".ibis_sf1.customer AS t8
+  JOIN hive.ibis_sf1.customer AS t8
     ON t7.o_custkey = t8.c_custkey
-  JOIN "hive".ibis_sf1.nation AS t9
+  JOIN hive.ibis_sf1.nation AS t9
     ON t8.c_nationkey = t9.n_nationkey
-  JOIN "hive".ibis_sf1.region AS t10
+  JOIN hive.ibis_sf1.region AS t10
     ON t9.n_regionkey = t10.r_regionkey
-  JOIN "hive".ibis_sf1.nation AS t11
+  JOIN hive.ibis_sf1.nation AS t11
     ON t6.s_nationkey = t11.n_nationkey
 ), t1 AS (
   SELECT
diff --git a/ibis/backends/tests/tpch/snapshots/test_h09/test_tpc_h09/trino/h09.sql b/ibis/backends/tests/tpch/snapshots/test_h09/test_tpc_h09/trino/h09.sql
--- a/ibis/backends/tests/tpch/snapshots/test_h09/test_tpc_h09/trino/h09.sql
+++ b/ibis/backends/tests/tpch/snapshots/test_h09/test_tpc_h09/trino/h09.sql
@@ -6,16 +6,16 @@ WITH t0 AS (
     CAST(EXTRACT(year FROM t6.o_orderdate) AS SMALLINT) AS o_year,
     t7.n_name AS nation,
     t5.p_name AS p_name
-  FROM "hive".ibis_sf1.lineitem AS t2
-  JOIN "hive".ibis_sf1.supplier AS t3
+  FROM hive.ibis_sf1.lineitem AS t2
+  JOIN hive.ibis_sf1.supplier AS t3
     ON t3.s_suppkey = t2.l_suppkey
-  JOIN "hive".ibis_sf1.partsupp AS t4
+  JOIN hive.ibis_sf1.partsupp AS t4
     ON t4.ps_suppkey = t2.l_suppkey AND t4.ps_partkey = t2.l_partkey
-  JOIN "hive".ibis_sf1.part AS t5
+  JOIN hive.ibis_sf1.part AS t5
     ON t5.p_partkey = t2.l_partkey
-  JOIN "hive".ibis_sf1.orders AS t6
+  JOIN hive.ibis_sf1.orders AS t6
     ON t6.o_orderkey = t2.l_orderkey
-  JOIN "hive".ibis_sf1.nation AS t7
+  JOIN hive.ibis_sf1.nation AS t7
     ON t3.s_nationkey = t7.n_nationkey
   WHERE
     t5.p_name LIKE '%green%'
diff --git a/ibis/backends/tests/tpch/snapshots/test_h10/test_tpc_h10/trino/h10.sql b/ibis/backends/tests/tpch/snapshots/test_h10/test_tpc_h10/trino/h10.sql
--- a/ibis/backends/tests/tpch/snapshots/test_h10/test_tpc_h10/trino/h10.sql
+++ b/ibis/backends/tests/tpch/snapshots/test_h10/test_tpc_h10/trino/h10.sql
@@ -10,12 +10,12 @@ WITH t0 AS (
     SUM(t4.l_extendedprice * (
       1 - t4.l_discount
     )) AS revenue
-  FROM "hive".ibis_sf1.customer AS t2
-  JOIN "hive".ibis_sf1.orders AS t3
+  FROM hive.ibis_sf1.customer AS t2
+  JOIN hive.ibis_sf1.orders AS t3
     ON t2.c_custkey = t3.o_custkey
-  JOIN "hive".ibis_sf1.lineitem AS t4
+  JOIN hive.ibis_sf1.lineitem AS t4
     ON t4.l_orderkey = t3.o_orderkey
-  JOIN "hive".ibis_sf1.nation AS t5
+  JOIN hive.ibis_sf1.nation AS t5
     ON t2.c_nationkey = t5.n_nationkey
   WHERE
     t3.o_orderdate >= FROM_ISO8601_DATE('1993-10-01')
diff --git a/ibis/backends/tests/tpch/snapshots/test_h11/test_tpc_h11/trino/h11.sql b/ibis/backends/tests/tpch/snapshots/test_h11/test_tpc_h11/trino/h11.sql
--- a/ibis/backends/tests/tpch/snapshots/test_h11/test_tpc_h11/trino/h11.sql
+++ b/ibis/backends/tests/tpch/snapshots/test_h11/test_tpc_h11/trino/h11.sql
@@ -2,10 +2,10 @@ WITH t0 AS (
   SELECT
     t2.ps_partkey AS ps_partkey,
     SUM(t2.ps_supplycost * t2.ps_availqty) AS value
-  FROM "hive".ibis_sf1.partsupp AS t2
-  JOIN "hive".ibis_sf1.supplier AS t3
+  FROM hive.ibis_sf1.partsupp AS t2
+  JOIN hive.ibis_sf1.supplier AS t3
     ON t2.ps_suppkey = t3.s_suppkey
-  JOIN "hive".ibis_sf1.nation AS t4
+  JOIN hive.ibis_sf1.nation AS t4
     ON t4.n_nationkey = t3.s_nationkey
   WHERE
     t4.n_name = 'GERMANY'
@@ -27,10 +27,10 @@ FROM (
       FROM (
         SELECT
           SUM(t2.ps_supplycost * t2.ps_availqty) AS total
-        FROM "hive".ibis_sf1.partsupp AS t2
-        JOIN "hive".ibis_sf1.supplier AS t3
+        FROM hive.ibis_sf1.partsupp AS t2
+        JOIN hive.ibis_sf1.supplier AS t3
           ON t2.ps_suppkey = t3.s_suppkey
-        JOIN "hive".ibis_sf1.nation AS t4
+        JOIN hive.ibis_sf1.nation AS t4
           ON t4.n_nationkey = t3.s_nationkey
         WHERE
           t4.n_name = 'GERMANY'
diff --git a/ibis/backends/tests/tpch/snapshots/test_h12/test_tpc_h12/trino/h12.sql b/ibis/backends/tests/tpch/snapshots/test_h12/test_tpc_h12/trino/h12.sql
--- a/ibis/backends/tests/tpch/snapshots/test_h12/test_tpc_h12/trino/h12.sql
+++ b/ibis/backends/tests/tpch/snapshots/test_h12/test_tpc_h12/trino/h12.sql
@@ -7,8 +7,8 @@ FROM (
     t2.l_shipmode AS l_shipmode,
     SUM(CASE t1.o_orderpriority WHEN '1-URGENT' THEN 1 WHEN '2-HIGH' THEN 1 ELSE 0 END) AS high_line_count,
     SUM(CASE t1.o_orderpriority WHEN '1-URGENT' THEN 0 WHEN '2-HIGH' THEN 0 ELSE 1 END) AS low_line_count
-  FROM "hive".ibis_sf1.orders AS t1
-  JOIN "hive".ibis_sf1.lineitem AS t2
+  FROM hive.ibis_sf1.orders AS t1
+  JOIN hive.ibis_sf1.lineitem AS t2
     ON t1.o_orderkey = t2.l_orderkey
   WHERE
     t2.l_shipmode IN ('MAIL', 'SHIP')
diff --git a/ibis/backends/tests/tpch/snapshots/test_h13/test_tpc_h13/trino/h13.sql b/ibis/backends/tests/tpch/snapshots/test_h13/test_tpc_h13/trino/h13.sql
--- a/ibis/backends/tests/tpch/snapshots/test_h13/test_tpc_h13/trino/h13.sql
+++ b/ibis/backends/tests/tpch/snapshots/test_h13/test_tpc_h13/trino/h13.sql
@@ -2,8 +2,8 @@ WITH t0 AS (
   SELECT
     t2.c_custkey AS c_custkey,
     COUNT(t3.o_orderkey) AS c_count
-  FROM "hive".ibis_sf1.customer AS t2
-  LEFT OUTER JOIN "hive".ibis_sf1.orders AS t3
+  FROM hive.ibis_sf1.customer AS t2
+  LEFT OUTER JOIN hive.ibis_sf1.orders AS t3
     ON t2.c_custkey = t3.o_custkey AND NOT t3.o_comment LIKE '%special%requests%'
   GROUP BY
     1
diff --git a/ibis/backends/tests/tpch/snapshots/test_h14/test_tpc_h14/trino/h14.sql b/ibis/backends/tests/tpch/snapshots/test_h14/test_tpc_h14/trino/h14.sql
--- a/ibis/backends/tests/tpch/snapshots/test_h14/test_tpc_h14/trino/h14.sql
+++ b/ibis/backends/tests/tpch/snapshots/test_h14/test_tpc_h14/trino/h14.sql
@@ -6,8 +6,8 @@ SELECT
   ) / SUM(t0.l_extendedprice * (
     1 - t0.l_discount
   )) AS promo_revenue
-FROM "hive".ibis_sf1.lineitem AS t0
-JOIN "hive".ibis_sf1.part AS t1
+FROM hive.ibis_sf1.lineitem AS t0
+JOIN hive.ibis_sf1.part AS t1
   ON t0.l_partkey = t1.p_partkey
 WHERE
   t0.l_shipdate >= FROM_ISO8601_DATE('1995-09-01')
diff --git a/ibis/backends/tests/tpch/snapshots/test_h15/test_tpc_h15/trino/h15.sql b/ibis/backends/tests/tpch/snapshots/test_h15/test_tpc_h15/trino/h15.sql
--- a/ibis/backends/tests/tpch/snapshots/test_h15/test_tpc_h15/trino/h15.sql
+++ b/ibis/backends/tests/tpch/snapshots/test_h15/test_tpc_h15/trino/h15.sql
@@ -4,7 +4,7 @@ WITH t0 AS (
     SUM(t3.l_extendedprice * (
       1 - t3.l_discount
     )) AS total_revenue
-  FROM "hive".ibis_sf1.lineitem AS t3
+  FROM hive.ibis_sf1.lineitem AS t3
   WHERE
     t3.l_shipdate >= FROM_ISO8601_DATE('1996-01-01')
     AND t3.l_shipdate < FROM_ISO8601_DATE('1996-04-01')
@@ -21,7 +21,7 @@ WITH t0 AS (
     t3.s_comment AS s_comment,
     t0.l_suppkey AS l_suppkey,
     t0.total_revenue AS total_revenue
-  FROM "hive".ibis_sf1.supplier AS t3
+  FROM hive.ibis_sf1.supplier AS t3
   JOIN t0
     ON t3.s_suppkey = t0.l_suppkey
   WHERE
diff --git a/ibis/backends/tests/tpch/snapshots/test_h16/test_tpc_h16/trino/h16.sql b/ibis/backends/tests/tpch/snapshots/test_h16/test_tpc_h16/trino/h16.sql
--- a/ibis/backends/tests/tpch/snapshots/test_h16/test_tpc_h16/trino/h16.sql
+++ b/ibis/backends/tests/tpch/snapshots/test_h16/test_tpc_h16/trino/h16.sql
@@ -9,8 +9,8 @@ FROM (
     t2.p_type AS p_type,
     t2.p_size AS p_size,
     COUNT(DISTINCT t1.ps_suppkey) AS supplier_cnt
-  FROM "hive".ibis_sf1.partsupp AS t1
-  JOIN "hive".ibis_sf1.part AS t2
+  FROM hive.ibis_sf1.partsupp AS t1
+  JOIN hive.ibis_sf1.part AS t2
     ON t2.p_partkey = t1.ps_partkey
   WHERE
     t2.p_brand <> 'Brand#45'
@@ -29,7 +29,7 @@ FROM (
             t4.s_phone AS s_phone,
             t4.s_acctbal AS s_acctbal,
             t4.s_comment AS s_comment
-          FROM "hive".ibis_sf1.supplier AS t4
+          FROM hive.ibis_sf1.supplier AS t4
           WHERE
             t4.s_comment LIKE '%Customer%Complaints%'
         ) AS t3
diff --git a/ibis/backends/tests/tpch/snapshots/test_h17/test_tpc_h17/trino/h17.sql b/ibis/backends/tests/tpch/snapshots/test_h17/test_tpc_h17/trino/h17.sql
--- a/ibis/backends/tests/tpch/snapshots/test_h17/test_tpc_h17/trino/h17.sql
+++ b/ibis/backends/tests/tpch/snapshots/test_h17/test_tpc_h17/trino/h17.sql
@@ -1,7 +1,7 @@
 SELECT
   SUM(t0.l_extendedprice) / 7.0 AS avg_yearly
-FROM "hive".ibis_sf1.lineitem AS t0
-JOIN "hive".ibis_sf1.part AS t1
+FROM hive.ibis_sf1.lineitem AS t0
+JOIN hive.ibis_sf1.part AS t1
   ON t1.p_partkey = t0.l_partkey
 WHERE
   t1.p_brand = 'Brand#23'
@@ -9,7 +9,7 @@ WHERE
   AND t0.l_quantity < (
     SELECT
       AVG(t0.l_quantity) AS "Mean(l_quantity)"
-    FROM "hive".ibis_sf1.lineitem AS t0
+    FROM hive.ibis_sf1.lineitem AS t0
     WHERE
       t0.l_partkey = t1.p_partkey
   ) * 0.2
\ No newline at end of file
diff --git a/ibis/backends/tests/tpch/snapshots/test_h18/test_tpc_h18/trino/h18.sql b/ibis/backends/tests/tpch/snapshots/test_h18/test_tpc_h18/trino/h18.sql
--- a/ibis/backends/tests/tpch/snapshots/test_h18/test_tpc_h18/trino/h18.sql
+++ b/ibis/backends/tests/tpch/snapshots/test_h18/test_tpc_h18/trino/h18.sql
@@ -2,7 +2,7 @@ WITH t0 AS (
   SELECT
     t2.l_orderkey AS l_orderkey,
     SUM(t2.l_quantity) AS qty_sum
-  FROM "hive".ibis_sf1.lineitem AS t2
+  FROM hive.ibis_sf1.lineitem AS t2
   GROUP BY
     1
 )
@@ -21,10 +21,10 @@ FROM (
     t3.o_orderdate AS o_orderdate,
     t3.o_totalprice AS o_totalprice,
     SUM(t4.l_quantity) AS sum_qty
-  FROM "hive".ibis_sf1.customer AS t2
-  JOIN "hive".ibis_sf1.orders AS t3
+  FROM hive.ibis_sf1.customer AS t2
+  JOIN hive.ibis_sf1.orders AS t3
     ON t2.c_custkey = t3.o_custkey
-  JOIN "hive".ibis_sf1.lineitem AS t4
+  JOIN hive.ibis_sf1.lineitem AS t4
     ON t3.o_orderkey = t4.l_orderkey
   WHERE
     t3.o_orderkey IN (
diff --git a/ibis/backends/tests/tpch/snapshots/test_h19/test_tpc_h19/trino/h19.sql b/ibis/backends/tests/tpch/snapshots/test_h19/test_tpc_h19/trino/h19.sql
--- a/ibis/backends/tests/tpch/snapshots/test_h19/test_tpc_h19/trino/h19.sql
+++ b/ibis/backends/tests/tpch/snapshots/test_h19/test_tpc_h19/trino/h19.sql
@@ -2,8 +2,8 @@ SELECT
   SUM(t0.l_extendedprice * (
     1 - t0.l_discount
   )) AS revenue
-FROM "hive".ibis_sf1.lineitem AS t0
-JOIN "hive".ibis_sf1.part AS t1
+FROM hive.ibis_sf1.lineitem AS t0
+JOIN hive.ibis_sf1.part AS t1
   ON t1.p_partkey = t0.l_partkey
 WHERE
   t1.p_brand = 'Brand#12'
diff --git a/ibis/backends/tests/tpch/snapshots/test_h20/test_tpc_h20/trino/h20.sql b/ibis/backends/tests/tpch/snapshots/test_h20/test_tpc_h20/trino/h20.sql
--- a/ibis/backends/tests/tpch/snapshots/test_h20/test_tpc_h20/trino/h20.sql
+++ b/ibis/backends/tests/tpch/snapshots/test_h20/test_tpc_h20/trino/h20.sql
@@ -11,8 +11,8 @@ WITH t0 AS (
     t3.n_name AS n_name,
     t3.n_regionkey AS n_regionkey,
     t3.n_comment AS n_comment
-  FROM "hive".ibis_sf1.supplier AS t2
-  JOIN "hive".ibis_sf1.nation AS t3
+  FROM hive.ibis_sf1.supplier AS t2
+  JOIN hive.ibis_sf1.nation AS t3
     ON t2.s_nationkey = t3.n_nationkey
   WHERE
     t3.n_name = 'CANADA'
@@ -26,7 +26,7 @@ WITH t0 AS (
           t5.ps_availqty AS ps_availqty,
           t5.ps_supplycost AS ps_supplycost,
           t5.ps_comment AS ps_comment
-        FROM "hive".ibis_sf1.partsupp AS t5
+        FROM hive.ibis_sf1.partsupp AS t5
         WHERE
           t5.ps_partkey IN (
             SELECT
@@ -42,7 +42,7 @@ WITH t0 AS (
                 t7.p_container AS p_container,
                 t7.p_retailprice AS p_retailprice,
                 t7.p_comment AS p_comment
-              FROM "hive".ibis_sf1.part AS t7
+              FROM hive.ibis_sf1.part AS t7
               WHERE
                 t7.p_name LIKE 'forest%'
             ) AS t6
@@ -50,7 +50,7 @@ WITH t0 AS (
           AND t5.ps_availqty > (
             SELECT
               SUM(t6.l_quantity) AS "Sum(l_quantity)"
-            FROM "hive".ibis_sf1.lineitem AS t6
+            FROM hive.ibis_sf1.lineitem AS t6
             WHERE
               t6.l_partkey = t5.ps_partkey
               AND t6.l_suppkey = t5.ps_suppkey
diff --git a/ibis/backends/tests/tpch/snapshots/test_h21/test_tpc_h21/trino/h21.sql b/ibis/backends/tests/tpch/snapshots/test_h21/test_tpc_h21/trino/h21.sql
--- a/ibis/backends/tests/tpch/snapshots/test_h21/test_tpc_h21/trino/h21.sql
+++ b/ibis/backends/tests/tpch/snapshots/test_h21/test_tpc_h21/trino/h21.sql
@@ -7,12 +7,12 @@ WITH t0 AS (
     t3.l_suppkey AS l1_suppkey,
     t2.s_name AS s_name,
     t5.n_name AS n_name
-  FROM "hive".ibis_sf1.supplier AS t2
-  JOIN "hive".ibis_sf1.lineitem AS t3
+  FROM hive.ibis_sf1.supplier AS t2
+  JOIN hive.ibis_sf1.lineitem AS t3
     ON t2.s_suppkey = t3.l_suppkey
-  JOIN "hive".ibis_sf1.orders AS t4
+  JOIN hive.ibis_sf1.orders AS t4
     ON t4.o_orderkey = t3.l_orderkey
-  JOIN "hive".ibis_sf1.nation AS t5
+  JOIN hive.ibis_sf1.nation AS t5
     ON t2.s_nationkey = t5.n_nationkey
 )
 SELECT
@@ -31,7 +31,7 @@ FROM (
       EXISTS(
         SELECT
           1 AS anon_1
-        FROM "hive".ibis_sf1.lineitem AS t2
+        FROM hive.ibis_sf1.lineitem AS t2
         WHERE
           t2.l_orderkey = t0.l1_orderkey AND t2.l_suppkey <> t0.l1_suppkey
       )
@@ -40,7 +40,7 @@ FROM (
       EXISTS(
         SELECT
           1 AS anon_2
-        FROM "hive".ibis_sf1.lineitem AS t2
+        FROM hive.ibis_sf1.lineitem AS t2
         WHERE
           t2.l_orderkey = t0.l1_orderkey
           AND t2.l_suppkey <> t0.l1_suppkey
diff --git a/ibis/backends/tests/tpch/snapshots/test_h22/test_tpc_h22/trino/h22.sql b/ibis/backends/tests/tpch/snapshots/test_h22/test_tpc_h22/trino/h22.sql
--- a/ibis/backends/tests/tpch/snapshots/test_h22/test_tpc_h22/trino/h22.sql
+++ b/ibis/backends/tests/tpch/snapshots/test_h22/test_tpc_h22/trino/h22.sql
@@ -8,7 +8,7 @@ WITH t0 AS (
       ELSE SUBSTR(t2.c_phone, 0 + 1 + LENGTH(t2.c_phone), 2)
     END AS cntrycode,
     t2.c_acctbal AS c_acctbal
-  FROM "hive".ibis_sf1.customer AS t2
+  FROM hive.ibis_sf1.customer AS t2
   WHERE
     CASE
       WHEN (
@@ -23,7 +23,7 @@ WITH t0 AS (
       FROM (
         SELECT
           AVG(t2.c_acctbal) AS avg_bal
-        FROM "hive".ibis_sf1.customer AS t2
+        FROM hive.ibis_sf1.customer AS t2
         WHERE
           t2.c_acctbal > 0.0
           AND CASE
@@ -39,7 +39,7 @@ WITH t0 AS (
       EXISTS(
         SELECT
           1 AS anon_2
-        FROM "hive".ibis_sf1.orders AS t3
+        FROM hive.ibis_sf1.orders AS t3
         WHERE
           t3.o_custkey = t2.c_custkey
       )
diff --git a/ibis/backends/trino/tests/test_client.py b/ibis/backends/trino/tests/test_client.py
--- a/ibis/backends/trino/tests/test_client.py
+++ b/ibis/backends/trino/tests/test_client.py
@@ -145,3 +145,19 @@ def test_create_table_timestamp():
     finally:
         con.drop_table(table)
         assert table not in con.list_tables()
+
+
+def test_table_access_from_connection_without_catalog_or_schema():
+    con = ibis.trino.connect()
+    # can't use the `system` catalog to test here, because the trino sqlalchemy
+    # dialect defaults to `system` if no catalog is passed, so it wouldn't be a
+    # useful test
+    assert con.current_database != "tpch"
+    assert con.current_schema is None
+
+    t = con.table("region", schema="tpch.sf1")
+
+    assert con.current_database != "tpch"
+    assert con.current_schema is None
+
+    assert t.count().execute()