-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathdistinct.go
256 lines (233 loc) · 6.72 KB
/
distinct.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Copyright 2015 The Cockroach 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.
//
// Author: Vivek Menezes ([email protected])
package sql
import (
"bytes"
"fmt"
"strings"
"unsafe"
"github.com/cockroachdb/cockroach/sql/mon"
"github.com/cockroachdb/cockroach/sql/parser"
"github.com/cockroachdb/cockroach/sql/sqlbase"
)
// distinctNode de-duplicates rows returned by a wrapped planNode.
type distinctNode struct {
plan planNode
mon *mon.MemoryUsageMonitor
top *selectTopNode
// All the columns that are part of the Sort. Set to nil if no-sort, or
// sort used an expression that was not part of the requested column set.
columnsInOrder []bool
// Encoding of the columnsInOrder columns for the previous row.
prefixSeen []byte
// Encoding of the non-columnInOrder columns for rows sharing the same
// prefixSeen value.
suffixSeen map[string]struct{}
explain explainMode
debugVals debugValues
allocated int64
}
// distinct constructs a distinctNode.
func (p *planner) Distinct(n *parser.SelectClause) *distinctNode {
if !n.Distinct {
return nil
}
return &distinctNode{mon: &p.mon}
}
// wrap connects the distinctNode to its source planNode.
// invoked by selectTopNode.expandPlan() prior
// to invoking distinctNode.expandPlan() below.
func (n *distinctNode) wrap(plan planNode) planNode {
if n == nil {
return plan
}
n.plan = plan
return n
}
func (n *distinctNode) expandPlan() error {
// At this point the selectTopNode has already expanded the plans
// upstream of distinctNode.
ordering := n.plan.Ordering()
if !ordering.isEmpty() {
n.columnsInOrder = make([]bool, len(n.plan.Columns()))
for colIdx := range ordering.exactMatchCols {
if colIdx >= len(n.columnsInOrder) {
// If the exact-match column is not part of the output, we can safely ignore it.
continue
}
n.columnsInOrder[colIdx] = true
}
for _, c := range ordering.ordering {
if c.ColIdx >= len(n.columnsInOrder) {
// Cannot use sort order. This happens when the
// columns used for sorting are not part of the output.
// e.g. SELECT a FROM t ORDER BY c.
n.columnsInOrder = nil
break
}
n.columnsInOrder[c.ColIdx] = true
}
}
return nil
}
func (n *distinctNode) Start() error {
n.suffixSeen = make(map[string]struct{})
return n.plan.Start()
}
// setTop connects the distinctNode back to the selectTopNode that
// caused its existence. This is needed because the distinctNode needs
// to refer to other nodes in the selectTopNode before its
// expandPlan() method has ran and its child plan is known and
// connected.
func (n *distinctNode) setTop(top *selectTopNode) {
if n != nil {
n.top = top
}
}
func (n *distinctNode) Columns() ResultColumns {
if n.plan != nil {
return n.plan.Columns()
}
// Pre-prepare: not connected yet. Ask the top select node.
return n.top.Columns()
}
func (n *distinctNode) Values() parser.DTuple { return n.plan.Values() }
func (n *distinctNode) Ordering() orderingInfo { return n.plan.Ordering() }
func (n *distinctNode) MarkDebug(mode explainMode) {
if mode != explainDebug {
panic(fmt.Sprintf("unknown debug mode %d", mode))
}
n.explain = mode
n.plan.MarkDebug(mode)
}
func (n *distinctNode) DebugValues() debugValues {
if n.explain != explainDebug {
panic(fmt.Sprintf("node not in debug mode (mode %d)", n.explain))
}
return n.debugVals
}
func (n *distinctNode) addSuffixSeen(sKey string) error {
sz := int64(unsafe.Sizeof(sKey))
if err := n.mon.ReserveMemory(sz); err != nil {
return err
}
n.allocated += sz
n.suffixSeen[sKey] = struct{}{}
return nil
}
func (n *distinctNode) Next() (bool, error) {
for {
next, err := n.plan.Next()
if !next {
return false, err
}
if n.explain == explainDebug {
n.debugVals = n.plan.DebugValues()
if n.debugVals.output != debugValueRow {
// Let the non-row debug values pass through.
return true, nil
}
}
// Detect duplicates
prefix, suffix, err := n.encodeValues(n.Values())
if err != nil {
return false, err
}
if !bytes.Equal(prefix, n.prefixSeen) {
// The prefix of the row which is ordered differs from the last row;
// reset our seen set.
if len(n.suffixSeen) > 0 {
n.mon.ReleaseMemory(n.allocated)
n.allocated = 0
n.suffixSeen = make(map[string]struct{})
}
n.mon.ReleaseMemory(int64(len(n.prefixSeen)))
if err := n.mon.ReserveMemory(int64(len(prefix))); err != nil {
return false, err
}
n.prefixSeen = prefix
if suffix != nil {
if err := n.addSuffixSeen(string(suffix)); err != nil {
return false, err
}
}
return true, nil
}
// The prefix of the row is the same as the last row; check
// to see if the suffix which is not ordered has been seen.
if suffix != nil {
sKey := string(suffix)
if _, ok := n.suffixSeen[sKey]; !ok {
if err := n.addSuffixSeen(sKey); err != nil {
return false, err
}
return true, nil
}
}
// The row is a duplicate
if n.explain == explainDebug {
// Return as a filtered row.
n.debugVals.output = debugValueFiltered
return true, nil
}
}
}
func (n *distinctNode) encodeValues(values parser.DTuple) ([]byte, []byte, error) {
var prefix, suffix []byte
var err error
for i, val := range values {
if n.columnsInOrder != nil && n.columnsInOrder[i] {
if prefix == nil {
prefix = make([]byte, 0, 100)
}
prefix, err = sqlbase.EncodeDatum(prefix, val)
} else {
if suffix == nil {
suffix = make([]byte, 0, 100)
}
suffix, err = sqlbase.EncodeDatum(suffix, val)
}
if err != nil {
break
}
}
return prefix, suffix, err
}
func (n *distinctNode) ExplainPlan(_ bool) (string, string, []planNode) {
var description string
if n.columnsInOrder != nil {
columns := n.Columns()
strs := make([]string, 0, len(columns))
for i, column := range columns {
if n.columnsInOrder[i] {
strs = append(strs, column.Name)
}
}
description = strings.Join(strs, ",")
}
return "distinct", description, []planNode{n.plan}
}
func (n *distinctNode) ExplainTypes(_ func(string, string)) {}
func (n *distinctNode) SetLimitHint(numRows int64, soft bool) {
// Any limit becomes a "soft" limit underneath.
n.plan.SetLimitHint(numRows, true)
}
func (n *distinctNode) Close() {
n.plan.Close()
n.prefixSeen = nil
n.suffixSeen = nil
n.mon.ReleaseMemory(n.allocated)
}