Skip to content

fix: preserve analyzer rewrites and simplify session instrumentation - #67

Merged
geoffreyclaude merged 2 commits into
mainfrom
fix/instrument-session-builder
Oct 8, 2026
Merged

geoffreyclaude merged 2 commits into
mainfrom
fix/instrument-session-builder

Conversation

@geoffreyclaude

@geoffreyclaude geoffreyclaude commented Sep 19, 2026 •

Copy link
Copy Markdown
Collaborator

Applying rule instrumentation to a SessionState currently drops registered analyzer function rewrites. When physical optimizer phase tracing is enabled, it also rebuilds the session twice to install tracing.

This PR preserves the analyzer's rewrites and installs rule and planner tracing during one rebuild. The two commits can be reviewed independently.

Preserve analyzer function rewrites

An analyzer contains both analyzer rules and function rewrites. The rewrites transform expressions before the analyzer rules run.

The current instrumentation code calls with_analyzer_rules to install tracing wrappers around the rules:

SessionStateBuilder::from(state)
    .with_analyzer_rules(instrumented_rules)

Although the builder initially carries the existing analyzer, with_analyzer_rules replaces it with a new analyzer whose function rewrite list is empty. This can change query results or cause queries to fail after instrumentation is applied.

For a simple example, suppose an application registers a rewrite that changes the literal 1 to 2. SELECT 1 AS value should return 2 both before and after instrumentation. Previously, instrumentation removed the rewrite, so the query returned 1 instead.

The fix keeps the existing analyzer and replaces only its rules:

let mut builder = SessionStateBuilder::from(state);
builder.analyzer().get_or_insert_default().rules = instrumented_rules;

This preserves the registered rewrite instances and their order. The regression test registers a rewrite, applies instrumentation, and checks that the rewrite is retained.

Install rule and planner instrumentation in one rebuild

When physical optimizer phase tracing is enabled, instrumentation currently rebuilds the state once to install the rule wrappers, then rebuilds it again to install the tracing query planner:

Before: existing state -> rebuild with rule tracing -> rebuild with planner tracing
After:  existing state -> rebuild with rule and planner tracing

Each rebuild reconstructs the function registries and registers the functions again. The fix wraps the state's existing query planner on the same builder used to install the rule wrappers, then builds once. This avoids one round of registry reconstruction and function registration.

Custom query planners continue to be wrapped, and planner tracing remains conditional on physical optimizer phase tracing being enabled.

Function alias precedence during DataFusion's registry reconstruction remains a separate issue: this change still performs one reconstruction.

@geoffreyclaude
geoffreyclaude force-pushed the fix/instrument-session-builder branch from 90bbd98 to 1841562 Compare September 20, 2026 02:50
@geoffreyclaude
geoffreyclaude force-pushed the fix/instrument-session-builder branch from 1841562 to fbd4b51 Compare September 20, 2026 15:29
@geoffreyclaude geoffreyclaude changed the title perf: instrument session builders without rebuilding them fix: preserve analyzer rewrites and simplify session instrumentation Sep 20, 2026
@geoffreyclaude
geoffreyclaude force-pushed the fix/instrument-session-builder branch from fbd4b51 to f8ec319 Compare October 8, 2026 13:47
@geoffreyclaude
geoffreyclaude merged commit d45b2fa into main Oct 8, 2026
4 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.

1 participant