Adversarial Query Synthesis via Bayesian Optimization

Jeffrey Tao, Yimeng Zeng, Haydn Thomas Jones, Natalie Maus, Osbert Bastani, Jacob R. Gardner, Ryan MarcusarXiv 2026

TL;DR

Designing challenging database benchmarks typically requires intense manual effort and rarely optimizes for actual query difficulty. This paper introduces an automated framework that uses Latent Space Bayesian Optimization to jointly search the space of SQL queries and execution plans. By explicitly maximizing "headroom"—the performance gap between a database's default plan and the optimal plan—the system automatically synthesizes simple queries that exhibit more than double the optimization headroom of existing standard benchmarks.

What Problem Does This Paper Address?

Database researchers and engineers rely heavily on benchmarks to evaluate query optimizers, especially as machine learning components become tightly integrated into database systems. However, traditional synthetic benchmarks (like the TPC family) often feature simplistic data distributions that fail to reflect real-world complexities. Conversely, human-curated "real-world" benchmarks, such as the Join Order Benchmark (JOB), require massive manual effort to construct.

Crucially, almost no existing benchmarking methodologies explicitly optimize for difficulty. The authors define difficulty as optimization headroom: the potential room for improvement over an existing system's default choices. Without targeting headroom, benchmarks risk being too "easy," failing to expose performance bugs or areas where optimizers make suboptimal decisions. The authors tackle this gap by automating the generation of highly portable, simple SQL queries that maximize headroom.

Methods

The authors frame benchmark generation as a black-box optimization problem aimed at maximizing either the relative or absolute speedup of a custom execution plan over the database's default plan.

To navigate the discrete, highly structured nature of SQL queries and execution plans, they employ Latent Space Bayesian Optimization (LS-BO). The core architecture relies on a composite Variational Autoencoder (VAE) that maps queries and plans into a joint, continuous latent space ($Z \in \mathbb{R}^{320}$):

  • Query VAE: To sidestep data scarcity, the authors avoid training a query VAE from scratch. Instead, they use a pre-trained text embedding model (OpenAI text-embedding-3-large) as the encoder and fine-tune a compact LLM (Qwen-2.5-0.5B) as the decoder. To guarantee that the decoded strings are syntactically and semantically valid SQL, they enforce grammar-constrained decoding via vLLM using a custom EBNF grammar tailored to a subset of SQL.
  • Plan VAE: Execution plans are encoded as integer strings representing operator selections and join orders. The vocabulary is closed, meaning any decoded sequence naturally maps to a valid execution plan.

During optimization, the system samples from this joint latent space, decodes the point into a valid query-plan pair, executes them, and uses the resulting latency data to update a Gaussian process surrogate model.

Datasets

The framework was evaluated using the IMDb dataset, chosen specifically to allow a direct comparison with the established Join Order Benchmark (JOB).

From a week-long Bayesian optimization run, the authors generated a workload of 122 query plans. These queries were restricted to a simple SQL subset: conjunctive queries featuring equijoins and table column predicates with an ungrouped COUNT(*) aggregation. This subset intentionally excludes complex features like subqueries or window functions to ensure the generated queries can run on virtually any relational database.

Key Insights and Ideas

  • Joint Query-Plan Search: Rather than just searching for complex queries, searching the joint space of queries and plans allows the system to discover "witness plans." A witness plan is a concrete, executable plan that proves the default optimizer made a poor choice, providing database developers with an immediate debugging path.
  • Simplicity is Sufficient for Difficulty: You don't need convoluted SQL features (like recursion or window functions) to break an optimizer. Simple conjunctive queries over real-world data distributions (like IMDb) provide more than enough structural complexity to trigger massive optimization failures.
  • Continuous Latent Spaces for Discrete Code: Combining large text embeddings with grammar-constrained LLM decoding provides a highly effective bridge for applying continuous optimization techniques (like Bayesian Optimization) to discrete code generation tasks.

Key Results

The preliminary results show that the automated framework successfully uncovers massive performance gaps in modern database engines like DuckDB:

  • Substantial Headroom: The median query generated by the system had an absolute headroom of 25 seconds and a relative headroom of 20x over DuckDB's default optimizer.
  • Extreme Outliers: The largest discovered performance gaps approached an absolute difference of 30 seconds and nearly an 80x relative speedup (as seen in the CDFs in Figure 1).
Figure 1: CDFs of absolute and relative differences between baseline system (DuckDB) and witness plan for our generated benchmark. Median values at red cross.
  • Outperforming Existing Benchmarks: When evaluating the geometric mean of relative headroom, the synthesized workload ("Ours") achieved an average headroom of ~17.5x. As shown in Figure 2, this is more than double the headroom of JOB-Complex (~11x) and vastly higher than standard JOB (~2.5x) or Stack (~8.5x).
Figure 2: Comparison of geometric mean relative headroom for four benchmarks. Values for JOB and JOB-Complex are from [25]. Values for Stack are approximated from [11]. Values for “Ours” are computed with respect to the witness plan.

Limitations and Future Work

The primary limitation of the current approach is the computational cost of the search; a week-long Bayesian optimization run is required to harvest a high-quality workload, and some generated queries still exhibit low headroom due to limited search time.

To scale this up, the authors propose a compelling future direction: using the high-headroom (SQL, plan, headroom) triples discovered via Bayesian optimization to fine-tune a schema-conditioned LLM. By applying Direct Preference Optimization (DPO) or a simple reward model that favors high headroom, the LLM could bulk-generate adversarial queries instantly, bypassing the expensive active optimization loop entirely.

Summary updated

BibTeX
@misc{tao2026adversarial,
  author = {Jeffrey Tao and Yimeng Zeng and Haydn Thomas Jones and Natalie Maus and Osbert Bastani and Jacob R. Gardner and Ryan Marcus},
  title = {{Adversarial Query Synthesis via Bayesian Optimization}},
  year = {2026},
  eprint = {2603.01570},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2603.01570},
}
Figure