[WIP] Simple table level sharding implementation by auxten · Pull Request #129 · CovenantSQL/CovenantSQL · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
e6ff8a0
Shard framework can work now fill in the logic latter
Nov 22, 2018
a1c02d9
Add shard plan
Nov 25, 2018
4111d0e
Sharding driver INSERT complete
Nov 27, 2018
3b242f0
Recover xenomint/sqlite/sqlite.go
Nov 27, 2018
46884ce
Simplify buildInsertPlan
Nov 27, 2018
2b0b704
Fix test case for shard
Nov 27, 2018
8fa4453
Fix named args in shard driver
Nov 27, 2018
382fe43
Make Primitive.ExecContext args with a sql.Tx
Nov 28, 2018
29c8f71
Add separate func ShardingConn.splitQueryArgs
Nov 28, 2018
42c0e9a
Add complex select for test
Nov 28, 2018
e29769e
Add doc.go to todo list
Dec 1, 2018
4d7a05a
Sharding supports INSERT, simple SELECT.
Dec 1, 2018
e928daf
Merge remote-tracking branch 'origin/develop' into feature/shard
Dec 1, 2018
c911c3c
Add test cases, fix bugs
Dec 3, 2018
6caa75f
Disable usage of github.com/y0ssar1an/q for debug
Dec 3, 2018
0d84304
Merge branch 'feature/cancelQuery' into feature/shard
Dec 5, 2018
a9d67a3
Shard driver supports UPDATE and DELETE
Dec 6, 2018
d813032
Merge remote-tracking branch 'origin/develop' into feature/shard
Dec 6, 2018
c5f737e
Mock query context for db_test.go
Dec 7, 2018
fbe4d42
Move context to the first arg
Dec 7, 2018
66da2ef
Merge remote-tracking branch 'origin/develop' into feature/shard
Dec 14, 2018
dfe8701
Add more test cases for shard
Dec 14, 2018
7e9fe7c
Format code
Dec 17, 2018
e51b713
Forbid sub query and unsupported funcs
Dec 17, 2018
c8a7c4e
Remove redundant mutex init
Dec 17, 2018
dad7354
Fix miss used ctx.Err()
Dec 17, 2018
fe7ed10
Add test case "select id, count(id), *, sum(id) from foo"
Dec 18, 2018
264cc99
Merge remote-tracking branch 'origin/develop' into feature/shard
Dec 24, 2018
503c530
Merge branch 'develop' into feature/shard
auxten Dec 24, 2018
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
5 changes: 3 additions & 2 deletions blockproducer/metastate.go
1 change: 1 addition & 0 deletions client/_example/gdpaverage.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
defer rows.Close()
log.Printf("\nExec:\n %s\n", Q)
log.Println("ID Name CountryCode District Population")
var ID, GDP, Population int
Expand Down
120 changes: 120 additions & 0 deletions shard/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Copyright 2018 The CovenantSQL Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package shard

import (
"context"
"database/sql"
"database/sql/driver"
"sync"

"github.com/CovenantSQL/CovenantSQL/utils/log"
"github.com/CovenantSQL/sqlparser"
"github.com/pkg/errors"
)

// buildDeletePlan builds the route for an DELETE statement.
func buildDeletePlan(query string,
del *sqlparser.Delete,
args []driver.NamedValue,
c *ShardingConn,
) (instructions Primitive, err error) {
log.Debugf("buildDeletePlan got %s\n delete:%#v\n args: %#v", query, del, args)
if del.OrderBy != nil || del.Limit != nil {
return nil, errors.New("unsupported: OrderBy or LIMIT in DELETE")
}

if len(del.TableExprs) == 1 {
var (
tableExpr *sqlparser.AliasedTableExpr
simpleTableExpr sqlparser.SimpleTableExpr
originTableName sqlparser.TableIdent
ok bool
)
if tableExpr, ok = del.TableExprs[0].(*sqlparser.AliasedTableExpr); !ok {
return nil, errors.New("unsupported: FROM table type")
}
if simpleTableExpr, ok = tableExpr.Expr.(sqlparser.SimpleTableExpr); !ok {
return nil, errors.New("unsupported: FROM table type")
}
originTableName = sqlparser.GetTableName(simpleTableExpr)

if conf, ok := c.conf[originTableName.CompliantName()]; ok {
if !conf.ShardColName.IsEmpty() && conf.ShardInterval > 0 {

// split delete to shard tables
var shards []string
shards, err = c.getTableShards(originTableName.CompliantName())
delInstructions := &Delete{
Instructions: make([]*SinglePrimitive, len(shards)),
}

originFrom := del.TableExprs[0]
//TODO(auxten) deep copy a new delete is better
for i, shard := range shards {
del.TableExprs[0] = &sqlparser.AliasedTableExpr{
Expr: sqlparser.TableName{
Name: sqlparser.NewTableIdent(shard),
Qualifier: sqlparser.TableIdent{},
},
}

buf := sqlparser.NewTrackedBuffer(nil)
del.Format(buf)
//FIXME(auxten) just use the same delete in shard table for now
fixedArgs := toNamedArgs(args)
delInstructions.Instructions[i] = &SinglePrimitive{
query: buf.String(),
namedArgs: fixedArgs,
rawDB: c.rawDB,
}
}
del.TableExprs[0] = originFrom
return delInstructions, nil

} else {
return nil, errors.Errorf("sharding conf set but not configured: %#v", conf)
}
} else {
// not sharding table
}
} else {
return nil, errors.Errorf("delete target must be 1: %s", query)
}

return &BasePrimitive{
query: query,
args: args,
rawConn: c.rawConn,
rawDB: c.rawDB,
}, nil
}

type Delete struct {
sync.Mutex
Instructions []*SinglePrimitive
}

func (del *Delete) ExecContext(ctx context.Context, tx *sql.Tx) (driver.Result, error) {
del.Lock()
defer del.Unlock()
return execInstructionsTx(ctx, del.Instructions)
}

func (del *Delete) QueryContext(ctx context.Context) (driver.Rows, error) {
panic("should not call query in delete")
}
30 changes: 30 additions & 0 deletions shard/doc.go
Loading