Skip to content

Commit

Permalink
proxy: add partition list to show create table radondb#491
Browse files Browse the repository at this point in the history
[summary]
add partition list to show create table
[test case]
src/proxy/show_test.go
[patch codecov]
src/proxy/show.go 88.1%
  • Loading branch information
andyli029 committed Nov 14, 2019
1 parent 7a568c3 commit 187d0d5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/proxy/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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))
Expand Down
46 changes: 46 additions & 0 deletions src/proxy/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -562,6 +584,30 @@ func TestProxyShowCreateTable(t *testing.T) {
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)
}

// create test table with global.
{
client, err := driver.NewConn("mock", "mock", address, "test", "utf8")
Expand Down

0 comments on commit 187d0d5

Please sign in to comment.