diff --git a/ast/vector_index.go b/ast/vector_index.go new file mode 100644 index 00000000..ee781ffd --- /dev/null +++ b/ast/vector_index.go @@ -0,0 +1,35 @@ +package ast + +// CreateVectorIndexStatement represents CREATE VECTOR INDEX name ON table (column) +// [WITH (options)] [ON filegroup]. +type CreateVectorIndexStatement struct { + Fragment + Name *Identifier `json:"Name,omitempty"` + OnName *SchemaObjectName `json:"OnName,omitempty"` + VectorColumn *Identifier `json:"VectorColumn,omitempty"` + IndexOptions []IndexOption `json:"IndexOptions,omitempty"` + OnFileGroupOrPartitionScheme *FileGroupOrPartitionScheme `json:"OnFileGroupOrPartitionScheme,omitempty"` +} + +func (*CreateVectorIndexStatement) node() {} +func (*CreateVectorIndexStatement) statement() {} + +// VectorMetricIndexOption represents METRIC = 'cosine' | 'dot' | 'euclidean'. +type VectorMetricIndexOption struct { + Fragment + MetricType string + OptionKind string +} + +func (*VectorMetricIndexOption) node() {} +func (*VectorMetricIndexOption) indexOption() {} + +// VectorTypeIndexOption represents TYPE = 'DiskANN'. +type VectorTypeIndexOption struct { + Fragment + VectorType string + OptionKind string +} + +func (*VectorTypeIndexOption) node() {} +func (*VectorTypeIndexOption) indexOption() {} diff --git a/ast/vector_search_table_reference.go b/ast/vector_search_table_reference.go new file mode 100644 index 00000000..e124effc --- /dev/null +++ b/ast/vector_search_table_reference.go @@ -0,0 +1,16 @@ +package ast + +// VectorSearchTableReference represents the VECTOR_SEARCH table-valued function. +type VectorSearchTableReference struct { + Fragment + Table TableReference `json:"Table,omitempty"` + Column *ColumnReferenceExpression `json:"Column,omitempty"` + SimilarTo ScalarExpression `json:"SimilarTo,omitempty"` + Metric *StringLiteral `json:"Metric,omitempty"` + TopN ScalarExpression `json:"TopN,omitempty"` + ForceAnnOnly bool `json:"ForceAnnOnly"` + Alias *Identifier `json:"Alias,omitempty"` +} + +func (*VectorSearchTableReference) node() {} +func (*VectorSearchTableReference) tableReference() {} diff --git a/parser/marshal.go b/parser/marshal.go index bf3b7524..1788d31d 100644 --- a/parser/marshal.go +++ b/parser/marshal.go @@ -482,6 +482,8 @@ func statementToJSON(stmt ast.Statement) jsonNode { return addSpan(dropLoginStatementToJSON(s), frag(stmt)) case *ast.CreateIndexStatement: return addSpan(createIndexStatementToJSON(s), frag(stmt)) + case *ast.CreateVectorIndexStatement: + return addSpan(createVectorIndexStatementToJSON(s), frag(stmt)) case *ast.CreateJsonIndexStatement: return addSpan(createJsonIndexStatementToJSON(s), frag(stmt)) case *ast.CreateSpatialIndexStatement: @@ -3262,6 +3264,31 @@ func tableReferenceToJSON(ref ast.TableReference) jsonNode { } node["ForPath"] = r.ForPath return addSpan(node, frag(ref)) + case *ast.VectorSearchTableReference: + node := jsonNode{ + "$type": "VectorSearchTableReference", + } + if r.Table != nil { + node["Table"] = tableReferenceToJSON(r.Table) + } + if r.Column != nil { + node["Column"] = scalarExpressionToJSON(r.Column) + } + if r.SimilarTo != nil { + node["SimilarTo"] = scalarExpressionToJSON(r.SimilarTo) + } + if r.Metric != nil { + node["Metric"] = stringLiteralToJSON(r.Metric) + } + if r.TopN != nil { + node["TopN"] = scalarExpressionToJSON(r.TopN) + } + node["ForceAnnOnly"] = r.ForceAnnOnly + if r.Alias != nil { + node["Alias"] = identifierToJSON(r.Alias) + } + node["ForPath"] = false + return addSpan(node, frag(ref)) case *ast.PredictTableReference: node := jsonNode{ "$type": "PredictTableReference", @@ -17265,6 +17292,18 @@ func partitionSpecifierToJSON(p *ast.PartitionSpecifier) jsonNode { func indexOptionToJSON(opt ast.IndexOption) jsonNode { switch o := opt.(type) { + case *ast.VectorMetricIndexOption: + return addSpan(jsonNode{ + "$type": "VectorMetricIndexOption", + "MetricType": o.MetricType, + "OptionKind": o.OptionKind, + }, frag(opt)) + case *ast.VectorTypeIndexOption: + return addSpan(jsonNode{ + "$type": "VectorTypeIndexOption", + "VectorType": o.VectorType, + "OptionKind": o.OptionKind, + }, frag(opt)) case *ast.IndexStateOption: return addSpan(jsonNode{ "$type": "IndexStateOption", @@ -22827,3 +22866,29 @@ func createJsonIndexStatementToJSON(s *ast.CreateJsonIndexStatement) jsonNode { } return addSpan(node, frag(s)) } + +func createVectorIndexStatementToJSON(s *ast.CreateVectorIndexStatement) jsonNode { + node := jsonNode{ + "$type": "CreateVectorIndexStatement", + } + if s.VectorColumn != nil { + node["VectorColumn"] = identifierToJSON(s.VectorColumn) + } + if s.OnFileGroupOrPartitionScheme != nil { + node["OnFileGroupOrPartitionScheme"] = fileGroupOrPartitionSchemeToJSON(s.OnFileGroupOrPartitionScheme) + } + if s.Name != nil { + node["Name"] = identifierToJSON(s.Name) + } + if s.OnName != nil { + node["OnName"] = schemaObjectNameToJSON(s.OnName) + } + if len(s.IndexOptions) > 0 { + opts := make([]jsonNode, len(s.IndexOptions)) + for i, opt := range s.IndexOptions { + opts[i] = indexOptionToJSON(opt) + } + node["IndexOptions"] = opts + } + return addSpan(node, frag(s)) +} diff --git a/parser/parse_select.go b/parser/parse_select.go index 9754377f..2f9d4b3b 100644 --- a/parser/parse_select.go +++ b/parser/parse_select.go @@ -3085,6 +3085,12 @@ func (p *Parser) parseSingleTableReference() (ast.TableReference, error) { return spanned(p, spanV41, astStart), spanErr41 } + // Check for VECTOR_SEARCH + if p.curTok.Type == TokenIdent && strings.ToUpper(p.curTok.Literal) == "VECTOR_SEARCH" && p.peekTok.Type == TokenLParen { + ref, err := p.parseVectorSearchTableReference() + return spanned(p, ref, astStart), err + } + // Check for PREDICT if p.curTok.Type == TokenIdent && strings.ToUpper(p.curTok.Literal) == "PREDICT" { spanV42, spanErr42 := p.parsePredictTableReference() @@ -9825,3 +9831,102 @@ func (p *Parser) groupByListEnded() bool { } return false } + +// parseVectorSearchTableReference parses +// VECTOR_SEARCH(TABLE = t [AS a], COLUMN = c, SIMILAR_TO = expr, +// METRIC = 'name' [, TOP_N = n]) [WITH (FORCE_ANN_ONLY)] [[AS] alias]. +func (p *Parser) parseVectorSearchTableReference() (*ast.VectorSearchTableReference, error) { + p.nextToken() // consume VECTOR_SEARCH + p.nextToken() // consume ( + + ref := &ast.VectorSearchTableReference{} + for p.curTok.Type != TokenRParen && p.curTok.Type != TokenEOF { + argName := strings.ToUpper(p.curTok.Literal) + p.nextToken() // consume argument name + if p.curTok.Type != TokenEquals { + return nil, fmt.Errorf("expected = after %s in VECTOR_SEARCH, got %s", argName, p.curTok.Literal) + } + p.nextToken() // consume = + + switch argName { + case "TABLE": + tbl, err := p.parseSingleTableReference() + if err != nil { + return nil, err + } + ref.Table = tbl + case "COLUMN": + expr, err := p.parseScalarExpression() + if err != nil { + return nil, err + } + col, ok := expr.(*ast.ColumnReferenceExpression) + if !ok { + return nil, fmt.Errorf("expected column reference for VECTOR_SEARCH COLUMN") + } + ref.Column = col + case "SIMILAR_TO": + expr, err := p.parseScalarExpression() + if err != nil { + return nil, err + } + ref.SimilarTo = expr + case "METRIC": + lit, err := p.parseStringLiteral() + if err != nil { + return nil, err + } + ref.Metric = lit + case "TOP_N": + expr, err := p.parseScalarExpression() + if err != nil { + return nil, err + } + ref.TopN = expr + default: + return nil, fmt.Errorf("unexpected VECTOR_SEARCH argument %s", argName) + } + + if p.curTok.Type != TokenComma { + break + } + p.nextToken() // consume , + } + if p.curTok.Type != TokenRParen { + return nil, fmt.Errorf("expected ) in VECTOR_SEARCH, got %s", p.curTok.Literal) + } + p.nextToken() // consume ) + + // WITH (FORCE_ANN_ONLY) + if p.curTok.Type == TokenWith && p.peekTok.Type == TokenLParen { + p.nextToken() // consume WITH + p.nextToken() // consume ( + if strings.ToUpper(p.curTok.Literal) == "FORCE_ANN_ONLY" { + ref.ForceAnnOnly = true + p.nextToken() + } + if p.curTok.Type != TokenRParen { + return nil, fmt.Errorf("expected ) after FORCE_ANN_ONLY, got %s", p.curTok.Literal) + } + p.nextToken() // consume ) + } + + // [AS] alias + if p.curTok.Type == TokenAs { + p.nextToken() // consume AS + ref.Alias = p.parseIdentifier() + } else if p.curTok.Type == TokenIdent && !isTableReferenceFollowKeyword(p.curTok.Literal) { + ref.Alias = p.parseIdentifier() + } + return ref, nil +} + +// isTableReferenceFollowKeyword reports whether an identifier token that +// follows a table reference is a keyword rather than an alias. +func isTableReferenceFollowKeyword(lit string) bool { + switch strings.ToUpper(lit) { + case "GO", "WINDOW", "APPLY", "FULL", "RIGHT", "OPTION", "FOR": + return true + } + return false +} diff --git a/parser/parse_statements.go b/parser/parse_statements.go index aa9c87a1..98bcc4e5 100644 --- a/parser/parse_statements.go +++ b/parser/parse_statements.go @@ -3066,6 +3066,11 @@ func (p *Parser) parseCreateStatement() (ast.Statement, error) { case "SPATIAL": spanV317, spanErr317 := p.parseCreateSpatialIndexStatement() return spanned(p, spanV317, astStart), spanErr317 + case "VECTOR": + if p.peekTok.Type == TokenIndex { + stmt, err := p.parseCreateVectorIndexStatement() + return spanned(p, stmt, astStart), err + } case "JSON": if p.peekTok.Type == TokenIndex { stmt, err := p.parseCreateJsonIndexStatement() @@ -12044,6 +12049,29 @@ func (p *Parser) parseCreateIndexOptions() []ast.IndexOption { p.nextToken() // consume value switch optionName { + case "METRIC": + metric := strings.Trim(valueToken.Literal, "'") + switch strings.ToLower(metric) { + case "cosine": + metric = "Cosine" + case "dot": + metric = "Dot" + case "euclidean": + metric = "Euclidean" + } + options = append(options, &ast.VectorMetricIndexOption{ + OptionKind: "VectorMetric", + MetricType: metric, + }) + case "TYPE": + vtype := strings.Trim(valueToken.Literal, "'") + if strings.EqualFold(vtype, "DiskANN") { + vtype = "DiskANN" + } + options = append(options, &ast.VectorTypeIndexOption{ + OptionKind: "VectorType", + VectorType: vtype, + }) case "OPTIMIZE_FOR_ARRAY_SEARCH": options = append(options, &ast.IndexStateOption{ OptionKind: "OptimizeForArraySearch", @@ -12313,6 +12341,11 @@ func (p *Parser) parseCreateIndexOptions() []ast.IndexOption { if o, ok := last.(spannable); ok { p.spanFrom(optStart, o) } + // ScriptDom spans vector index METRIC / TYPE on the option keyword only. + switch last.(type) { + case *ast.VectorMetricIndexOption, *ast.VectorTypeIndexOption: + p.tokSpan(last.(spannable), optStart) + } // ScriptDom spans BUCKET_COUNT on its value only. if eo, ok := last.(*ast.IndexExpressionOption); ok && eo.OptionKind == "BucketCount" { p.spanFromChild(eo, eo.Expression) @@ -16921,3 +16954,52 @@ func (p *Parser) parseCreateJsonIndexStatement() (*ast.CreateJsonIndexStatement, } return stmt, nil } + +// parseCreateVectorIndexStatement parses +// CREATE VECTOR INDEX name ON table (column) [WITH (options)] [ON filegroup]. +// The current token is VECTOR. +func (p *Parser) parseCreateVectorIndexStatement() (*ast.CreateVectorIndexStatement, error) { + p.nextToken() // consume VECTOR + p.nextToken() // consume INDEX + + stmt := &ast.CreateVectorIndexStatement{Name: p.parseIdentifier()} + + if p.curTok.Type != TokenOn { + return nil, fmt.Errorf("expected ON in CREATE VECTOR INDEX, got %s", p.curTok.Literal) + } + p.nextToken() // consume ON + onName, err := p.parseSchemaObjectName() + if err != nil { + return nil, err + } + stmt.OnName = onName + + if p.curTok.Type != TokenLParen { + return nil, fmt.Errorf("expected ( in CREATE VECTOR INDEX, got %s", p.curTok.Literal) + } + p.nextToken() // consume ( + stmt.VectorColumn = p.parseIdentifier() + if p.curTok.Type != TokenRParen { + return nil, fmt.Errorf("expected ) in CREATE VECTOR INDEX, got %s", p.curTok.Literal) + } + p.nextToken() // consume ) + + if p.curTok.Type == TokenWith { + p.nextToken() // consume WITH + stmt.IndexOptions = p.parseCreateIndexOptions() + } + + if p.curTok.Type == TokenOn { + p.nextToken() // consume ON + fg, err := p.parseFileGroupOrPartitionScheme() + if err != nil { + return nil, err + } + stmt.OnFileGroupOrPartitionScheme = fg + } + + if p.curTok.Type == TokenSemicolon { + p.nextToken() + } + return stmt, nil +} diff --git a/parser/testdata/Baselines170_VectorIndexTests170/metadata.json b/parser/testdata/Baselines170_VectorIndexTests170/metadata.json index ef120d97..0967ef42 100644 --- a/parser/testdata/Baselines170_VectorIndexTests170/metadata.json +++ b/parser/testdata/Baselines170_VectorIndexTests170/metadata.json @@ -1 +1 @@ -{"todo": true} +{} diff --git a/parser/testdata/Baselines170_VectorSearchApproximateTests170/metadata.json b/parser/testdata/Baselines170_VectorSearchApproximateTests170/metadata.json index ef120d97..0967ef42 100644 --- a/parser/testdata/Baselines170_VectorSearchApproximateTests170/metadata.json +++ b/parser/testdata/Baselines170_VectorSearchApproximateTests170/metadata.json @@ -1 +1 @@ -{"todo": true} +{} diff --git a/parser/testdata/Baselines170_VectorSearchCrossApplyTests170/metadata.json b/parser/testdata/Baselines170_VectorSearchCrossApplyTests170/metadata.json index ef120d97..0967ef42 100644 --- a/parser/testdata/Baselines170_VectorSearchCrossApplyTests170/metadata.json +++ b/parser/testdata/Baselines170_VectorSearchCrossApplyTests170/metadata.json @@ -1 +1 @@ -{"todo": true} +{} diff --git a/parser/testdata/Baselines170_VectorSearchOptionalTopNTests170/metadata.json b/parser/testdata/Baselines170_VectorSearchOptionalTopNTests170/metadata.json index ef120d97..0967ef42 100644 --- a/parser/testdata/Baselines170_VectorSearchOptionalTopNTests170/metadata.json +++ b/parser/testdata/Baselines170_VectorSearchOptionalTopNTests170/metadata.json @@ -1 +1 @@ -{"todo": true} +{} diff --git a/parser/testdata/VectorIndexTests170/metadata.json b/parser/testdata/VectorIndexTests170/metadata.json index ef120d97..0967ef42 100644 --- a/parser/testdata/VectorIndexTests170/metadata.json +++ b/parser/testdata/VectorIndexTests170/metadata.json @@ -1 +1 @@ -{"todo": true} +{} diff --git a/parser/testdata/VectorSearchApproximateTests170/metadata.json b/parser/testdata/VectorSearchApproximateTests170/metadata.json index ef120d97..0967ef42 100644 --- a/parser/testdata/VectorSearchApproximateTests170/metadata.json +++ b/parser/testdata/VectorSearchApproximateTests170/metadata.json @@ -1 +1 @@ -{"todo": true} +{} diff --git a/parser/testdata/VectorSearchCrossApplyTests170/metadata.json b/parser/testdata/VectorSearchCrossApplyTests170/metadata.json index ef120d97..0967ef42 100644 --- a/parser/testdata/VectorSearchCrossApplyTests170/metadata.json +++ b/parser/testdata/VectorSearchCrossApplyTests170/metadata.json @@ -1 +1 @@ -{"todo": true} +{} diff --git a/parser/testdata/VectorSearchOptionalTopNTests170/metadata.json b/parser/testdata/VectorSearchOptionalTopNTests170/metadata.json index ef120d97..0967ef42 100644 --- a/parser/testdata/VectorSearchOptionalTopNTests170/metadata.json +++ b/parser/testdata/VectorSearchOptionalTopNTests170/metadata.json @@ -1 +1 @@ -{"todo": true} +{}