Repository: ibis-project/ibis

Issue:
bug: `to_sql` always shows DuckDB SQL for a memtable even if there's a default backend set
### What happened?

```python

[ins] In [1]: import ibis

[ins] In [2]: con = ibis.bigquery.connect(project_id="ibis-gbq", dataset_id="ibis_gbq
         ...: _testing")

[ins] In [3]: ibis.set_backend(con)

[ins] In [4]: t = ibis.memtable({"b": [1, 2]})

[ins] In [5]: ibis.to_sql(t.b.count())
Out[5]: 
SELECT
  COUNT("t0"."b") AS "Count(b)"
FROM "ibis_pandas_memtable_cr5nlla4tffofda5zgamctyks4" AS "t0"
```

Note the incorrect quotes (should be backticks) -- falling back to DuckDB SQL makes sense, but not when a user has explicitly set the backend.

### What version of ibis are you using?

main

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

All

### Relevant log output

_No response_

### Code of Conduct

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


Discussion:
xref #9179 
There may be other issues with Oracle table aliases, but this behavior of `to_sql` is what sent me down a rabbit hole trying to change how we use `alias` (that may still be necessary).

Resolving patch:
diff --git a/ibis/expr/sql.py b/ibis/expr/sql.py
--- a/ibis/expr/sql.py
+++ b/ibis/expr/sql.py
@@ -366,7 +366,7 @@ def to_sql(
     # the default pretty dialect for expressions
     if dialect is None:
         try:
-            backend = expr._find_backend()
+            backend = expr._find_backend(use_default=True)
         except com.IbisError:
             # default to duckdb for SQL compilation because it supports the
             # widest array of ibis features for SQL backends


Test patch:
diff --git a/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/bigquery/to_sql.sql b/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/bigquery/to_sql.sql
new file mode 100644
--- /dev/null
+++ b/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/bigquery/to_sql.sql
@@ -0,0 +1,7 @@
+SELECT
+  COUNT(*) AS `CountStar`
+FROM (
+  SELECT
+    *
+  FROM `mytable` AS `t0`
+) AS `t1`
\ No newline at end of file
diff --git a/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/clickhouse/to_sql.sql b/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/clickhouse/to_sql.sql
new file mode 100644
--- /dev/null
+++ b/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/clickhouse/to_sql.sql
@@ -0,0 +1,7 @@
+SELECT
+  COUNT(*) AS "CountStar()"
+FROM (
+  SELECT
+    *
+  FROM "mytable" AS "t0"
+) AS "t1"
\ No newline at end of file
diff --git a/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/datafusion/to_sql.sql b/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/datafusion/to_sql.sql
new file mode 100644
--- /dev/null
+++ b/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/datafusion/to_sql.sql
@@ -0,0 +1,7 @@
+SELECT
+  COUNT(*) AS "CountStar()"
+FROM (
+  SELECT
+    *
+  FROM "mytable" AS "t0"
+) AS "t1"
\ No newline at end of file
diff --git a/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/druid/to_sql.sql b/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/druid/to_sql.sql
new file mode 100644
--- /dev/null
+++ b/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/druid/to_sql.sql
@@ -0,0 +1,13 @@
+SELECT
+  COUNT(*) AS "CountStar()"
+FROM (
+  SELECT
+    *
+  FROM (
+    SELECT
+      "b"
+    FROM (VALUES
+      (1),
+      (2)) AS "mytable"("b")
+  ) AS "t0"
+) AS "t1"
\ No newline at end of file
diff --git a/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/duckdb/to_sql.sql b/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/duckdb/to_sql.sql
new file mode 100644
--- /dev/null
+++ b/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/duckdb/to_sql.sql
@@ -0,0 +1,7 @@
+SELECT
+  COUNT(*) AS "CountStar()"
+FROM (
+  SELECT
+    *
+  FROM "mytable" AS "t0"
+) AS "t1"
\ No newline at end of file
diff --git a/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/exasol/to_sql.sql b/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/exasol/to_sql.sql
new file mode 100644
--- /dev/null
+++ b/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/exasol/to_sql.sql
@@ -0,0 +1,7 @@
+SELECT
+  COUNT(*) AS "CountStar()"
+FROM (
+  SELECT
+    *
+  FROM "mytable" AS "t0"
+) AS "t1"
\ No newline at end of file
diff --git a/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/flink/to_sql.sql b/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/flink/to_sql.sql
new file mode 100644
--- /dev/null
+++ b/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/flink/to_sql.sql
@@ -0,0 +1,13 @@
+SELECT
+  COUNT(*) AS `CountStar()`
+FROM (
+  SELECT
+    *
+  FROM (
+    SELECT
+      `b`
+    FROM (VALUES
+      (1),
+      (2)) AS `mytable`(`b`)
+  ) AS `t0`
+) AS `t1`
\ No newline at end of file
diff --git a/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/impala/to_sql.sql b/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/impala/to_sql.sql
new file mode 100644
--- /dev/null
+++ b/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/impala/to_sql.sql
@@ -0,0 +1,7 @@
+SELECT
+  COUNT(*) AS `CountStar()`
+FROM (
+  SELECT
+    *
+  FROM `mytable` AS `t0`
+) AS `t1`
\ No newline at end of file
diff --git a/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/mssql/to_sql.sql b/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/mssql/to_sql.sql
new file mode 100644
--- /dev/null
+++ b/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/mssql/to_sql.sql
@@ -0,0 +1,7 @@
+SELECT
+  COUNT(*) AS [CountStar()]
+FROM (
+  SELECT
+    [t0].[b]
+  FROM [mytable] AS [t0]
+) AS [t1]
\ No newline at end of file
diff --git a/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/mysql/to_sql.sql b/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/mysql/to_sql.sql
new file mode 100644
--- /dev/null
+++ b/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/mysql/to_sql.sql
@@ -0,0 +1,7 @@
+SELECT
+  COUNT(*) AS `CountStar()`
+FROM (
+  SELECT
+    *
+  FROM `mytable` AS `t0`
+) AS `t1`
\ No newline at end of file
diff --git a/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/oracle/to_sql.sql b/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/oracle/to_sql.sql
new file mode 100644
--- /dev/null
+++ b/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/oracle/to_sql.sql
@@ -0,0 +1,7 @@
+SELECT
+  COUNT(*) AS "CountStar()"
+FROM (
+  SELECT
+    *
+  FROM "mytable" "t0"
+) "t1"
\ No newline at end of file
diff --git a/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/postgres/to_sql.sql b/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/postgres/to_sql.sql
new file mode 100644
--- /dev/null
+++ b/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/postgres/to_sql.sql
@@ -0,0 +1,7 @@
+SELECT
+  COUNT(*) AS "CountStar()"
+FROM (
+  SELECT
+    *
+  FROM "mytable" AS "t0"
+) AS "t1"
\ No newline at end of file
diff --git a/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/pyspark/to_sql.sql b/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/pyspark/to_sql.sql
new file mode 100644
--- /dev/null
+++ b/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/pyspark/to_sql.sql
@@ -0,0 +1,7 @@
+SELECT
+  COUNT(*) AS `CountStar()`
+FROM (
+  SELECT
+    *
+  FROM `mytable` AS `t0`
+) AS `t1`
\ No newline at end of file
diff --git a/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/risingwave/to_sql.sql b/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/risingwave/to_sql.sql
new file mode 100644
--- /dev/null
+++ b/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/risingwave/to_sql.sql
@@ -0,0 +1,7 @@
+SELECT
+  COUNT(*) AS "CountStar()"
+FROM (
+  SELECT
+    *
+  FROM "mytable" AS "t0"
+) AS "t1"
\ No newline at end of file
diff --git a/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/snowflake/to_sql.sql b/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/snowflake/to_sql.sql
new file mode 100644
--- /dev/null
+++ b/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/snowflake/to_sql.sql
@@ -0,0 +1,7 @@
+SELECT
+  COUNT(*) AS "CountStar()"
+FROM (
+  SELECT
+    *
+  FROM "mytable" AS "t0"
+) AS "t1"
\ No newline at end of file
diff --git a/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/sqlite/to_sql.sql b/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/sqlite/to_sql.sql
new file mode 100644
--- /dev/null
+++ b/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/sqlite/to_sql.sql
@@ -0,0 +1,7 @@
+SELECT
+  COUNT(*) AS "CountStar()"
+FROM (
+  SELECT
+    *
+  FROM "mytable" AS "t0"
+) AS "t1"
\ No newline at end of file
diff --git a/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/trino/to_sql.sql b/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/trino/to_sql.sql
new file mode 100644
--- /dev/null
+++ b/ibis/backends/tests/snapshots/test_sql/test_to_sql_default_backend/trino/to_sql.sql
@@ -0,0 +1,7 @@
+SELECT
+  COUNT(*) AS "CountStar()"
+FROM (
+  SELECT
+    *
+  FROM "mytable" AS "t0"
+) AS "t1"
\ No newline at end of file
diff --git a/ibis/backends/tests/test_sql.py b/ibis/backends/tests/test_sql.py
--- a/ibis/backends/tests/test_sql.py
+++ b/ibis/backends/tests/test_sql.py
@@ -205,3 +205,13 @@ def test_selects_with_impure_operations_not_merged(con, snapshot, value):
 
     sql = str(ibis.to_sql(t, dialect=con.name))
     snapshot.assert_match(sql, "out.sql")
+
+
+@pytest.mark.notyet(["polars"], reason="no sql generation")
+@pytest.mark.never(["pandas", "dask"], reason="no sql generation")
+def test_to_sql_default_backend(con, snapshot, monkeypatch):
+    monkeypatch.setattr(ibis.options, "default_backend", con)
+
+    t = ibis.memtable({"b": [1, 2]}, name="mytable")
+    expr = t.select("b").count()
+    snapshot.assert_match(ibis.to_sql(expr), "to_sql.sql")