Dual is a dummy table, `select 1, 2` is equivalent to `select 1, 2 from dual`, and dual is widely known.
92 lines
2.5 KiB
Go
92 lines
2.5 KiB
Go
// Copyright 2014 The ql Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSES/QL-LICENSE file.
|
|
|
|
// 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 (
|
|
"strings"
|
|
|
|
"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 = (*SelectFieldsRset)(nil)
|
|
_ plan.Planner = (*SelectFromDualRset)(nil)
|
|
)
|
|
|
|
// SelectFieldsRset is record set to select fields.
|
|
type SelectFieldsRset struct {
|
|
Src plan.Plan
|
|
SelectList *plans.SelectList
|
|
}
|
|
|
|
// Plan gets SrcPlan/SelectFieldsDefaultPlan.
|
|
// If all fields are equal to src plan fields, then gets SrcPlan.
|
|
// Default gets SelectFieldsDefaultPlan.
|
|
func (r *SelectFieldsRset) Plan(ctx context.Context) (plan.Plan, error) {
|
|
fields := r.SelectList.Fields
|
|
srcFields := r.Src.GetFields()
|
|
if len(fields) == len(srcFields) {
|
|
match := true
|
|
for i, v := range fields {
|
|
// TODO: is it this check enough? e.g, the ident field is t.c.
|
|
if x, ok := v.Expr.(*expression.Ident); ok && strings.EqualFold(x.L, srcFields[i].Name) && strings.EqualFold(v.Name, srcFields[i].Name) {
|
|
continue
|
|
}
|
|
|
|
match = false
|
|
break
|
|
}
|
|
|
|
if match {
|
|
return r.Src, nil
|
|
}
|
|
}
|
|
|
|
src := r.Src
|
|
if x, ok := src.(*plans.TableDefaultPlan); ok {
|
|
// check whether src plan will be set TableNilPlan, like `select 1, 2 from t`.
|
|
isConst := true
|
|
for _, v := range fields {
|
|
if expression.FastEval(v.Expr) == nil {
|
|
isConst = false
|
|
break
|
|
}
|
|
}
|
|
if isConst {
|
|
src = &plans.TableNilPlan{T: x.T}
|
|
}
|
|
}
|
|
|
|
p := &plans.SelectFieldsDefaultPlan{Src: src, SelectList: r.SelectList}
|
|
return p, nil
|
|
}
|
|
|
|
// SelectFromDualRset is Recordset for select from dual, like `select 1, 1+1` or `select 1 from dual`.
|
|
type SelectFromDualRset struct {
|
|
Fields []*field.Field
|
|
}
|
|
|
|
// Plan gets SelectExprPlan.
|
|
func (r *SelectFromDualRset) Plan(ctx context.Context) (plan.Plan, error) {
|
|
return &plans.SelectFromDualPlan{Fields: r.Fields}, nil
|
|
}
|