Skip to content

Commit

Permalink
memo.Literal has different type than lookup (#1851)
Browse files Browse the repository at this point in the history
  • Loading branch information
jycor authored Jun 26, 2023
1 parent e2b38d0 commit 2fc0613
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
18 changes: 18 additions & 0 deletions enginetest/queries/script_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -3332,6 +3332,24 @@ var ScriptTests = []ScriptTest{
},
},
},
{
Name: "test index scan over floats",
SetUpScript: []string{
"CREATE TABLE tab2(pk INTEGER PRIMARY KEY, col0 INTEGER, col1 FLOAT, col2 TEXT, col3 INTEGER, col4 FLOAT, col5 TEXT);",
"CREATE UNIQUE INDEX idx_tab2_0 ON tab2 (col1 DESC,col4 DESC);",
"CREATE INDEX idx_tab2_1 ON tab2 (col1,col0);",
"CREATE INDEX idx_tab2_2 ON tab2 (col4,col0);",
"CREATE INDEX idx_tab2_3 ON tab2 (col3 DESC);",
"INSERT INTO tab2 VALUES(0,344,171.98,'nwowg',833,149.54,'wjiif');",
"INSERT INTO tab2 VALUES(1,353,589.18,'femmh',44,621.85,'qedct');",
},
Assertions: []ScriptTestAssertion{
{
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);",
Expected: []sql.Row{},
},
},
},
}

var SpatialScriptTests = []ScriptTest{
Expand Down
2 changes: 1 addition & 1 deletion sql/analyzer/indexed_joins.go
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ func sortedIndexScansForTableCol(indexes []*memo.Index, targetCol *memo.ColRef,
}
}
}
rang[j] = sql.ClosedRangeColumnExpr(lit.Val, lit.Val, lit.Typ)
rang[j] = sql.ClosedRangeColumnExpr(lit.Val, lit.Val, idx.SqlIdx().ColumnExpressionTypes()[j].Type)
}
for j := matchedIdx; j < len(idx.Cols()); j++ {
// all range bound Compare() is type insensitive
Expand Down
20 changes: 18 additions & 2 deletions sql/memo/exec_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package memo
import (
"fmt"

"github.com/dolthub/go-mysql-server/sql/fixidx"

"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/expression"
"github.com/dolthub/go-mysql-server/sql/fixidx"
"github.com/dolthub/go-mysql-server/sql/plan"
"github.com/dolthub/go-mysql-server/sql/types"
)

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

func checkIndexTypeMismatch(idx sql.Index, rang sql.Range) bool {
for i, typ := range idx.ColumnExpressionTypes() {
if !types.Null.Equals(rang[i].Typ) && !typ.Type.Equals(rang[i].Typ) {
return true
}
}
return false
}

func (b *ExecBuilder) buildMergeJoin(j *MergeJoin, input sql.Schema, children ...sql.Node) (sql.Node, error) {
inner, err := b.buildIndexScan(j.InnerScan, input, children[0])
if err != nil {
return nil, err
}
if checkIndexTypeMismatch(j.InnerScan.Idx.SqlIdx(), j.InnerScan.Range) {
return nil, fmt.Errorf("index scan type mismatch")
}

outer, err := b.buildIndexScan(j.OuterScan, input, children[1])
if err != nil {
return nil, err
}
if checkIndexTypeMismatch(j.OuterScan.Idx.SqlIdx(), j.OuterScan.Range) {
return nil, fmt.Errorf("index scan type mismatch")
}
if j.SwapCmp {
cmp, ok := j.Filter[0].(*Equal)
if !ok {
Expand Down

0 comments on commit 2fc0613

Please sign in to comment.