@@ -88,17 +88,15 @@ def __exit__(self, exc_type, exc_value, traceback):
88
88
89
89
class PySparkTableSetFormatter (TableSetFormatter ):
90
90
def _format_in_memory_table (self , op ):
91
- names = op .schema .names
92
- rows = ", " .join (
93
- f"({ ', ' .join (map (repr , row ))} )"
94
- for row in op .data .itertuples (index = False )
95
- )
96
- signature = ", " .join (map (self ._quote_identifier , names ))
97
- name = self ._quote_identifier (op .name or "_" )
98
- return f"(VALUES { rows } AS { name } ({ signature } ))"
91
+ # we don't need to compile the table to a VALUES statement because the
92
+ # table has been registered already by createOrReplaceTempView.
93
+ #
94
+ # The only place where the SQL API is currently used is DDL operations
95
+ return op .name
99
96
100
97
101
98
class PySparkCompiler (Compiler ):
99
+ cheap_in_memory_tables = True
102
100
table_set_formatter_class = PySparkTableSetFormatter
103
101
104
102
@@ -463,6 +461,8 @@ def create_table(
463
461
table_name , format = format , mode = mode
464
462
)
465
463
return
464
+ else :
465
+ self ._register_in_memory_tables (obj )
466
466
467
467
ast = self .compiler .to_ast (obj )
468
468
select = ast .queries [0 ]
@@ -487,6 +487,10 @@ def create_table(
487
487
488
488
return self .raw_sql (statement .compile ())
489
489
490
+ def _register_in_memory_table (self , table_op ):
491
+ spark_df = self .compile (table_op .to_expr ())
492
+ spark_df .createOrReplaceTempView (table_op .name )
493
+
490
494
def create_view (
491
495
self ,
492
496
name : str ,
0 commit comments