-
Notifications
You must be signed in to change notification settings - Fork 5.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor builtin #250
Merged
Merged
refactor builtin #250
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ffb8fdd
expression: move builtin to builtin package, cannot run
siddontang 1a0998b
expression: move case when test from builtin to expression
siddontang 76d1a49
*: rename package name and do some export
siddontang e825e7c
*: use builtin eval key instead
siddontang 2c363b7
builtin: simplify file name
siddontang 85b436c
builtin: Address comment
siddontang 6d5b187
expression: cleanup creating distinct in call
siddontang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// 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 builtin | ||
|
||
import ( | ||
"errors" | ||
|
||
. "github.com/pingcap/check" | ||
) | ||
|
||
func (s *testBuiltinSuite) TestIf(c *C) { | ||
tbl := []struct { | ||
Arg1 interface{} | ||
Arg2 interface{} | ||
Arg3 interface{} | ||
Ret interface{} | ||
}{ | ||
{1, 1, 2, 1}, | ||
{nil, 1, 2, 2}, | ||
{0, 1, 2, 2}, | ||
} | ||
|
||
for _, t := range tbl { | ||
v, err := builtinIf([]interface{}{t.Arg1, t.Arg2, t.Arg3}, nil) | ||
c.Assert(err, IsNil) | ||
c.Assert(v, DeepEquals, t.Ret) | ||
} | ||
|
||
_, err := builtinIf([]interface{}{errors.New("must error"), 1, 2}, nil) | ||
c.Assert(err, NotNil) | ||
} | ||
|
||
func (s *testBuiltinSuite) TestIfNull(c *C) { | ||
tbl := []struct { | ||
Arg1 interface{} | ||
Arg2 interface{} | ||
Ret interface{} | ||
}{ | ||
{1, 2, 1}, | ||
{nil, 2, 2}, | ||
{nil, nil, nil}, | ||
} | ||
|
||
for _, t := range tbl { | ||
v, err := builtinIfNull([]interface{}{t.Arg1, t.Arg2}, nil) | ||
c.Assert(err, IsNil) | ||
c.Assert(v, DeepEquals, t.Ret) | ||
} | ||
} | ||
|
||
func (s *testBuiltinSuite) TestNullIf(c *C) { | ||
tbl := []struct { | ||
Arg1 interface{} | ||
Arg2 interface{} | ||
Ret interface{} | ||
}{ | ||
{1, 1, nil}, | ||
{nil, 2, nil}, | ||
{1, nil, 1}, | ||
{1, 2, 1}, | ||
} | ||
|
||
for _, t := range tbl { | ||
v, err := builtinNullIf([]interface{}{t.Arg1, t.Arg2}, nil) | ||
c.Assert(err, IsNil) | ||
c.Assert(v, DeepEquals, t.Ret) | ||
} | ||
|
||
_, err := builtinNullIf([]interface{}{errors.New("must error"), 1}, nil) | ||
c.Assert(err, NotNil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ | |
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package expression | ||
package builtin | ||
|
||
import ( | ||
"bytes" | ||
|
@@ -30,18 +30,21 @@ import ( | |
|
||
// see https://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html | ||
|
||
type aggregateDistinct struct { | ||
// now we have to use memkv Temp, later may be use map directly | ||
// AggregateDistinct handles distinct data for aggregate function: count, sum, avg, and group_concat. | ||
type AggregateDistinct struct { | ||
// Distinct is a memory key-value map. | ||
// Now we have to use memkv Temp, later may be use map directly | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comment end of . |
||
Distinct memkv.Temp | ||
} | ||
|
||
func (c *Call) createDistinct() *aggregateDistinct { | ||
a := new(aggregateDistinct) | ||
// CreateAggregateDistinct creates a distinct for function f. | ||
func CreateAggregateDistinct(f string, distinct bool) *AggregateDistinct { | ||
a := &AggregateDistinct{} | ||
|
||
switch strings.ToLower(c.F) { | ||
switch strings.ToLower(f) { | ||
case "count", "sum", "avg", "group_concat": | ||
// only these aggregate functions support distinct | ||
if c.Distinct { | ||
if distinct { | ||
a.Distinct, _ = memkv.CreateTemp(true) | ||
} | ||
} | ||
|
@@ -50,7 +53,7 @@ func (c *Call) createDistinct() *aggregateDistinct { | |
} | ||
|
||
// check whether v is distinct or not, return true for distinct | ||
func (a *aggregateDistinct) isDistinct(v ...interface{}) (bool, error) { | ||
func (a *AggregateDistinct) isDistinct(v ...interface{}) (bool, error) { | ||
// no distinct flag | ||
if a.Distinct == nil { | ||
return true, nil | ||
|
@@ -74,7 +77,7 @@ func (a *aggregateDistinct) isDistinct(v ...interface{}) (bool, error) { | |
return true, nil | ||
} | ||
|
||
func (a *aggregateDistinct) clear() { | ||
func (a *AggregateDistinct) clear() { | ||
if a.Distinct == nil { | ||
return | ||
} | ||
|
@@ -86,30 +89,15 @@ func (a *aggregateDistinct) clear() { | |
a.Distinct, _ = memkv.CreateTemp(true) | ||
} | ||
|
||
func getDistinct(ctx map[interface{}]interface{}, fn interface{}) *aggregateDistinct { | ||
c, ok := fn.(*Call) | ||
func getDistinct(ctx map[interface{}]interface{}, fn interface{}) *AggregateDistinct { | ||
v, ok := ctx[ExprAggDistinct] | ||
if !ok { | ||
// if fn is not a Call, maybe error | ||
// but now we just return a dummpy aggregate distinct | ||
return new(aggregateDistinct) | ||
// here maybe an error, but now we just return a dummpy aggregate distinct | ||
return new(AggregateDistinct) | ||
} | ||
|
||
// we may have multi aggregate function in one query | ||
// e.g, select sum(c1) + count(*) from t | ||
// so here we use a map to keep all aggregate disctinct | ||
m := map[interface{}]interface{}{} | ||
if v, ok := ctx[ExprAggDistinct]; ok { | ||
m = v.(map[interface{}]interface{}) | ||
} | ||
|
||
if d, ok := m[c]; ok { | ||
return d.(*aggregateDistinct) | ||
} | ||
|
||
d := c.createDistinct() | ||
m[c] = d | ||
|
||
ctx[ExprAggDistinct] = m | ||
// must be AggregateDistinct | ||
d := v.(*AggregateDistinct) | ||
return d | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$agg0
is not good, rename another value?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is the old code, change it in another PR if possible.