Skip to content
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

multi-valued index support #63

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ddl/datatype.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ var TestFieldType = []int{
KindTIMESTAMP,
KindYEAR,

//KindJSON, // have `admin check table when index is virtual generated column` bug unfixed.
KindJSON, // have `admin check table when index is virtual generated column` bug unfixed.
KindEnum,
KindSet,
}
Expand Down
20 changes: 18 additions & 2 deletions ddl/ddl_ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,9 @@ func (c *testCase) checkTableIndexes(table *ddlTestTable) error {
if err != nil {
return err
}
if idx.expressionIndex {
continue
}
if idx.signature != columnNames {
return errors.Errorf("table index columns doesn't match, index name: %s, expected: %s, got: %s", idx.name, idx.signature, columnNames)
}
Expand Down Expand Up @@ -562,8 +565,8 @@ func (c *testCase) execParaDDLSQL(taskCh chan *ddlJobTask, num int) error {
log.Infof("seq:%d, query:%s, task.sql:%s", seqNum, query, task.sql)
}

log.Infof("[ddl] [instance %d] TiDB execute %s , err %v, elapsed time:%v", c.caseIndex, task.sql, err, time.Since(opStart).Seconds())
if !ddlIgnoreError(ddlErr) {
log.Infof("[ddl] [instance %d] TiDB execute %s , err %v, elapsed time:%v", c.caseIndex, task.sql, err, time.Since(opStart).Seconds())
task.err = ddlErr
unExpectedErr = ddlErr
// No need to update schema.
Expand Down Expand Up @@ -1253,9 +1256,16 @@ func (c *testCase) prepareAddIndex(ctx interface{}, taskCh chan *ddlJobTask) err
numberOfColumns = 10
}
perm := rand.Perm(table.columns.Size())[:numberOfColumns]
hasJSON := false
for _, idx := range perm {
column := getColumnFromArrayList(table.columns, idx)
if column.canBeIndex() {
if column.k == KindJSON {
if hasJSON && rand.Intn(10) != 0 {
continue
}
hasJSON = true
}
index.columns = append(index.columns, column)
}
}
Expand Down Expand Up @@ -1284,12 +1294,18 @@ func (c *testCase) prepareAddIndex(ctx interface{}, taskCh chan *ddlJobTask) err
uniqueString = "unique"
}
// build SQL
randTp := []string{"SIGNED", "UNSIGNED", "DOUBLE", "CHAR(64)", "binary(64)", "SIGNED", "UNSIGNED", "DOUBLE", "CHAR(64)", "binary(64)", "date", "datetime", "time"}
sql := fmt.Sprintf("ALTER TABLE `%s` ADD %s INDEX `%s` (", table.name, uniqueString, index.name)
for i, column := range index.columns {
if i > 0 {
sql += ", "
}
sql += fmt.Sprintf("`%s`", column.name)
if column.k == KindJSON {
index.expressionIndex = true
sql += fmt.Sprintf("(cast(`%s` as %s array))", column.name, randTp[rand.Intn(len(randTp))])
} else {
sql += fmt.Sprintf("`%s`", column.name)
}
}
sql += ")"

Expand Down
137 changes: 88 additions & 49 deletions ddl/dml_ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package ddl
import (
"context"
"database/sql"
"fmt"
"math"
"math/rand"
"regexp"
"sort"
"strconv"
"strings"

"github.com/PingCAP-QE/clustered-index-rand-test/sqlgen"
"github.com/juju/errors"
Expand Down Expand Up @@ -69,20 +71,57 @@ func (c *testCase) execSerialDMLSQL(taskCh chan *dmlJobTask) error {
defer conn.Close()
task := <-taskCh
// disable compare for now since it's not stable.
if task.k == dmlSelect && false {
if rand.Intn(5) == 0 {
r, _ := regexp.Compile("/\\*.*?\\*/")
var rows [][]string
// Check plan
_, err := conn.ExecContext(ctx, "begin")
_, err = conn.ExecContext(ctx, "begin")
if err != nil {
return err
}
err = compareXPlans(ctx, task, conn, c.caseIndex)
if err != nil {
if dmlIgnoreError(err) {
return nil
}
return errors.Trace(err)
}
_, _ = conn.ExecContext(ctx, "commit")
err = c.sendDMLRequest(ctx, conn, task)
if err != nil {
if dmlIgnoreError(err) {
return nil
}
return errors.Trace(err)
}
if task.err != nil {
return nil
}
return nil
}

func printPlans(ctx context.Context, conn *sql.Conn, s string) error {
exp := fmt.Sprintf("explain %s", s)
rows, err := readData(ctx, conn, exp)
if err != nil {
return errors.Trace(err)
}
log.Infof("check plan: %v", exp)
for _, row := range rows {
log.Infof("explain: %v", row)
}
return nil
}

func compareXPlans(ctx context.Context, task *dmlJobTask, conn *sql.Conn, caseIndex int) error {
if task.k == dmlSelect && !strings.Contains(task.sql, "limit") {
if true {
log.Infof("[check plan] [instance %d] %s, ", caseIndex, task.sql)
err := printPlans(ctx, conn, task.sql)
if err != nil {
return err
return errors.Trace(err)
}
defer func() {
conn.ExecContext(ctx, "commit")
}()
r, _ := regexp.Compile("/\\*.*?\\((.*)\\).*?\\*/")
var rows [][]string
// Check plan
rows, err = sendQueryRequest(ctx, conn, task)
log.Infof("[dml] [instance %d] %s, err: %v", c.caseIndex, task.sql, err)
log.Infof("[dml] [instance %d] %s, err: %v", caseIndex, task.sql, err)
if err != nil {
if dmlIgnoreError(err) {
return nil
Expand All @@ -92,10 +131,14 @@ func (c *testCase) execSerialDMLSQL(taskCh chan *dmlJobTask) error {

// Check no hint plan
origianlSql := task.sql
noHintSql := r.ReplaceAllString(task.sql, "")
noHintSql := r.ReplaceAllString(task.sql, "/*+ use_index(${1},) */")
task.sql = noHintSql
err = printPlans(ctx, conn, task.sql)
if err != nil {
return errors.Trace(err)
}
newRow, err := sendQueryRequest(ctx, conn, task)
log.Infof("[dml] [instance %d] %s, err: %v", c.caseIndex, task.sql, err)
log.Infof("[dml] [instance %d] %s, err: %v", caseIndex, task.sql, err)
if err != nil {
return errors.Trace(err)
}
Expand All @@ -106,32 +149,22 @@ func (c *testCase) execSerialDMLSQL(taskCh chan *dmlJobTask) error {
}

// Make sure the query is stable itself.
task.sql = origianlSql
orignalRowToCheck, err2 := sendQueryRequest(ctx, conn, task)
if err2 != nil {
return nil
}
err2 = compareTwoRows(rows, orignalRowToCheck)
if err2 != nil {
// The query is not stable.
return nil
}
//task.sql = origianlSql
//orignalRowToCheck, err2 := sendQueryRequest(ctx, conn, task)
//if err2 != nil {
// return nil
//}
//err2 = compareTwoRows(rows, orignalRowToCheck)
//if err2 != nil {
// // The query is not stable.
// return nil
//}

task.sql = "select @@tidb_current_ts"
ts, _ = sendQueryRequest(ctx, conn, task)
return errors.Errorf("[dml] [instance %d] found inconsistent data for two plans, sql-with-hint %s, sql-without-hint %s, %s, err: %v", c.caseIndex, origianlSql, noHintSql, ts[0][0], err)
return errors.Errorf("[dml] [instance %d] found inconsistent data for two plans, sql-with-hint %s, sql-without-hint %s, %s, err: %v", caseIndex, origianlSql, noHintSql, ts[0][0], err)
}
}
err = c.sendDMLRequest(ctx, conn, task)
if err != nil {
if dmlIgnoreError(err) {
return nil
}
return errors.Trace(err)
}
if task.err != nil {
return nil
}
return nil
}

Expand Down Expand Up @@ -183,7 +216,7 @@ func (c *testCase) execDMLInTransactionSQL(taskCh chan *dmlJobTask) error {
tasksLen := len(taskCh)

ctx := context.Background()
conn, err := c.dbs[1].Conn(ctx)
conn, err := c.dbs[rand.Intn(len(c.dbs))].Conn(ctx)
if err != nil {
return nil
}
Expand All @@ -198,31 +231,37 @@ func (c *testCase) execDMLInTransactionSQL(taskCh chan *dmlJobTask) error {
tasks := make([]*dmlJobTask, 0, tasksLen)
for i := 0; i < tasksLen; i++ {
task := <-taskCh
err := compareXPlans(ctx, task, conn, c.caseIndex)
if err != nil {
task.err = err
tasks = append(tasks, task)
continue
}
err = c.sendDMLRequest(ctx, conn, task)
tasks = append(tasks, task)
}

_, err = conn.ExecContext(ctx, "commit")
log.Infof("[dml] [instance %d] commit error: %v", c.caseIndex, err)
if rand.Intn(10) == 0 {
_, err = conn.ExecContext(ctx, "rollback")
log.Infof("[dml] [instance %d] rollback error: %v", c.caseIndex, err)
} else {
_, err = conn.ExecContext(ctx, "commit")
log.Infof("[dml] [instance %d] commit error: %v", c.caseIndex, err)
}
for i := 0; i < tasksLen; i++ {
task := tasks[i]
if task.err != nil && dmlIgnoreError(err) {
continue
}
return errors.Annotatef(err, "Error when executing SQL: %s, %d of %d in %d", task.sql, i, tasksLen, c.caseIndex)
}
if err != nil {
if dmlIgnoreError(err) {
return nil
}
for i := 0; i < tasksLen; i++ {
task := tasks[i]
if task.err == nil {
return nil
}
}
return errors.Annotatef(err, "Error when executing SQL: %s", "commit")
}

for i := 0; i < tasksLen; i++ {
task := tasks[i]
if task.err != nil {
continue
}
}
log.Infof("[dml] [instance %d] finish transaction dml", c.caseIndex)
return nil
}
Expand Down
27 changes: 10 additions & 17 deletions ddl/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,17 +601,9 @@ func getRandDDLTestColumnForJson() *ddlTestColumn {

func getRandDDLTestColumns() []*ddlTestColumn {
n := RandDataType()
cols := make([]*ddlTestColumn, 0)

if n == KindJSON {
// Json column itself doesn't mean a lot, it's value is in generated column.
// eg: create table t(a json, b int as(json_extract(`a`, '$.haha'))).
// for this instance: a is the target column, b is depending on column a.
cols = getRandJsonCol()
} else {
column := getDDLTestColumn(n)
cols = append(cols, column)
}
cols := make([]*ddlTestColumn, 0, 1)
column := getDDLTestColumn(n)
cols = append(cols, column)
return cols
}

Expand Down Expand Up @@ -804,7 +796,7 @@ func (col *ddlTestColumn) randValueUnique(rows *arraylist.List) (interface{}, bo
}

func (col *ddlTestColumn) canBePrimary() bool {
return col.canBeIndex() && col.notGenerated()
return col.canBeIndex() && col.notGenerated() && col.k != KindJSON
}

func (col *ddlTestColumn) canBeIndex() bool {
Expand All @@ -815,7 +807,7 @@ func (col *ddlTestColumn) canBeIndex() bool {
} else {
return true
}
case KindBLOB, KindTINYBLOB, KindMEDIUMBLOB, KindLONGBLOB, KindTEXT, KindTINYTEXT, KindMEDIUMTEXT, KindLONGTEXT, KindJSON:
case KindBLOB, KindTINYBLOB, KindMEDIUMBLOB, KindLONGBLOB, KindTEXT, KindTINYTEXT, KindMEDIUMTEXT, KindLONGTEXT:
return false
default:
return true
Expand Down Expand Up @@ -846,10 +838,11 @@ func (col *ddlTestColumn) canHaveDefaultValue() bool {
}

type ddlTestIndex struct {
name string
signature string
columns []*ddlTestColumn
uniques bool
name string
signature string
columns []*ddlTestColumn
uniques bool
expressionIndex bool
}

func (col *ddlTestColumn) normalizeDataType() string {
Expand Down
Loading