-
Notifications
You must be signed in to change notification settings - Fork 218
Expand file tree
/
Copy pathothers_plan.go
More file actions
140 lines (119 loc) · 2.78 KB
/
others_plan.go
File metadata and controls
140 lines (119 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* Radon
*
* Copyright 2018-2019 The Radon Authors.
* Code is licensed under the GPLv3.
*
*/
package planner
import (
"encoding/json"
"sort"
"router"
"xcontext"
"github.com/xelabs/go-mysqlstack/sqlparser"
"github.com/xelabs/go-mysqlstack/sqlparser/depends/common"
"github.com/xelabs/go-mysqlstack/xlog"
)
var (
_ Plan = &OthersPlan{}
)
// OthersPlan -- represents a special plan.
type OthersPlan struct {
log *xlog.Log
// router
router *router.Router
// ast
node sqlparser.Statement
// database
database string
// type
typ PlanType
// raw query
RawQuery string
// mode
ReqMode xcontext.RequestMode
// query and backend tuple
Querys []xcontext.QueryTuple
}
// NewOthersPlan -- used to create OthersPlan.
func NewOthersPlan(log *xlog.Log, database string, query string, node sqlparser.Statement, router *router.Router) *OthersPlan {
return &OthersPlan{
log: log,
node: node,
router: router,
database: database,
typ: PlanTypeOthers,
RawQuery: query,
Querys: make([]xcontext.QueryTuple, 0, 16),
}
}
// Build used to build distributed querys.
func (p *OthersPlan) Build() error {
node := p.node
router := p.router
switch node := node.(type) {
// Checksum Table.
case *sqlparser.Checksum:
database := p.database
if !node.Table.Qualifier.IsEmpty() {
database = node.Table.Qualifier.String()
}
table := node.Table.Name.String()
route, err := router.TableConfig(database, table)
if err != nil {
return err
}
// Global table or Single table.
if route.ShardKey == "" {
segment := route.Partitions[0]
tuple := xcontext.QueryTuple{
Query: p.RawQuery,
Backend: segment.Backend,
Range: segment.Segment,
}
p.Querys = append(p.Querys, tuple)
} else {
segments := route.Partitions
for _, segment := range segments {
buf := sqlparser.NewTrackedBuffer(nil)
buf.Myprintf("checksum table %s.%s", database, segment.Table)
tuple := xcontext.QueryTuple{
Query: buf.String(),
Backend: segment.Backend,
Range: segment.Segment,
}
p.Querys = append(p.Querys, tuple)
}
}
}
return nil
}
// Type returns the type of the plan.
func (p *OthersPlan) Type() PlanType {
return p.typ
}
// JSON returns the plan info.
func (p *OthersPlan) JSON() string {
type explain struct {
RawQuery string `json:",omitempty"`
Partitions []xcontext.QueryTuple `json:",omitempty"`
}
var parts []xcontext.QueryTuple
// Sort.
sort.Sort(xcontext.QueryTuples(p.Querys))
parts = append(parts, p.Querys...)
exp := &explain{
RawQuery: p.RawQuery,
Partitions: parts,
}
bout, err := json.MarshalIndent(exp, "", "\t")
if err != nil {
return err.Error()
}
return common.BytesToString(bout)
}
// Size returns the memory size.
func (p *OthersPlan) Size() int {
return 0
}