forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
distinct.go
155 lines (146 loc) · 3.74 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
// 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. See the AUTHORS file
// for names of contributors.
//
// Author: Vivek Menezes (vivek.menezes@gmail.com)
package sql
import (
"bytes"
"strings"
"github.com/cockroachdb/cockroach/sql/parser"
)
// distinct constructs a distinctNode.
func (*planner) distinct(n *parser.Select, p planNode) planNode {
if !n.Distinct {
return p
}
d := &distinctNode{
planNode: p,
suffixSeen: make(map[string]struct{}),
}
ordering, prefix := p.Ordering()
if len(ordering) != 0 {
d.columnsInOrder = make([]bool, len(p.Columns()))
}
for _, p := range ordering {
if p == 0 {
if prefix > 0 {
prefix--
continue
}
break
}
if p < 0 {
p = -p
}
if p <= len(d.columnsInOrder) {
d.columnsInOrder[p-1] = true
} else {
// 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.
d.columnsInOrder = nil
break
}
}
return d
}
type distinctNode struct {
planNode
// 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{}
err error
}
func (n *distinctNode) Next() bool {
if n.err != nil {
return false
}
for n.planNode.Next() {
// Detect duplicates
prefix, suffix := n.encodeValues(n.Values())
if n.err != nil {
return false
}
if bytes.Equal(prefix, n.prefixSeen) {
// 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 {
// duplicate
continue
}
sKey := string(suffix)
if _, ok := n.suffixSeen[sKey]; ok {
// duplicate
continue
}
n.suffixSeen[sKey] = struct{}{}
} else {
// The prefix of the row which is ordered differs from the last row;
// reset our seen set.
if len(n.suffixSeen) > 0 {
n.suffixSeen = make(map[string]struct{})
}
n.prefixSeen = prefix
if suffix != nil {
n.suffixSeen[string(suffix)] = struct{}{}
}
}
return true
}
n.err = n.planNode.Err()
return false
}
func (n *distinctNode) Err() error {
return n.err
}
func (n *distinctNode) encodeValues(values parser.DTuple) ([]byte, []byte) {
var prefix, suffix []byte
for i, val := range values {
if n.columnsInOrder != nil && n.columnsInOrder[i] {
if prefix == nil {
prefix = make([]byte, 0, 100)
}
prefix, n.err = encodeDatum(prefix, val)
} else {
if suffix == nil {
suffix = make([]byte, 0, 100)
}
suffix, n.err = encodeDatum(suffix, val)
}
if n.err != nil {
break
}
}
return prefix, suffix
}
func (n *distinctNode) ExplainPlan() (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)
}
}
description = strings.Join(strs, ",")
}
return "distinct", description, []planNode{n.planNode}
}