Skip to content

SQL-3293: Implement algebrization for HigherOrderFunction expressions - #181

Open
mattChiaravalloti wants to merge 14 commits into
mongodb:mainfrom
mattChiaravalloti:SQL-3293
Open

SQL-3293: Implement algebrization for HigherOrderFunction expressions#181
mattChiaravalloti wants to merge 14 commits into
mongodb:mainfrom
mattChiaravalloti:SQL-3293

Conversation

@mattChiaravalloti

Copy link
Copy Markdown
Collaborator

This PR implements algebrization for HigherOrderFunction expressions (including the disambiguation of unqualified identifiers into Variables), as described by the design doc. This builds off of the refactor that was previously merged in #179 .

This PR touched 10 files, but 6 of those files are simply incidental changes because of updated test macro apis and imports.

The relevant changes are in algebrizer/definitions.rs, where the actual algebrization implementations are written, and in algebrizer/tests/expressions/{higher_order_functions.rs, identifier_and_subpath.rs}, where the unit tests for all new algebrization is written.

Similar to schema-checking for higher order functions, this implementation introduces a HigherOrderFunctionWrapper error variant that wraps algebrizer errors in some additional context to help users understand (1) that an issue happened within a higher order function and (2) what specific component of the higher order function caused it. Fortunately, wrapping errors in the algebrizer is much more straightforward than in the schema checking module.

Building off of #179 , I added an additional field to the ExpressionContext that tracks if we are in a higher order function argument context and, if so, which one. Recall from the design doc that since we are not allowing users to specify variable names (they must use this and value) we also implicitly accepted that shadowing will happen if higher order functions are nested within each other. Keep that in mind when you observe uses of the new context field! Notice that we never actually "restore" previous context since it will never really matter! A higher order function function argument is algebrized with a new algebrizer with properly set state, so subsequent algebrization afterwards is unaffected by its state since it uses a different algebrizer struct.

@mattChiaravalloti
mattChiaravalloti requested a review from a team as a code owner July 28, 2026 21:09
Comment on lines +556 to +557
expression_context = ExpressionContext::default()
.with_higher_order_function_arg_ctx(HigherOrderFunctionCtx::Map),

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This mimics how algebrization within the context of a Map expression works. It's saying "if the ExpressionContext indicates we're in a Map, how would we algebrize this unqualified Identifier"?

Comment on lines +573 to +575
variables = map! {
"this" => Schema::Atomic(Atomic::Integer),
},

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Keep an eye on the variables presence/value in these tests! If it is present and does not indicate the variable is nullable, is_nullable is false. If it is not present or is present and indicates the variable is nullable, is_nullable is true.

allow_order_by_missing_columns: bool,
clause_type: RefCell<ClauseType>,
expression_context: ExpressionContext,
variables: BTreeMap<&'static str, schema::Schema>,

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Similar to SchemaInferenceState, we need to track which variables are in scope. I considered putting this in the ExpressionContext but ultimately decided against it for reasons I can no longer justify. I think it may make more sense to put this ExpressionContext but I'm not sure... Curious to hear your thoughts!

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.

1 participant