-
Notifications
You must be signed in to change notification settings - Fork 218
Expand file tree
/
Copy pathunion_plan.go
More file actions
129 lines (108 loc) · 2.57 KB
/
union_plan.go
File metadata and controls
129 lines (108 loc) · 2.57 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
/*
* Radon
*
* Copyright 2019 The Radon Authors.
* Code is licensed under the GPLv3.
*
*/
package planner
import (
"encoding/json"
"planner/builder"
"router"
"xcontext"
"github.com/xelabs/go-mysqlstack/sqlparser"
"github.com/xelabs/go-mysqlstack/sqlparser/depends/common"
"github.com/xelabs/go-mysqlstack/xlog"
)
var (
_ Plan = &UnionPlan{}
)
// UnionPlan represents union plan.
type UnionPlan struct {
log *xlog.Log
// router
router *router.Router
// select ast
node *sqlparser.Union
// database
database string
// raw query
RawQuery string
// type
typ PlanType
Root builder.PlanNode
}
// NewUnionPlan used to create SelectPlan.
func NewUnionPlan(log *xlog.Log, database string, query string, node *sqlparser.Union, router *router.Router) *UnionPlan {
return &UnionPlan{
log: log,
node: node,
router: router,
database: database,
RawQuery: query,
typ: PlanTypeUnion,
}
}
// Build used to build distributed querys.
func (p *UnionPlan) Build() error {
var err error
p.Root, err = builder.BuildNode(p.log, p.router, p.database, p.node)
return err
}
// Type returns the type of the plan.
func (p *UnionPlan) Type() PlanType {
return p.typ
}
// JSON returns the plan info.
func (p *UnionPlan) JSON() string {
type limit struct {
Offset int
Limit int
}
type explain struct {
RawQuery string `json:",omitempty"`
Project string `json:",omitempty"`
Partitions []xcontext.QueryTuple `json:",omitempty"`
UnionType *string `json:",omitempty"`
GatherMerge []string `json:",omitempty"`
Limit *limit `json:",omitempty"`
}
// Union.
var uni *string
if u, ok := p.Root.(*builder.UnionNode); ok {
uni = &u.Typ
}
var gatherMerge []string
var lim *limit
for _, sub := range p.Root.Children() {
switch sub.Type() {
case builder.ChildTypeOrderby:
plan := sub.(*builder.OrderByPlan)
for _, order := range plan.OrderBys {
field := order.Field
gatherMerge = append(gatherMerge, field)
}
case builder.ChildTypeLimit:
plan := sub.(*builder.LimitPlan)
lim = &limit{Offset: plan.Offset, Limit: plan.Limit}
}
}
exp := &explain{Project: builder.GetProject(p.Root),
RawQuery: p.RawQuery,
Partitions: p.Root.GetQuery(),
UnionType: uni,
GatherMerge: gatherMerge,
Limit: lim,
}
bout, err := json.MarshalIndent(exp, "", "\t")
if err != nil {
return err.Error()
}
return common.BytesToString(bout)
}
// Size returns the memory size.
func (p *UnionPlan) Size() int {
size := len(p.RawQuery)
return size
}