Merge pull request #8372 from dolthub/fulghum/doltgresql-725

Passing through schema name to fix index creation bug in Doltgres
This commit is contained in:
Jason Fulghum
2024-09-23 11:36:32 -07:00
committed by GitHub
10 changed files with 44 additions and 29 deletions

View File

@@ -428,6 +428,7 @@ func CombineColCollections(ctx context.Context, root doltdb.RootValue, inferredC
return nil, verr
}
// NOTE: This code is only used in the import codepath for Dolt, so we don't use a schema to qualify the table name
newCols, err := doltdb.GenerateTagsForNewColColl(ctx, root, impOpts.tableName, newCols)
if err != nil {
return nil, errhand.BuildDError("failed to generate new schema").AddCause(err).Build()

View File

@@ -328,6 +328,9 @@ func (root *rootValue) HasTable(ctx context.Context, tName TableName) (bool, err
return !a.IsEmpty(), nil
}
// GenerateTagsForNewColColl creates a new ColCollection for the specified |tableName|. Note that this function is only
// intended to be used from Dolt code, and does not support qualifying a table with a schema name, so it will not work
// correctly for Doltgres.
func GenerateTagsForNewColColl(ctx context.Context, root RootValue, tableName string, cc *schema.ColCollection) (*schema.ColCollection, error) {
newColNames := make([]string, 0, cc.Size())
newColKinds := make([]types.NomsKind, 0, cc.Size())
@@ -337,7 +340,7 @@ func GenerateTagsForNewColColl(ctx context.Context, root RootValue, tableName st
return false, nil
})
newTags, err := GenerateTagsForNewColumns(ctx, root, tableName, newColNames, newColKinds, nil)
newTags, err := GenerateTagsForNewColumns(ctx, root, TableName{Name: tableName}, newColNames, newColKinds, nil)
if err != nil {
return nil, err
}
@@ -355,7 +358,7 @@ func GenerateTagsForNewColColl(ctx context.Context, root RootValue, tableName st
func GenerateTagsForNewColumns(
ctx context.Context,
root RootValue,
tableName string,
tableName TableName,
newColNames []string,
newColKinds []types.NomsKind,
headRoot RootValue,
@@ -405,9 +408,9 @@ func GenerateTagsForNewColumns(
continue
}
outputTags[i] = schema.AutoGenerateTag(existingTags, tableName, existingColKinds, newColNames[i], newColKinds[i])
outputTags[i] = schema.AutoGenerateTag(existingTags, tableName.Name, existingColKinds, newColNames[i], newColKinds[i])
existingColKinds = append(existingColKinds, newColKinds[i])
existingTags.Add(outputTags[i], tableName)
existingTags.Add(outputTags[i], tableName.Name)
}
return outputTags, nil
@@ -416,13 +419,13 @@ func GenerateTagsForNewColumns(
func GetExistingColumns(
ctx context.Context,
root, headRoot RootValue,
tableName string,
tableName TableName,
newColNames []string,
newColKinds []types.NomsKind,
) ([]schema.Column, error) {
var existingCols []schema.Column
tbl, found, err := root.GetTable(ctx, TableName{Name: tableName})
tbl, found, err := root.GetTable(ctx, tableName)
if err != nil {
return nil, err
}
@@ -437,7 +440,7 @@ func GetExistingColumns(
return false, nil
})
} else if headRoot != nil {
tbl, found, err := headRoot.GetTable(ctx, TableName{Name: tableName})
tbl, found, err := headRoot.GetTable(ctx, tableName)
if err != nil {
return nil, err
}

View File

@@ -158,6 +158,7 @@ func InferSchema(ctx context.Context, root doltdb.RootValue, rd table.ReadCloser
}
}
// NOTE: This code is only used in the import codepath for Dolt, so we don't use a schema to qualify the table name
newCols, err = doltdb.GenerateTagsForNewColColl(ctx, root, tableName, newCols)
if err != nil {
return nil, errhand.BuildDError("failed to generate new schema").AddCause(err).Build()

View File

@@ -88,7 +88,9 @@ func NewSqlEngineReader(ctx context.Context, dEnv *env.DoltEnv, tableName string
return nil, err
}
doltSchema, err := sqlutil.ToDoltSchema(ctx, root, tableName, create.PrimaryKeySchema, nil, sql.Collation_Default)
// NOTE: We don't support setting a schema name to qualify the table name here, so this code will not work
// correctly with Doltgres yet.
doltSchema, err := sqlutil.ToDoltSchema(ctx, root, doltdb.TableName{Name: tableName}, create.PrimaryKeySchema, nil, sql.Collation_Default)
if err != nil {
return nil, err
}
@@ -109,7 +111,9 @@ func NewSqlEngineTableReaderWithEngine(sqlCtx *sql.Context, se *sqle.Engine, db
return nil, err
}
doltSchema, err := sqlutil.ToDoltSchema(sqlCtx, root, tableName, sql.NewPrimaryKeySchema(sch), nil, sql.Collation_Default)
// NOTE: We don't support setting a schema name to qualify the table name here, so this code will not work
// correctly with Doltgres yet.
doltSchema, err := sqlutil.ToDoltSchema(sqlCtx, root, doltdb.TableName{Name: tableName}, sql.NewPrimaryKeySchema(sch), nil, sql.Collation_Default)
if err != nil {
return nil, err
}

View File

@@ -1181,7 +1181,7 @@ OuterLoop:
}
// createSqlTable is the private version of CreateTable. It doesn't enforce any table name checks.
func (db Database) createSqlTable(ctx *sql.Context, tableName string, schemaName string, sch sql.PrimaryKeySchema, collation sql.CollationID, comment string) error {
func (db Database) createSqlTable(ctx *sql.Context, table string, schemaName string, sch sql.PrimaryKeySchema, collation sql.CollationID, comment string) error {
ws, err := db.GetWorkingSet(ctx)
if err != nil {
return err
@@ -1196,10 +1196,11 @@ func (db Database) createSqlTable(ctx *sql.Context, tableName string, schemaName
db.schemaName = schemaName
}
if exists, err := root.HasTable(ctx, doltdb.TableName{Name: tableName, Schema: schemaName}); err != nil {
tableName := doltdb.TableName{Name: table, Schema: schemaName}
if exists, err := root.HasTable(ctx, tableName); err != nil {
return err
} else if exists {
return sql.ErrTableAlreadyExists.New(tableName)
return sql.ErrTableAlreadyExists.New(table)
}
headRoot, err := db.GetHeadRoot(ctx)
@@ -1215,7 +1216,7 @@ func (db Database) createSqlTable(ctx *sql.Context, tableName string, schemaName
// Prevent any tables that use Spatial Types as Primary Key from being created
if schema.IsUsingSpatialColAsKey(doltSch) {
return schema.ErrUsingSpatialKey.New(tableName)
return schema.ErrUsingSpatialKey.New(tableName.Name)
}
// Prevent any tables that use BINARY, CHAR, VARBINARY, VARCHAR prefixes
@@ -1225,14 +1226,14 @@ func (db Database) createSqlTable(ctx *sql.Context, tableName string, schemaName
if err != nil {
return err
}
ait.AddNewTable(tableName)
ait.AddNewTable(tableName.Name)
}
return db.createDoltTable(ctx, tableName, schemaName, root, doltSch)
return db.createDoltTable(ctx, tableName.Name, tableName.Schema, root, doltSch)
}
// createIndexedSqlTable is the private version of createSqlTable. It doesn't enforce any table name checks.
func (db Database) createIndexedSqlTable(ctx *sql.Context, tableName string, schemaName string, sch sql.PrimaryKeySchema, idxDef sql.IndexDef, collation sql.CollationID) error {
func (db Database) createIndexedSqlTable(ctx *sql.Context, table string, schemaName string, sch sql.PrimaryKeySchema, idxDef sql.IndexDef, collation sql.CollationID) error {
ws, err := db.GetWorkingSet(ctx)
if err != nil {
return err
@@ -1247,10 +1248,11 @@ func (db Database) createIndexedSqlTable(ctx *sql.Context, tableName string, sch
db.schemaName = schemaName
}
if exists, err := root.HasTable(ctx, doltdb.TableName{Name: tableName, Schema: schemaName}); err != nil {
tableName := doltdb.TableName{Name: table, Schema: schemaName}
if exists, err := root.HasTable(ctx, tableName); err != nil {
return err
} else if exists {
return sql.ErrTableAlreadyExists.New(tableName)
return sql.ErrTableAlreadyExists.New(tableName.Name)
}
headRoot, err := db.GetHeadRoot(ctx)
@@ -1265,7 +1267,7 @@ func (db Database) createIndexedSqlTable(ctx *sql.Context, tableName string, sch
// Prevent any tables that use Spatial Types as Primary Key from being created
if schema.IsUsingSpatialColAsKey(doltSch) {
return schema.ErrUsingSpatialKey.New(tableName)
return schema.ErrUsingSpatialKey.New(tableName.Name)
}
// Prevent any tables that use BINARY, CHAR, VARBINARY, VARCHAR prefixes in Primary Key
@@ -1281,10 +1283,10 @@ func (db Database) createIndexedSqlTable(ctx *sql.Context, tableName string, sch
if err != nil {
return err
}
ait.AddNewTable(tableName)
ait.AddNewTable(tableName.Name)
}
return db.createDoltTable(ctx, tableName, schemaName, root, doltSch)
return db.createDoltTable(ctx, tableName.Name, tableName.Schema, root, doltSch)
}
// createDoltTable creates a table on the database using the given dolt schema while not enforcing table baseName checks.

View File

@@ -266,7 +266,7 @@ func newRowConverterForDoltTable(ctx *sql.Context, t *DoltTable) (func(ctx *sql.
return nil, fmt.Errorf("unable to get roots for database '%s'", t.db.Name())
}
doltSchema, err := sqlutil.ToDoltSchema(ctx, roots.Working, t.Name(), t.sqlSch, roots.Head, t.Collation())
doltSchema, err := sqlutil.ToDoltSchema(ctx, roots.Working, t.TableName(), t.sqlSch, roots.Head, t.Collation())
if err != nil {
return nil, err
}

View File

@@ -77,7 +77,7 @@ func FromDoltSchema(dbName, tableName string, sch schema.Schema) (sql.PrimaryKey
func ToDoltSchema(
ctx context.Context,
root doltdb.RootValue,
tableName string,
tableName doltdb.TableName,
sqlSchema sql.PrimaryKeySchema,
headRoot doltdb.RootValue,
collation sql.CollationID,

View File

@@ -40,7 +40,9 @@ func ParseCreateTableStatement(ctx *sql.Context, root doltdb.RootValue, engine *
return "", nil, fmt.Errorf("expected create table, found %T", create)
}
sch, err := ToDoltSchema(ctx, root, create.Name(), create.PkSchema(), nil, create.Collation)
// NOTE: We don't support setting a schema name here since this code is only intended to be used from the Dolt
// codebase, and will not work correctly from Doltgres.
sch, err := ToDoltSchema(ctx, root, doltdb.TableName{Name: create.Name()}, create.PkSchema(), nil, create.Collation)
if err != nil {
return "", nil, err
}

View File

@@ -922,7 +922,7 @@ func emptyFulltextTable(
}
// TODO: this should be the head root, not working root
doltSchema, err := sqlutil.ToDoltSchema(ctx, workingRoot, doltTable.tableName, sql.NewPrimaryKeySchema(fulltextSch), workingRoot, parentTable.Collation())
doltSchema, err := sqlutil.ToDoltSchema(ctx, workingRoot, doltTable.TableName(), sql.NewPrimaryKeySchema(fulltextSch), workingRoot, parentTable.Collation())
if err != nil {
return nil, nil, err
}
@@ -1594,7 +1594,7 @@ func (t *AlterableDoltTable) AddColumn(ctx *sql.Context, column *sql.Column, ord
if err != nil {
return err
}
tags, err := doltdb.GenerateTagsForNewColumns(ctx, root, t.tableName, []string{column.Name}, []types.NomsKind{ti.NomsKind()}, nil)
tags, err := doltdb.GenerateTagsForNewColumns(ctx, root, t.TableName(), []string{column.Name}, []types.NomsKind{ti.NomsKind()}, nil)
if err != nil {
return err
}
@@ -2108,7 +2108,7 @@ func validateFullTextColumnChange(ctx *sql.Context, idx schema.Index, oldColumn
func (t *AlterableDoltTable) createSchemaForColumnChange(ctx context.Context, oldColumn, newColumn *sql.Column, oldSch schema.Schema, newSchema sql.PrimaryKeySchema, root, headRoot doltdb.RootValue) (schema.Schema, error) {
// Adding or dropping a column
if oldColumn == nil || newColumn == nil {
newSch, err := sqlutil.ToDoltSchema(ctx, root, t.Name(), newSchema, headRoot, sql.CollationID(oldSch.GetCollation()))
newSch, err := sqlutil.ToDoltSchema(ctx, root, t.TableName(), newSchema, headRoot, sql.CollationID(oldSch.GetCollation()))
if err != nil {
return nil, err
}
@@ -2116,7 +2116,7 @@ func (t *AlterableDoltTable) createSchemaForColumnChange(ctx context.Context, ol
}
// Modifying a column
newSch, err := sqlutil.ToDoltSchema(ctx, root, t.Name(), newSchema, headRoot, sql.CollationID(oldSch.GetCollation()))
newSch, err := sqlutil.ToDoltSchema(ctx, root, t.TableName(), newSchema, headRoot, sql.CollationID(oldSch.GetCollation()))
if err != nil {
return nil, err
}

View File

@@ -95,7 +95,9 @@ func NewTempTable(
colKinds[i] = ti.NomsKind()
}
tags, err := doltdb.GenerateTagsForNewColumns(ctx, ws.WorkingRoot(), name, colNames, colKinds, ws.WorkingRoot())
// NOTE: We don't support setting a schema name to qualify the table name here, so this code will not work
// correctly with Doltgres yet.
tags, err := doltdb.GenerateTagsForNewColumns(ctx, ws.WorkingRoot(), doltdb.TableName{Name: name}, colNames, colKinds, ws.WorkingRoot())
if err != nil {
return nil, err
}