-
Notifications
You must be signed in to change notification settings - Fork 92
/
composite.go
166 lines (146 loc) · 4.07 KB
/
composite.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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 dml
import (
"context"
"fmt"
"io"
)
import (
"github.com/pkg/errors"
)
import (
"github.com/arana-db/arana/pkg/dataset"
"github.com/arana-db/arana/pkg/proto"
"github.com/arana-db/arana/pkg/resultx"
"github.com/arana-db/arana/pkg/runtime/plan"
"github.com/arana-db/arana/pkg/util/log"
)
// CompositePlan merges multiple query plan.
type CompositePlan struct {
Plans []proto.Plan
}
func (u CompositePlan) Type() proto.PlanType {
return proto.PlanTypeQuery
}
func (u CompositePlan) ExecIn(ctx context.Context, conn proto.VConn) (proto.Result, error) {
ctx, span := plan.Tracer.Start(ctx, "CompositePlan.ExecIn")
defer span.End()
switch u.Plans[0].Type() {
case proto.PlanTypeQuery:
return u.query(ctx, conn)
case proto.PlanTypeExec:
return u.exec(ctx, conn)
default:
panic("unreachable")
}
}
func (u CompositePlan) showOpenTables(ctx context.Context, conn proto.VConn) (proto.Result, error) {
var (
fields []proto.Field
rows []proto.Row
filterMap = make(map[string]struct{}, 0) // map[database-table]
)
for _, it := range u.Plans {
it := it
res, err := it.ExecIn(ctx, conn)
if err != nil {
return nil, errors.WithStack(err)
}
ds, err := res.Dataset()
if err != nil {
return nil, errors.WithStack(err)
}
fields, err = ds.Fields()
if err != nil {
return nil, errors.WithStack(err)
}
var row proto.Row
for {
row, err = ds.Next()
if errors.Is(err, io.EOF) {
break
}
if err != nil {
return nil, errors.WithStack(err)
}
var values []proto.Value
if err = row.Scan(values); err != nil {
return nil, errors.WithStack(err)
}
// Database Table In_use Name_locked
key := fmt.Sprintf("%s-%s", values[0].String(), values[1].String())
if _, ok := filterMap[key]; ok {
continue
}
filterMap[key] = struct{}{}
rows = append(rows, row)
}
}
ds := &dataset.VirtualDataset{
Columns: fields,
Rows: rows,
}
return resultx.New(resultx.WithDataset(ds)), nil
}
func (u CompositePlan) query(ctx context.Context, conn proto.VConn) (proto.Result, error) {
var generators []dataset.GenerateFunc
for _, it := range u.Plans {
it := it
generators = append(generators, func() (proto.Dataset, error) {
res, err := it.ExecIn(ctx, conn)
if err != nil {
return nil, errors.WithStack(err)
}
return res.Dataset()
})
}
ds, err := dataset.Fuse(generators[0], generators[1:]...)
if err != nil {
log.Errorf("CompositePlan Fuse error:%v", err)
return nil, err
}
return resultx.New(resultx.WithDataset(ds)), nil
}
func (u CompositePlan) exec(ctx context.Context, conn proto.VConn) (proto.Result, error) {
var id, affects uint64
for _, it := range u.Plans {
i, n, err := u.execOne(ctx, conn, it)
if err != nil {
return nil, errors.WithStack(err)
}
affects += n
id += i
}
return resultx.New(resultx.WithLastInsertID(id), resultx.WithRowsAffected(affects)), nil
}
func (u CompositePlan) execOne(ctx context.Context, conn proto.VConn, p proto.Plan) (uint64, uint64, error) {
res, err := p.ExecIn(ctx, conn)
if err != nil {
return 0, 0, errors.WithStack(err)
}
defer resultx.Drain(res)
id, err := res.LastInsertId()
if err != nil {
return 0, 0, errors.WithStack(err)
}
affected, err := res.RowsAffected()
if err != nil {
return 0, 0, errors.WithStack(err)
}
return id, affected, nil
}