-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
expressions: update subquery #110
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,46 +17,72 @@ import ( | |
"fmt" | ||
"strings" | ||
|
||
"github.com/juju/errors" | ||
"github.com/pingcap/tidb/context" | ||
"github.com/pingcap/tidb/expression" | ||
"github.com/pingcap/tidb/plan" | ||
"github.com/pingcap/tidb/stmt" | ||
) | ||
|
||
// SubQueryStatement implements stmt.Statement and plan.Planner interface | ||
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 . |
||
type SubQueryStatement interface { | ||
stmt.Statement | ||
plan.Planner | ||
} | ||
|
||
var _ expression.Expression = (*SubQuery)(nil) | ||
|
||
// SubQuery expresion holds a select statement. | ||
// TODO: complete according to https://dev.mysql.com/doc/refman/5.7/en/subquery-restrictions.html | ||
type SubQuery struct { | ||
// Stmt is the sub select statement. | ||
Stmt stmt.Statement | ||
Stmt SubQueryStatement | ||
// Value holds the sub select result. | ||
Value interface{} | ||
|
||
p plan.Plan | ||
} | ||
|
||
// Clone implements the Expression Clone interface. | ||
func (sq *SubQuery) Clone() (expression.Expression, error) { | ||
// TODO: Statement does not have Clone interface. So we need to check this | ||
nsq := &SubQuery{Stmt: sq.Stmt, Value: sq.Value} | ||
nsq := &SubQuery{Stmt: sq.Stmt, Value: sq.Value, p: sq.p} | ||
return nsq, nil | ||
} | ||
|
||
// Eval implements the Expression Eval interface. | ||
// Eval doesn't support multi rows return, so we can only get a scalar or a row result. | ||
// If you want to get multi rows, use Plan to get a execution plan and run Do() directly. | ||
func (sq *SubQuery) Eval(ctx context.Context, args map[interface{}]interface{}) (v interface{}, err error) { | ||
if sq.Value != nil { | ||
return sq.Value, nil | ||
} | ||
rs, err := sq.Stmt.Exec(ctx) | ||
|
||
p, err := sq.Plan(ctx) | ||
if err != nil { | ||
return nil, err | ||
return nil, errors.Trace(err) | ||
} | ||
// TODO: check row/column number | ||
// Output all the data and let the outer caller check the row/column number | ||
// This simple implementation is used to pass tpc-c | ||
rows, err := rs.Rows(1, 0) | ||
if err != nil || len(rows) == 0 || len(rows[0]) == 0 { | ||
return nil, err | ||
|
||
count := 0 | ||
err = p.Do(ctx, func(id interface{}, data []interface{}) (bool, error) { | ||
if count > 0 { | ||
return false, errors.Errorf("Subquery returns more than 1 row") | ||
} | ||
|
||
if len(p.GetFields()) == 1 { | ||
// a scalar value is a single value | ||
sq.Value = data[0] | ||
} else { | ||
// a row value is []interface{} | ||
sq.Value = data | ||
} | ||
count++ | ||
return true, nil | ||
}) | ||
|
||
if err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
sq.Value = rows[0][0] | ||
|
||
return sq.Value, nil | ||
} | ||
|
||
|
@@ -73,3 +99,23 @@ func (sq *SubQuery) String() string { | |
} | ||
return "" | ||
} | ||
|
||
// ColumnCount returns column count for the sub query. | ||
func (sq *SubQuery) ColumnCount(ctx context.Context) (int, error) { | ||
p, err := sq.Plan(ctx) | ||
if err != nil { | ||
return 0, errors.Trace(err) | ||
} | ||
return len(p.GetFields()), nil | ||
} | ||
|
||
// Plan implements plan.Planner interface. | ||
func (sq *SubQuery) Plan(ctx context.Context) (plan.Plan, error) { | ||
if sq.p != nil { | ||
return sq.p, nil | ||
} | ||
|
||
var err error | ||
sq.p, err = sq.Stmt.Plan(ctx) | ||
return sq.p, errors.Trace(err) | ||
} |
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
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.
Remove this empty line