Skip to content

Commit 817084a

Browse files
committed
Ensure that vector indexes can only be created on JSON and Vector columns.
1 parent 92114b7 commit 817084a

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

sql/errors.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,9 @@ var (
923923
// ErrFullTextInvalidColumnType is returned when a Full-Text index is declared on a non-text column.
924924
ErrFullTextInvalidColumnType = errors.NewKind("all Full-Text columns must be declared on a non-binary text type")
925925

926+
// ErrVectorInvalidColumnType is returned when a Vector index is declared on a non-vector column.
927+
ErrVectorInvalidColumnType = errors.NewKind("a vector index colum must be a vector or JSON")
928+
926929
// ErrGeneratedColumnValue is returned when a value is provided for a generated column
927930
ErrGeneratedColumnValue = errors.NewKind("The value specified for generated column %q in table %q is not allowed.")
928931

sql/rowexec/ddl_iters.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2008,6 +2008,22 @@ func (b *BaseBuilder) executeAlterIndex(ctx *sql.Context, n *plan.AlterIndex) er
20082008
return nil
20092009
}
20102010

2011+
if indexDef.IsVector() {
2012+
// Validate that the type is exactly one column and it's something we can make a vector index of.
2013+
if len(indexDef.Columns) != 1 {
2014+
return fmt.Errorf("a vector index must have exactly one column")
2015+
}
2016+
indexColNameLower := strings.ToLower(indexDef.Columns[0].Name)
2017+
for _, tblCol := range idxAltTbl.Schema() {
2018+
if indexColNameLower == strings.ToLower(tblCol.Name) {
2019+
if !types.IsVectorConvertable(tblCol.Type) {
2020+
return sql.ErrVectorInvalidColumnType.New()
2021+
}
2022+
break
2023+
}
2024+
}
2025+
}
2026+
20112027
err = idxAltTbl.CreateIndex(ctx, indexDef)
20122028
if err != nil {
20132029
if sql.ErrDuplicateKey.Is(err) && n.IfNotExists {

sql/types/typecheck.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,16 @@ func IsYear(t sql.Type) bool {
238238
_, ok := t.(YearType_)
239239
return ok
240240
}
241+
242+
// IsVectorConvertable checks if t can be implicitly converted to a vector of floats.
243+
func IsVectorConvertable(t sql.Type) bool {
244+
if t == nil {
245+
return false
246+
}
247+
switch t.Type() {
248+
case sqltypes.TypeJSON, sqltypes.Vector, sqltypes.Binary:
249+
return true
250+
default:
251+
return false
252+
}
253+
}

0 commit comments

Comments
 (0)