Skip to content

Commit f782230

Browse files
committed
feat: add output collection metadata and prevent attaching to output collections
- Add chroma:attached_function_id metadata when creating output collections - Add check in AttachFunction to prevent attaching functions to output collections - Add ErrCannotAttachToOutputCollection error
1 parent edab0e1 commit f782230

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

go/pkg/common/constants.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@ package common
33
const (
44
DefaultTenant = "default_tenant"
55
DefaultDatabase = "default_database"
6+
7+
// SourceAttachedFunctionIDKey is the metadata key used to mark output collections
8+
// and link them to their attached function.
9+
SourceAttachedFunctionIDKey = "chroma:source_attached_function_id"
610
)

go/pkg/common/errors.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,12 @@ var (
4949
ErrUnknownSegmentMetadataType = errors.New("segment metadata value type not supported")
5050

5151
// AttachedFunction errors
52-
ErrAttachedFunctionAlreadyExists = errors.New("the attached function that was being created already exists for this collection")
53-
ErrAttachedFunctionNotFound = errors.New("the requested attached function was not found")
54-
ErrAttachedFunctionNotReady = errors.New("the requested attached function exists but is still initializing")
55-
ErrInvalidAttachedFunctionName = errors.New("attached function name cannot start with reserved prefix '_deleted_'")
56-
ErrHeapServiceNotEnabled = errors.New("heap service is not enabled")
52+
ErrAttachedFunctionAlreadyExists = errors.New("the attached function that was being created already exists for this collection")
53+
ErrAttachedFunctionNotFound = errors.New("the requested attached function was not found")
54+
ErrAttachedFunctionNotReady = errors.New("the requested attached function exists but is still initializing")
55+
ErrInvalidAttachedFunctionName = errors.New("attached function name cannot start with reserved prefix '_deleted_'")
56+
ErrHeapServiceNotEnabled = errors.New("heap service is not enabled")
57+
ErrCannotAttachToOutputCollection = errors.New("cannot attach function to an output collection")
5758

5859
// Function errors
5960
ErrFunctionNotFound = errors.New("function not found")

go/pkg/sysdb/coordinator/task.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,16 @@ func (s *Coordinator) AttachFunction(ctx context.Context, req *coordinatorpb.Att
158158
return common.ErrCollectionNotFound
159159
}
160160

161+
// Check if input collection is an output collection (prevent chaining)
162+
inputCollection := collections[0]
163+
for _, meta := range inputCollection.CollectionMetadata {
164+
if meta.Key != nil && *meta.Key == common.SourceAttachedFunctionIDKey {
165+
log.Error("AttachFunction: cannot attach function to an output collection",
166+
zap.String("input_collection_id", req.InputCollectionId))
167+
return common.ErrCannotAttachToOutputCollection
168+
}
169+
}
170+
161171
// Serialize params
162172
var paramsJSON string
163173
if req.Params != nil {
@@ -574,14 +584,19 @@ func (s *Coordinator) FinishCreateAttachedFunction(ctx context.Context, req *coo
574584

575585
// 5. Create the output collection with segments
576586
dimension := int32(1) // Default dimension for attached function output collections
587+
588+
// Add metadata to mark this as an output collection
589+
outputCollectionMetadata := model.NewCollectionMetadata[model.CollectionMetadataValueType]()
590+
outputCollectionMetadata.Add(common.SourceAttachedFunctionIDKey, &model.CollectionMetadataValueStringType{Value: attachedFunctionID.String()})
591+
577592
collection := &model.CreateCollection{
578593
ID: collectionID,
579594
Name: attachedFunction.OutputCollectionName,
580595
ConfigurationJsonStr: "{}", // Empty JSON object for default config
581596
TenantID: attachedFunction.TenantID,
582597
DatabaseName: database.Name,
583598
Dimension: &dimension,
584-
Metadata: nil,
599+
Metadata: outputCollectionMetadata,
585600
}
586601

587602
// Create segments for the collection (distributed setup)

0 commit comments

Comments
 (0)