/
subquery_transform.go
210 lines (176 loc) · 5.58 KB
/
subquery_transform.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
/*
Copyright 2022 Huawei Cloud Computing Technologies Co., Ltd.
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 executor
import (
"context"
"errors"
"github.com/openGemini/openGemini/engine/hybridqp"
"github.com/openGemini/openGemini/lib/tracing"
"github.com/openGemini/openGemini/lib/util/lifted/influx/influxql"
"github.com/openGemini/openGemini/lib/util/lifted/influx/query"
)
type SubQueryTransform struct {
BaseProcessor
input *ChunkPort
output *ChunkPort
ops []hybridqp.ExprOptions
opt *query.ProcessorOptions
chunkPool *CircularChunkPool
transparents []func(dst Column, src Column)
mapTransToIn []int
mapTransToName []string
workTracing *tracing.Span
}
func NewSubQueryTransform(inRowDataType hybridqp.RowDataType, outRowDataType hybridqp.RowDataType, ops []hybridqp.ExprOptions, opt *query.ProcessorOptions) *SubQueryTransform {
trans := &SubQueryTransform{
input: NewChunkPort(inRowDataType),
output: NewChunkPort(outRowDataType),
ops: ops,
opt: opt,
transparents: nil,
chunkPool: NewCircularChunkPool(CircularChunkNum*2, NewChunkBuilder(outRowDataType)),
}
err := trans.initTransformMapping()
if err != nil {
panic(err.Error())
}
return trans
}
type SubQueryTransformCreator struct {
}
func (c *SubQueryTransformCreator) Create(plan LogicalPlan, opt *query.ProcessorOptions) (Processor, error) {
p := NewSubQueryTransform(plan.Children()[0].RowDataType(), plan.RowDataType(), plan.RowExprOptions(), opt)
return p, nil
}
var _ = RegistryTransformCreator(&LogicalSubQuery{}, &SubQueryTransformCreator{})
func (trans *SubQueryTransform) initTransformMapping() error {
trans.transparents = make([]func(dst Column, src Column), len(trans.ops))
trans.mapTransToIn = make([]int, len(trans.ops))
trans.mapTransToName = make([]string, len(trans.ops))
for i, op := range trans.ops {
if vr, ok := op.Expr.(*influxql.VarRef); ok {
trans.mapTransToName[i] = vr.Val
trans.mapTransToIn[i] = trans.input.RowDataType.FieldIndex(vr.Val)
switch vr.Type {
case influxql.Integer:
trans.transparents[i] = TransparentForwardIntegerColumn
case influxql.Float:
trans.transparents[i] = TransparentForwardFloatColumn
case influxql.Boolean:
trans.transparents[i] = TransparentForwardBooleanColumn
case influxql.String, influxql.Tag:
trans.transparents[i] = TransparentForwardStringColumn
}
} else {
return errors.New("only varref in subquery transform")
}
}
return nil
}
func (trans *SubQueryTransform) Name() string {
return GetTypeName(trans)
}
func (trans *SubQueryTransform) Explain() []ValuePair {
pairs := make([]ValuePair, 0, len(trans.ops))
for _, option := range trans.ops {
pairs = append(pairs, ValuePair{First: option.Expr.String(), Second: option.Ref.String()})
}
return pairs
}
func (trans *SubQueryTransform) Close() {
trans.output.Close()
}
func (trans *SubQueryTransform) Release() error {
return nil
}
func (trans *SubQueryTransform) Work(ctx context.Context) error {
span := trans.StartSpan("[SubQuery]TotalWorkCost", false)
trans.workTracing = tracing.Start(span, "cost_for_subquery", false)
defer func() {
trans.Close()
tracing.Finish(span, trans.workTracing)
}()
for {
select {
case chunk, ok := <-trans.input.State:
if !ok {
return nil
}
var outputChunk Chunk
tracing.StartPP(span)
tracing.SpanElapsed(trans.workTracing, func() {
outputChunk = trans.transform(chunk)
})
trans.output.State <- outputChunk
tracing.EndPP(span)
case <-ctx.Done():
return nil
}
}
}
func (trans *SubQueryTransform) tagValueFromChunk(chunk Chunk, name string) Column {
tagsLen := len(chunk.TagIndex())
column := NewColumnImpl(influxql.Tag)
if tagsLen > 1 {
for i := 0; i < tagsLen-1; i++ {
begin := chunk.TagIndex()[i]
end := chunk.TagIndex()[i+1]
value, _ := chunk.Tags()[i].GetChunkTagValue(name)
for j := begin; j < end; j++ {
column.AppendStringValue(value)
}
}
}
begin := chunk.TagIndex()[tagsLen-1]
end := chunk.NumberOfRows()
value, ok := chunk.Tags()[tagsLen-1].GetChunkTagValue(name)
if !ok {
column.AppendManyNil(chunk.Len())
return column
}
for j := begin; j < end; j++ {
column.AppendStringValue(value)
}
column.AppendManyNotNil(chunk.Len())
return column
}
func (trans *SubQueryTransform) transform(chunk Chunk) Chunk {
oChunk := trans.chunkPool.GetChunk()
oChunk.SetName(chunk.Name())
oChunk.AppendTimes(chunk.Time())
oChunk.AppendTagsAndIndexes(chunk.Tags(), chunk.TagIndex())
oChunk.AppendIntervalIndexes(chunk.IntervalIndex())
for i, f := range trans.transparents {
dst := oChunk.Column(i)
var src Column
if trans.mapTransToIn[i] == -1 {
src = trans.tagValueFromChunk(chunk, trans.mapTransToName[i])
} else {
src = chunk.Column(trans.mapTransToIn[i])
}
f(dst, src)
}
return oChunk
}
func (trans *SubQueryTransform) GetOutputs() Ports {
return Ports{trans.output}
}
func (trans *SubQueryTransform) GetInputs() Ports {
return Ports{trans.input}
}
func (trans *SubQueryTransform) GetOutputNumber(_ Port) int {
return 0
}
func (trans *SubQueryTransform) GetInputNumber(_ Port) int {
return 0
}