Skip to content

Commit 3dba9b2

Browse files
author
James Cor
committed
fix some queries
1 parent 2b528c5 commit 3dba9b2

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

enginetest/queries/script_queries.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,8 @@ var ScriptTests = []ScriptTest{
337337
},
338338
},
339339
{
340-
Name: "string to number comparison correctly truncates",
341340
Dialect: "mysql",
341+
Name: "string to number comparison correctly truncates",
342342
Assertions: []ScriptTestAssertion{
343343
{
344344
Query: "SELECT 'A' = 0;",
@@ -551,13 +551,12 @@ var ScriptTests = []ScriptTest{
551551
Expected: []sql.Row{{true}},
552552
},
553553
{
554-
// TODO: 123.456 is converted to a DECIMAL by Builder.ConvertVal, when it should be a DOUBLE
555554
SkipResultCheckOnServerEngine: true, // TODO: warnings do not make it to server engine
556555
Query: "SELECT '123.456ABC' in (123.456);",
557556
Expected: []sql.Row{{true}},
558557
ExpectedWarningsCount: 1,
559558
ExpectedWarning: mysql.ERTruncatedWrongValue,
560-
ExpectedWarningMessageSubstring: "Truncated incorrect decimal(65,30) value: 123.456ABC",
559+
ExpectedWarningMessageSubstring: "Truncated incorrect double value: 123.456ABC",
561560
},
562561
{
563562
Query: "SELECT '123.456e2' in (12345.6);",
@@ -7271,9 +7270,10 @@ CREATE TABLE tab3 (
72717270
},
72727271
},
72737272
{
7274-
// This actually matches MySQL behavior
7275-
Query: "select * from t where (f in (null, 0.8));",
7276-
Expected: []sql.Row{},
7273+
Query: "select count(*) from t where (f in (null, 0.8));",
7274+
Expected: []sql.Row{
7275+
{1},
7276+
},
72777277
},
72787278
{
72797279
// select count to avoid floating point comparison

sql/expression/in.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,17 @@ func NewInTuple(left sql.Expression, right sql.Expression) *InTuple {
6262

6363
// validateAndEvalRightTuple will evaluate the right tuple, check if leftType and the right Tuple are comparable,
6464
// determine what type to use to compare the two sides, and indicate if right Tuple contains any NULL elements.
65-
// The NULL handling for IN expressions is tricky. According to
66-
// https://dev.mysql.com/doc/refman/8.0/en/comparison-operators.html#operator_in:
67-
// To comply with the SQL standard, IN() returns NULL not only if the expression on the left hand side is NULL, but
68-
// also if no match is found in the list and one of the expressions in the list is NULL.
65+
// Returns
66+
// - slice of the evaluated elements
67+
// - sql.Type to convert elements to before hashing
68+
// - bool indicating if there are null elements
69+
// - error
6970
func validateAndEvalRightTuple(ctx *sql.Context, lType sql.Type, right Tuple, row sql.Row) ([]any, sql.Type, bool, error) {
71+
// The NULL handling for IN expressions is tricky. According to
72+
// https://dev.mysql.com/doc/refman/8.0/en/comparison-operators.html#operator_in:
73+
// To comply with the SQL standard, IN() returns NULL not only if the expression on the left hand side is NULL, but
74+
// also if no match is found in the list and one of the expressions in the list is NULL.
75+
7076
// If left is StringType and ANY of the right is NumberType, then we should use Double Type for comparison
7177
// If left is NumberType and ANT of the left is StringType, then we should use Double Type for comparison
7278
lColCount := types.NumColumns(lType)
@@ -112,7 +118,10 @@ func validateAndEvalRightTuple(ctx *sql.Context, lType sql.Type, right Tuple, ro
112118
} else if types.IsEnum(lType) || types.IsSet(lType) || types.IsText(lType) {
113119
cmpType = lType
114120
} else {
115-
cmpType = types.GetCompareType(lType, right[0].Type())
121+
cmpType = lType
122+
for _, el := range right {
123+
cmpType = types.GetCompareType(cmpType, el.Type())
124+
}
116125
}
117126

118127
return rVals, cmpType, rHasNull, nil
@@ -240,6 +249,11 @@ func NewHashInTuple(ctx *sql.Context, left, right sql.Expression) (*HashInTuple,
240249
}
241250

242251
// newInMap hashes static expressions in the right child Tuple of a InTuple node
252+
// returns
253+
// - map of the hashed elements
254+
// - sql.Type to convert elements to before hashing
255+
// - bool indicating if there are null elements
256+
// - error
243257
func newInMap(ctx *sql.Context, lType sql.Type, right Tuple) (map[uint64]struct{}, sql.Type, bool, error) {
244258
if lType == types.Null {
245259
return nil, nil, true, nil

0 commit comments

Comments
 (0)