-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve sharded query routing for tuple list (#14892)
Signed-off-by: Harshit Gangal <[email protected]>
- Loading branch information
1 parent
c534201
commit 42afc72
Showing
18 changed files
with
727 additions
and
48 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
Copyright 2024 The Vitess Authors. | ||
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 evalengine | ||
|
||
import ( | ||
"errors" | ||
|
||
"vitess.io/vitess/go/mysql/collations" | ||
"vitess.io/vitess/go/sqltypes" | ||
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc" | ||
"vitess.io/vitess/go/vt/vterrors" | ||
) | ||
|
||
type ( | ||
TupleBindVariable struct { | ||
Key string | ||
|
||
Index int | ||
Type sqltypes.Type | ||
Collation collations.ID | ||
|
||
// dynamicTypeOffset is set when the type of this bind variable cannot be calculated | ||
// at translation time. Since expressions with dynamic types cannot be compiled ahead of time, | ||
// compilation will be delayed until the expression is first executed with the bind variables | ||
// sent by the user. See: UntypedExpr | ||
dynamicTypeOffset int | ||
} | ||
) | ||
|
||
var _ IR = (*TupleBindVariable)(nil) | ||
var _ Expr = (*TupleBindVariable)(nil) | ||
|
||
func (bv *TupleBindVariable) IR() IR { | ||
return bv | ||
} | ||
|
||
func (bv *TupleBindVariable) IsExpr() {} | ||
|
||
// eval implements the expression interface | ||
func (bv *TupleBindVariable) eval(env *ExpressionEnv) (eval, error) { | ||
bvar, err := env.lookupBindVar(bv.Key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if bvar.Type != sqltypes.Tuple { | ||
return nil, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "query argument '%s' must be a tuple (is %s)", bv.Key, bvar.Type.String()) | ||
} | ||
|
||
tuple := make([]eval, 0, len(bvar.Values)) | ||
for _, value := range bvar.Values { | ||
if value.Type != sqltypes.Tuple { | ||
return nil, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "result value must be a tuple (is %s)", value.Type.String()) | ||
} | ||
sValue := sqltypes.ProtoToValue(value) | ||
var evalErr error | ||
idx := 0 | ||
found := false | ||
// looking for a single index on each Tuple Value. | ||
loopErr := sValue.ForEachValue(func(val sqltypes.Value) { | ||
if found || idx != bv.Index { | ||
idx++ | ||
return | ||
} | ||
found = true | ||
e, err := valueToEval(val, typedCoercionCollation(val.Type(), collations.CollationForType(val.Type(), bv.Collation))) | ||
if err != nil { | ||
evalErr = err | ||
return | ||
} | ||
tuple = append(tuple, e) | ||
|
||
}) | ||
if err = errors.Join(loopErr, evalErr); err != nil { | ||
return nil, err | ||
} | ||
if !found { | ||
return nil, vterrors.VT13001("value not found in the bind variable") | ||
} | ||
} | ||
return &evalTuple{t: tuple}, nil | ||
} | ||
|
||
// typeof implements the expression interface | ||
func (bv *TupleBindVariable) typeof(env *ExpressionEnv) (ctype, error) { | ||
_, err := env.lookupBindVar(bv.Key) | ||
if err != nil { | ||
return ctype{}, err | ||
} | ||
|
||
return ctype{Type: sqltypes.Tuple}, nil | ||
} | ||
|
||
func (bv *TupleBindVariable) compile(c *compiler) (ctype, error) { | ||
return ctype{}, c.unsupported(bv) | ||
} |
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,156 @@ | ||
/* | ||
Copyright 2024 The Vitess Authors. | ||
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 evalengine | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"vitess.io/vitess/go/mysql/collations" | ||
"vitess.io/vitess/go/sqltypes" | ||
querypb "vitess.io/vitess/go/vt/proto/query" | ||
) | ||
|
||
// TestTupleBindVarEval tests TupleBindVariable eval function. | ||
func TestTupleBindVarEval(t *testing.T) { | ||
key := "vals" | ||
c := &TupleBindVariable{ | ||
Key: key, | ||
Index: 1, | ||
} | ||
collation := collations.TypedCollation{ | ||
Coercibility: collations.CoerceCoercible, | ||
Repertoire: collations.RepertoireUnicode, | ||
} | ||
|
||
tcases := []struct { | ||
tName string | ||
bv *querypb.BindVariable | ||
|
||
expEval []eval | ||
expErr string | ||
}{{ | ||
tName: "bind variable not provided", | ||
expErr: "query arguments missing for vals", | ||
}, { | ||
tName: "bind variable provided - wrong type", | ||
bv: sqltypes.Int64BindVariable(1), | ||
expErr: "query argument 'vals' must be a tuple (is INT64)", | ||
}, { | ||
tName: "bind variable provided", | ||
bv: &querypb.BindVariable{ | ||
Type: querypb.Type_TUPLE, | ||
Values: []*querypb.Value{sqltypes.ValueToProto(sqltypes.TestTuple(sqltypes.NewInt64(1), sqltypes.NewVarChar("a")))}, | ||
}, | ||
expEval: []eval{newEvalText([]byte("a"), collation)}, | ||
}, { | ||
tName: "bind variable provided - multi values", | ||
bv: &querypb.BindVariable{ | ||
Type: querypb.Type_TUPLE, | ||
Values: []*querypb.Value{ | ||
sqltypes.ValueToProto(sqltypes.TestTuple(sqltypes.NewInt64(1), sqltypes.NewVarChar("a"))), | ||
sqltypes.ValueToProto(sqltypes.TestTuple(sqltypes.NewInt64(2), sqltypes.NewVarChar("b"))), | ||
sqltypes.ValueToProto(sqltypes.TestTuple(sqltypes.NewInt64(3), sqltypes.NewVarChar("c"))), | ||
}, | ||
}, | ||
expEval: []eval{ | ||
newEvalText([]byte("a"), collation), | ||
newEvalText([]byte("b"), collation), | ||
newEvalText([]byte("c"), collation)}, | ||
}} | ||
|
||
for _, tcase := range tcases { | ||
t.Run(tcase.tName, func(t *testing.T) { | ||
env := &ExpressionEnv{ | ||
BindVars: make(map[string]*querypb.BindVariable), | ||
} | ||
if tcase.bv != nil { | ||
env.BindVars[key] = tcase.bv | ||
} | ||
|
||
res, err := c.eval(env) | ||
if tcase.expErr != "" { | ||
require.ErrorContains(t, err, tcase.expErr) | ||
return | ||
} | ||
require.Equal(t, sqltypes.Tuple, res.SQLType()) | ||
resTuple := res.(*evalTuple) | ||
require.Len(t, resTuple.t, len(tcase.expEval)) | ||
for idx, e := range tcase.expEval { | ||
require.Equal(t, e, resTuple.t[idx]) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// TestTupleBindVarTypeOf tests TupleBindVariable typeOf function. | ||
func TestTupleBindVarTypeOf(t *testing.T) { | ||
key := "vals" | ||
c := &TupleBindVariable{ | ||
Key: key, | ||
Index: 1, | ||
} | ||
|
||
tcases := []struct { | ||
tName string | ||
bv *querypb.BindVariable | ||
|
||
expErr string | ||
}{{ | ||
tName: "bind variable not provided", | ||
expErr: "query arguments missing for vals", | ||
}, { | ||
// typeOf does not evaluate the bind variable value | ||
tName: "bind variable provided - wrong type", | ||
bv: sqltypes.Int64BindVariable(1), | ||
}, { | ||
tName: "bind variable provided", | ||
bv: &querypb.BindVariable{ | ||
Type: querypb.Type_TUPLE, | ||
Values: []*querypb.Value{sqltypes.ValueToProto(sqltypes.TestTuple(sqltypes.NewInt64(1), sqltypes.NewVarChar("a")))}, | ||
}, | ||
}, { | ||
tName: "bind variable provided - multi values", | ||
bv: &querypb.BindVariable{ | ||
Type: querypb.Type_TUPLE, | ||
Values: []*querypb.Value{ | ||
sqltypes.ValueToProto(sqltypes.TestTuple(sqltypes.NewInt64(1), sqltypes.NewVarChar("a"))), | ||
sqltypes.ValueToProto(sqltypes.TestTuple(sqltypes.NewInt64(2), sqltypes.NewVarChar("b"))), | ||
sqltypes.ValueToProto(sqltypes.TestTuple(sqltypes.NewInt64(3), sqltypes.NewVarChar("c"))), | ||
}, | ||
}, | ||
}} | ||
|
||
for _, tcase := range tcases { | ||
t.Run(tcase.tName, func(t *testing.T) { | ||
env := &ExpressionEnv{ | ||
BindVars: make(map[string]*querypb.BindVariable), | ||
} | ||
if tcase.bv != nil { | ||
env.BindVars[key] = tcase.bv | ||
} | ||
|
||
res, err := c.typeof(env) | ||
if tcase.expErr != "" { | ||
require.ErrorContains(t, err, tcase.expErr) | ||
return | ||
} | ||
require.Equal(t, sqltypes.Tuple, res.Type) | ||
}) | ||
} | ||
} |
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.