-
Notifications
You must be signed in to change notification settings - Fork 219
Expand file tree
/
Copy pathunion_node.go
More file actions
90 lines (78 loc) · 1.98 KB
/
union_node.go
File metadata and controls
90 lines (78 loc) · 1.98 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
/*
* Radon
*
* Copyright 2019 The Radon Authors.
* Code is licensed under the GPLv3.
*
*/
package planner
import (
"xcontext"
"github.com/xelabs/go-mysqlstack/sqlparser"
"github.com/xelabs/go-mysqlstack/xlog"
)
// UnionNode represents union plan.
type UnionNode struct {
log *xlog.Log
Left, Right PlanNode
// Union Type.
Typ string
children *PlanTree
// referred tables' tableInfo map.
referredTables map[string]*TableInfo
}
func newUnionNode(log *xlog.Log, left, right PlanNode, typ string) *UnionNode {
return &UnionNode{
log: log,
Left: left,
Right: right,
Typ: typ,
children: NewPlanTree(),
}
}
// buildQuery used to build the QueryTuple.
func (u *UnionNode) buildQuery(tbInfos map[string]*TableInfo) {
u.Left.buildQuery(tbInfos)
u.Right.buildQuery(tbInfos)
}
// Children returns the children of the plan.
func (u *UnionNode) Children() *PlanTree {
return u.children
}
// getReferredTables get the referredTables.
func (u *UnionNode) getReferredTables() map[string]*TableInfo {
return u.referredTables
}
// GetQuery used to get the Querys.
func (u *UnionNode) GetQuery() []xcontext.QueryTuple {
querys := u.Left.GetQuery()
querys = append(querys, u.Right.GetQuery()...)
return querys
}
func (u *UnionNode) getFields() []selectTuple {
return u.Left.getFields()
}
// pushOrderBy used to push the order by exprs.
func (u *UnionNode) pushOrderBy(sel sqlparser.SelectStatement) error {
node := sel.(*sqlparser.Union)
if len(node.OrderBy) > 0 {
orderPlan := NewOrderByPlan(u.log, node.OrderBy, u.getFields(), u.referredTables)
if err := orderPlan.Build(); err != nil {
return err
}
u.children.Add(orderPlan)
}
return nil
}
// pushLimit used to push limit.
func (u *UnionNode) pushLimit(sel sqlparser.SelectStatement) error {
node := sel.(*sqlparser.Union)
if node.Limit != nil {
limitPlan := NewLimitPlan(u.log, node.Limit)
if err := limitPlan.Build(); err != nil {
return err
}
u.children.Add(limitPlan)
}
return nil
}