Source-linked AI summary
SAGE: A Unified Algebra and Self-Adaptive Execution for AI Functions in SQL
Xiangqi Wang, Nhan H. Pham, Oktie Hassanzadeh, Dharmashankar Subramanian, Xiangliang Zhang
TL;DR
AI functions expose diverse APIs despite sharing three relational roles, leaving execution to balance model quality, cost, and latency across different physical problems. SAGE unifies them as AI_SCALAR, AI_AGG, and AI_JOIN with shared adaptive execution and primitive-specific planning. Across audits and workloads it reports broad coverage, strongest overall SemBench performance, and a 358-fold measured cost reduction on a representative factorable join.
Problem
AI functions have diverse APIs and different scalar, aggregate, and join optimization problems, while fixed cascades cannot reliably balance quality and model cost across varying inputs.
Method
SAGE compiles query-time AI functions into AI_SCALAR, AI_AGG, and AI_JOIN, then uses shared confidence-gated adaptation and constrained whole-plan configuration selection.
Results
SAGE achieves the best SemBench average and best or tied-best quality on every updated join except Q5; one factorable join drops from 16,256 to 128 generative calls, a measured 358× cost reduction.
Takeaways & Limitations
The results support a small logical interface with guarded, primitive-specific execution rather than one shared implementation for all primitives.
Takeaways & Limitations
The coverage result is empirical and excludes recursive agents, training, side effects, raw multimodal conversion, and index construction; probing must also be amortized over enough inputs.
Abstract
from arXiv · showhide
SQL systems increasingly expose AI functions for tasks such as classification, extraction, filtering, ranking, retrieval, joining, and summarization. Despite their diverse APIs, these functions play only three relational roles: transforming individual rows, aggregating groups, or generating relationships between row pairs. We present SAGE (Self-Adaptive Generative Execution), a unified logical and physical framework that captures these roles with three typed primitives, AI_SCALAR, AI_AGG, and AI_JOIN, and composes them naturally with standard relational operators. All primitives share a confidence-gated execution interface while supporting physical strategies tailored to their relational shape. The main challenge is AI_JOIN, where SAGE analyzes the predicate, decomposes compound conditions when possible, and uses a recipe card together with a small label-free probe to select among complete execution strategies. Across a broad audit of public AI operators and evaluations spanning scalar, aggregate, and join workloads, this formulation covers common AI functionality while consistently improving execution quality and efficiency. SAGE achieves the strongest overall SemBench performance and, on a representative factorable join, reduces pairwise model calls by more than two orders of magnitude, yielding a 358-fold measured cost reduction.
1 Introduction
SAGE reduces diverse query-time AI functions to three relational roles and combines a shared adaptive interface with primitive-specific execution strategies. It evaluates broad operator coverage and workloads, reporting strong quality and substantial savings on a representative factorable join.
- Unified formulation: AI_SCALAR maps rows to values, AI_AGG reduces groups to values, and AI_JOIN determines which row pairs survive.More complex tasks compose these primitives with standard relational operators.
- Adaptive execution: SAGE uses a shared confidence-gated cascade while tailoring physical front ends to scalar, aggregate, and join execution problems.The framework separates typed logical compilation from physical adaptation.
- Coverage and evaluation: The formulation maps 63 of 78 audited model-invoking operators directly to the three primitives, while 94.6% of 37 semantic-query intents require at most two primitives.The audit covers eleven public systems, and no evaluated intent requires more than three primitives.
- Coverage and evaluation: SAGE obtains the best SemBench average and best or tied-best quality on every updated join except Q5.The evaluation spans scalar, aggregate, join, and ranking workloads across SemBench and Multi-XScience.
- Coverage and evaluation: 358× measured cost reduction results when a representative exact all-pairs execution falls from 16,256 generative calls to 128.This result comes from the join specialization on one factorable join.
- Adaptive execution: A label-free probe races complete operator-specific configurations and freezes a quality-maximizing plan under cost and latency budgets.Configuration selection is performed at the whole-plan level rather than independently for isolated operators.
2 A Unified Algebra for AI Functions
SAGE defines AI functions by their role in relational dataflow: row transformation, group reduction, or pairwise judgment. It then composes these typed primitives with SQL and selects constrained physical configurations for complete plans.
- AI_SCALAR: AI_SCALAR evaluates each bound row independently and preserves cardinality by appending a virtual output column.Its outputs can feed WHERE, SELECT, ORDER BY, or later primitives.
- AI_AGG: AI_AGG evaluates a bag of tuples sharing a group key and reduces N rows to G group outputs.Summaries, group-level judgments, semantic counts, consensus extraction, and theme lists instantiate this role.
- Primitive distinctions: The three primitives are distinguished by relational dataflow roles rather than prompt arity: row-to-value, group-to-value, and pair-to-predicate.Their principal risks are per-item error, lost evidence, and pair explosion, respectively.
- AI_JOIN: AI_JOIN is a Boolean judgment over candidate row pairs, covering matching, relevance, similarity, membership, and natural-language relations.Exposing the N×M pair space lets the optimizer enumerate, block, retrieve, or factor candidates before model invocation.
- Plan-level optimization: SAGE selects one physical configuration per AI node to maximize task quality while satisfying monetary cost and end-to-end latency budgets.The optimizer evaluates complete plans using Q(q,a), C(q,a), and L(q,a).
- Plan-level optimization: Plan-level optimization materializes reusable scalar values, pushes lossless selective predicates early, and avoids partial execution of holistic aggregates.These choices reflect downstream cost and the possibility that lossy operations irreversibly remove evidence.
3 Primitive-Aware Physical Execution
SAGE separates logical AI-function roles from primitive-specific physical execution while sharing confidence-gated escalation across rows, groups, and pair spaces. AI_JOIN additionally uses predicate-aware routing, decomposition, and cached recipe cards to reduce unnecessary pairwise model work.
- Shared adaptive skeleton: SAGE shares confidence, escalation, and configuration mechanisms across primitives while preserving distinct optimization needs for rows, groups, and pair spaces.The shared execution skeleton adapts effort without collapsing the different physical requirements of scalar, aggregate, and join operations.
- Shared adaptive skeleton: TokenSAR weights answer-relevant tokens, and its calibrated confidence signal improves routing over random escalation under the same large-model budget.SAGE uses answer-token confidence for label-valued functions and relevance-weighted confidence for free-form outputs.
- AI_SCALAR: AI_SCALAR executes independently per row, enabling batching, caching, vectorized inference, and escalation only for low-confidence outputs.A cheap labeler may front-end closed-set classification, while extraction and rewriting begin with the first generative model.
- AI_AGG: AI_AGG optionally compacts near-duplicate group evidence before entering the shared cascade, but compaction is approximate and chunking is exact only for modular or algebraic aggregates.Holistic median-like judgments and cross-document summaries generally require retaining the full group.
- AI_JOIN: AI_JOIN selects membership, relational, or reasoning routes from a cached recipe card, with only uncertain residual pairs entering the shared cascade.The route choice is predicate-aware: factorable predicates can use row-local virtual columns, relational predicates can filter the pair grid, and reasoning routes conservatively prune only high-margin negatives.
- AI_JOIN: A factorable membership join reduces predicate work from |A||B| pair evaluations to |A| + |B| row evaluations by materializing row-local virtual columns and applying an ordinary hash join.The transformation is exact if the rewrite and predicted row values are correct; model error remains ordinary virtual-column error.
4 Test-Time Configuration Selection
SAGE selects a complete physical configuration per query by proposing a small slate of valid plans and racing them on a shared, label-free probe. The probe estimates quality, cost, and latency, while the selection process respects execution budgets and adapts to observed outcomes.
- Configuration spaces and slates: Each AI node admits only contract-preserving physical configurations, with scalar, aggregate, and join candidates exposing different strategy parameters.Join candidates include recipe routing, blocking or factorization, and front-end cutoff choices; aggregate candidates additionally include compaction and partial reduction.
- Configuration spaces and slates: The joint configuration space is too large for exhaustive execution, so each primitive proposer emits cheap, quality-oriented, and exploratory candidates.For multi-node queries, compatible node candidates form a slate of at most K whole-plan configurations.
- Probe-and-race selection: SAGE evaluates every slate candidate on the same stratified probe, recording quality, token usage, cost, latency, and extrapolated full-query resource use.Composed queries execute the sampled subplan far enough to capture downstream effects, without using benchmark gold labels at deployment.
- Probe-and-race selection: An iRace-style procedure progressively eliminates inferior or budget-infeasible candidates, then freezes the surviving whole-plan configuration for full execution.Probe outcomes also update the proposer for future queries, making selection adaptive across queries.
- Probe-and-race selection: On SemBench Q5, quality varies substantially across front-end and escalation thresholds, supporting per-query selection rather than fixed thresholds.Additional validation reports stabilization with a small probe, high near-oracle slate coverage, and close tracking between the label-free proxy and gold oracle.
5 Evaluation
SAGE is evaluated across scalar, aggregate, join, ranking, and cross-domain workloads using unified quality, cost, and latency measurements. It leads the SemBench average, performs especially strongly on selective and factorable joins, and its adaptive mechanisms improve quality–cost tradeoffs while retaining measurable proxy-selection error.
- Setup and unified evaluation: The evaluation combines SemBench Q1–Q10, Multi-XScience, and compound-predicate workloads to test cross-primitive quality, efficiency, decomposition, mechanisms, and label-free estimation.SemBench covers classification, aggregation, joins, and ranking; Multi-XScience adds aggregation.
- Cross-primitive and per-query gains (RQ1): SAGE leads the SemBench average at 0.908, ties on MXS, and achieves best or tied-best updated-join quality except Q5.Gains are largest on Q2, Q7, Q9, FewRel, and BRIGHT; costs and latency include probing, judging, recipe generation, and execution.
- Performance at Confounded Predicate (RQ2): 0.846 mean F1 is achieved by selective-first SAGE on confounded predicates, while SAGE (No Racing) costs $0.002–$0.007 versus $0.009–$0.031.The first atom prunes the pair space, and removing an atom or decomposition degrades precision or nearly doubles pairwise calls.
- Scalability: 16,256 pairwise calls are replaced by 128 calls, reducing wall-clock time by 89× and measured cost by 358× on a representative factorable join.Across Q6/Q7 scaling sweeps, F1 remains stable while cost and latency fall by one to three orders of magnitude versus always-large execution.
- Ablation across mechanisms (RQ3): Routing and confidence mechanisms materially affect results: removing routing makes membership a $0.563 pairwise scan and reduces FewRel F1 to 0.039.Using mean log probability also reduces membership F1 from 0.850 to 0.634 and reasoning F1 from 0.805 to 0.746.
- Ablation across mechanisms (RQ3): At K=3, the learned proposer finds a near-oracle configuration on 60.8% of replay queries versus 3.4% for a uniform slate.Cold-reset history falls to chance, whereas warm history sustains the hit rate.
- Validation of Estimators (RQ4): Proxy-selected configurations average 0.843 quality versus 0.892 for gold selection, quantifying residual label-free routing and selection error.The largest gap occurs on BRIGHT.
- Cross-domain recovery: 80.8% cross-domain recovery is obtained by SAGE, compared with 73.5% for LOTUS and 60.1% for FDJ across twelve directions.SAGE leads or ties on eight directions using only unlabeled target probes.
6 Related Work
SAGE builds on semantic-query systems that expose model-backed relational operations and on structure-aware execution methods for reducing pairwise search. Its distinction is combining predicate-aware physical choices with query-local, label-free selection of a complete configuration.
- AI functions and relational structure: Existing systems expose model-backed mapping, filtering, extraction, retrieval, joining, and aggregation, while production warehouses provide similarly broad AI-SQL suites.SAGE organizes these diverse functions by relational role rather than user-facing API names.
- Structure-aware and adaptive execution: Semantic-join systems reduce naive |A||B| search through retrieval, batching, approximation, decomposition, or per-table factorization.SAGE guards these choices by predicate structure and retains pair semantics when a join cannot factor.
- Structure-aware and adaptive execution: Across the Q6/Q7 scaling sweeps, F1 stays stable while cost and latency decrease by one to three orders of magnitude versus always-large execution.The comparison is based on completed operating points under the AI-Join protocol.
- Structure-aware and adaptive execution: Every mechanism carries weight in the component ablation, with routing removal producing the largest failure across the evaluated join classes.The ablation compares SemBench, FewRel, and BRIGHT joins using quality and dollar cost.
- Structure-aware and adaptive execution: SAGE combines item-level answer-relevance confidence with a query-local probe that selects and freezes a complete physical configuration.This extends model cascades from per-item routing toward plan-level adaptive execution.
Conclusion
SAGE unifies audited single-pass, query-time AI functions through three model-invocation signatures while preserving primitive-specific physical execution. Within the evaluated scope, it achieves strong benchmark and join-efficiency results, with explicit boundaries on coverage and transfer.
- Conclusion: SAGE replaces surface-specific APIs with row-to-value, group-to-value, and pair-to-predicate signatures.These correspond to AI_SCALAR, AI_AGG, and AI_JOIN, while physical planning preserves each primitive’s correctness obligations.
- Conclusion: The audited coverage excludes recursive agents, training, side effects, raw multimodal conversion, and index construction.
- Conclusion: About fifty probed samples stabilize the selected AI_JOIN configuration while deployed F1 rises from 0.685 to 0.746.
- Conclusion: At deployed slate size K=3, the proposer reaches 60.8% near-oracle hit rate versus 3.4% for a uniform slate.
- Conclusion: 358× measured cost reduction accompanies replacing 16,256 pairwise calls with 128 on a representative factorable join.
A.1 Probe-Set Sensitivity of Recipe-Card Generation
Recipe-card generation is generally stable with small probes, but sensitivity varies by workload. Five row pairs are therefore used as the default probe-set size.
- A.1 Probe-Set Sensitivity of Recipe-Card Generation: The evaluation varies probe sets over n∈{1, 2, 3, 5} row pairs with eight independently sampled sets per workload and size.
- A.1 Probe-Set Sensitivity of Recipe-Card Generation: Recipe-card agreement compares operator type, featurizer, and direction while ignoring wording differences that do not change execution.
- A.1 Probe-Set Sensitivity of Recipe-Card Generation: Five probe pairs raise FewRel recipe-card agreement from 62.5% to 100%.
- A.1 Probe-Set Sensitivity of Recipe-Card Generation: Confounded predicates improve from 75.0% to 91.7% agreement as the probe grows to five pairs.
- A.1 Probe-Set Sensitivity of Recipe-Card Generation: SemBench and BRIGHT remain stable even with one sampled pair.
C Confidence Signal and Escalation
SAGE uses confidence to escalate uncertain pair decisions and selects escalation thresholds per query. In the evaluated opposite-sentiment join, confidence ordering reaches large-model quality after escalating about 40% of pairs.
- C Confidence Signal and Escalation: TokenSAR shows stronger separation than mean log-probability in the diagnostic sample and improves routing over random escalation under the same large-model budget.
- C Confidence Signal and Escalation: Confidence-ordered escalation reaches the large-model reference after escalating about 40% of pairs.
- C Confidence Signal and Escalation: The saturation point differs across predicates, motivating per-query rather than global escalation-threshold selection.
- C Confidence Signal and Escalation: The race evaluates candidate configurations on a shared probe, removes statistically inferior or infeasible options, and continues until one feasible configuration survives or the action space is exhausted.
F Proposer Replay Evaluation
The proposer replay evaluates slate selection across workload families, history conditions, probe costs, and configuration-pool quality. The surrounding implementation material specifies route types, model-role assignments, and pair-judging output modes.
- F Proposer Replay Evaluation: Replay samples 40 queries from each of four workload families, executes all 96 configurations per query, and averages curves over 20 seeds.
- F Proposer Replay Evaluation: A slate is a hit when it contains a configuration satisfying the near-oracle criterion in Eq. 12.
- F Proposer Replay Evaluation: The replay compares warm-history with cold-reset streams, measured probe costs, and varying fractions of low-quality configurations.
- F Proposer Replay Evaluation: The replay evaluates its specified stream and does not assume identical history for a new deployment domain.
- F Proposer Replay Evaluation: The recipe compiler supports per-side, joint, and reasoning routes, assigning specialized featurizers such as gliclass, gliner, numeric, embed, or glirel.
- F Proposer Replay Evaluation: Pair judging treats a relationship as true only when it clearly holds for the specific left-right pair.
I Detailed Experimental Setup
SAGE evaluates unified AI-function execution across scalar, aggregate, join, and ranking workloads using shared provenance, controlled join settings, and adaptive configuration selection. The setup combines operator audits, intent decompositions, and algebraic analysis of exact factorization.
- Coverage and workload design: Full-suite evaluation uses SemBench Q1–Q4 and Q8–Q10 plus Multi-XScience, covering scalar, aggregate, join, and ranking workloads.Table 3 reports these workloads under their standard task definitions and captioned quality metrics.
- Metrics and provenance: Quality scores are scaled to [0, 1] using task-specific precision, relative-error, Spearman, and ROUGE-1 transformations.SAGE traces use granite-3.3-8b-instruct and gpt-oss-120b, while external systems retain native configurations and share one LiteLLM pricing schedule.
- Adaptive join execution: 96 configurations vary confidence thresholds, reasoning, and temperature, while each race tests K = 3 candidates on a uniform probe of n = min(50, N) pairs.Probe cost and latency are scaled linearly to the full workload; the selection procedure uses a Bayesian linear Thompson sampler and a blinded judge.
- Decomposability analysis: Exact MapReduce factorization is class-dependent: modular aggregation and membership joins factorize, while holistic aggregation requires an application-supplied merge contract.For joins, exact tiling covers the full N×M pair grid; per-row keys reduce membership-join predicate cost to N + M.