forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhaving.go
99 lines (85 loc) · 2.99 KB
/
having.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
// Copyright 2015 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 rsets
import (
"github.com/juju/errors"
"github.com/pingcap/tidb/context"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/field"
"github.com/pingcap/tidb/plan"
"github.com/pingcap/tidb/plan/plans"
)
var (
_ plan.Planner = (*HavingRset)(nil)
)
// HavingRset is record set for having fields.
type HavingRset struct {
Src plan.Plan
Expr expression.Expression
}
// CheckAndUpdateSelectList checks having fields validity and set hidden fields to selectList.
func (r *HavingRset) CheckAndUpdateSelectList(selectList *plans.SelectList, groupBy []expression.Expression, tableFields []*field.ResultField) error {
if expression.ContainAggregateFunc(r.Expr) {
expr, err := selectList.UpdateAggFields(r.Expr, tableFields)
if err != nil {
return errors.Errorf("%s in 'having clause'", err.Error())
}
r.Expr = expr
} else {
// having can only contain group by column and select list, e.g,
// `select c1 from t group by c2 having c3 > 0` is invalid,
// because c3 is not in group by and select list.
names := expression.MentionedColumns(r.Expr)
for _, name := range names {
found := false
// check name whether in select list.
// notice that `select c1 as c2 from t group by c1, c2, c3 having c2 > c3`,
// will use t.c2 not t.c1 here.
if field.ContainFieldName(name, selectList.ResultFields, field.OrgFieldNameFlag) {
continue
}
if field.ContainFieldName(name, selectList.ResultFields, field.FieldNameFlag) {
if field.ContainFieldName(name, tableFields, field.OrgFieldNameFlag) {
selectList.CloneHiddenField(name, tableFields)
}
continue
}
// check name whether in group by.
// group by must only have column name, e.g,
// `select c1 from t group by c2 having c2 > 0` is valid,
// but `select c1 from t group by c2 + 1 having c2 > 0` is invalid.
for _, by := range groupBy {
if !field.CheckFieldsEqual(name, by.String()) {
continue
}
// if name is not in table fields, it will get an unknown field error in GroupByRset,
// so no need to check return value.
selectList.CloneHiddenField(name, tableFields)
found = true
break
}
if !found {
return errors.Errorf("Unknown column '%s' in 'having clause'", name)
}
}
}
return nil
}
// Plan gets HavingPlan.
func (r *HavingRset) Plan(ctx context.Context) (plan.Plan, error) {
return &plans.HavingPlan{Src: r.Src, Expr: r.Expr}, nil
}
// String implements fmt.Stringer interface.
func (r *HavingRset) String() string {
return r.Expr.String()
}