Repository: pingcap/tidb
File: pkg/ddl/create_table.go (Go)

Code before:
		replica := *tblInfo.TiFlashReplica
		// Keep the tiflash replica setting, remove the replica available status.
		replica.AvailablePartitionIDs = nil
		replica.Available = false
		tblInfo.TiFlashReplica = &replica
	}
	if referTblInfo.Partition != nil {
		pi := *referTblInfo.Partition
		pi.Definitions = make([]model.PartitionDefinition, len(referTblInfo.Partition.Definitions))
		copy(pi.Definitions, referTblInfo.Partition.Definitions)
		tblInfo.Partition = &pi
	}

	// for issue #64948, temporary table does not support TLL, we should remove it
	if s.TemporaryKeyword != ast.TemporaryNone {
		tblInfo.TTLInfo = nil
	} else if tblInfo.TTLInfo != nil {
		tblInfo.TTLInfo = referTblInfo.TTLInfo.Clone()
	}

	if s.TemporaryKeyword != ast.TemporaryNone {
		// temporary table does not support affinity, we should remove it
		tblInfo.Affinity = nil
	} else if referTblInfo.Affinity != nil {
		tblInfo.Affinity = referTblInfo.Affinity.Clone()
	}

	renameCheckConstraint(&tblInfo)
	return &tblInfo, nil
}

func renameCheckConstraint(tblInfo *model.TableInfo) {
	for _, cons := range tblInfo.Constraints {
		cons.Name = ast.NewCIStr("")
		cons.Table = tblInfo.Name
	}
	setNameForConstraintInfo(tblInfo.Name.L, map[string]bool{}, tblInfo.Constraints)
}

// BuildTableInfo creates a TableInfo.
func BuildTableInfo(
	ctx *metabuild.Context,
	tableName ast.CIStr,
	cols []*table.Column,
	constraints []*ast.Constraint,
	charset string,
	collate string,
) (tbInfo *model.TableInfo, err error) {
	tbInfo = &model.TableInfo{
		Name:    tableName,
		Version: model.CurrLatestTableInfoVersion,

Review comment (suggestion):
IMO, it'd be better to validate after it builds the table. I'd suggest to add the check in `(*DDLExec).createSessionTemporaryTable`: `if tblInfo.TTLInfo != nil {return dbterror.ErrTempTableNotAllowedWithTTL}`.

Because for other cases, `create table like` and `create global temporary table like ON COMMIT DELETE ROWS;`, they are also validated after `BuildTableInfoWithLike`.

Code after:
	tblInfo.Columns = newColumns
	tblInfo.Indices = newIndices
	tblInfo.Name = ident.Name
	tblInfo.AutoIncID = 0
	tblInfo.ForeignKeys = nil
	tblInfo.TableCacheStatusType = model.TableCacheStatusDisable
	// Ignore TiFlash replicas for temporary tables.
	if s.TemporaryKeyword != ast.TemporaryNone {
		tblInfo.TiFlashReplica = nil
	} else if tblInfo.TiFlashReplica != nil {
		replica := *tblInfo.TiFlashReplica
		// Keep the tiflash replica setting, remove the replica available status.
		replica.AvailablePartitionIDs = nil
		replica.Available = false
		tblInfo.TiFlashReplica = &replica
	}
	if referTblInfo.Partition != nil {
		pi := *referTblInfo.Partition
		pi.Definitions = make([]model.PartitionDefinition, len(referTblInfo.Partition.Definitions))
		copy(pi.Definitions, referTblInfo.Partition.Definitions)
		tblInfo.Partition = &pi
	}

	// for issue #64948, temporary table does not support TLL, we should remove it
	if s.TemporaryKeyword != ast.TemporaryNone {
		tblInfo.TTLInfo = nil
	} else if referTblInfo.TTLInfo != nil {
		tblInfo.TTLInfo = referTblInfo.TTLInfo.Clone()
	}

	if s.TemporaryKeyword != ast.TemporaryNone {
		// temporary table does not support affinity, we should remove it
		tblInfo.Affinity = nil
	} else if referTblInfo.Affinity != nil {
		tblInfo.Affinity = referTblInfo.Affinity.Clone()
	}

	renameCheckConstraint(&tblInfo)
	return &tblInfo, nil
}

func renameCheckConstraint(tblInfo *model.TableInfo) {
	for _, cons := range tblInfo.Constraints {
		cons.Name = ast.NewCIStr("")
		cons.Table = tblInfo.Name
	}
	setNameForConstraintInfo(tblInfo.Name.L, map[string]bool{}, tblInfo.Constraints)
}

// BuildTableInfo creates a TableInfo.
func BuildTableInfo(