Skip to content

[ENH]: Allow cascading functions#7129

Merged
tanujnay112 merged 2 commits into
mainfrom
fn_on_output
May 27, 2026
Merged

[ENH]: Allow cascading functions#7129
tanujnay112 merged 2 commits into
mainfrom
fn_on_output

Conversation

@tanujnay112
Copy link
Copy Markdown
Contributor

@tanujnay112 tanujnay112 commented May 25, 2026

Description of changes

This PR allows "cascading" (chained) functions: attaching a function whose input collection is itself the output of another function. Previously this was unconditionally blocked. Now it's allowed only when every upstream function writing to that collection is async. Sync upstream functions still block the attachment.

Two additional safety checks are also introduced:

  • Cycle detection: prevents creating a cycle like A → B → C → A
  • Depth limit: rejects chains deeper than maxAttachedFunctionDepth = 5

Improvements & Bug fixes

  • ...

New functionality

  • ...

Test plan

How are these changes tested?

test_task_api.py has been adjusted for ths PR. Tests have been added and adjusted in that file.

  • Tests pass locally with pytest for python, yarn test for js, cargo test for rust

Migration plan

Are there any migrations, or any forwards/backwards compatibility changes needed in order to make sure this change deploys reliably?

Observability plan

What is the plan to instrument and monitor this change?

Documentation Changes

Are all docstrings for user-facing APIs updated if required? Do we need to make documentation changes in the _docs section?_

Copy link
Copy Markdown
Contributor Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@github-actions
Copy link
Copy Markdown

Reviewer Checklist

Please leverage this checklist to ensure your code review is thorough before approving

Testing, Bugs, Errors, Logs, Documentation

  • Can you think of any use case in which the code does not behave as intended? Have they been tested?
  • Can you think of any inputs or external events that could break the code? Is user input validated and safe? Have they been tested?
  • If appropriate, are there adequate property based tests?
  • If appropriate, are there adequate unit tests?
  • Should any logging, debugging, tracing information be added or removed?
  • Are error messages user-friendly?
  • Have all documentation changes needed been made?
  • Have all non-obvious changes been commented?

System Compatibility

  • Are there any potential impacts on other parts of the system or backward compatibility?
  • Does this change intersect with any items on our roadmap, and if so, is there a plan for fitting them together?

Quality

  • Is this code of a unexpectedly high quality (Readability, Modularity, Intuitiveness)

@tanujnay112 tanujnay112 marked this pull request as ready for review May 25, 2026 22:07
@blacksmith-sh

This comment has been minimized.

@blacksmith-sh

This comment has been minimized.

@blacksmith-sh

This comment has been minimized.

return globalDB.WithContext(ctx)
}

func TryGetDB(ctx context.Context) *gorm.DB {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

return maxDepth, nil
}

func (g *attachedFunctionGraphState) incomingCollectionIDs(collectionID string) ([]string, error) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make graph tests

Comment thread go/pkg/sysdb/coordinator/task.go Outdated
coordinator *Coordinator
ctx context.Context
databaseName string
incomingByOutput map[string][]*dbmodel.AttachedFunction
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be good to comment these fields

return incoming, nil
}

incoming, err := g.coordinator.catalog.metaDomain.AttachedFunctionDb(g.ctx).GetAttachedFunctions(nil, nil, nil, &collectionID, nil, false)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this an indexed lookup, is that a concern if not?

return outgoing, nil
}

outgoing, err := g.coordinator.catalog.metaDomain.AttachedFunctionDb(g.ctx).GetAttachedFunctions(nil, nil, &collectionID, nil, nil, false)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this an indexed lookup, is that a concern if not?


// Rebuild the graph under locks before validating/inserting.
graphState, err = s.buildAttachFunctionGraph(txCtx, req.InputCollectionId, existingOutputCollectionID, req.Database)
if err != nil {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didnt detect that it changed??

return err
}

// Rebuild the graph under locks before validating/inserting.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like we just lock and rebuild?

}

// Check if input collection is being used as an output collection by any attached function
inputCollectionIDStr := req.InputCollectionId
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would appreciate some commentary here

functionIDs := make([]uuid.UUID, 0, len(attachedFunctionsUsingAsOutput))
seenFunctionIDs := make(map[uuid.UUID]struct{}, len(attachedFunctionsUsingAsOutput))
for _, attachedFunction := range attachedFunctionsUsingAsOutput {
if _, ok := seenFunctionIDs[attachedFunction.FunctionID]; ok {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more commenting in this block, it's a bit unreadable

Comment thread go/pkg/sysdb/coordinator/task.go Outdated
inputCollectionIDStr := req.InputCollectionId
attachedFunctionsUsingAsOutput, err := s.catalog.metaDomain.AttachedFunctionDb(txCtx).GetAttachedFunctions(nil, nil, nil, &inputCollectionIDStr, nil, false)
// Check if output collection already exists so we can materialize and then lock the full graph in a stable order.
existingOutputCollections, err := s.catalog.metaDomain.CollectionDb(txCtx).GetCollections(nil, &req.OutputCollectionName, req.TenantId, req.Database, nil, nil, false)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this singular?

Comment thread go/pkg/sysdb/coordinator/task.go Outdated
inputCollectionIDStr := req.InputCollectionId
attachedFunctionsUsingAsOutput, err := s.catalog.metaDomain.AttachedFunctionDb(txCtx).GetAttachedFunctions(nil, nil, nil, &inputCollectionIDStr, nil, false)
// Check if output collection already exists so we can materialize and then lock the full graph in a stable order.
existingOutputCollections, err := s.catalog.metaDomain.CollectionDb(txCtx).GetCollections(nil, &req.OutputCollectionName, req.TenantId, req.Database, nil, nil, false)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plurality is confusing


outputTailDepth := 0
if len(existingOutputCollections) > 0 {
outputTailDepth, err = graphState.collectionTailDepth(existingOutputCollectionID, map[string]int{}, map[string]struct{}{})
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be in 547's if block

Comment thread go/pkg/sysdb/coordinator/task.go Outdated
}

outputTailDepth := 0
if len(existingOutputCollections) > 0 {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like it can be collapes with reaches check block above

@tanujnay112 tanujnay112 force-pushed the fn_on_output branch 3 times, most recently from cd63962 to f147dab Compare May 27, 2026 05:06
@blacksmith-sh

This comment has been minimized.

@tanujnay112 tanujnay112 force-pushed the fn_on_output branch 2 times, most recently from 2223dbb to ea10072 Compare May 27, 2026 06:52
@blacksmith-sh

This comment has been minimized.

@tanujnay112 tanujnay112 enabled auto-merge (squash) May 27, 2026 09:08
@tanujnay112 tanujnay112 merged commit ed3c9d0 into main May 27, 2026
119 of 121 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants