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?
Methods
OptiSQL introduces an end-to-end framework consisting of three main parts:
- 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.
- Unified Input Interface: The optical tokens are directly concatenated with the tokenized natural language question into a single sequence.
- 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.
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
FROZENENCsetting 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) andWRONGTABLE(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.
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