Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into shenli/wrap-data
Browse files Browse the repository at this point in the history
  • Loading branch information
shenli committed Oct 19, 2015
2 parents 9973326 + 06e7e89 commit 3442b77
Show file tree
Hide file tree
Showing 124 changed files with 989 additions and 547 deletions.
2 changes: 1 addition & 1 deletion bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"runtime/debug"

"github.com/ngaut/log"
mysql "github.com/pingcap/tidb/mysqldef"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/util/errors"
"github.com/pingcap/tidb/util/errors2"
)
Expand Down
2 changes: 1 addition & 1 deletion column/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/pingcap/tidb/context"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/model"
mysql "github.com/pingcap/tidb/mysqldef"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/util/types"
)

Expand Down
2 changes: 1 addition & 1 deletion column/column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

. "github.com/pingcap/check"
"github.com/pingcap/tidb/model"
mysql "github.com/pingcap/tidb/mysqldef"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/util/types"
)

Expand Down
2 changes: 1 addition & 1 deletion ddl/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/meta"
"github.com/pingcap/tidb/model"
mysql "github.com/pingcap/tidb/mysqldef"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/parser/coldef"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/table/tables"
Expand Down
2 changes: 1 addition & 1 deletion ddl/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/pingcap/tidb/ddl"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/model"
mysql "github.com/pingcap/tidb/mysqldef"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/stmt"
Expand Down
2 changes: 1 addition & 1 deletion driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"github.com/juju/errors"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/model"
mysql "github.com/pingcap/tidb/mysqldef"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/rset"
"github.com/pingcap/tidb/sessionctx"
qerror "github.com/pingcap/tidb/util/errors"
Expand Down
2 changes: 1 addition & 1 deletion expression/binop.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

"github.com/juju/errors"
"github.com/pingcap/tidb/context"
mysql "github.com/pingcap/tidb/mysqldef"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/parser/opcode"
"github.com/pingcap/tidb/util/types"
)
Expand Down
4 changes: 2 additions & 2 deletions expression/binop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
. "github.com/pingcap/check"
"github.com/pingcap/tidb/expression/builtin"
"github.com/pingcap/tidb/model"
mysql "github.com/pingcap/tidb/mysqldef"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/parser/opcode"
"github.com/pingcap/tidb/util/types"
)
Expand Down Expand Up @@ -186,7 +186,7 @@ func (s *testBinOpSuite) TestIdentRelOp(c *C) {

f := func(name string) *Ident {
return &Ident{
model.NewCIStr(name),
CIStr: model.NewCIStr(name),
}
}

Expand Down
2 changes: 1 addition & 1 deletion expression/builtin/groupby.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

"github.com/juju/errors"
"github.com/pingcap/tidb/kv/memkv"
mysql "github.com/pingcap/tidb/mysqldef"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/util/types"
)

Expand Down
2 changes: 1 addition & 1 deletion expression/builtin/groupby_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ package builtin

import (
. "github.com/pingcap/check"
mysql "github.com/pingcap/tidb/mysqldef"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/util/types"
)

Expand Down
2 changes: 1 addition & 1 deletion expression/builtin/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"time"

. "github.com/pingcap/check"
mysql "github.com/pingcap/tidb/mysqldef"
"github.com/pingcap/tidb/mysql"
)

func (s *testBuiltinSuite) TestLength(c *C) {
Expand Down
2 changes: 1 addition & 1 deletion expression/builtin/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"time"

"github.com/juju/errors"
mysql "github.com/pingcap/tidb/mysqldef"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/util/types"
)

Expand Down
2 changes: 1 addition & 1 deletion expression/builtin/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"time"

. "github.com/pingcap/check"
mysql "github.com/pingcap/tidb/mysqldef"
"github.com/pingcap/tidb/mysql"
)

func (s *testBuiltinSuite) TestDate(c *C) {
Expand Down
2 changes: 1 addition & 1 deletion expression/cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

"github.com/pingcap/tidb/context"

mysql "github.com/pingcap/tidb/mysqldef"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/util/types"
)

Expand Down
2 changes: 1 addition & 1 deletion expression/cast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"errors"

. "github.com/pingcap/check"
mysql "github.com/pingcap/tidb/mysqldef"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/util/charset"
"github.com/pingcap/tidb/util/types"
)
Expand Down
2 changes: 1 addition & 1 deletion expression/date_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

"github.com/juju/errors"
"github.com/pingcap/tidb/context"
mysql "github.com/pingcap/tidb/mysqldef"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/util/types"
)

Expand Down
2 changes: 1 addition & 1 deletion expression/date_add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ package expression

import (
. "github.com/pingcap/check"
mysql "github.com/pingcap/tidb/mysqldef"
"github.com/pingcap/tidb/mysql"
)

var _ = Suite(&testDateAddSuite{})
Expand Down
2 changes: 1 addition & 1 deletion expression/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

"github.com/juju/errors"
"github.com/pingcap/tidb/context"
mysql "github.com/pingcap/tidb/mysqldef"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/util/types"
)

Expand Down
36 changes: 28 additions & 8 deletions expression/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"github.com/pingcap/tidb/context"
"github.com/pingcap/tidb/expression/builtin"
"github.com/pingcap/tidb/model"
mysql "github.com/pingcap/tidb/mysqldef"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/parser/opcode"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/util/types"
Expand All @@ -45,13 +45,15 @@ const (
ExprEvalPositionFunc = "$positionFunc"
// ExprEvalValuesFunc is the key saving a function to retrieve value for column name.
ExprEvalValuesFunc = "$valuesFunc"
// ExprEvalIdentReferFunc is the key saving a function to retrieve value with identifier reference index.
ExprEvalIdentReferFunc = "$identReferFunc"
)

var (
// CurrentTimestamp is the keyword getting default value for datetime and timestamp type.
CurrentTimestamp = "CURRENT_TIMESTAMP"
// CurrentTimeExpr is the expression retireving default value for datetime and timestamp type.
CurrentTimeExpr = &Ident{model.NewCIStr(CurrentTimestamp)}
CurrentTimeExpr = &Ident{CIStr: model.NewCIStr(CurrentTimestamp)}
// ZeroTimestamp shows the zero datetime and timestamp.
ZeroTimestamp = "0000-00-00 00:00:00"
)
Expand Down Expand Up @@ -148,22 +150,40 @@ func newMentionedAggregateFuncsVisitor() *mentionedAggregateFuncsVisitor {
}

func (v *mentionedAggregateFuncsVisitor) VisitCall(c *Call) (Expression, error) {
isAggregate := IsAggregateFunc(c.F)

if isAggregate {
v.exprs = append(v.exprs, c)
}

n := len(v.exprs)
for _, e := range c.Args {
_, err := e.Accept(v)
if err != nil {
return nil, errors.Trace(err)
}
}
f, ok := builtin.Funcs[strings.ToLower(c.F)]
if !ok {
return nil, errors.Errorf("unknown function %s", c.F)
}
if f.IsAggregate {
v.exprs = append(v.exprs, c)

if isAggregate && len(v.exprs) != n {
// aggregate function can't use aggregate function as the arg.
// here means we have aggregate function in arg.
return nil, errors.Errorf("Invalid use of group function")
}

return c, nil
}

// IsAggregateFunc checks whether name is an aggregate function or not.
func IsAggregateFunc(name string) bool {
// TODO: use switch defined aggregate name "sum", "count", etc... directly.
// Maybe we can remove builtin IsAggregate field later.
f, ok := builtin.Funcs[strings.ToLower(name)]
if !ok {
return false
}
return f.IsAggregate
}

// MentionedColumns returns a list of names for Ident expression.
func MentionedColumns(e Expression) []string {
var names []string
Expand Down
17 changes: 13 additions & 4 deletions expression/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

. "github.com/pingcap/check"
"github.com/pingcap/tidb/model"
mysql "github.com/pingcap/tidb/mysqldef"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/parser/opcode"
"github.com/pingcap/tidb/sessionctx/variable"
)
Expand Down Expand Up @@ -46,6 +46,15 @@ func (s *testHelperSuite) TestContainAggFunc(c *C) {
b := ContainAggregateFunc(t.Expr)
c.Assert(b, Equals, t.Expect)
}

expr := &Call{
F: "count",
Args: []Expression{
&Call{F: "count", Args: []Expression{v}},
},
}
_, err := MentionedAggregateFuncs(expr)
c.Assert(err, NotNil)
}

func (s *testHelperSuite) TestMentionedColumns(c *C) {
Expand All @@ -56,7 +65,7 @@ func (s *testHelperSuite) TestMentionedColumns(c *C) {
}{
{Value{1}, 0},
{&BinaryOperation{L: v, R: v}, 0},
{&Ident{model.NewCIStr("id")}, 1},
{&Ident{CIStr: model.NewCIStr("id")}, 1},
{&Call{F: "count", Args: []Expression{v}}, 0},
{&IsNull{Expr: v}, 0},
{&PExpr{Expr: v}, 0},
Expand Down Expand Up @@ -105,7 +114,7 @@ func (s *testHelperSuite) TestBase(c *C) {
{int64(1), int64(1)},
{&UnaryOperation{Op: opcode.Plus, V: Value{1}}, 1},
{&UnaryOperation{Op: opcode.Not, V: Value{1}}, nil},
{&UnaryOperation{Op: opcode.Plus, V: &Ident{model.NewCIStr("id")}}, nil},
{&UnaryOperation{Op: opcode.Plus, V: &Ident{CIStr: model.NewCIStr("id")}}, nil},
{nil, nil},
}

Expand Down Expand Up @@ -227,7 +236,7 @@ func (s *testHelperSuite) TestGetTimeValue(c *C) {
{Value{"2012-13-12 00:00:00"}},
{Value{0}},
{Value{int64(1)}},
{&Ident{model.NewCIStr("xxx")}},
{&Ident{CIStr: model.NewCIStr("xxx")}},
{NewUnaryOperation(opcode.Minus, Value{int64(1)})},
}

Expand Down
21 changes: 21 additions & 0 deletions expression/ident.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,23 @@ var (
_ Expression = (*Ident)(nil)
)

const (
// IdentReferSelectList means the identifier reference is in select list.
IdentReferSelectList = 1
// IdentReferFromTable means the identifier reference is in FROM table.
IdentReferFromTable = 2
)

// Ident is the identifier expression.
type Ident struct {
// model.CIStr contains origin identifier name and its lowercase name.
model.CIStr

// ReferScope means where the identifer reference is, select list or from.
ReferScope int

// ReferIndex is the index to get the identifer data.
ReferIndex int
}

// Clone implements the Expression Clone interface.
Expand Down Expand Up @@ -63,6 +76,14 @@ func (i *Ident) Eval(ctx context.Context, args map[interface{}]interface{}) (v i
return nil, nil
}

// TODO: we will unify ExprEvalIdentReferFunc and ExprEvalIdentFunc later,
// now just put them here for refactor step by step.
if f, ok := args[ExprEvalIdentReferFunc]; ok {
if got, ok := f.(func(string, int, int) (interface{}, error)); ok {
return got(i.L, i.ReferScope, i.ReferIndex)
}
}

if f, ok := args[ExprEvalIdentFunc]; ok {
if got, ok := f.(func(string) (interface{}, error)); ok {
return got(i.L)
Expand Down
12 changes: 11 additions & 1 deletion expression/ident_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type testIdentSuite struct {

func (s *testIdentSuite) TestIdent(c *C) {
e := Ident{
model.NewCIStr("id"),
CIStr: model.NewCIStr("id"),
}

c.Assert(e.IsStatic(), IsFalse)
Expand Down Expand Up @@ -55,4 +55,14 @@ func (s *testIdentSuite) TestIdent(c *C) {
v, err = e.Eval(nil, m)
c.Assert(err, IsNil)
c.Assert(v, Equals, 1)

delete(m, ExprEvalIdentFunc)
e.ReferScope = IdentReferSelectList
e.ReferIndex = 1
m[ExprEvalIdentReferFunc] = func(string, int, int) (interface{}, error) {
return 2, nil
}
v, err = e.Eval(nil, m)
c.Assert(err, IsNil)
c.Assert(v, Equals, 2)
}
2 changes: 1 addition & 1 deletion expression/unary.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/juju/errors"
"github.com/pingcap/tidb/context"

mysql "github.com/pingcap/tidb/mysqldef"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/parser/opcode"
"github.com/pingcap/tidb/util/types"
)
Expand Down
2 changes: 1 addition & 1 deletion expression/unary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

. "github.com/pingcap/check"

mysql "github.com/pingcap/tidb/mysqldef"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/parser/opcode"
"github.com/pingcap/tidb/util/types"
)
Expand Down
2 changes: 1 addition & 1 deletion expression/visitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
. "github.com/pingcap/check"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/model"
mysql "github.com/pingcap/tidb/mysqldef"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/parser/opcode"
"github.com/pingcap/tidb/util/types"
)
Expand Down
2 changes: 1 addition & 1 deletion field/field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
. "github.com/pingcap/check"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/field"
mysql "github.com/pingcap/tidb/mysqldef"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/util/types"
)

Expand Down
Loading

0 comments on commit 3442b77

Please sign in to comment.