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

feat: list materialized views #5962

Closed
1 task done
dpprdan opened this issue Apr 11, 2023 · 6 comments
Closed
1 task done

feat: list materialized views #5962

dpprdan opened this issue Apr 11, 2023 · 6 comments
Labels
feature Features or general enhancements

Comments

@dpprdan
Copy link

dpprdan commented Apr 11, 2023

Is your feature request related to a problem?

ibis does not list_tables() materialized views in Postgres.

PS C:\> psql service=test
psql (15.2)
Type "help" for help.

postgres=# CREATE MATERIALIZED VIEW mymatview AS SELECT 1 AS col1;
SELECT 1
postgres=# \d
                 List of relations
 Schema |   Name    |       Type        |  Owner
--------+-----------+-------------------+----------
 public | mymatview | materialized view | postgres
(1 row)

postgres=# exit
import ibis
ibis.options.interactive = True

pg_url = "postgresql+psycopg:///?service=test"
con = ibis.postgres.connect(url=pg_url)

con.list_tables()
C:\ProgramData\Miniconda3\envs\ibis_test\lib\site-packages\ibis\backends\postgres\registry.py:119: UserWarning: locale specific date formats (%c, %x, %X) are not yet implemented for Windows
  warnings.warn(

[]

Describe the solution you'd like

IIUC (and that’s a big IF as I’ve been trying out Ibis only for a few days now) is using these SQLAlchemy inspector functions.

def list_tables(self, like=None, database=None):
tables = self.inspector.get_table_names(schema=database)
views = self.inspector.get_view_names(schema=database)
return self._filter_with_like(tables + views, like)

Fortunately, SQLAlchemy has gained a get_materialized_view_names() function in version 2.0.

import sqlalchemy as sa
pg_eng = sa.create_engine(pg_url)
pg_con = pg_eng.connect()
inspector = sa.inspect(pg_con)
inspector.get_materialized_view_names()
['mymatview']

So a fix might possibly be as easy as (plus a SQLAlchemy version check)

def list_tables(self, like=None, database=None):
    tables = self.inspector.get_table_names(schema=database)
    views = self.inspector.get_view_names(schema=database)
    matviews = self.inspector.get_materialized_view_names(schema=database)
    return self._filter_with_like(tables + views + matviews, like)

get_materialized_view_names() seems to behave if an engine does not support materialized views (yet).

ddb_eng = sa.create_engine("duckdb:///palmer_penguins.ddb")
ddb_con = ddb_eng.connect()

ddb_inspector = sa.inspect(ddb_con)
ddb_inspector.get_materialized_view_names()
[]

Ideally Ibis would support materialized views even more, of course, e.g. with

Accessing materialized views does not seem to be a problem.

con.table("mymatview")
┏━━━━━━━┓
┃ col1  ┃
┡━━━━━━━┩
│ int32 │
├───────┤
│     1 │
└───────┘

What version of ibis are you running?

5.0.0

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

PostgreSQL (DuckDB as a does-not-support-matviews backend)

Code of Conduct

  • I agree to follow this project's Code of Conduct
@dpprdan dpprdan added the feature Features or general enhancements label Apr 11, 2023
@dpprdan
Copy link
Author

dpprdan commented Apr 11, 2023

Relatedly, I wonder whether list_tables() ought to list temporary tables and views as well (get_temp_table_names(), get_temp_view_names())

@cpcloud
Copy link
Member

cpcloud commented Apr 11, 2023

Hi @dpprdan 👋🏻! Thanks for creating an issue about this.

Listing materialized views in list_tables is definitely something we can look into!

As far as DDL for create/refresh/drop, we'll need to get a list of the backends we support that support materialized views and take a look at their APIs and decide what options to support.

Here are the backend we support in ibis that support materialized views along with links to their documentation:

  1. PostgreSQL
  2. BigQuery
  3. Snowflake
  4. ClickHouse
  5. Trino
  6. MSSQL ⚠️ Only in Azure Synapse

@cpcloud
Copy link
Member

cpcloud commented Apr 11, 2023

Relatedly, I wonder whether list_tables() ought to list temporary tables and views as well (get_temp_table_names(), get_temp_view_names())

Those inspector methods appear to be SQLAlchemy 2.0 only, and ibis supports SQLAlchemy<2 so we'd need to figure out a way to get that information without relying on 2.0-only APIs.

@dpprdan
Copy link
Author

dpprdan commented Apr 11, 2023

Those inspector methods appear to be SQLAlchemy 2.0 only, and ibis supports SQLAlchemy<2 so we'd need to figure out a way to get that information without relying on 2.0-only APIs.

AFAICT it is the other way around?

I see "New in version 1.0.0." for get_temp_table_names(), get_temp_view_names() and "New in version 2.0." for get_materialized_view_names().

Should we split the matview-DDL issue from this "list matviews" issue?

@cpcloud
Copy link
Member

cpcloud commented Apr 11, 2023

Those inspector methods appear to be SQLAlchemy 2.0 only, and ibis supports SQLAlchemy<2 so we'd need to figure out a way to get that information without relying on 2.0-only APIs.

AFAICT it is the other way around?

I see "New in version 1.0.0." for get_temp_table_names(), get_temp_view_names() and "New in version 2.0." for get_materialized_view_names().

Ah, yep, you're right!

Should we split the matview-DDL issue from this "list matviews" issue?

Yeah, definitely.

@cpcloud cpcloud added this to the 6.1 milestone Jul 5, 2023
@cpcloud cpcloud modified the milestones: 6.1, 7.0 Aug 1, 2023
@cpcloud cpcloud removed this from the 7.0 milestone Sep 14, 2023
@cpcloud
Copy link
Member

cpcloud commented Jul 24, 2024

Closing in favor of #8382. Thanks for the discussion!

@cpcloud cpcloud closed this as not planned Won't fix, can't repro, duplicate, stale Jul 24, 2024
@github-project-automation github-project-automation bot moved this from backlog to done in Ibis planning and roadmap Jul 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Features or general enhancements
Projects
Archived in project
Development

No branches or pull requests

2 participants