19
19
DropTable ,
20
20
InsertSelect ,
21
21
)
22
+ from ibis .backends .flink .utils import ibis_schema_to_flink_schema
22
23
23
24
if TYPE_CHECKING :
24
25
from collections .abc import Mapping
@@ -145,6 +146,34 @@ def list_tables(
145
146
# but executing the SQL string directly yields a `TableResult` object
146
147
return self ._filter_with_like (tables , like )
147
148
149
+ def list_views (
150
+ self ,
151
+ like : str | None = None ,
152
+ temporary : bool = True ,
153
+ ) -> list [str ]:
154
+ """Return the list of view names.
155
+
156
+ Return the list of view names in the specified database and catalog.
157
+ or the default one if no database/catalog is specified.
158
+
159
+ Parameters
160
+ ----------
161
+ like : str, optional
162
+ A pattern in Python's regex format.
163
+ temporary : bool, optional
164
+ Whether to list temporary views or permanent views.
165
+
166
+ Returns
167
+ -------
168
+ list[str]
169
+ The list of the view names that match the pattern `like`.
170
+ """
171
+ if temporary :
172
+ views = self ._table_env .list_temporary_views ()
173
+ else :
174
+ views = self ._table_env .list_views ()
175
+ return self ._filter_with_like (views , like )
176
+
148
177
def _fully_qualified_name (
149
178
self ,
150
179
name : str ,
@@ -319,26 +348,29 @@ def create_table(
319
348
if obj is None and schema is None :
320
349
raise exc .IbisError ("The schema or obj parameter is required" )
321
350
322
- if overwrite :
323
- self .drop_table (name = name , catalog = catalog , database = database , force = True )
351
+ # in-memory data is created as views in `pyflink`
352
+ elif obj is not None :
353
+ if isinstance (obj , pa .Table ):
354
+ obj = obj .to_pandas ()
355
+ if isinstance (obj , pd .DataFrame ):
356
+ table = self ._table_env .from_pandas (
357
+ obj , ibis_schema_to_flink_schema (schema )
358
+ )
359
+ if isinstance (obj , ir .Table ):
360
+ table = obj
361
+ return self .create_view (name , table , database = database , overwrite = overwrite )
324
362
325
- if isinstance (obj , pa .Table ):
326
- obj = obj .to_pandas ()
327
- if isinstance (obj , pd .DataFrame ):
328
- qualified_name = self ._fully_qualified_name (name , database , catalog )
329
- table = self ._table_env .from_pandas (obj )
330
- # in-memory data is created as views in `pyflink`
331
- # TODO(chloeh13q): alternatively, we can do CREATE TABLE and then INSERT
332
- # INTO ... VALUES to keep things consistent
333
- self ._table_env .create_temporary_view (qualified_name , table )
334
- if isinstance (obj , ir .Table ):
335
- # TODO(chloeh13q): implement CREATE TABLE for expressions
336
- raise NotImplementedError
337
- if schema is not None :
363
+ # external data is created as tables in `pyflink`
364
+ else :
338
365
if not tbl_properties :
339
366
raise exc .IbisError (
340
367
"tbl_properties is required when creating table with schema"
341
368
)
369
+ if overwrite :
370
+ self .drop_table (
371
+ name = name , catalog = catalog , database = database , force = True
372
+ )
373
+
342
374
statement = CreateTableFromConnector (
343
375
table_name = name ,
344
376
schema = schema ,
@@ -349,8 +381,7 @@ def create_table(
349
381
catalog = catalog ,
350
382
)
351
383
self ._exec_sql (statement .compile ())
352
-
353
- return self .table (name , database = database )
384
+ return self .table (name , database = database )
354
385
355
386
def drop_table (
356
387
self ,
@@ -391,6 +422,7 @@ def create_view(
391
422
obj : ir .Table ,
392
423
* ,
393
424
database : str | None = None ,
425
+ catalog : str | None = None ,
394
426
overwrite : bool = False ,
395
427
) -> ir .Table :
396
428
"""Create a new view from an expression.
@@ -404,6 +436,8 @@ def create_view(
404
436
database
405
437
Name of the database where the view will be created, if not
406
438
provided the database's default is used.
439
+ catalog
440
+ Name of the catalog where the table exists, if not the default.
407
441
overwrite
408
442
Whether to clobber an existing view with the same name.
409
443
@@ -412,7 +446,15 @@ def create_view(
412
446
Table
413
447
The view that was created.
414
448
"""
415
- raise NotImplementedError
449
+ if obj is None :
450
+ raise exc .IbisError ("The obj parameter is required" )
451
+
452
+ if overwrite and self .list_views (name ):
453
+ self .drop_view (name = name , catalog = catalog , database = database , force = True )
454
+
455
+ qualified_name = self ._fully_qualified_name (name , database , catalog )
456
+ self ._table_env .create_temporary_view (qualified_name , obj )
457
+ return self .table (name , database = database )
416
458
417
459
def drop_view (
418
460
self ,
0 commit comments