/
func_bitfuncs.go
129 lines (110 loc) · 3.21 KB
/
func_bitfuncs.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
// Copyright 2018 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 aggfuncs
import (
"math"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/util/chunk"
"github.com/pkg/errors"
)
type baseBitAggFunc struct {
baseAggFunc
}
type partialResult4BitFunc = uint64
func (e *baseBitAggFunc) AllocPartialResult() PartialResult {
return PartialResult(new(partialResult4BitFunc))
}
func (e *baseBitAggFunc) ResetPartialResult(pr PartialResult) {
p := (*partialResult4BitFunc)(pr)
*p = 0
}
func (e *baseBitAggFunc) AppendFinalResult2Chunk(sctx sessionctx.Context, pr PartialResult, chk *chunk.Chunk) error {
p := (*partialResult4BitFunc)(pr)
chk.AppendUint64(e.ordinal, *p)
return nil
}
type bitOrUint64 struct {
baseBitAggFunc
}
func (e *bitOrUint64) UpdatePartialResult(sctx sessionctx.Context, rowsInGroup []chunk.Row, pr PartialResult) error {
p := (*partialResult4BitFunc)(pr)
for _, row := range rowsInGroup {
inputValue, isNull, err := e.args[0].EvalInt(sctx, row)
if err != nil {
return errors.Trace(err)
}
if isNull {
continue
}
*p |= uint64(inputValue)
}
return nil
}
func (*bitOrUint64) MergePartialResult(sctx sessionctx.Context, src, dst PartialResult) error {
p1, p2 := (*partialResult4BitFunc)(src), (*partialResult4BitFunc)(dst)
*p2 |= uint64(*p1)
return nil
}
type bitXorUint64 struct {
baseBitAggFunc
}
func (e *bitXorUint64) UpdatePartialResult(sctx sessionctx.Context, rowsInGroup []chunk.Row, pr PartialResult) error {
p := (*partialResult4BitFunc)(pr)
for _, row := range rowsInGroup {
inputValue, isNull, err := e.args[0].EvalInt(sctx, row)
if err != nil {
return errors.Trace(err)
}
if isNull {
continue
}
*p ^= uint64(inputValue)
}
return nil
}
func (*bitXorUint64) MergePartialResult(sctx sessionctx.Context, src, dst PartialResult) error {
p1, p2 := (*partialResult4BitFunc)(src), (*partialResult4BitFunc)(dst)
*p2 ^= uint64(*p1)
return nil
}
type bitAndUint64 struct {
baseBitAggFunc
}
func (e *bitAndUint64) AllocPartialResult() PartialResult {
p := new(partialResult4BitFunc)
*p = math.MaxUint64
return PartialResult(p)
}
func (e *bitAndUint64) ResetPartialResult(pr PartialResult) {
p := (*partialResult4BitFunc)(pr)
*p = math.MaxUint64
}
func (e *bitAndUint64) UpdatePartialResult(sctx sessionctx.Context, rowsInGroup []chunk.Row, pr PartialResult) error {
p := (*partialResult4BitFunc)(pr)
for _, row := range rowsInGroup {
inputValue, isNull, err := e.args[0].EvalInt(sctx, row)
if err != nil {
return errors.Trace(err)
}
if isNull {
continue
}
*p &= uint64(inputValue)
}
return nil
}
func (*bitAndUint64) MergePartialResult(sctx sessionctx.Context, src, dst PartialResult) error {
p1, p2 := (*partialResult4BitFunc)(src), (*partialResult4BitFunc)(dst)
*p2 &= uint64(*p1)
return nil
}