forked from couchbaselabs/tuqtng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
order.go
146 lines (128 loc) · 4.08 KB
/
order.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
// Copyright (c) 2013 Couchbase, 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, 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.
package xpipeline
import (
"sort"
"github.com/couchbaselabs/clog"
"github.com/couchbaselabs/dparval"
"github.com/couchbaselabs/tuqtng/ast"
"github.com/couchbaselabs/tuqtng/misc"
"github.com/couchbaselabs/tuqtng/network"
)
type Order struct {
Base *BaseOperator
OrderBy []*ast.SortExpression
buffer dparval.ValueCollection
explicitAliases []string
}
func NewOrder(orderBy []*ast.SortExpression, explicitAliases []string) *Order {
return &Order{
Base: NewBaseOperator(),
OrderBy: orderBy,
buffer: make(dparval.ValueCollection, 0),
explicitAliases: explicitAliases,
}
}
func (this *Order) SetSource(source Operator) {
this.Base.SetSource(source)
}
func (this *Order) GetChannels() (dparval.ValueChannel, PipelineSupportChannel) {
return this.Base.GetChannels()
}
func (this *Order) Run(stopChannel misc.StopChannel) {
clog.To(CHANNEL, "order operator starting")
this.Base.RunOperator(this, stopChannel)
clog.To(CHANNEL, "order operator finished")
}
func (this *Order) processItem(item *dparval.Value) bool {
if this.explicitAliases != nil {
projection := item.GetAttachment("projection")
projectionValue, ok := projection.(*dparval.Value)
if ok {
for _, explicitAlias := range this.explicitAliases {
// put the explicit alias values from the projection into the item
aliasedProjectedValue, err := projectionValue.Path(explicitAlias)
if err == nil {
item.SetPath(explicitAlias, aliasedProjectedValue)
}
}
}
}
this.buffer = append(this.buffer, item)
return true
}
func (this *Order) afterItems() {
// sort
sort.Sort(this)
// write the output
for _, item := range this.buffer {
this.Base.SendItem(item)
}
}
func (this *Order) SetQuery(q network.Query) {
this.Base.SetQuery(q)
}
// sort.Interface interface
func (this *Order) Len() int { return len(this.buffer) }
func (this *Order) Swap(i, j int) { this.buffer[i], this.buffer[j] = this.buffer[j], this.buffer[i] }
func (this *Order) Less(i, j int) bool {
left := this.buffer[i]
right := this.buffer[j]
for _, oe := range this.OrderBy {
leftVal, lerr := this.Base.Evaluate(oe.Expr, left)
if lerr != nil {
switch lerr := lerr.(type) {
case *dparval.Undefined:
default:
clog.Error(lerr)
return false
}
}
rightVal, rerr := this.Base.Evaluate(oe.Expr, right)
if rerr != nil {
switch rerr := rerr.(type) {
case *dparval.Undefined:
default:
clog.Error(rerr)
return false
}
}
// at this point, the only errors left should be MISSING/UNDEFINED
if oe.Ascending && lerr != nil && rerr == nil {
// ascending, left missing, right not, left is less
return true
} else if !oe.Ascending && rerr != nil && lerr == nil {
// descending right missing, left not, left is more
return true
} else if !oe.Ascending && lerr != nil && rerr == nil {
// descending, left missing, right not, left is less
return false
} else if oe.Ascending && rerr != nil && lerr == nil {
//ascending, left not, left is more
return false
} else if lerr == nil && rerr == nil {
lv := leftVal.Value()
rv := rightVal.Value()
// both not missing, compare values
result := ast.CollateJSON(lv, rv)
if result != 0 {
if oe.Ascending && result < 0 {
return true
} else if !oe.Ascending && result > 0 {
return true
} else {
return false
}
}
}
// at this level they are the same, keep going
}
// if we go to this point the order expressions could not differentiate between the elements
return false
}