diff --git a/src/proxy/show.go b/src/proxy/show.go index 54f2a9f5..fd4179ed 100644 --- a/src/proxy/show.go +++ b/src/proxy/show.go @@ -257,6 +257,12 @@ func (spanner *Spanner) handleShowCreateTable(session *driver.Session, query str var qr *sqltypes.Result var err error + tableConfig, err := router.TableConfig(database, table) + if err != nil { + return nil, err + } + tableType := tableConfig.ShardType + shardKey, err := router.ShardKey(database, table) if err != nil { return nil, err @@ -279,11 +285,6 @@ func (spanner *Spanner) handleShowCreateTable(session *driver.Session, query str return nil, err } - tableConfig, err := router.TableConfig(database, table) - if err != nil { - return nil, err - } - tableType := tableConfig.ShardType // 'show create table' has two columns. c2 := qr.Rows[0][1] // Add tableType to the end of c2Val @@ -317,7 +318,7 @@ func (spanner *Spanner) handleShowCreateTable(session *driver.Session, query str // Add partition info to the end of c2Val c2Buf := common.NewBuffer(0) c2Buf.WriteString(c2Val) - partInfo := fmt.Sprintf("\n/*!50100 PARTITION BY HASH (%s) */", shardKey) + partInfo := fmt.Sprintf("\n/*!50100 PARTITION BY %s(%s) */", tableType, shardKey) c2Buf.WriteString(partInfo) qr.Rows[0][0] = sqltypes.MakeTrusted(c1.Type(), []byte(c1Val)) diff --git a/src/proxy/show_test.go b/src/proxy/show_test.go index 358686a0..929b58c5 100644 --- a/src/proxy/show_test.go +++ b/src/proxy/show_test.go @@ -474,6 +474,26 @@ func TestProxyShowCreateTable(t *testing.T) { }, } + lr := &sqltypes.Result{ + Fields: []*querypb.Field{ + { + Name: "table", + Type: querypb.Type_VARCHAR, + }, + { + Name: "create table", + Type: querypb.Type_VARCHAR, + }, + }, + Rows: [][]sqltypes.Value{ + { + sqltypes.MakeTrusted(querypb.Type_VARCHAR, []byte("l_0000")), + sqltypes.MakeTrusted(querypb.Type_VARCHAR, []byte("create table l_0000")), + }, + }, + } + + r2 := &sqltypes.Result{ Fields: []*querypb.Field{ { @@ -516,12 +536,14 @@ func TestProxyShowCreateTable(t *testing.T) { fakedbs, proxy, cleanup := MockProxy(log) defer cleanup() address := proxy.Address() + backends := fakedbs.BackendConfs() // fakedbs. { fakedbs.AddQueryPattern("use .*", &sqltypes.Result{}) fakedbs.AddQueryPattern("create .*", &sqltypes.Result{}) fakedbs.AddQuerys("show create table test.t1_0000", r1) + fakedbs.AddQuerys("show create table test.l_0000", lr) fakedbs.AddQuerys("show create table test.t1", r1) fakedbs.AddQuerys("show create table t1", r1) fakedbs.AddQuerys("show create table MYSQL.t1", r1) @@ -557,7 +579,31 @@ func TestProxyShowCreateTable(t *testing.T) { query := "show create table test.t1" qr, err := client.FetchAll(query, -1) assert.Nil(t, err) - want := "[t1 create table t1\n/*!50100 PARTITION BY HASH (id) */]" + want := "[t1 create table t1\n/*!50100 PARTITION BY HASH(id) */]" + got := fmt.Sprintf("%+v", qr.Rows[0]) + assert.Equal(t, want, got) + } + + // create test table with list. + { + client, err := driver.NewConn("mock", "mock", address, "test", "utf8") + assert.Nil(t, err) + b1 := backends[0].Name + query := fmt.Sprintf("create table l(id int, b int) partition by list(id)(partition %s values in (1,2));", b1) + _, err = client.FetchAll(query, -1) + assert.Nil(t, err) + client.Quit() + } + + // show create table which shardType is list. + { + client, err := driver.NewConn("mock", "mock", address, "", "utf8") + assert.Nil(t, err) + defer client.Close() + query := "show create table test.l" + qr, err := client.FetchAll(query, -1) + assert.Nil(t, err) + want := "[l create table l\n/*!50100 PARTITION BY LIST(id) */]" got := fmt.Sprintf("%+v", qr.Rows[0]) assert.Equal(t, want, got) } @@ -738,7 +784,7 @@ func TestProxyShowColumns(t *testing.T) { query := "show create table test.t1" qr, err := client.FetchAll(query, -1) assert.Nil(t, err) - want := "[t1 create table t1\n/*!50100 PARTITION BY HASH (id) */]" + want := "[t1 create table t1\n/*!50100 PARTITION BY HASH(id) */]" got := fmt.Sprintf("%+v", qr.Rows[0]) assert.Equal(t, want, got) }