Skip to content

Commit 2fc0613

Browse files
authored
memo.Literal has different type than lookup (#1851)
1 parent e2b38d0 commit 2fc0613

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

enginetest/queries/script_queries.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3332,6 +3332,24 @@ var ScriptTests = []ScriptTest{
33323332
},
33333333
},
33343334
},
3335+
{
3336+
Name: "test index scan over floats",
3337+
SetUpScript: []string{
3338+
"CREATE TABLE tab2(pk INTEGER PRIMARY KEY, col0 INTEGER, col1 FLOAT, col2 TEXT, col3 INTEGER, col4 FLOAT, col5 TEXT);",
3339+
"CREATE UNIQUE INDEX idx_tab2_0 ON tab2 (col1 DESC,col4 DESC);",
3340+
"CREATE INDEX idx_tab2_1 ON tab2 (col1,col0);",
3341+
"CREATE INDEX idx_tab2_2 ON tab2 (col4,col0);",
3342+
"CREATE INDEX idx_tab2_3 ON tab2 (col3 DESC);",
3343+
"INSERT INTO tab2 VALUES(0,344,171.98,'nwowg',833,149.54,'wjiif');",
3344+
"INSERT INTO tab2 VALUES(1,353,589.18,'femmh',44,621.85,'qedct');",
3345+
},
3346+
Assertions: []ScriptTestAssertion{
3347+
{
3348+
Query: "SELECT pk FROM tab2 WHERE ((((((col0 IN (SELECT col3 FROM tab2 WHERE ((col1 = 672.71)) AND col4 IN (SELECT col1 FROM tab2 WHERE ((col4 > 169.88 OR col0 > 939 AND ((col3 > 578))))) AND col0 >= 377) AND col4 >= 817.87 AND (col4 > 597.59)) OR col4 >= 434.59 AND ((col4 < 158.43)))))) AND col0 < 303) OR ((col0 > 549)) AND (col4 BETWEEN 816.92 AND 983.96) OR (col3 BETWEEN 421 AND 96);",
3349+
Expected: []sql.Row{},
3350+
},
3351+
},
3352+
},
33353353
}
33363354

33373355
var SpatialScriptTests = []ScriptTest{

sql/analyzer/indexed_joins.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ func sortedIndexScansForTableCol(indexes []*memo.Index, targetCol *memo.ColRef,
790790
}
791791
}
792792
}
793-
rang[j] = sql.ClosedRangeColumnExpr(lit.Val, lit.Val, lit.Typ)
793+
rang[j] = sql.ClosedRangeColumnExpr(lit.Val, lit.Val, idx.SqlIdx().ColumnExpressionTypes()[j].Type)
794794
}
795795
for j := matchedIdx; j < len(idx.Cols()); j++ {
796796
// all range bound Compare() is type insensitive

sql/memo/exec_builder.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package memo
33
import (
44
"fmt"
55

6-
"github.com/dolthub/go-mysql-server/sql/fixidx"
7-
86
"github.com/dolthub/go-mysql-server/sql"
97
"github.com/dolthub/go-mysql-server/sql/expression"
8+
"github.com/dolthub/go-mysql-server/sql/fixidx"
109
"github.com/dolthub/go-mysql-server/sql/plan"
10+
"github.com/dolthub/go-mysql-server/sql/types"
1111
)
1212

1313
type ExecBuilder struct{}
@@ -240,15 +240,31 @@ func (b *ExecBuilder) buildIndexScan(i *IndexScan, input sql.Schema, children ..
240240
return ret, nil
241241
}
242242

243+
func checkIndexTypeMismatch(idx sql.Index, rang sql.Range) bool {
244+
for i, typ := range idx.ColumnExpressionTypes() {
245+
if !types.Null.Equals(rang[i].Typ) && !typ.Type.Equals(rang[i].Typ) {
246+
return true
247+
}
248+
}
249+
return false
250+
}
251+
243252
func (b *ExecBuilder) buildMergeJoin(j *MergeJoin, input sql.Schema, children ...sql.Node) (sql.Node, error) {
244253
inner, err := b.buildIndexScan(j.InnerScan, input, children[0])
245254
if err != nil {
246255
return nil, err
247256
}
257+
if checkIndexTypeMismatch(j.InnerScan.Idx.SqlIdx(), j.InnerScan.Range) {
258+
return nil, fmt.Errorf("index scan type mismatch")
259+
}
260+
248261
outer, err := b.buildIndexScan(j.OuterScan, input, children[1])
249262
if err != nil {
250263
return nil, err
251264
}
265+
if checkIndexTypeMismatch(j.OuterScan.Idx.SqlIdx(), j.OuterScan.Range) {
266+
return nil, fmt.Errorf("index scan type mismatch")
267+
}
252268
if j.SwapCmp {
253269
cmp, ok := j.Filter[0].(*Equal)
254270
if !ok {

0 commit comments

Comments
 (0)