UQE: A Query Engine for Unstructured Databases

Hanjun Dai, Bethany Wang, Xingchen Wan, Bo Dai, Sherry Yang, Azade Nova, Pengcheng Yin, Phitchaya Mangpo Phothilimthana, Charles Sutton, Dale Schuurmans

Most real-world data—such as images, audio, and text conversations—is unstructured, making traditional SQL analytics impossible without heavy preprocessing. Relational databases and SQL are incredibly powerful, but they require data to be neatly organized into predefined tables and columns.

Currently, analyzing unstructured data leaves developers with suboptimal choices. Simple keyword or regex searches fail at complex semantic reasoning. Retrieval-Augmented Generation (RAG) is excellent for Q&A but struggles with database-wide aggregations. Recently, developers have tried using Large Language Models (LLMs) as User-Defined Functions (UDFs) within SQL to evaluate rows one by one. However, scanning a massive database with an LLM is prohibitively slow and astronomically expensive.

This paper introduces the Universal Query Engine (UQE), a system that combines LLMs with statistical sampling and compiler optimizations to run SQL-like queries directly on raw, unstructured data efficiently and cheaply. It asks a critical question: How can we perform flexible, semantic data analysis on unstructured data without going bankrupt on LLM API calls?

The Universal Query Language and Engine

The authors propose a two-part solution: a flexible query language and an optimized execution engine.

1. Universal Query Language (UQL)

UQL is a dialect of SQL that allows natural language in its clauses. Instead of relying solely on strict schema definitions, users can write queries like SELECT COUNT(*) FROM movie_reviews WHERE "the review is positive".

Comparison of standard SQL on structured data versus UQL on unstructured data.

2. Universal Query Engine (UQE)

To avoid running an LLM on every single row, UQE introduces two major innovations:

  • "Indexing" via Smart Sampling: In traditional databases, indexes prevent full-table scans. UQE proxies this concept using mathematics. For aggregation queries (like COUNT), it uses multi-modal embeddings to cluster the data, then performs stratified sampling to create a highly accurate, unbiased mathematical estimate of the total. For retrieval queries (finding specific rows), it uses online active learning—training a lightweight surrogate model on the fly to hunt down relevant rows within a strict token budget.
  • LLM Compilation: UQE borrows from classical compiler theory. Just as a C++ compiler optimizes machine code, UQE acts as an "LLM compiler." It plans the execution order and fuses operations together (e.g., combining a SELECT extraction and a GROUP BY classification into a single LLM prompt) to minimize expensive API calls.

Key Insights

  • Sampling is the new indexing: You don't need to read a million reviews to know what percentage are positive. By clustering the semantic embeddings of the data and sampling smartly, an LLM can give you a highly accurate aggregate statistic for a fraction of the cost.
  • Prompting is compiling: Treating LLM calls like expensive CPU instructions unlocks decades of compiler optimization research. By fusing clauses (like WHERE + LIMIT or SELECT + GROUP BY), the engine drastically reduces token usage while preserving the query's intent.
  • Embeddings aren't enough for complex retrieval: Standard vector search (Max Inner-Product Search, or MIPS) fails on queries that require actual reasoning (e.g., "find images with exactly 3 spheres"). UQE's active learning approach solves this by dynamically learning what the LLM considers a "match" during the search process.

Empirical Results

To test the engine, the authors utilized existing datasets with structured labels (IMDB, ABCD, AirDialog, and Clevr), hid those labels to treat the data as purely unstructured, and then used the hidden labels as the "ground truth" to verify UQE's accuracy.

Benchmark results for conditional aggregation showing UQE's superior accuracy and lower cost.

The results demonstrate significant improvements over baseline methods:

  • Massive Cost Reduction: On conditional aggregation tasks, UQE reduced the cost per query by over 20x. Queries that cost $0.37 to $0.61 using standard long-context LLMs cost just $0.01 with UQE.
  • Higher Accuracy: Despite relying on sampling, UQE achieved a ~10x reduction in relative error for counting tasks compared to baseline LLM approaches.
  • Superior Retrieval: For semantic retrieval (finding specific rows), UQE consistently outperformed standard long-context LLMs and MIPS in F1 scores. For example, on IMDB sentiment retrieval, UQE scored an F1 of 0.978, compared to 0.397 for GPT-4-Turbo.
  • High Efficiency: UQE can achieve the same quality as a brute-force LLM scan while using 16x less compute budget.

Limitations and Future Work

While UQE represents a significant leap forward, it currently has a few limitations. The engine does not yet support table joins, and certain aggregations like MAX or MIN are mathematically difficult to estimate via sampling (unlike COUNT or AVG). Additionally, the system currently relies on default or manually set hyperparameters for sampling budgets and online learning configurations.

Future work will focus on automating the selection of LLMs and sampling configurations, adding support for more complex SQL operations, and scaling the engine to handle even larger enterprise databases.

Summary updated

BibTeX
@inproceedings{dai2024uqe,
  author = {Hanjun Dai and Bethany Wang and Xingchen Wan and Bo Dai and Sherry Yang and Azade Nova and Pengcheng Yin and Phitchaya Mangpo Phothilimthana and Charles Sutton and Dale Schuurmans},
  title = {{UQE: A Query Engine for Unstructured Databases}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=k2zJaHoHmf},
}
Figure