fix(rust/sedona-query-planner): recognize wrapped RS_EnsureLoaded in idempotency guard#976
Merged
paleolimbot merged 1 commit intoJun 18, 2026
Conversation
…idempotency guard The EnsureLoaded optimizer rule and the async-UDF metadata wrapper run in the same fixpoint loop: the rule injects RS_EnsureLoaded around a raster argument, then the wrapper re-stamps that async call as sd_restore_metadata(RS_EnsureLoaded(...)). On the next pass the idempotency guard only matched a bare RS_EnsureLoaded, so it re-injected another wrapper, and so on each pass. The result is a tower of nested async calls where only the innermost is extracted into AsyncFuncExec; the outer ones remain inline and are invoked synchronously, failing with "async functions should not be called directly". Teach the guard to see through the sd_restore_metadata wrapper so an already-loaded argument is left alone. Add a regression test covering the cross-rule fixpoint, which the existing single-rule idempotency test could not exercise.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Theres 2 bugs here:
This solves 1 so 2 doesnt trigger.
🤖 AI-generated description
Summary
EnsureLoadedOptimizerRulewraps the raster arguments ofneeds_pixelsUDFs (e.g.RS_Slice,RS_DimBand) withRS_EnsureLoaded, andWrapAsyncUdfRulere-stamps that async call assd_restore_metadata(RS_EnsureLoaded(...))to preserve field metadata. Both rules run in the same logical-optimizer fixpoint loop. The idempotency guardis_loaded_wraponly recognized a bareRS_EnsureLoaded, not one already wrapped insd_restore_metadata, so on every subsequent pass it failed to see the argument as already-loaded and injected another wrapper. DataFusion runs the rule list repeatedly until the plan stops changing or it hitsmax_passes(default 3), so the steady state is three nestedRS_EnsureLoadedcalls.This regressed in #969, which split metadata handling into the separate
sd_restore_metadatawrapper but did not teach the guard to look through it. It went unnoticed because there is no end-to-end execution test forneeds_pixelsfunctions — the Rust tests invoke kernels directly and never run a real query plan, and #969's own idempotency test runs only the single rule, never withWrapAsyncUdfRulein between.Why the nesting is fatal (a DataFusion limitation)
A redundant tower of an idempotent function would normally be harmless waste. It is fatal here because
RS_EnsureLoadedis an async UDF, and DataFusion cannot evaluate an async UDF inside a synchronous expression. Instead it hoists each async call out into a dedicatedAsyncFuncExecoperator, computes it as a precomputed column (__async_fn_N), and rewrites the surrounding expression to reference that column.That hoist does not correctly handle an async UDF nested as an argument to another async UDF call.
AsyncMapper::find_referenceswalks the expression pre-order and registers every nested async call as a separateasync_expr(so it clearly intends to support nesting), butAsyncMapper::map_exprsubstitutes by whole-subtree structural equality and is applied bottom-up viatransform_up. Substituting the innermost call mutates every enclosing subtree, so they no longer structurally match theasync_exprregistered for them, and only the innermost call is replaced with a column reference. The outerRS_EnsureLoadedcalls remain literal inside the synchronousProjectionExecand are invoked directly at runtime, failing withInternal error: async functions should not be called directly. The two halves of DataFusion's async extraction are thus mutually inconsistent under nesting — but this only surfaces when something generates nested async calls, which normal usage does not and the fixpoint regression above did.Fix
Teach
is_loaded_wrapto see through thesd_restore_metadatawrapper so an already-loaded argument is recognized and left alone. The rule then reaches a fixpoint after the first pass and produces a singleRS_EnsureLoaded, which the async hoister handles correctly. Adds a regression test exercising the cross-rule fixpoint that the existing single-rule idempotency test could not reproduce.