-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Support last_insert_id()
with argument in SELECT
#17295
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d69df36
feat: make last_insert_id with argument work for sharded plans
systay ef0ce60
feat: make it possible to use the last_insert_id() with argument on u…
systay ab2df6b
codegen
systay 005f9df
added note to release summary
systay 533b258
refactor: removed unneccessary code
systay d9cb7fc
feat: handle last_insert_id with arguments in complex expressions
systay f8cdb7c
comment
systay 00cca4d
test: add tests using aggregation and last_insert_id
systay 70996ad
feat: fail if last_insert_id is used in other contexts that SELECT
systay 0ab3717
feat: keep the last_insert_id function to get the correct types in th…
systay 31af9e0
test: update query to use valid column names
systay 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,125 @@ | ||||||
/* | ||||||
Copyright 2019 The Vitess Authors. | ||||||
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.
Suggested change
|
||||||
|
||||||
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, | ||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||
See the License for the specific language governing permissions and | ||||||
limitations under the License. | ||||||
*/ | ||||||
|
||||||
package engine | ||||||
|
||||||
import ( | ||||||
"context" | ||||||
|
||||||
"vitess.io/vitess/go/sqltypes" | ||||||
querypb "vitess.io/vitess/go/vt/proto/query" | ||||||
"vitess.io/vitess/go/vt/sqlparser" | ||||||
"vitess.io/vitess/go/vt/vtgate/evalengine" | ||||||
) | ||||||
|
||||||
type SaveToSession struct { | ||||||
noTxNeeded | ||||||
|
||||||
Source Primitive | ||||||
Offset evalengine.Expr | ||||||
} | ||||||
|
||||||
var _ Primitive = (*SaveToSession)(nil) | ||||||
|
||||||
func (s *SaveToSession) RouteType() string { | ||||||
return s.Source.RouteType() | ||||||
} | ||||||
|
||||||
func (s *SaveToSession) GetKeyspaceName() string { | ||||||
return s.Source.GetKeyspaceName() | ||||||
} | ||||||
|
||||||
func (s *SaveToSession) GetTableName() string { | ||||||
return s.Source.GetTableName() | ||||||
} | ||||||
|
||||||
func (s *SaveToSession) GetFields(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable) (*sqltypes.Result, error) { | ||||||
return s.Source.GetFields(ctx, vcursor, bindVars) | ||||||
} | ||||||
|
||||||
// TryExecute on SaveToSession will execute the Source and save the last row's value of Offset into the session. | ||||||
func (s *SaveToSession) TryExecute(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool) (*sqltypes.Result, error) { | ||||||
result, err := s.Source.TryExecute(ctx, vcursor, bindVars, wantfields) | ||||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
|
||||||
intEvalResult, ok, err := s.getUintFromOffset(ctx, vcursor, bindVars, result) | ||||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
if ok { | ||||||
vcursor.Session().SetLastInsertID(intEvalResult) | ||||||
} | ||||||
return result, nil | ||||||
} | ||||||
|
||||||
func (s *SaveToSession) getUintFromOffset(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable, result *sqltypes.Result) (uint64, bool, error) { | ||||||
if len(result.Rows) == 0 { | ||||||
return 0, false, nil | ||||||
} | ||||||
|
||||||
env := evalengine.NewExpressionEnv(ctx, bindVars, vcursor) | ||||||
env.Row = result.Rows[len(result.Rows)-1] // last row | ||||||
evalResult, err := env.Evaluate(s.Offset) | ||||||
if err != nil { | ||||||
return 0, false, err | ||||||
} | ||||||
value, err := evalResult.Value(vcursor.ConnCollation()).ToCastUint64() | ||||||
if err != nil { | ||||||
return 0, false, err | ||||||
} | ||||||
return value, true, nil | ||||||
} | ||||||
|
||||||
// TryStreamExecute on SaveToSession will execute the Source and save the last row's value of Offset into the session. | ||||||
func (s *SaveToSession) TryStreamExecute(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool, callback func(*sqltypes.Result) error) error { | ||||||
var value *uint64 | ||||||
|
||||||
f := func(qr *sqltypes.Result) error { | ||||||
v, ok, err := s.getUintFromOffset(ctx, vcursor, bindVars, qr) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
if ok { | ||||||
value = &v | ||||||
} | ||||||
return callback(qr) | ||||||
} | ||||||
|
||||||
err := s.Source.TryStreamExecute(ctx, vcursor, bindVars, wantfields, f) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
if value != nil { | ||||||
vcursor.Session().SetLastInsertID(*value) | ||||||
} | ||||||
|
||||||
return nil | ||||||
} | ||||||
|
||||||
func (s *SaveToSession) Inputs() ([]Primitive, []map[string]any) { | ||||||
return []Primitive{s.Source}, nil | ||||||
} | ||||||
|
||||||
func (s *SaveToSession) description() PrimitiveDescription { | ||||||
return PrimitiveDescription{ | ||||||
OperatorType: "SaveToSession", | ||||||
Other: map[string]interface{}{ | ||||||
"Offset": sqlparser.String(s.Offset), | ||||||
}, | ||||||
} | ||||||
} |
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
Oops, something went wrong.
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.
It would be good to link to the PR and/or Issue so there is more context for those wanting to learn more.