Skip to content

Commit f5a5bce

Browse files
authored
implement drop temporary table ... and display temporary in show create table statements (#2824)
=
1 parent 0e32f1d commit f5a5bce

File tree

5 files changed

+45
-10
lines changed

5 files changed

+45
-10
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/dolthub/go-icu-regex v0.0.0-20241215010122-db690dd53c90
77
github.com/dolthub/jsonpath v0.0.2-0.20240227200619-19675ab05c71
88
github.com/dolthub/sqllogictest/go v0.0.0-20201107003712-816f3ae12d81
9-
github.com/dolthub/vitess v0.0.0-20250115003116-d6f17c220028
9+
github.com/dolthub/vitess v0.0.0-20250123002143-3b45b8cacbfa
1010
github.com/go-kit/kit v0.10.0
1111
github.com/go-sql-driver/mysql v1.7.2-0.20231213112541-0004702b931d
1212
github.com/gocraft/dbr/v2 v2.7.2

go.sum

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,8 @@ github.com/dolthub/jsonpath v0.0.2-0.20240227200619-19675ab05c71 h1:bMGS25NWAGTE
5858
github.com/dolthub/jsonpath v0.0.2-0.20240227200619-19675ab05c71/go.mod h1:2/2zjLQ/JOOSbbSboojeg+cAwcRV0fDLzIiWch/lhqI=
5959
github.com/dolthub/sqllogictest/go v0.0.0-20201107003712-816f3ae12d81 h1:7/v8q9XGFa6q5Ap4Z/OhNkAMBaK5YeuEzwJt+NZdhiE=
6060
github.com/dolthub/sqllogictest/go v0.0.0-20201107003712-816f3ae12d81/go.mod h1:siLfyv2c92W1eN/R4QqG/+RjjX5W2+gCTRjZxBjI3TY=
61-
github.com/dolthub/vitess v0.0.0-20241220202600-b18f18d0cde7 h1:w130WLeARGGNYWmhGPugsHXzJEelKKimt3kTWg6/Puk=
62-
github.com/dolthub/vitess v0.0.0-20241220202600-b18f18d0cde7/go.mod h1:1gQZs/byeHLMSul3Lvl3MzioMtOW1je79QYGyi2fd70=
63-
github.com/dolthub/vitess v0.0.0-20241231200706-18992bb25fdc h1:3FuwEDwyue/JuHdnwGSbQhE9xKAFM+k1y3uXi58h7Gk=
64-
github.com/dolthub/vitess v0.0.0-20241231200706-18992bb25fdc/go.mod h1:1gQZs/byeHLMSul3Lvl3MzioMtOW1je79QYGyi2fd70=
65-
github.com/dolthub/vitess v0.0.0-20250115003116-d6f17c220028 h1:lgcIsCUaNDde+lS+aYGYGML95qrQlBMEpaXFN1Pmk+4=
66-
github.com/dolthub/vitess v0.0.0-20250115003116-d6f17c220028/go.mod h1:1gQZs/byeHLMSul3Lvl3MzioMtOW1je79QYGyi2fd70=
61+
github.com/dolthub/vitess v0.0.0-20250123002143-3b45b8cacbfa h1:kyoPzxViSXAyqfO0Mab7Qo1UogFIrxZKKyBU6kBOl+E=
62+
github.com/dolthub/vitess v0.0.0-20250123002143-3b45b8cacbfa/go.mod h1:1gQZs/byeHLMSul3Lvl3MzioMtOW1je79QYGyi2fd70=
6763
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
6864
github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs=
6965
github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU=

sql/planbuilder/ddl.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,13 @@ func (b *Builder) buildDropTable(inScope *scope, c *ast.DDL) (outScope *scope) {
253253

254254
tableScope, ok := b.buildResolvedTableForTablename(inScope, t, nil)
255255
if ok {
256+
// attempting to drop a non-temporary table with DROP TEMPORARY, results in Unknown table
257+
if tbl, ok := tableScope.node.(sql.Table); ok {
258+
if tmpTbl := getTempTable(tbl); tmpTbl != nil && !tmpTbl.IsTemporary() && c.Temporary {
259+
err := sql.ErrUnknownTable.New(tableName)
260+
b.handleErr(err)
261+
}
262+
}
256263
dropTables = append(dropTables, tableScope.node)
257264
} else if !c.IfExists {
258265
err := sql.ErrTableNotFound.New(tableName)
@@ -264,6 +271,19 @@ func (b *Builder) buildDropTable(inScope *scope, c *ast.DDL) (outScope *scope) {
264271
return
265272
}
266273

274+
func getTempTable(t sql.Table) sql.TemporaryTable {
275+
switch t := t.(type) {
276+
case sql.TemporaryTable:
277+
return t
278+
case sql.TableWrapper:
279+
return getTempTable(t.Underlying())
280+
case *plan.ResolvedTable:
281+
return getTempTable(t.Table)
282+
default:
283+
return nil
284+
}
285+
}
286+
267287
func (b *Builder) buildTruncateTable(inScope *scope, c *ast.DDL) (outScope *scope) {
268288
outScope = inScope.push()
269289
tableScope, ok := b.buildResolvedTableForTablename(inScope, c.Table, nil)

sql/rowexec/show_iters.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,12 @@ func (i *showCreateTablesIter) produceCreateTableStatement(ctx *sql.Context, tab
498498
}
499499
}
500500

501-
return sql.GenerateCreateTableStatement(table.Name(), colStmts, autoInc, table.Collation().CharacterSet().Name(), table.Collation().Name(), comment), nil
501+
temp := ""
502+
if tbl := getTempTable(table); tbl != nil && tbl.IsTemporary() {
503+
temp = " TEMPORARY"
504+
}
505+
506+
return sql.GenerateCreateTableStatement(table.Name(), colStmts, temp, autoInc, table.Collation().CharacterSet().Name(), table.Collation().Name(), comment), nil
502507
}
503508

504509
func produceCreateViewStatement(view *plan.SubqueryAlias) string {
@@ -539,6 +544,19 @@ func getAutoIncrementTable(t sql.Table) sql.AutoIncrementTable {
539544
}
540545
}
541546

547+
func getTempTable(t sql.Table) sql.TemporaryTable {
548+
switch t := t.(type) {
549+
case sql.TemporaryTable:
550+
return t
551+
case sql.TableWrapper:
552+
return getTempTable(t.Underlying())
553+
case *plan.ResolvedTable:
554+
return getTempTable(t.Table)
555+
default:
556+
return nil
557+
}
558+
}
559+
542560
func formatReplicaStatusTimestamp(t *time.Time) string {
543561
if t == nil {
544562
return ""

sql/sqlfmt.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626

2727
// GenerateCreateTableStatement returns 'CREATE TABLE' statement with given table names
2828
// and column definition statements in order and the collation and character set names for the table
29-
func GenerateCreateTableStatement(tblName string, colStmts []string, autoInc, tblCharsetName, tblCollName, comment string) string {
29+
func GenerateCreateTableStatement(tblName string, colStmts []string, temp, autoInc, tblCharsetName, tblCollName, comment string) string {
3030
if comment != "" {
3131
// Escape any single quotes in the comment and add the COMMENT keyword
3232
comment = strings.ReplaceAll(comment, "'", "''")
@@ -38,7 +38,8 @@ func GenerateCreateTableStatement(tblName string, colStmts []string, autoInc, tb
3838
}
3939

4040
return fmt.Sprintf(
41-
"CREATE TABLE %s (\n%s\n) ENGINE=InnoDB%s DEFAULT CHARSET=%s COLLATE=%s%s",
41+
"CREATE%s TABLE %s (\n%s\n) ENGINE=InnoDB%s DEFAULT CHARSET=%s COLLATE=%s%s",
42+
temp,
4243
QuoteIdentifier(tblName),
4344
strings.Join(colStmts, ",\n"),
4445
autoInc,

0 commit comments

Comments
 (0)