Skip to content

Commit

Permalink
Remove checks for unique index (#952)
Browse files Browse the repository at this point in the history
  • Loading branch information
VinaiRachakonda authored Apr 20, 2022
1 parent 2a98630 commit 264e64b
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 16 deletions.
30 changes: 30 additions & 0 deletions enginetest/enginetests.go
Original file line number Diff line number Diff line change
Expand Up @@ -6166,6 +6166,36 @@ func TestPersist(t *testing.T, harness Harness, newPersistableSess func(ctx *sql
}
}

func TestKeylessUniqueIndex(t *testing.T, harness Harness) {
for _, insertion := range InsertIntoKeylessUnique {
e := NewEngine(t, harness)
defer e.Close()

TestQuery(t, harness, e, insertion.WriteQuery, insertion.ExpectedWriteResult, nil)

// If we skipped the insert, also skip the select
if sh, ok := harness.(SkippingHarness); ok {
if sh.SkipQueryTest(insertion.WriteQuery) {
t.Logf("Skipping query %s", insertion.SelectQuery)
continue
}
}

TestQuery(t, harness, e, insertion.SelectQuery, insertion.ExpectedSelect, nil)
}

for _, expectedFailure := range InsertIntoKeylessUniqueError {
t.Run(expectedFailure.Name, func(t *testing.T) {
if sh, ok := harness.(SkippingHarness); ok {
if sh.SkipQueryTest(expectedFailure.Query) {
t.Skipf("skipping query %s", expectedFailure.Query)
}
}
AssertErr(t, NewEngine(t, harness), harness, expectedFailure.Query, nil)
})
}
}

func TestPrepared(t *testing.T, harness Harness) {
qtests := []QueryTest{
{
Expand Down
26 changes: 26 additions & 0 deletions enginetest/insert_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,32 @@ var SpatialInsertQueries = []WriteQueryTest{
},
}

var InsertIntoKeylessUnique = []WriteQueryTest{
{
WriteQuery: "INSERT INTO unique_keyless VALUES (3, 3);",
ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}},
SelectQuery: "SELECT * FROM unique_keyless order by c0;",
ExpectedSelect: []sql.Row{{0, 0}, {1, 1}, {2, 2}, {3, 3}},
},
{
WriteQuery: "INSERT INTO unique_keyless VALUES (3, 4);",
ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}},
SelectQuery: "SELECT * FROM unique_keyless order by c0;",
ExpectedSelect: []sql.Row{{0, 0}, {1, 1}, {2, 2}, {3, 4}},
},
}

var InsertIntoKeylessUniqueError = []GenericErrorQueryTest{
{
Name: "Try to insert into a unique keyless table",
Query: "INSERT INTO unique_keyless (100, 2)",
},
{
Name: "Try to insert into a unique keyless table",
Query: "INSERT INTO unique_keyless (1, 1)",
},
}

var InsertScripts = []ScriptTest{
{
Name: "insert into sparse auto_increment table",
Expand Down
6 changes: 6 additions & 0 deletions enginetest/memory_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,12 @@ func TestPreparedInsert(t *testing.T) {
enginetest.TestPreparedInsert(t, enginetest.NewMemoryHarness("default", 1, testNumPartitions, true, mergableIndexDriver))
}

func TestKeylessUniqueIndex(t *testing.T) {
// TODO: GMS does not support unique indexes for keyless tables.
t.Skip()
enginetest.TestKeylessUniqueIndex(t, enginetest.NewDefaultMemoryHarness())
}

func mergableIndexDriver(dbs []sql.Database) sql.IndexDriver {
return memory.NewIndexDriver("mydb", map[string][]sql.DriverIndex{
"mytable": {
Expand Down
17 changes: 17 additions & 0 deletions enginetest/script_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -2354,4 +2354,21 @@ var BrokenScriptTests = []ScriptTest{
},
},
},
// TODO: We should implement unique indexes with GMS
{
Name: "Keyless Table with Unique Index",
SetUpScript: []string{
"create table a (x int, val int unique)",
},
Assertions: []ScriptTestAssertion{
{
Query: "INSERT INTO a VALUES (1, 1)",
Expected: []sql.Row{{sql.NewOkResult(1)}},
},
{
Query: "INSERT INTO a VALUES (1, 1)",
ExpectedErr: sql.ErrUniqueKeyViolation,
},
},
},
}
16 changes: 16 additions & 0 deletions enginetest/testdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,22 @@ func createSubsetTestData(t *testing.T, harness Harness, includedTables []string
t.Logf("Warning: could not create table %s: %s", "keyless", err)
}
})

wrapInTransaction(t, myDb, harness, func() {
table, err = harness.NewTable(myDb, "unique_keyless", sql.NewPrimaryKeySchema(sql.Schema{
{Name: "c0", Type: sql.Int64, Source: "unique_keyless", Nullable: true},
{Name: "c1", Type: sql.Int64, Source: "unique_keyless", Nullable: true},
}))

if err == nil {
InsertRows(t, NewContext(harness), mustInsertableTable(t, table),
sql.NewRow(int64(0), int64(0)),
sql.NewRow(int64(1), int64(1)),
sql.NewRow(int64(2), int64(2)))
} else {
t.Logf("Warning: could not create table %s: %s", "keyless", err)
}
})
}

return []sql.Database{myDb, foo}
Expand Down
13 changes: 0 additions & 13 deletions sql/parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -1552,17 +1552,13 @@ func convertCreateTable(ctx *sql.Context, c *sqlparser.DDL) (sql.Node, error) {
}
}

seenPrimary := false
seenUnique := false
var idxDefs []*plan.IndexDefinition
for _, idxDef := range c.TableSpec.Indexes {
constraint := sql.IndexConstraint_None
if idxDef.Info.Primary {
constraint = sql.IndexConstraint_Primary
seenPrimary = true
} else if idxDef.Info.Unique {
constraint = sql.IndexConstraint_Unique
seenUnique = true
} else if idxDef.Info.Spatial {
constraint = sql.IndexConstraint_Spatial
} else if idxDef.Info.Fulltext {
Expand Down Expand Up @@ -1608,11 +1604,7 @@ func convertCreateTable(ctx *sql.Context, c *sqlparser.DDL) (sql.Node, error) {
if colDef.Type.KeyOpt == colKeyFulltextKey {
return nil, sql.ErrUnsupportedFeature.New("fulltext keys are unsupported")
}
if colDef.Type.KeyOpt == colKeyPrimary {
seenPrimary = true
}
if colDef.Type.KeyOpt == colKeyUnique || colDef.Type.KeyOpt == colKeyUniqueKey {
seenUnique = true
idxDefs = append(idxDefs, &plan.IndexDefinition{
IndexName: "",
Using: sql.IndexUsing_Default,
Expand All @@ -1626,11 +1618,6 @@ func convertCreateTable(ctx *sql.Context, c *sqlparser.DDL) (sql.Node, error) {
}
}

// can't use unique constraint on keyless tables
if seenUnique && !seenPrimary {
return nil, sql.ErrUnsupportedFeature.New("unique constraint on keyless tables")
}

qualifier := c.Table.Qualifier.String()

schema, err := TableSpecToSchema(nil, c.TableSpec)
Expand Down
3 changes: 0 additions & 3 deletions sql/parse/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3867,9 +3867,6 @@ var fixturesErrors = map[string]*errors.Kind{
`DROP TABLE IF EXISTS curdb.foo, otherdb.bar`: sql.ErrUnsupportedFeature,
`DROP TABLE curdb.t1, t2`: sql.ErrUnsupportedFeature,
`CREATE TABLE test (i int fulltext key)`: sql.ErrUnsupportedFeature,
`CREATE TABLE test (i int unique)`: sql.ErrUnsupportedFeature,
`CREATE TABLE test (i int, j int unique)`: sql.ErrUnsupportedFeature,
`CREATE TABLE test (i int, unique(i))`: sql.ErrUnsupportedFeature,
}

func TestParseOne(t *testing.T) {
Expand Down

0 comments on commit 264e64b

Please sign in to comment.