OptiSQL: Executable SQL Generation from Optical Tokens

Sifan Li, Hongkai Chen, Yujun Cai, Liyang Chen, Qingwen Ye, Yiwei WangarXiv 2026

TL;DR

OptiSQL is a vision-driven framework that generates executable SQL queries directly from table images and natural language questions without explicit text reconstruction. By leveraging a frozen, OCR-oriented visual encoder, it compresses complex table structures into a compact set of "optical tokens," slashing token overhead by over 90% while outperforming standard vision-language models.

What Problem Does This Paper Address?

Standard text-to-SQL systems are highly effective, but they operate under a comfortable assumption: that clean, machine-readable, structured text schemas are readily available. In the real world, data is messy. Tables frequently live inside scanned PDFs, financial reports, images, or web pages.

The traditional way to handle this is a multi-stage pipeline: run OCR to extract the text, reconstruct the table layout, linearize it into a massive text string, and feed it to a text-to-SQL model. This approach is brittle—OCR errors compound down the line—and incredibly expensive, as linearizing large tables consumes massive amounts of context window tokens. OptiSQL asks a fundamental question: Can we bypass explicit text reconstruction entirely and use compact visual abstractions to generate executable SQL?

Figure 1: OptiSQL generates executable SQL directly from table images using compact optical tokens. A frozen OCR-oriented visual encoder converts a table image into a fixed-size sequence of optical tokens, which are combined with a natural language question and processed by a trainable decoder. By operating under an explicit token budget, OptiSQL enables an efficient and robust alternative to text-based table encodings.

Methods

OptiSQL introduces an end-to-end framework consisting of three main parts:

  1. Optical Tokenization: It uses a pretrained, OCR-oriented visual encoder (derived from DeepSeek-OCR) to compress a 2D table image into a fixed-length sequence of 1D "optical tokens." These tokens implicitly capture text, row-column alignments, and hierarchical associations.
  2. Unified Input Interface: The optical tokens are directly concatenated with the tokenized natural language question into a single sequence.
  3. Autoregressive Decoder: A pretrained language model decoder processes this combined input to generate the final SQL statement.

Crucially, the authors introduce a training setting called FROZENENC, where the visual encoder remains completely frozen, and only the decoder is fine-tuned. This isolates the representational sufficiency of the optical tokens, proving that the pretrained visual features alone contain enough structural information for complex program synthesis. They also experiment with a full fine-tuning variant (FULLFT) that updates both the encoder and decoder using heavy data augmentation.

Datasets

The framework was evaluated on a newly constructed visualized version of Spider 2.0-Snow. Spider 2.0-Snow is a challenging, enterprise-oriented text-to-SQL benchmark. To adapt it for a vision-grounded setting, the authors developed a pipeline that renders the original database tables into high-resolution PNG images using various HTML-CSS templates, while keeping the natural language questions and ground-truth SQL queries unchanged.

Examples of different visual renderings and styles used for the dataset

Key Insights and Ideas

  • Implicit Layout Preservation: Optical tokens act as a highly efficient latent interface. Instead of spending tokens to spell out | Column 1 | Column 2 |, the visual encoder packs spatial alignment and relational cues directly into the token embeddings.
  • The Frozen Encoder Proof: Keeping the encoder frozen prevents the model from simply adapting its visual weights to cheat on the dataset. The strong performance under the FROZENENC setting demonstrates that modern OCR encoders naturally retain the semantic and structural regularities needed for downstream relational reasoning.
  • True Visual Grounding: To ensure the model wasn't just guessing based on language priors (e.g., predicting column names from the question text), the authors ran stress tests like NOIMAGE (removing table tokens) and WRONGTABLE (swapping table images). The catastrophic drop in performance confirms that OptiSQL genuinely "reads" and relies on the visual table input.

Key Results

  • Massive Token Savings: OptiSQL achieves a 92.7% reduction in table input tokens, compressing an average of 3,500 textual tokens down to just 256 optical tokens.
  • Superior VLM Performance: With an execution accuracy (EXAcc) of 66%, OptiSQL significantly outperforms general-purpose vision-language models like Pix2Struct (55%) and Donut (57%).
  • Approaching OCR Pipelines: It performs within striking distance of explicit OCR-reconstruction pipelines (62% accuracy) while using an order of magnitude fewer tokens, presenting a much better efficiency-accuracy trade-off for long-context applications.
  • Token Budget Sweet Spot: Sweeping different token budgets revealed that execution accuracy increases monotonically with token count, but hits a clear point of diminishing returns around 256 tokens.
Figure 5: Execution accuracy and inference latency as a function of the optical token budget. Increasing the number of optical tokens improves execution accuracy with diminishing returns beyond 256 tokens, while la- tency grows approximately linearly, revealing a clear efficiency-accuracy trade-off.

Limitations and Future Work

While promising, OptiSQL has a few boundaries:

  • Encoder Dependency: Its performance is strictly capped by the pretrained visual encoder. It struggles with highly distorted images, low resolutions, or handwritten text.
  • Single-Table Constraint: The current evaluation focuses entirely on single-table queries. Real-world enterprise databases involve complex multi-table joins. Extending optical tokenization to represent multi-table schemas and relational databases remains an open challenge.
  • The Robustness Trade-off: Interestingly, while full fine-tuning (FULLFT) slightly boosted accuracy on clean images, it made the model less robust to visual perturbations (like shifting fonts or masking headers), indicating a tendency to overfit to superficial styling.

Summary updated

Abstract

Executable SQL generation is typically studied in text-to-SQL settings, where tables are provided as fully linearized textual schemas and contents. While effective, this formulation assumes access to structured text and incurs substantial token overhead, which is misaligned with many real-world scenarios where tables appear as visual artifacts in documents or webpages. We investigate whether compact optical representations can serve as an efficient interface for executable semantic parsing. We present OptiSQL, a vision-driven framework that generates executable SQL directly from table images and natural language questions using compact optical tokens. OptiSQL leverages an OCR-oriented visual encoder to compress table structure and content into a small set of optical tokens and fine-tunes a pretrained decoder for SQL generation while freezing the encoder to isolate representation sufficiency. Experiments on a visualized version of Spider 2.0-Snow show that OptiSQL retains strong execution accuracy while reducing table input tokens by an order of magnitude. Robustness analyses further demonstrate that optical tokens preserve essential structural information under visual perturbations.
BibTeX
@misc{li2026optisql,
  author = {Sifan Li and Hongkai Chen and Yujun Cai and Liyang Chen and Qingwen Ye and Yiwei Wang},
  title = {{OptiSQL: Executable SQL Generation from Optical Tokens}},
  year = {2026},
  eprint = {2601.13695},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2601.13695},
}
Figure