-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
*: support create a local temporary table #25851
Changes from 7 commits
33cac83
4b72c32
b01934e
696cabc
f4c5f38
af15824
9101528
815bc26
05113ff
a2e4a4b
a4e7929
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,9 +27,12 @@ import ( | |
"github.com/pingcap/tidb/ddl" | ||
"github.com/pingcap/tidb/domain" | ||
"github.com/pingcap/tidb/infoschema" | ||
"github.com/pingcap/tidb/kv" | ||
"github.com/pingcap/tidb/meta" | ||
"github.com/pingcap/tidb/meta/autoid" | ||
"github.com/pingcap/tidb/planner/core" | ||
"github.com/pingcap/tidb/sessionctx/variable" | ||
"github.com/pingcap/tidb/table/tables" | ||
"github.com/pingcap/tidb/util/admin" | ||
"github.com/pingcap/tidb/util/chunk" | ||
"github.com/pingcap/tidb/util/gcutil" | ||
|
@@ -200,10 +203,59 @@ func (e *DDLExec) executeAlterDatabase(s *ast.AlterDatabaseStmt) error { | |
} | ||
|
||
func (e *DDLExec) executeCreateTable(s *ast.CreateTableStmt) error { | ||
if s.TemporaryKeyword == ast.TemporaryLocal { | ||
return e.createSessionTemporaryTable(s) | ||
} | ||
|
||
err := domain.GetDomain(e.ctx).DDL().CreateTable(e.ctx, s) | ||
return err | ||
} | ||
|
||
func (e *DDLExec) createSessionTemporaryTable(s *ast.CreateTableStmt) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it better to modify the method There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Local temporary table is not a normal table, it doesn't need to do the DDL schema change and store table info. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I checked the function ddl.CreateTable, ddl.CreateTableWithInfo, most codes can be reused here. We can just return if local temporary if founded before creating table in tikv. Current modification is easy to lost some checks. For example, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check the table options will be done in some other PR. |
||
is := e.ctx.GetInfoSchema().(infoschema.InfoSchema) | ||
dbInfo, ok := is.SchemaByName(s.Table.Schema) | ||
if !ok { | ||
return infoschema.ErrDatabaseNotExists.GenWithStackByArgs(s.Table.Schema.O) | ||
} | ||
tbInfo, err := ddl.BuildTableInfoWithCheck(e.ctx, s, dbInfo.Charset, dbInfo.Collate) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does BuildTableInfoWithCheck support There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure, |
||
if err != nil { | ||
return err | ||
} | ||
|
||
dom := domain.GetDomain(e.ctx) | ||
// Local temporary table uses a real table ID. | ||
// We could mock a table ID, but the mocked ID might be identical to an existing | ||
// real table, and then we'll get into trouble. | ||
err = kv.RunInNewTxn(context.Background(), dom.Store(), true, func(ctx context.Context, txn kv.Transaction) error { | ||
m := meta.NewMeta(txn) | ||
tblID, err := m.GenGlobalID() | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
tbInfo.ID = tblID | ||
tbInfo.State = model.StatePublic | ||
return nil | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// AutoID is allocated in mocked.. | ||
alloc := autoid.NewAllocatorFromTempTblInfo(tbInfo) | ||
tbl, err := tables.TableFromMeta([]autoid.Allocator{alloc}, tbInfo) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Store this temporary table to the session. | ||
sessVars := e.ctx.GetSessionVars() | ||
if sessVars.LocalTemporaryTables == nil { | ||
sessVars.LocalTemporaryTables = infoschema.NewLocalTemporaryTables() | ||
} | ||
localTempTables := sessVars.LocalTemporaryTables.(*infoschema.LocalTemporaryTables) | ||
return localTempTables.AddTable(dbInfo, tbl) | ||
} | ||
|
||
func (e *DDLExec) executeCreateView(s *ast.CreateViewStmt) error { | ||
err := domain.GetDomain(e.ctx).DDL().CreateView(e.ctx, s) | ||
return err | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why we still need to set tidb_enable_noop_functions=1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't want to touch more places than necessary. For example, in this pr I just want to add the create local temporary table.
If a remove this switch, I need to update some of the tests, I prefer to do it in another PR.