Sema: A High-performance System for LLM-based Semantic Query Processing

Kangkang Qi, Dongyang Xie, Wenbo Li, Hao Zhang, Yuanyuan Zhu, Jeffrey Xu Yu, Kangfei ZhaoarXiv 2026

TL;DR

Sema is a high-performance semantic query engine built on DuckDB that integrates LLM-powered operators as first-class database citizens. By combining a declarative dialect (SemaSQL) with logical expression compression and runtime Adaptive Query Execution, Sema achieves a $2-10\times$ speedup over existing systems while balancing token costs, latency, and accuracy constraints.

What Problem Does This Paper Address?

Data analytics increasingly requires complex reasoning over bulk structured and unstructured data—such as summarizing corporate transcripts, extracting insights from medical records, or performing sentiment analysis on product reviews. Large Language Models (LLMs) excel at these tasks, but integrating them into data pipelines introduces a harsh trade-off.

Existing solutions typically fall into two camps:

  1. DataFrame Primitives: Systems built on lightweight libraries (like Pandas) offer flexible programming models but lack the robust execution infrastructure of modern database management systems (DBMSs), such as vectorized execution, pipeline parallelism, and efficient memory management.
  2. SQL User-Defined Functions (UDFs): Embedding LLM calls inside standard SQL UDFs keeps data inside the database but treats the LLM as an opaque black box. This isolates semantic logic from the query optimizer, preventing critical optimizations.

Furthermore, LLM invocations are expensive, slow, and non-deterministic. They break the foundational assumptions of traditional query optimizers, which rely on static cost models and stable algebraic rewrite rules.

Methods

The authors present Sema, a system designed to treat LLM-powered semantic operators as native database components. Built on top of DuckDB, Sema introduces optimizations across the entire query lifecycle:

  • SemaSQL Dialect: A declarative syntax extension that allows users to seamlessly inject natural language (NL) expressions directly into standard SQL clauses (e.g., WHERE s'{translated_review} is a valid user review').
  • Logical Optimization:
    • NL Expression Compression: Sema uses a lightweight auxiliary LLM with Chain-of-Thought (CoT) prompting to strip redundancies from user-written natural language predicates, shortening prompts and reducing token costs.
  • Predicate Deduction: The optimizer automatically deduces standard symbolic SQL filters from natural language conditions (e.g., extracting translated_review != 'nan') and pushes them down to prune tuples early via the CPU, bypassing expensive LLM calls entirely for those rows.
  • Execution Optimization:
    • Semantic Operator Fusion & Prompt Batching: Sema combines consecutive semantic operations into a single LLM request and batches multiple tuples into structured JSON arrays to maximize throughput.
  • Adaptive Query Execution (AQE): Because LLM selectivity and costs cannot be predicted statically, Sema micro-executes candidate physical plans on a small sample of data at runtime. It calculates predicate correlations using the Matthews Correlation Coefficient (MCC) and dynamically selects a Pareto-optimal path that balances latency and token cost under user-defined accuracy constraints.
Figure 1: System architecture of Sema
Table 1: Semantic Operators

Datasets

Sema was evaluated using 20 semantic queries across 9 distinct datasets from the BIRD benchmark (including Appstore, Superstore, Food_Inspection, Movies_4, Authors, Social_Media, and CoinMarketCap). These datasets represent diverse analytical workloads spanning text classification, summarization, and entity-level keyword extraction.

Key Insights and Ideas

  • LLMs as Query Optimizer Assistants: Rather than just using LLMs to process data at runtime, Sema cleverly leverages an auxiliary LLM during the optimization phase to rewrite queries and deduce relational constraints.
  • Environment-Aware Optimization: The paper highlights a critical shift between local and remote LLM deployments. Local LLM throughput (e.g., via vLLM) is highly sensitive to token length due to GPU memory limits, making prompt compression vital. Conversely, remote APIs are bottlenecked by network round-trip times, meaning reducing the number of calls via batching and operator fusion yields the highest savings.
  • Embracing Non-Determinism at Runtime: Because static cost estimation is impossible for LLMs, postponing plan selection to a runtime micro-execution phase (AQE) allows the database to safely navigate accuracy-efficiency trade-offs.

Key Results

  • Significant Speedups: Sema consistently outperformed three baseline systems (Lotus, Palimpzest, and FlockMTL), achieving a $2-10\times$ speedup on complex, multi-operator queries.
  • High Quality Maintained: Despite aggressive runtime optimizations like prompt batching and operator fusion—which can sometimes degrade LLM reasoning quality—Sema maintained highly competitive accuracy and F1 scores compared to unoptimized reference execution paths.
  • Effective Pruning: Predicate deduction successfully identified symbolic shortcuts, filtering out up to 41.8% of invalid rows using cheap CPU operations before invoking the LLM.
Comparison of system capabilities

Limitations and Future Work

  • AQE Scope: Currently, Sema's Adaptive Query Execution framework only supports SemFilter (semantic filter) operators within a single execution pipeline. Extending runtime adaptivity to more complex operators (like semantic joins or aggregations) across multiple pipelines remains an open challenge.
  • Architectural Extensions: The authors note that future work will need to address specialized algorithmic improvements for individual operators, storage-level optimizations, dynamic LLM routing/model selection, and comprehensive frameworks for privacy, governance, and fault tolerance in external inference.

Summary updated

BibTeX
@misc{qi2026sema,
  author = {Kangkang Qi and Dongyang Xie and Wenbo Li and Hao Zhang and Yuanyuan Zhu and Jeffrey Xu Yu and Kangfei Zhao},
  title = {{Sema: A High-performance System for LLM-based Semantic Query Processing}},
  year = {2026},
  eprint = {2603.11622},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2603.11622},
}
Figure