Skip to content

Commit

Permalink
opt: convert from IsInverted/IsVector to idxtype.T
Browse files Browse the repository at this point in the history
Now that we have the new idxtype.T enumeration that specifies the type
of index (e.g. forward, inverted, vector), use that in the opt packages
rather than IsInverted and IsVector boolean functions. This will make
our code more extensible, such that adding new index types is easier,
starting with vector indexes.

Epic: CRDB-42943

Release note: None
  • Loading branch information
andy-kimball committed Jan 24, 2025
1 parent 63009ad commit 772ee7c
Show file tree
Hide file tree
Showing 38 changed files with 165 additions and 169 deletions.
1 change: 0 additions & 1 deletion pkg/internal/sqlsmith/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ go_library(
"//pkg/sql/sem/cast",
"//pkg/sql/sem/catid",
"//pkg/sql/sem/eval",
"//pkg/sql/sem/idxtype",
"//pkg/sql/sem/plpgsqltree",
"//pkg/sql/sem/tree",
"//pkg/sql/sem/tree/treebin",
Expand Down
9 changes: 4 additions & 5 deletions pkg/internal/sqlsmith/alter.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/catalog/colinfo"
"github.com/cockroachdb/cockroach/pkg/sql/randgen"
"github.com/cockroachdb/cockroach/pkg/sql/sem/cast"
"github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree/treebin"
"github.com/cockroachdb/cockroach/pkg/sql/types"
Expand Down Expand Up @@ -341,7 +340,7 @@ func makeCreateIndex(s *Smither) (tree.Statement, bool) {
}
var cols tree.IndexElemList
seen := map[tree.Name]bool{}
indexType := idxtype.FORWARD
indexType := tree.IndexTypeForward
unique := s.coin()
for len(cols) < 1 || s.coin() {
col := tableRef.Columns[s.rnd.Intn(len(tableRef.Columns))]
Expand All @@ -352,7 +351,7 @@ func makeCreateIndex(s *Smither) (tree.Statement, bool) {
// If this is the first column and it's invertible (i.e., JSONB), make an inverted index.
if len(cols) == 0 &&
colinfo.ColumnTypeIsOnlyInvertedIndexable(tree.MustBeStaticallyKnownType(col.Type)) {
indexType = idxtype.INVERTED
indexType = tree.IndexTypeInverted
unique = false
cols = append(cols, tree.IndexElem{
Column: col.Name,
Expand All @@ -367,7 +366,7 @@ func makeCreateIndex(s *Smither) (tree.Statement, bool) {
}
}
var storing tree.NameList
for idxtype.SupportsStoring(indexType) && s.coin() {
for indexType == tree.IndexTypeForward && s.coin() {
col := tableRef.Columns[s.rnd.Intn(len(tableRef.Columns))]
if seen[col.Name] {
continue
Expand All @@ -391,7 +390,7 @@ func makeCreateIndex(s *Smither) (tree.Statement, bool) {
Unique: unique,
Columns: cols,
Storing: storing,
Type: tree.IndexType(indexType),
Type: indexType,
Concurrently: s.coin(),
Invisibility: invisibility,
}, true
Expand Down
7 changes: 3 additions & 4 deletions pkg/internal/sqlsmith/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
_ "github.com/cockroachdb/cockroach/pkg/sql/sem/builtins"
"github.com/cockroachdb/cockroach/pkg/sql/sem/catid"
"github.com/cockroachdb/cockroach/pkg/sql/sem/eval"
"github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree/treebin"
"github.com/cockroachdb/cockroach/pkg/sql/types"
Expand Down Expand Up @@ -500,14 +499,14 @@ func (s *Smither) extractIndexes(
return nil, err
}
if _, ok := indexes[idx]; !ok {
indexType := idxtype.FORWARD
indexType := tree.IndexTypeForward
if inverted {
indexType = idxtype.INVERTED
indexType = tree.IndexTypeInverted
}
indexes[idx] = &tree.CreateIndex{
Name: idx,
Table: *t.TableName,
Type: tree.IndexType(indexType),
Type: indexType,
}
}
create := indexes[idx]
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/opt/cat/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ go_library(
"//pkg/sql/privilege",
"//pkg/sql/roleoption",
"//pkg/sql/sem/catid",
"//pkg/sql/sem/idxtype",
"//pkg/sql/sem/tree",
"//pkg/sql/sessiondata",
"//pkg/sql/types",
Expand Down
10 changes: 4 additions & 6 deletions pkg/sql/opt/cat/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/geo/geopb"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
)

Expand Down Expand Up @@ -45,15 +46,12 @@ type Index interface {
// Specifically idx = Table().Index(idx.Ordinal).
Ordinal() IndexOrdinal

// Type returns the type of this index: forward, inverted, vector, etc.
Type() idxtype.T

// IsUnique returns true if this index is declared as UNIQUE in the schema.
IsUnique() bool

// IsInverted returns true if this is an inverted index.
IsInverted() bool

// IsVector returns true if this is a vector index.
IsVector() bool

// GetInvisibility returns index invisibility.
GetInvisibility() float64

Expand Down
46 changes: 25 additions & 21 deletions pkg/sql/opt/cat/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"

"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/util/encoding"
"github.com/cockroachdb/cockroach/pkg/util/treeprinter"
Expand Down Expand Up @@ -117,66 +118,69 @@ func FormatTable(
// formatCatalogIndex nicely formats a catalog index using a treeprinter for
// debugging and testing.
func formatCatalogIndex(tab Table, ord int, tp treeprinter.Node, redactableValues bool) {
idx := tab.Index(ord)
idxType := ""
if idx.Ordinal() == PrimaryIndex {
idxType = "PRIMARY "
} else if idx.IsUnique() {
idxType = "UNIQUE "
} else if idx.IsInverted() {
idxType = "INVERTED "
} else if idx.IsVector() {
idxType = "VECTOR "
index := tab.Index(ord)
indexType := ""
if index.Ordinal() == PrimaryIndex {
indexType = "PRIMARY "
} else if index.IsUnique() {
indexType = "UNIQUE "
} else {
switch index.Type() {
case idxtype.INVERTED:
indexType = "INVERTED "
case idxtype.VECTOR:
indexType = "VECTOR "
}
}
mutation := ""
if IsMutationIndex(tab, ord) {
mutation = " (mutation)"
}

idxVisibililty := ""
if invisibility := idx.GetInvisibility(); invisibility != 0.0 {
if invisibility := index.GetInvisibility(); invisibility != 0.0 {
if invisibility == 1.0 {
idxVisibililty = " NOT VISIBLE"
} else {
idxVisibililty = " VISIBILITY " + fmt.Sprintf("%.2f", 1-invisibility)
}
}

child := tp.Childf("%sINDEX %s%s%s", idxType, idx.Name(), mutation, idxVisibililty)
child := tp.Childf("%sINDEX %s%s%s", indexType, index.Name(), mutation, idxVisibililty)

var buf bytes.Buffer
colCount := idx.ColumnCount()
colCount := index.ColumnCount()
if ord == PrimaryIndex {
// Omit the "stored" columns from the primary index.
colCount = idx.KeyColumnCount()
colCount = index.KeyColumnCount()
}

for i := 0; i < colCount; i++ {
buf.Reset()

idxCol := idx.Column(i)
idxCol := index.Column(i)
formatColumn(idxCol.Column, &buf, redactableValues)
if idxCol.Descending {
fmt.Fprintf(&buf, " desc")
}

if i >= idx.LaxKeyColumnCount() {
if i >= index.LaxKeyColumnCount() {
fmt.Fprintf(&buf, " (storing)")
}

if i < idx.ImplicitColumnCount() {
if i < index.ImplicitColumnCount() {
fmt.Fprintf(&buf, " (implicit)")
}

child.Child(buf.String())
}

FormatZone(idx.Zone(), child)
FormatZone(index.Zone(), child)

if n := idx.PartitionCount(); n > 0 {
if n := index.PartitionCount(); n > 0 {
c := child.Child("partitions")
for i := 0; i < n; i++ {
p := idx.Partition(i)
p := index.Partition(i)
part := c.Child(p.Name())
prefixes := part.Child("partition by list prefixes")
for _, datums := range p.PartitionByListPrefixes() {
Expand All @@ -185,7 +189,7 @@ func formatCatalogIndex(tab Table, ord int, tp treeprinter.Node, redactableValue
FormatZone(p.Zone(), part)
}
}
if pred, isPartial := idx.Predicate(); isPartial {
if pred, isPartial := index.Predicate(); isPartial {
child.Childf("WHERE %s", MaybeMarkRedactable(pred, redactableValues))
}
}
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/opt/exec/execbuilder/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ go_library(
"//pkg/sql/sem/builtins/builtinsregistry",
"//pkg/sql/sem/catconstants",
"//pkg/sql/sem/eval",
"//pkg/sql/sem/idxtype",
"//pkg/sql/sem/tree",
"//pkg/sql/sem/tree/treebin",
"//pkg/sql/sem/tree/treecmp",
Expand Down
13 changes: 8 additions & 5 deletions pkg/sql/opt/exec/execbuilder/relational.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/sem/builtins/builtinsregistry"
"github.com/cockroachdb/cockroach/pkg/sql/sem/catconstants"
"github.com/cockroachdb/cockroach/pkg/sql/sem/eval"
"github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree/treewindow"
"github.com/cockroachdb/cockroach/pkg/sql/sqlerrors"
Expand Down Expand Up @@ -584,7 +585,7 @@ func (b *Builder) scanParams(
// index in the memo.
if scan.Flags.ForceIndex && scan.Flags.Index != scan.Index {
idx := tab.Index(scan.Flags.Index)
isInverted := idx.IsInverted()
isInverted := idx.Type() == idxtype.INVERTED
_, isPartial := idx.Predicate()

var err error
Expand Down Expand Up @@ -756,11 +757,13 @@ func (b *Builder) buildScan(scan *memo.ScanExpr) (_ execPlan, outputCols colOrdM
}

idx := tab.Index(scan.Index)
if idx.IsInverted() && len(scan.InvertedConstraint) == 0 && scan.Constraint == nil {
return execPlan{}, colOrdMap{},
errors.AssertionFailedf("expected inverted index scan to have a constraint")
if idx.Type() == idxtype.INVERTED {
if len(scan.InvertedConstraint) == 0 && scan.Constraint == nil {
return execPlan{}, colOrdMap{},
errors.AssertionFailedf("expected inverted index scan to have a constraint")
}
}
if idx.IsVector() {
if idx.Type() == tree.IndexTypeVector {
return execPlan{}, colOrdMap{}, errors.AssertionFailedf(
"only VectorSearch operators can use vector indexes")
}
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/opt/exec/explain/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ go_library(
"//pkg/sql/pgwire/pgerror",
"//pkg/sql/sem/catid",
"//pkg/sql/sem/eval",
"//pkg/sql/sem/idxtype",
"//pkg/sql/sem/tree",
"//pkg/sql/sessiondatapb",
"//pkg/sql/types",
Expand Down
9 changes: 3 additions & 6 deletions pkg/sql/opt/exec/explain/plan_gist_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/sem/catid"
"github.com/cockroachdb/cockroach/pkg/sql/sem/eval"
"github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util"
Expand Down Expand Up @@ -681,12 +682,8 @@ func (u *unknownIndex) IsUnique() bool {
return false
}

func (u *unknownIndex) IsInverted() bool {
return false
}

func (u *unknownIndex) IsVector() bool {
return false
func (u *unknownIndex) Type() idxtype.T {
return idxtype.FORWARD
}

func (u *unknownIndex) GetInvisibility() float64 {
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/opt/indexrec/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ go_library(
"//pkg/sql/opt",
"//pkg/sql/opt/cat",
"//pkg/sql/opt/memo",
"//pkg/sql/sem/idxtype",
"//pkg/sql/sem/tree",
"//pkg/sql/types",
"//pkg/util/intsets",
Expand Down
Loading

0 comments on commit 772ee7c

Please sign in to comment.