/
logical_plans.go
150 lines (140 loc) · 4.57 KB
/
logical_plans.go
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
141
142
143
144
145
146
147
148
149
150
//
// This source code is a modified form of original source from the TiDB project, which has the following copyright header(s):
//
// Copyright 2016 PingCAP, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package planner
import (
"github.com/pingcap/parser/model"
"github.com/pingcap/parser/mysql"
"github.com/squareup/pranadb/tidb"
"github.com/squareup/pranadb/tidb/expression"
"github.com/squareup/pranadb/tidb/planner/property"
"github.com/squareup/pranadb/tidb/planner/util"
)
var (
_ LogicalPlan = &LogicalJoin{}
_ LogicalPlan = &LogicalAggregation{}
_ LogicalPlan = &LogicalProjection{}
_ LogicalPlan = &LogicalSelection{}
_ LogicalPlan = &LogicalTableDual{}
_ LogicalPlan = &LogicalDataSource{}
_ LogicalPlan = &LogicalTableScan{}
_ LogicalPlan = &LogicalIndexScan{}
_ LogicalPlan = &LogicalUnionAll{}
_ LogicalPlan = &LogicalTopN{}
_ LogicalPlan = &LogicalSort{}
_ LogicalPlan = &LogicalLimit{}
)
// checkIndexCanBeKey checks whether an Index can be a Key in schema.
func checkIndexCanBeKey(idx *model.IndexInfo, columns []*model.ColumnInfo, schema *expression.Schema) (uniqueKey, newKey expression.KeyInfo) {
if !idx.Unique {
return nil, nil
}
newKeyOK := true
uniqueKeyOK := true
for _, idxCol := range idx.Columns {
// The columns of this index should all occur in column schema.
// Since null value could be duplicate in unique key. So we check NotNull flag of every column.
findUniqueKey := false
for i, col := range columns {
if idxCol.Name.L == col.Name.L {
uniqueKey = append(uniqueKey, schema.Columns[i])
findUniqueKey = true
if newKeyOK {
if !mysql.HasNotNullFlag(col.Flag) {
newKeyOK = false
break
}
newKey = append(newKey, schema.Columns[i])
break
}
}
}
if !findUniqueKey {
newKeyOK = false
uniqueKeyOK = false
break
}
}
if newKeyOK {
return nil, newKey
} else if uniqueKeyOK {
return uniqueKey, nil
}
return nil, nil
}
func pruneByItems(old []*util.ByItems) (new []*util.ByItems, parentUsedCols []*expression.Column) {
new = make([]*util.ByItems, 0, len(old))
seen := make(map[string]struct{}, len(old))
for _, byItem := range old {
hash := string(byItem.Expr.HashCode(nil))
_, hashMatch := seen[hash]
seen[hash] = struct{}{}
cols := expression.ExtractColumns(byItem.Expr)
if hashMatch {
// do nothing, should be filtered
} else if len(cols) == 0 {
if !expression.IsRuntimeConstExpr(byItem.Expr) {
new = append(new, byItem)
}
} else if byItem.Expr.GetType().Tp == mysql.TypeNull {
// do nothing, should be filtered
} else {
parentUsedCols = append(parentUsedCols, cols...)
new = append(new, byItem)
}
}
return
}
func (p *baseLogicalPlan) recursiveDeriveStats(colGroups [][]*expression.Column) (*property.StatsInfo, error) {
childStats := make([]*property.StatsInfo, len(p.children))
childSchema := make([]*expression.Schema, len(p.children))
cumColGroups := p.self.ExtractColGroups(colGroups)
for i, child := range p.children {
childProfile, err := child.recursiveDeriveStats(cumColGroups)
if err != nil {
return nil, err
}
childStats[i] = childProfile
childSchema[i] = child.Schema()
}
return p.self.DeriveStats(childStats, p.self.Schema(), childSchema, colGroups)
}
// ExtractColGroups implements LogicalPlan ExtractColGroups interface.
func (p *baseLogicalPlan) ExtractColGroups(_ [][]*expression.Column) [][]*expression.Column {
return nil
}
// DeriveStats implement LogicalPlan DeriveStats interface.
func (p *baseLogicalPlan) DeriveStats(childStats []*property.StatsInfo, selfSchema *expression.Schema, childSchema []*expression.Schema, _ [][]*expression.Column) (*property.StatsInfo, error) {
if len(childStats) == 1 {
p.stats = childStats[0]
return p.stats, nil
}
if len(childStats) > 1 {
err := tidb.ErrInternal.GenWithStack("LogicalPlans with more than one child should implement their own DeriveStats().")
return nil, err
}
if p.stats != nil {
return p.stats, nil
}
profile := &property.StatsInfo{
RowCount: float64(1),
ColNDVs: make(map[int64]float64, selfSchema.Len()),
}
for _, col := range selfSchema.Columns {
profile.ColNDVs[col.UniqueID] = 1
}
p.stats = profile
return profile, nil
}