/
resolve_generators.go
109 lines (92 loc) · 2.91 KB
/
resolve_generators.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
// Copyright 2020-2021 Dolthub, 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 analyzer
import (
"gopkg.in/src-d/go-errors.v1"
"github.com/Ciyfly/FakerMysql/sql"
"github.com/Ciyfly/FakerMysql/sql/expression"
"github.com/Ciyfly/FakerMysql/sql/expression/function"
"github.com/Ciyfly/FakerMysql/sql/plan"
)
var (
errMultipleGenerators = errors.NewKind("there can't be more than 1 instance of EXPLODE in a SELECT")
errExplodeNotArray = errors.NewKind("argument of type %q given to EXPLODE, expecting array")
)
func resolveGenerators(ctx *sql.Context, a *Analyzer, n sql.Node, scope *Scope) (sql.Node, error) {
return plan.TransformUp(n, func(n sql.Node) (sql.Node, error) {
p, ok := n.(*plan.Project)
if !ok {
return n, nil
}
projection := p.Projections
g, err := findGenerator(ctx, projection)
if err != nil {
return nil, err
}
// There might be no generator in the project, in that case we don't
// have to do anything.
if g == nil {
return n, nil
}
projection[g.idx] = g.expr
var name string
if n, ok := g.expr.(sql.Nameable); ok {
name = n.Name()
} else {
name = g.expr.String()
}
return plan.NewGenerate(
plan.NewProject(projection, p.Child),
expression.NewGetField(g.idx, g.expr.Type(), name, g.expr.IsNullable()),
), nil
})
}
type generator struct {
idx int
expr sql.Expression
}
// findGenerator will find in the given projection a generator column. If there
// is no generator, it will return nil.
// If there are is than one generator or the argument to explode is not an
// array it will fail.
// All occurrences of Explode will be replaced with Generate.
func findGenerator(ctx *sql.Context, exprs []sql.Expression) (*generator, error) {
var g = &generator{idx: -1}
for i, e := range exprs {
var found bool
switch e := e.(type) {
case *function.Explode:
found = true
g.expr = function.NewGenerate(e.Child)
case *expression.Alias:
if exp, ok := e.Child.(*function.Explode); ok {
found = true
g.expr = expression.NewAlias(e.Name(), function.NewGenerate(exp.Child))
}
}
if found {
if g.idx >= 0 {
return nil, errMultipleGenerators.New()
}
g.idx = i
if !sql.IsArray(g.expr.Type()) {
return nil, errExplodeNotArray.New(g.expr.Type())
}
}
}
if g.expr == nil {
return nil, nil
}
return g, nil
}