-
Notifications
You must be signed in to change notification settings - Fork 305
fix variables #26762
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
Open
daviszhen
wants to merge
19
commits into
matrixorigin:main
Choose a base branch
from
daviszhen:0806-fix-var
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
fix variables #26762
Changes from 3 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
05f3738
update
daviszhen b772308
update
daviszhen 2b290fc
Merge branch 'main' into 0806-fix-var
mergify[bot] c3c8b54
update
daviszhen 25852b3
Merge branch 'main' into 0806-fix-var
daviszhen 34c7e26
update
daviszhen 6834ccc
Merge branch '0806-fix-var' of https://github.com/daviszhen/matrixone…
daviszhen 4dbea68
Merge branch 'main' into 0806-fix-var
daviszhen 113913c
update
daviszhen 8fe4e90
Merge branch 'main' into 0806-fix-var
daviszhen f48800d
update
daviszhen 73c77ff
update
daviszhen d58e101
Merge branch 'main' into 0806-fix-var
daviszhen ce302fc
Merge branch 'main' into 0806-fix-var
daviszhen 7299022
update
daviszhen 8702931
Merge branch 'main' into 0806-fix-var
daviszhen 1e7f625
update
daviszhen 5d1312f
update
daviszhen 9ee65d2
Merge branch 'main' of https://github.com/matrixorigin/matrixone into…
daviszhen 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,96 @@ | ||
| // Copyright 2026 Matrix Origin | ||
| // | ||
| // 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 frontend | ||
|
|
||
| import ( | ||
| "context" | ||
| "sync" | ||
|
|
||
| "github.com/matrixorigin/matrixone/pkg/common/moerr" | ||
| "github.com/matrixorigin/matrixone/pkg/container/batch" | ||
| "github.com/matrixorigin/matrixone/pkg/perfcounter" | ||
| "github.com/matrixorigin/matrixone/pkg/sql/parsers/tree" | ||
| ) | ||
|
|
||
| // selectIntoUserVariables collects the result of SELECT ... INTO @var. The | ||
| // output callback can receive several batches, so assignments are delayed | ||
| // until the complete result is known. This also keeps existing variables | ||
| // unchanged for a zero-row query, as MySQL does. | ||
| type selectIntoUserVariables struct { | ||
| mu sync.Mutex | ||
| vars []*tree.VarExpr | ||
| row []any | ||
| rowCount uint64 | ||
| } | ||
|
|
||
| func newSelectIntoUserVariables(vars []*tree.VarExpr) *selectIntoUserVariables { | ||
| return &selectIntoUserVariables{vars: vars} | ||
| } | ||
|
|
||
| func selectIntoUserVariablesOutputCallback( | ||
| execCtx *ExecCtx, | ||
| ses FeSession, | ||
| stmt tree.Statement, | ||
| fill func(*batch.Batch, *perfcounter.CounterSet) error, | ||
| ) func(*batch.Batch, *perfcounter.CounterSet) error { | ||
| selectStmt, ok := stmt.(*tree.Select) | ||
| if !ok || len(selectStmt.IntoVars) == 0 { | ||
| return fill | ||
| } | ||
| if execCtx.selectInto == nil { | ||
| execCtx.selectInto = newSelectIntoUserVariables(selectStmt.IntoVars) | ||
| } | ||
| return func(bat *batch.Batch, _ *perfcounter.CounterSet) error { | ||
| return execCtx.selectInto.capture(execCtx.reqCtx, ses, bat) | ||
| } | ||
| } | ||
|
|
||
| func (collector *selectIntoUserVariables) capture(ctx context.Context, ses FeSession, bat *batch.Batch) error { | ||
| if bat == nil || bat.RowCount() == 0 { | ||
| return nil | ||
| } | ||
| if len(bat.Vecs) != len(collector.vars) { | ||
| return moerr.NewInvalidInputf(ctx, | ||
| "SELECT INTO has %d expressions for %d user variables", len(bat.Vecs), len(collector.vars)) | ||
| } | ||
|
|
||
| collector.mu.Lock() | ||
| defer collector.mu.Unlock() | ||
| if collector.rowCount == 0 { | ||
| collector.row = make([]any, len(collector.vars)) | ||
| if err := extractRowFromEveryVector(ctx, ses, bat, 0, collector.row, false); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| collector.rowCount += uint64(bat.RowCount()) | ||
|
daviszhen marked this conversation as resolved.
|
||
| return nil | ||
| } | ||
|
|
||
| func (collector *selectIntoUserVariables) apply(ctx context.Context, ses FeSession, sql string) error { | ||
| collector.mu.Lock() | ||
| defer collector.mu.Unlock() | ||
| if collector.rowCount == 0 { | ||
|
daviszhen marked this conversation as resolved.
|
||
| return nil | ||
| } | ||
| if collector.rowCount > 1 { | ||
| return moerr.NewTooManyRows(ctx) | ||
| } | ||
| for i, variable := range collector.vars { | ||
| if err := ses.SetUserDefinedVar(variable.Name, collector.row[i], sql); err != nil { | ||
|
daviszhen marked this conversation as resolved.
Outdated
|
||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
This file contains hidden or 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,71 @@ | ||
| // Copyright 2026 Matrix Origin | ||
| // | ||
| // 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 frontend | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/matrixorigin/matrixone/pkg/common/moerr" | ||
| "github.com/matrixorigin/matrixone/pkg/common/mpool" | ||
| "github.com/matrixorigin/matrixone/pkg/container/batch" | ||
| "github.com/matrixorigin/matrixone/pkg/container/types" | ||
| "github.com/matrixorigin/matrixone/pkg/container/vector" | ||
| "github.com/matrixorigin/matrixone/pkg/sql/parsers/tree" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestSelectIntoUserVariablesCapturesAndAssignsOneRow(t *testing.T) { | ||
| ctx := context.Background() | ||
| mp := mpool.MustNewZero() | ||
| defer mpool.DeleteMPool(mp) | ||
| ses := &Session{userDefinedVars: make(map[string]*UserDefinedVar)} | ||
| collector := newSelectIntoUserVariables([]*tree.VarExpr{{Name: "out"}}) | ||
|
|
||
| bat := batch.NewWithSize(1) | ||
| bat.Vecs[0] = vector.NewVec(types.T_int64.ToType()) | ||
| require.NoError(t, vector.AppendFixed(bat.Vecs[0], int64(5), false, mp)) | ||
| bat.SetRowCount(1) | ||
| defer bat.Clean(mp) | ||
|
|
||
| require.NoError(t, collector.capture(ctx, ses, bat)) | ||
| require.NoError(t, collector.apply(ctx, ses, "select abs(-5) into @out")) | ||
| variable, err := ses.GetUserDefinedVar("OUT") | ||
| require.NoError(t, err) | ||
| require.Equal(t, int64(5), variable.Value) | ||
| require.Equal(t, "select abs(-5) into @out", variable.Sql) | ||
| } | ||
|
|
||
| func TestSelectIntoUserVariablesZeroOrManyRowsDoNotAssign(t *testing.T) { | ||
| ctx := context.Background() | ||
| ses := &Session{userDefinedVars: make(map[string]*UserDefinedVar)} | ||
| require.NoError(t, ses.SetUserDefinedVar("out", "old", "set @out = 'old'")) | ||
|
|
||
| zeroRows := newSelectIntoUserVariables([]*tree.VarExpr{{Name: "out"}}) | ||
| require.NoError(t, zeroRows.apply(ctx, ses, "select value into @out from empty_table")) | ||
| variable, err := ses.GetUserDefinedVar("out") | ||
| require.NoError(t, err) | ||
| require.Equal(t, "old", variable.Value) | ||
|
|
||
| manyRows := newSelectIntoUserVariables([]*tree.VarExpr{{Name: "out"}}) | ||
| manyRows.row = []any{"new"} | ||
| manyRows.rowCount = 2 | ||
| err = manyRows.apply(ctx, ses, "select value into @out from two_rows") | ||
| require.ErrorContains(t, err, "Result consisted of more than one row") | ||
| require.True(t, moerr.IsMoErrCode(err, moerr.ErrTooManyRows)) | ||
| variable, err = ses.GetUserDefinedVar("out") | ||
| require.NoError(t, err) | ||
| require.Equal(t, "old", variable.Value) | ||
| } |
This file contains hidden or 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 hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.