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

fix(sql): only return tables in current_database #9748

Merged
merged 21 commits into from
Aug 5, 2024

Conversation

gforsyth
Copy link
Member

@gforsyth gforsyth commented Aug 1, 2024

Description of changes

This started as a fix of #9686 but it turned up a few issues in other backends.

The short version is: when we call get_schema without specifying a
catalog.database prefix, we get tables that exist in many (sometimes all) the
databases on a given backend (that match the name passed to get_schema).

Postgres has things like search_path to help with this, but the tables on search_path are slightly different than what's returned by \dt (just to make this extra fun).

What's implemented here (and what I'm proposing as a general standard) is:

When you ask for a table named foo, we will look through the
current_database and wherever temp tables are stored for a match and return
the schema of that table.

My idea here is that anything that shows up in the output of list_tables (or
con.tables) should be accessible via con.table without additional arguments.

There's at least one edge-case which I discovered while writing this
description, which is if the temp table has the same name as a table in the
current_database, the table returned will be the interleaved schema of the two
tables, which is terrible.

I'll fix that, too.

Issues closed

Todo List of things to fix (not all in this PR):

  • decide on whether temp tables take precedence over concrete tables or vice-versa
  • add spark, datafusion and clickhouse exceptions to test
  • add cross-database table creation to mysql
  • fix mssql table collection
  • fix trino table collection
  • separate out risingwave get_schema because it doesn't have postgres-specific function
  • confirm that exasol can't create tables in other databases

@cpcloud
Copy link
Member

cpcloud commented Aug 1, 2024

It looks like temp takes precedence if the names collide, at least in DuckDB:

D create table t (x int);
D create temp table t (y int);
D table t;
┌────────┐
│   y    │
│ int32  │
├────────┤
│ 0 rows │
└────────┘

@cpcloud
Copy link
Member

cpcloud commented Aug 1, 2024

Pushed up the additional test. It passed without changes 🥳

@gforsyth
Copy link
Member Author

gforsyth commented Aug 1, 2024

It looks like temp takes precedence if the names collide, at least in DuckDB:

Ok, I guess we need to decide on what convention we want to enforce?

@gforsyth
Copy link
Member Author

gforsyth commented Aug 1, 2024

lol, I'm going to have to relax the cross-backend exception check a fair bit until we get Naty's TableNotFound PR in, but I think this should go in before 10.0

@gforsyth
Copy link
Member Author

gforsyth commented Aug 1, 2024

ok, datafusion and clickhouse are failing b/c the exception doesn't match.

MSSQL is failing the way postgres and duckdb did before the fix.

@gforsyth
Copy link
Member Author

gforsyth commented Aug 1, 2024

A hilarious error message from MySQL:

        if database is not None and database != self.current_database:
>           raise com.UnsupportedOperationError(
                "Creating tables in other databases is not supported by Postgres"
            )
E           ibis.common.exceptions.UnsupportedOperationError: Creating tables in other databases is not supported by Postgres

Need to fix that, too...

edit: actually, you CAN create tables in other databases using mysql, so that needs to be fixed.

@gforsyth
Copy link
Member Author

gforsyth commented Aug 1, 2024

Ok, this turned into a bit of a nightmare. I'll pick it back up tomorrow or Monday

@cpcloud cpcloud force-pushed the postgres_search_path branch 2 times, most recently from d8f8c5c to 456f95a Compare August 3, 2024 13:02
@cpcloud cpcloud added pyspark The Apache PySpark backend duckdb The DuckDB backend mssql The Microsoft SQL Server backend trino The Trino backend exasol Issues related to the exasol backend postgres The PostgreSQL backend mysql The MySQL backend labels Aug 3, 2024
@@ -1,5 +1,4 @@
CREATE USER 'ibis'@'localhost' IDENTIFIED BY 'ibis';
CREATE SCHEMA IF NOT EXISTS test_schema;
GRANT CREATE, DROP ON *.* TO 'ibis'@'%';
GRANT CREATE,SELECT,DROP ON `test_schema`.* TO 'ibis'@'%';
GRANT CREATE,SELECT,DROP ON *.* TO 'ibis'@'%';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Necessary to allow the ibis user to operate on newly created databases.

"column_name",
"data_type",
sg.column("is_nullable").eq(sge.convert("YES")).as_("nullable"),
query = sge.Describe(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DESCRIBE in DuckDB used to be different than using information_schema, but only for CSV files, and that case has been fixed in a version that we no longer support.

@@ -586,3 +586,9 @@ def drop_sink(
)
with self._safe_raw_sql(src):
pass

@property
def _session_temp_db(self) -> str | None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was actually enough to make tests pass, since otherwise get_schema works just fine.

sg.column("is_nullable").eq(sge.convert("YES")).as_("nullable"),
C.column_name,
C.data_type,
C.is_nullable.eq(sge.convert("YES")).as_("nullable"),
)
.from_(sg.table("columns", db="information_schema", catalog=catalog))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wish we could use DESCRIBE here, but it doesn't contain a nullability column 😞

@cpcloud
Copy link
Member

cpcloud commented Aug 4, 2024

Exasol does indeed allow creating tables in other databases, but there was a bug in our code (we weren't quoting identifiers).

@cpcloud
Copy link
Member

cpcloud commented Aug 4, 2024

The only remaining task is to decide how we want to scope temp tables versus non-temp tables in backends that support them. We can defer that to a follow-up IMO.

@cpcloud cpcloud force-pushed the postgres_search_path branch 2 times, most recently from 56e3ed2 to d6d2405 Compare August 4, 2024 12:53
@cpcloud cpcloud changed the title fix(postgres, duckdb): only return tables in current_database fix(sql): only return tables in current_database Aug 4, 2024
@cpcloud cpcloud added the ci-run-cloud Add this label to trigger a run of Bigquery and Snowflake in CI label Aug 4, 2024
gforsyth and others added 20 commits August 5, 2024 10:24
DuckDB puts temp tables into a catalog named `temp` (not a `database`).
Copy link
Member Author

@gforsyth gforsyth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

@cpcloud cpcloud added the ci-run-cloud Add this label to trigger a run of Bigquery and Snowflake in CI label Aug 5, 2024
@cpcloud cpcloud added this to the 9.3 milestone Aug 5, 2024
@ibis-docs-bot ibis-docs-bot bot removed the ci-run-cloud Add this label to trigger a run of Bigquery and Snowflake in CI label Aug 5, 2024
@cpcloud cpcloud merged commit c7f5717 into ibis-project:main Aug 5, 2024
88 checks passed
@gforsyth gforsyth deleted the postgres_search_path branch August 5, 2024 17:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duckdb The DuckDB backend exasol Issues related to the exasol backend mssql The Microsoft SQL Server backend mysql The MySQL backend postgres The PostgreSQL backend pyspark The Apache PySpark backend trino The Trino backend
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug(postgres): cross-schema column mixing
2 participants