Skip to content

Commit

Permalink
*: change LowerCaseTableNames type to int and add some test cases #725
Browse files Browse the repository at this point in the history
[summary]
1.lower-case-table-names:
If set 0, table names are stored as specified and comparisons are case-sensitive.
If set 1, table names are stored in lowercase on disk and comparisons are not case-sensitive.
2.Add some test cases.

[test case]
src/ctl/v1/radon_test.go
src/proxy/initdb_test.go
src/proxy/proxy_test.go
src/proxy/query_test.go

[patch codecov]
src/ctl/v1/radon.go 92.8%
src/proxy/proxy.go 86.5%
src/proxy/spanner.go 95.2%
  • Loading branch information
zhyass committed Jan 28, 2021
1 parent d9d9b85 commit 1bdd2f4
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 28 deletions.
2 changes: 1 addition & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Request: {
"audit-mode": The audit log mode, "N": disabled, "R": read enabled, "W": write enabled, "A": read/write enabled,
"blocks-readonly": The size of a block when create hash tables,
"load-balance": Enables(0 or 1) load balance, for read-write separation,
"lower-case-table-names": If set false, table names are stored as specified and comparisons are case-sensitive, else not case-sensitive.
"lower-case-table-names": If set 0, table names are stored as specified and comparisons are case-sensitive. If set 1, not case-sensitive.
}
```
Expand Down
9 changes: 9 additions & 0 deletions intergration/radon-test/r/select.result
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ sum(a) b
select c from integrate_test.t;
ERROR 1054 (42S22): Unknown column 'c' in 'field list'

select a from integrate_test.T;
ERROR 1146 (42S02): Table 'T' doesn't exist

select a from INTEGRATE_TEST.t;
ERROR 1146 (42S02): Table 'INTEGRATE_TEST.t' doesn't exist

select tt.a from integrate_test.t as TT;
ERROR 1105 (HY000): unsupported: unknown.column.'tt.a'.in.field.list


create table integrate_test.s(a int key, b int) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Expand Down
3 changes: 3 additions & 0 deletions intergration/radon-test/t/select.test
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ select a, b c from integrate_test.t where a in (0,1,2) order by C desc;
select a, b from integrate_test.t where A>0;
select sum(a), b from integrate_test.t where a>=0 group by B;
select c from integrate_test.t;
select a from integrate_test.T;
select a from INTEGRATE_TEST.t;
select tt.a from integrate_test.t as TT;

create table integrate_test.s(a int key, b int) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into integrate_test.s(a, b) values(0,1), (2,2);
Expand Down
29 changes: 15 additions & 14 deletions src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ type ProxyConfig struct {
MetaDir string `json:"meta-dir"`
Endpoint string `json:"endpoint"`
TwopcEnable bool `json:"twopc-enable"`
LoadBalance int `json:"load-balance"` // 0 -- disable balance, 1 -- enable balance to replica
LowerCaseTableNames bool `json:"lower-case-table-names"`
LoadBalance int `json:"load-balance"` // 0 -- disable balance, 1 -- enable balance to replica
LowerCaseTableNames int `json:"lower-case-table-names"` // 0 -- case sensitive, 1 -- case insensitive

MaxConnections int `json:"max-connections"`
MaxResultSize int `json:"max-result-size"`
Expand All @@ -49,18 +49,19 @@ type ProxyConfig struct {
// DefaultProxyConfig returns default proxy config.
func DefaultProxyConfig() *ProxyConfig {
return &ProxyConfig{
MetaDir: "./radon-meta",
Endpoint: "127.0.0.1:3308",
LoadBalance: 0,
MaxConnections: 1024,
MaxResultSize: 1024 * 1024 * 1024, // 1GB
MaxJoinRows: 32768,
DDLTimeout: 10 * 3600 * 1000, // 10hours
QueryTimeout: 5 * 60 * 1000, // 5minutes
PeerAddress: "127.0.0.1:8080",
LongQueryTime: 5, // 5 seconds
StreamBufferSize: 1024 * 1024 * 32, // 32MB
IdleTxnTimeout: 60, // 60 seconds
MetaDir: "./radon-meta",
Endpoint: "127.0.0.1:3308",
LoadBalance: 0,
LowerCaseTableNames: 0,
MaxConnections: 1024,
MaxResultSize: 1024 * 1024 * 1024, // 1GB
MaxJoinRows: 32768,
DDLTimeout: 10 * 3600 * 1000, // 10hours
QueryTimeout: 5 * 60 * 1000, // 5minutes
PeerAddress: "127.0.0.1:8080",
LongQueryTime: 5, // 5 seconds
StreamBufferSize: 1024 * 1024 * 32, // 32MB
IdleTxnTimeout: 60, // 60 seconds
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/ctl/v1/radon.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type radonParams struct {
AuditMode *string `json:"audit-mode"`
StreamBufferSize *int `json:"stream-buffer-size"`
Blocks *int `json:"blocks-readonly"`
LowerCaseTableNames *bool `json:"lower-case-table-names"`
LowerCaseTableNames *int `json:"lower-case-table-names"`
}

// RadonConfigHandler impl.
Expand Down
6 changes: 3 additions & 3 deletions src/ctl/v1/radon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestCtlV1RadonConfig(t *testing.T) {
AuditMode string `json:"audit-mode"`
StreamBufferSize int `json:"stream-buffer-size"`
Blocks int `json:"blocks-readonly"`
LowerCaseTableNames bool `json:"lower-case-table-names"`
LowerCaseTableNames int `json:"lower-case-table-names"`
}

// 200.
Expand All @@ -64,7 +64,7 @@ func TestCtlV1RadonConfig(t *testing.T) {
AuditMode: "A",
StreamBufferSize: 16777216,
Blocks: 128,
LowerCaseTableNames: true,
LowerCaseTableNames: 1,
}
recorded := test.RunRequest(t, handler, test.MakeSimpleRequest("PUT", "http://localhost/v1/radon/config", p))
recorded.CodeIs(200)
Expand All @@ -81,7 +81,7 @@ func TestCtlV1RadonConfig(t *testing.T) {
assert.Equal(t, "A", radonConf.Audit.Mode)
assert.Equal(t, 16777216, radonConf.Proxy.StreamBufferSize)
assert.Equal(t, 128, radonConf.Router.Blocks)
assert.Equal(t, true, radonConf.Proxy.LowerCaseTableNames)
assert.Equal(t, 1, radonConf.Proxy.LowerCaseTableNames)
}

// Unset AllowIP.
Expand Down
2 changes: 1 addition & 1 deletion src/proxy/initdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestProxyUseDatabase(t *testing.T) {
spanner.ComInitDB(session, "TEST")
assert.Equal(t, "TEST", session.Schema())

proxy.SetLowerCaseTableNames(true)
proxy.SetLowerCaseTableNames(1)
spanner.ComInitDB(session, "TEST")
assert.Equal(t, "test", session.Schema())
}
Expand Down
2 changes: 1 addition & 1 deletion src/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ func (p *Proxy) SetBlocks(blocks int) {
}

// SetLowerCaseTableNames used to set LowerCaseTableNames to false or true.
func (p *Proxy) SetLowerCaseTableNames(lowerCase bool) {
func (p *Proxy) SetLowerCaseTableNames(lowerCase int) {
p.mu.Lock()
defer p.mu.Unlock()
p.log.Info("proxy.SetLowerCaseTableNames:[%v->%v]", p.conf.Proxy.LowerCaseTableNames, lowerCase)
Expand Down
8 changes: 4 additions & 4 deletions src/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ func TestProxy1(t *testing.T) {

// SetLowerCaseTableNames.
{
proxy.SetLowerCaseTableNames(true)
assert.Equal(t, true, proxy.conf.Proxy.LowerCaseTableNames)
proxy.SetLowerCaseTableNames(false)
assert.Equal(t, false, proxy.conf.Proxy.LowerCaseTableNames)
proxy.SetLowerCaseTableNames(1)
assert.Equal(t, 1, proxy.conf.Proxy.LowerCaseTableNames)
proxy.SetLowerCaseTableNames(0)
assert.Equal(t, 0, proxy.conf.Proxy.LowerCaseTableNames)
}

// FlushConfig.
Expand Down
115 changes: 113 additions & 2 deletions src/proxy/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -739,18 +739,46 @@ func TestProxyLowerCase(t *testing.T) {
fakedbs, proxy, cleanup := MockProxy(log)
defer cleanup()
address := proxy.Address()
proxy.SetLowerCaseTableNames(true)

r1 := &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("t1")),
sqltypes.MakeTrusted(querypb.Type_VARCHAR, []byte("create table t1")),
},
},
}

// fakedbs.
{
fakedbs.AddQueryPattern("create .*", &sqltypes.Result{})
fakedbs.AddQueryPattern("insert .*", &sqltypes.Result{})
fakedbs.AddQueryPattern("delete .*", &sqltypes.Result{})
fakedbs.AddQueryPattern("update .*", &sqltypes.Result{})
fakedbs.AddQueryPattern("select .*", &sqltypes.Result{})
fakedbs.AddQueryPattern("use .*", &sqltypes.Result{})
fakedbs.AddQueryPattern("show create .*", r1)
}

{
proxy.SetLowerCaseTableNames(1)
}

// create database.
{
client, err := driver.NewConn("mock", "mock", address, "", "utf8")
assert.Nil(t, err)
defer client.Close()
query := "create database TEST"
_, err = client.FetchAll(query, -1)
assert.Nil(t, err)
Expand All @@ -760,17 +788,100 @@ func TestProxyLowerCase(t *testing.T) {
{
client, err := driver.NewConn("mock", "mock", address, "", "utf8")
assert.Nil(t, err)
defer client.Close()
query := "create table test.T1(id int, b int) partition by hash(id)"
_, err = client.FetchAll(query, -1)
assert.Nil(t, err)
}

// insert.
{
client, err := driver.NewConn("mock", "mock", address, "", "utf8")
assert.Nil(t, err)
defer client.Close()
query := "insert into Test.t1(id,b) values(1,1)"
_, err = client.FetchAll(query, -1)
assert.Nil(t, err)
}

// update.
{
client, err := driver.NewConn("mock", "mock", address, "test", "utf8")
assert.Nil(t, err)
defer client.Close()
query := "update t1 set b=2 where id=1"
_, err = client.FetchAll(query, -1)
assert.Nil(t, err)
}

// select.
{
client, err := driver.NewConn("mock", "mock", address, "", "utf8")
assert.Nil(t, err)
defer client.Close()
query := "select * from tEst.t1 as T where t.b=2"
_, err = client.FetchAll(query, -1)
assert.Nil(t, err)
}

// delete.
{
client, err := driver.NewConn("mock", "mock", address, "TEST", "utf8")
assert.Nil(t, err)
defer client.Close()
query := "delete from t1 where T1.id=1"
_, err = client.FetchAll(query, -1)
assert.Nil(t, err)
}

// show.
{
client, err := driver.NewConn("mock", "mock", address, "", "utf8")
assert.Nil(t, err)
defer client.Close()
query := "show create table test.t1"
_, err = client.FetchAll(query, -1)
assert.Nil(t, err)
}

// lower_case_table_names=0
{
proxy.SetLowerCaseTableNames(0)
}

// select.
{
client, err := driver.NewConn("mock", "mock", address, "", "utf8")
assert.Nil(t, err)
query := "select * from test.t1"
defer client.Close()
query := "select * from test.t1 as t where t.b=2"
_, err = client.FetchAll(query, -1)
assert.Nil(t, err)
}

// select error 1.
{
client, err := driver.NewConn("mock", "mock", address, "", "utf8")
assert.Nil(t, err)
defer client.Close()
query := "select * from TEST.T1 as t where t.b=2"
_, err = client.FetchAll(query, -1)
assert.NotNil(t, err)
want := "Table 'TEST.T1' doesn't exist (errno 1146) (sqlstate 42S02)"
got := err.Error()
assert.Equal(t, want, got)
}

// select error 2.
{
client, err := driver.NewConn("mock", "mock", address, "", "utf8")
assert.Nil(t, err)
defer client.Close()
query := "select * from test.t1 as T where t.b=2"
_, err = client.FetchAll(query, -1)
assert.NotNil(t, err)
want := "unsupported: unknown.column.'t.b'.in.clause (errno 1105) (sqlstate HY000)"
got := err.Error()
assert.Equal(t, want, got)
}
}
5 changes: 4 additions & 1 deletion src/proxy/spanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,8 @@ func (spanner *Spanner) isAutocommitFalseIsTxn() bool {
}

func (spanner *Spanner) isLowerCaseTableNames() bool {
return spanner.conf.Proxy.LowerCaseTableNames
if spanner.conf.Proxy.LowerCaseTableNames == 0 {
return false
}
return true
}

0 comments on commit 1bdd2f4

Please sign in to comment.