Skip to content

Commit

Permalink
Merge pull request #715 from hustjieke/feature_complete_checksum_stmt
Browse files Browse the repository at this point in the history
*.: complete checksum statement #703
  • Loading branch information
BohuTANG authored Jan 13, 2021
2 parents 3885e5a + c221a95 commit 07f42c3
Show file tree
Hide file tree
Showing 15 changed files with 3,406 additions and 3,013 deletions.
2 changes: 1 addition & 1 deletion docs/sql_statements/data_definition_statements.md
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ Query OK, 0 rows affected (0.09 sec)

`Syntax`
```
DROP TABLE [IF EXISTS] table_name
DROP {TABLE | TABLES} [IF EXISTS] table_name
```
---------------------------------------------------------------------------------------------------

Expand Down
88 changes: 61 additions & 27 deletions docs/sql_statements/database_administration_statements.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ Table of Contents
=================

* [Database Administration Statements](#database-administration-statements)
* [CHECKSUM](#checksum)
* [CHECKSUM TABLE](#checksum-table)
* [SET](#set)
* [SHOW](#show)
* [SHOW CHARSET](#show-charset)
Expand All @@ -17,36 +15,13 @@ Table of Contents
* [SHOW INDEX](#show-index)
* [SHOW PROCESSLIST](#show-processlist)
* [SHOW VARIABLES](#show-variables)
* [Table Maintenance Statements](#table-maintenance-statements)
* [CHECKSUM TABLE](#checksum-table)
* [Other Administrative Statements](#other-administrative-statements)
* [KILL Statement](#kill-statement)

# Database Administration Statements

## CHECKSUM

### CHECKSUM TABLE

`Syntax`
```
CHECKSUM TABLE [database_name.]table_name
```

`Instructions`
* Reports a checksum for the contents of a table
* RadonDB gives same result as MySQL

`Example: `

```
mysql> checksum table test.t1;
+----------+------------+
| Table | Checksum |
+----------+------------+
| test.t1 | 2464930879 |
+----------+------------+
1 row in set (0.00 sec)
```

## SET

`Instructions`
Expand Down Expand Up @@ -346,6 +321,65 @@ SHOW VARIABLES
* For compatibility JDBC/mydumper
* The SHOW VARIABLES command is sent to the backend partition MySQL (random partition) to get and return

## Table Maintenance Statements

### CHECKSUM TABLE

`Syntax`
```
CHECKSUM {TABLE | TABLES} tbl_name [, tbl_name] ... [QUICK | EXTENDED]
```

`Instructions`
* Reports a checksum for the contents of a table
* RadonDB gives same result as MySQL

`Example: `

```
mysql> checksum tables t1, t extended;
+---------+------------+
| Table | Checksum |
+---------+------------+
| test.t1 | 1910461541 |
| test.t | 2643913285 |
+---------+------------+
2 rows in set (0.00 sec)
mysql> checksum tables t1;
+---------+------------+
| Table | Checksum |
+---------+------------+
| test.t1 | 1910461541 |
+---------+------------+
1 row in set (0.00 sec)
mysql> checksum /*db not exsit*/ tables t1, db.t;
+---------+------------+
| Table | Checksum |
+---------+------------+
| test.t1 | 1910461541 |
| db.t | NULL |
+---------+------------+
2 rows in set (0.00 sec)
mysql> create table t2(a int key, b int);
insertQuery OK, 0 rows affected (1.20 sec)
mysql> insert into t2(a,b) values (1,2),(3,4);
Query OK, 2 rows affected (0.01 sec)
mysql> checksum tables t,t1,t2 quick;
+---------+------------+
| Table | Checksum |
+---------+------------+
| test.t | NULL |
| test.t1 | NULL |
| test.t2 | NULL |
+---------+------------+
3 rows in set (0.03 sec)
```

## Other Administrative Statements

### KILL Statement
Expand Down
84 changes: 84 additions & 0 deletions intergration/radon-test/r/checksum.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
create database if not exists integrate_test DEFAULT CHARSET=utf8;

drop table if exists integrate_test.t1,integrate_test.t2;

create /*test partition hash, Test of refering to old values*/ table integrate_test.t1 (a int not null key);

insert into integrate_test.t1 values (1), (6);

checksum table integrate_test.t1;
Table Checksum
integrate_test.t1 3727148865


create /*Test of duplicate key values with packed keys */ table integrate_test.t2 (id bigint not null auto_increment primary key, username varchar(32) not null);

insert into integrate_test.t2 values (0,"mysql");

insert into integrate_test.t2 values (0,"mysql ab");

insert into integrate_test.t2 values (0,"mysql a");

insert into integrate_test.t2 values (0,"r1manic");

insert into integrate_test.t2 values (0,"r1man");

checksum table integrate_test.t1, integrate_test.t2;
Table Checksum
integrate_test.t1 3727148865
integrate_test.t2 92212347


create /*test partition single, Test of refering to old values*/ table integrate_test.t3 (a int not null key) single;

insert into integrate_test.t3 values (1);

insert into integrate_test.t3 values (a+2);

insert into integrate_test.t3 values (a+3),(a+4);

insert into integrate_test.t3 values (5),(a+6);

checksum tables integrate_test.t3;
Table Checksum
integrate_test.t3 1646839619

checksum tables integrate_test.t3 quick;
Table Checksum
integrate_test.t3 NULL

checksum tables integrate_test.t3 extended;
Table Checksum
integrate_test.t3 1646839619


checksum table integrate_test.t1, integrate_test.t2, integrate_test.t3 extended;
Table Checksum
integrate_test.t1 3727148865
integrate_test.t2 92212347
integrate_test.t3 1646839619

checksum table integrate_test.t1, integrate_test.t2, integrate_test.t3 quick;
Table Checksum
integrate_test.t1 NULL
integrate_test.t2 NULL
integrate_test.t3 NULL

checksum table integrate_test.t1, integrate_test.t2, xx.t, integrate_test.t3 quick;
Table Checksum
integrate_test.t1 NULL
integrate_test.t2 NULL
xx.t NULL
integrate_test.t3 NULL

checksum table integrate_test.t1, integrate_test.t2, integrate_test.t3, xx.t extended;
Table Checksum
integrate_test.t1 3727148865
integrate_test.t2 92212347
integrate_test.t3 1646839619
xx.t NULL


drop table integrate_test.t1, integrate_test.t2, integrate_test.t3;

drop database integrate_test;
30 changes: 30 additions & 0 deletions intergration/radon-test/t/checksum.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
create database if not exists integrate_test DEFAULT CHARSET=utf8;
drop table if exists integrate_test.t1,integrate_test.t2;
create /*test partition hash, Test of refering to old values*/ table integrate_test.t1 (a int not null key);
insert into integrate_test.t1 values (1), (6);
checksum table integrate_test.t1;

create /*Test of duplicate key values with packed keys */ table integrate_test.t2 (id bigint not null auto_increment primary key, username varchar(32) not null);
insert into integrate_test.t2 values (0,"mysql");
insert into integrate_test.t2 values (0,"mysql ab");
insert into integrate_test.t2 values (0,"mysql a");
insert into integrate_test.t2 values (0,"r1manic");
insert into integrate_test.t2 values (0,"r1man");
checksum table integrate_test.t1, integrate_test.t2;

create /*test partition single, Test of refering to old values*/ table integrate_test.t3 (a int not null key) single;
insert into integrate_test.t3 values (1);
insert into integrate_test.t3 values (a+2);
insert into integrate_test.t3 values (a+3),(a+4);
insert into integrate_test.t3 values (5),(a+6);
checksum tables integrate_test.t3;
checksum tables integrate_test.t3 quick;
checksum tables integrate_test.t3 extended;

checksum table integrate_test.t1, integrate_test.t2, integrate_test.t3 extended;
checksum table integrate_test.t1, integrate_test.t2, integrate_test.t3 quick;
checksum table integrate_test.t1, integrate_test.t2, xx.t, integrate_test.t3 quick;
checksum table integrate_test.t1, integrate_test.t2, integrate_test.t3, xx.t extended;

drop table integrate_test.t1, integrate_test.t2, integrate_test.t3;
drop database integrate_test;
37 changes: 21 additions & 16 deletions src/planner/others_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"router"
"xcontext"

"github.com/pkg/errors"
"github.com/xelabs/go-mysqlstack/sqlparser"
"github.com/xelabs/go-mysqlstack/sqlparser/depends/common"
"github.com/xelabs/go-mysqlstack/xlog"
Expand Down Expand Up @@ -64,43 +65,47 @@ func NewOthersPlan(log *xlog.Log, database string, query string, node sqlparser.

// Build used to build distributed querys.
func (p *OthersPlan) Build() error {
node := p.node
router := p.router

switch node := node.(type) {
switch node := p.node.(type) {
// Checksum Table.
case *sqlparser.Checksum:
database := p.database
if !node.Table.Qualifier.IsEmpty() {
database = node.Table.Qualifier.String()
newNode := *node
// We`ll rewrite ast on newNode and the table`s format should be like "db.t1", so the "Qualifier" in ast should not be empty.
if newNode.Tables[0].Qualifier.IsEmpty() {
newNode.Tables[0].Qualifier = sqlparser.NewTableIdent(p.database)
}
table := node.Table.Name.String()
route, err := router.TableConfig(database, table)
database := newNode.Tables[0].Qualifier.String()
table := newNode.Tables[0].Name.String()
route, err := p.router.TableConfig(database, table)
if err != nil {
return err
}

// Global table or Single table.
if route.ShardKey == "" {
methodType, err := p.router.PartitionType(database, table)
if err != nil {
return err
}
switch methodType {
case router.MethodTypeGlobal, router.MethodTypeSingle:
segment := route.Partitions[0]
tuple := xcontext.QueryTuple{
Query: p.RawQuery,
Query: sqlparser.String(&newNode),
Backend: segment.Backend,
Range: segment.Segment,
}
p.Querys = append(p.Querys, tuple)
} else {
case router.MethodTypeHash, router.MethodTypeList:
segments := route.Partitions
for _, segment := range segments {
buf := sqlparser.NewTrackedBuffer(nil)
buf.Myprintf("checksum table %s.%s", database, segment.Table)
newNode.Tables[0].Name = sqlparser.NewTableIdent(segment.Table)
tuple := xcontext.QueryTuple{
Query: buf.String(),
Query: sqlparser.String(&newNode),
Backend: segment.Backend,
Range: segment.Segment,
}
p.Querys = append(p.Querys, tuple)
}
default:
return errors.Errorf("unsupported: radon.not.support.method.type[%s].", methodType)
}
}
return nil
Expand Down
54 changes: 52 additions & 2 deletions src/planner/others_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,27 @@ func TestOthersPlanChecksumTable(t *testing.T) {
"RawQuery": "checksum table G",
"Partitions": [
{
"Query": "checksum table G",
"Query": "checksum table sbtest.G",
"Backend": "backend1",
"Range": ""
}
]
}`,
`{
"RawQuery": "checksum table sbtest.S",
"Partitions": [
{
"Query": "checksum table sbtest.S",
"Backend": "backend1",
"Range": ""
}
]
}`,
`{
"RawQuery": "checksum tables A, G, sbtest.S",
"Partitions": [
{
"Query": checksum tables A, G, sbtest.S",
"Backend": "backend1",
"Range": ""
}
Expand All @@ -104,6 +124,7 @@ func TestOthersPlanChecksumTable(t *testing.T) {
"checksum table A",
"checksum table sbtest.A",
"checksum table G",
"checksum table sbtest.S",
}

log := xlog.NewStdLog(xlog.Level(xlog.PANIC))
Expand All @@ -114,7 +135,7 @@ func TestOthersPlanChecksumTable(t *testing.T) {

err := route.CreateDatabase(database)
assert.Nil(t, err)
err = route.AddForTest(database, router.MockTableMConfig(), router.MockTableGConfig())
err = route.AddForTest(database, router.MockTableMConfig(), router.MockTableGConfig(), router.MockTableSConfig())
assert.Nil(t, err)
planTree := NewPlanTree()
for i, query := range querys {
Expand All @@ -137,3 +158,32 @@ func TestOthersPlanChecksumTable(t *testing.T) {
}
}
}

func TestOthersPlanChecksumTableError(t *testing.T) {
querys := []string{
"checksum table A",
"checksum table xx.A",
}

log := xlog.NewStdLog(xlog.Level(xlog.PANIC))
database := "sbtest"

route, cleanup := router.MockNewRouter(log)
defer cleanup()

err := route.CreateDatabase(database)
assert.Nil(t, err)
err = route.AddForTest(database, router.MockTableMConfig(), router.MockTableGConfig(), router.MockTableSConfig())
assert.Nil(t, err)
for _, query := range querys {
node, err := sqlparser.Parse(query)
assert.Nil(t, err)
plan := NewOthersPlan(log, "", query, node, route)

// plan build
{
err := plan.Build()
assert.NotNil(t, err)
}
}
}
Loading

0 comments on commit 07f42c3

Please sign in to comment.