@@ -485,9 +485,11 @@ def create_table(
485485 if schema is not None :
486486 schema = ibis .schema (schema )
487487
488+ in_memory = False
488489 if obj is not None :
489490 if not isinstance (obj , ir .Expr ):
490491 obj = ibis .memtable (obj )
492+ in_memory = True
491493
492494 self ._run_pre_execute_hooks (obj )
493495
@@ -503,43 +505,49 @@ def create_table(
503505 else :
504506 database = "temp"
505507
508+ quoted = self .compiler .quoted
509+ dialect = self .dialect
510+
506511 if overwrite :
507512 created_table = sg .table (
508513 util .gen_name (f"{ self .name } _table" ),
509514 catalog = database ,
510- quoted = self . compiler . quoted ,
515+ quoted = quoted ,
511516 )
512- table = sg .table (name , catalog = database , quoted = self . compiler . quoted )
517+ table = sg .table (name , catalog = database , quoted = quoted )
513518 else :
514- created_table = table = sg .table (
515- name , catalog = database , quoted = self .compiler .quoted
516- )
519+ created_table = table = sg .table (name , catalog = database , quoted = quoted )
517520
518521 create_stmt = self ._generate_create_table (
519522 created_table , schema = (schema or obj .schema ())
520- ).sql (self . name )
523+ ).sql (dialect )
521524
522525 with self .begin () as cur :
523526 cur .execute (create_stmt )
524527
525528 if insert_query is not None :
526529 cur .execute (
527- sge .Insert (this = created_table , expression = insert_query ).sql (
528- self .name
529- )
530+ sge .Insert (this = created_table , expression = insert_query ).sql (dialect )
530531 )
531532
533+ if in_memory :
534+ cur .execute (
535+ sge .Drop (kind = "TABLE" , this = obj .get_name (), exists = True ).sql (
536+ dialect
537+ )
538+ )
539+
532540 if overwrite :
533541 cur .execute (
534- sge .Drop (kind = "TABLE" , this = table , exists = True ).sql (self . name )
542+ sge .Drop (kind = "TABLE" , this = table , exists = True ).sql (dialect )
535543 )
536544 # SQLite's ALTER TABLE statement doesn't support using a
537545 # fully-qualified table reference after RENAME TO. Since we
538546 # never rename between databases, we only need the table name
539547 # here.
540548 quoted_name = _quote (name )
541549 cur .execute (
542- f"ALTER TABLE { created_table .sql (self . name )} RENAME TO { quoted_name } "
550+ f"ALTER TABLE { created_table .sql (dialect )} RENAME TO { quoted_name } "
543551 )
544552
545553 if schema is None :
0 commit comments