Skip to content
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

Assume text index comparisons are exact #2619

Merged
merged 7 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion enginetest/enginetests.go
Original file line number Diff line number Diff line change
Expand Up @@ -4770,7 +4770,6 @@ func TestTracing(t *testing.T, harness Harness) {
"plan.Distinct",
"plan.Project",
"plan.Sort",
"plan.Filter",
"plan.IndexedTableAccess",
}

Expand Down
1,624 changes: 725 additions & 899 deletions enginetest/queries/integration_plans.go

Large diffs are not rendered by default.

480 changes: 189 additions & 291 deletions enginetest/queries/query_plans.go

Large diffs are not rendered by default.

160 changes: 64 additions & 96 deletions enginetest/queries/tpcc_plans.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions sql/analyzer/costed_index_scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func indexSearchableLookup(n sql.Node, rt sql.TableNode, lookup sql.IndexLookup,
return n, transform.SameTree, nil
}

if !iat.PreciseMatch() {
if !preciseIndexAccess(iat, lookup.Index) {
// cannot drop any filters
newFilter = oldFilter
}
Expand Down Expand Up @@ -284,7 +284,7 @@ func getCostedIndexScan(ctx *sql.Context, statsProv sql.StatsProvider, rt sql.Ta
}

var retFilters []sql.Expression
if !iat.PreciseMatch() {
if !preciseIndexAccess(iat, lookup.Index) {
// cannot drop filters
retFilters = filters
} else if len(b.leftover) > 0 {
Expand Down Expand Up @@ -357,7 +357,7 @@ func addIndexScans(m *memo.Memo) error {
}

var keepFilters []sql.Expression
if !iat.PreciseMatch() {
if !preciseIndexAccess(iat, lookup.Index) {
// cannot drop any filters
keepFilters = filter.Filters
} else {
Expand Down Expand Up @@ -402,6 +402,12 @@ func addIndexScans(m *memo.Memo) error {
})
}

// preciseIndexAccess returns whether an indexed access into a table is a
// replacement for relational filters.
func preciseIndexAccess(t sql.IndexAddressableTable, i sql.Index) bool {
return t.PreciseMatch() && !i.IsFullText() && !i.IsSpatial() && len(i.PrefixLengths()) == 0
}

func newIndexCoster(ctx *sql.Context, underlyingName string) *indexCoster {
return &indexCoster{
ctx: ctx,
Expand Down
28 changes: 16 additions & 12 deletions sql/expression/comparison.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,25 @@ func PreciseComparison(e sql.Expression) bool {
return true
}

// string type comparisons are exact
if types.IsText(left) && types.IsText(right) {
return true
}

if tupType, ok := right.(types.TupleType); ok {
if types.IsInteger(left) {
rightIsAllInt := true
for _, typ := range tupType {
if !types.IsInteger(typ) {
rightIsAllInt = false
break
for _, typCb := range []func(t sql.Type) bool{types.IsText, types.IsInteger} {
max-hoffman marked this conversation as resolved.
Show resolved Hide resolved
if typCb(left) {
rightAllSameType := true
for _, typ := range tupType {
if !typCb(typ) {
rightAllSameType = false
break
}
}
if rightAllSameType {
return true
}
}
if rightIsAllInt {
// left integer and right tuple integer types is OK
return true
}
imprecise = true
return false
}
for _, right := range tupType {
if !left.Equals(right) {
Expand Down
Loading