In modern data systems, we often need to ask questions that combine high-level domain knowledge with precise temporal constraints. For example, an industrial monitoring system might need to detect a "turbine power trip," which a domain expert defines as a specific sequence of overheating events followed by a sudden pressure drop within a five-minute window.
To answer such questions, computer scientists use Ontology-Mediated Query Answering (OMQA). This approach pairs a database with an "ontology"—a set of logical rules describing how low-level sensor data relates to high-level concepts. When data changes over time, we extend these rules with temporal logics like Linear Temporal Logic (LTL) or Metric Temporal Logic (MTL).
The core challenge is scalability. Running complex temporal logic directly over gigabytes of streaming or historical data using specialized reasoning engines is often too slow for production environments. As noted in a retrospective by Christian S. Jensen, integrating temporal processing directly into a database engine yields massive optimization advantages over external translation layers.
This realization drives the core idea of temporal query rewriting: instead of building a new database engine from scratch, we can mathematically transform (or "rewrite") a high-level temporal semantic query into a standard database language like SQL or Datalog. By doing so, we offload the heavy lifting of temporal reasoning to mature, highly optimized relational database management systems (RDBMS).
The Core Intuition of Query Rewriting
To understand why query rewriting works, consider how a database processes a standard query versus an ontology-mediated one. In a standard setup, you query raw tables directly. In an ontology-mediated setup, the query asks for abstract concepts (like "critical failure state") that do not exist as raw tables.
Query rewriting bridges this gap by compiling the background rules and temporal operators directly into the query itself before it hits the database.
[ High-Level Temporal Query ] + [ Ontological Rules ]
│
▼
┌───────────────────────┐
│ Rewriting Engine │
└───────────────────────┘
│
▼
[ Standard SQL Query (with Windows/Joins) ]
│
▼
┌───────────────────────┐
│ Relational Engine │ ◄─── [ Raw Temporal Data ]
└───────────────────────┘
│
▼
[ Answers ]
As shown by Anni-Yasmin Turhan et al., this transformation allows sophisticated situation awareness queries to run on top of legacy relational infrastructure without requiring specialized, high-overhead reasoning hardware. Similarly, Jan Holste et al. demonstrate that wrapping ontological expansions inside temporal SQL clauses allows semantic applications to leverage standard database optimization techniques. Even when dealing with vague or imprecise data, Erik Zenker shows that a recursive rewriting mechanism can successfully transform complex temporal and fuzzy logic constraints into executable SQL.
Mapping Temporal Operators to Relational Primitives
How do you express a temporal operator like "always in the past" or "within an interval of 5 minutes" in standard SQL? The secret lies in leveraging native relational features like window functions, temporal joins, and interval coalescing.
In a paper by Sebastian Brandt 0001 et al., the authors demonstrate how nonrecursive queries written in datalogMTL (a combination of Datalog and Metric Temporal Logic) can be systematically translated into standard SQL queries utilizing window functions. Instead of stepping through time point by point, the rewritten SQL query uses window functions to look across rows sorted by timestamps, evaluating whether metric constraints are satisfied over a given range.
This approach becomes even more powerful when dealing with intervals. Diego Calvanese et al. show that standard relational database features provide an excellent foundation for answering temporal path queries on graphs. By inductively mapping temporal operators to SQL overlap and range joins, their framework performs interval merging directly within the database engine. This achieves significant data compression and execution speedups compared to naive, time-point-based approaches.
This translation strategy is not limited to historical databases; it also applies to real-time streams. For instance, Thomas Eiter et al. designed a framework that rewrites semantic queries containing spatial and temporal constraints into First-Order logic to process high-throughput mobility streams. To ensure consistency during these translations, Minh Dao-Tran et al. established formal mappings that translate stream-windowing operators (like time- or count-based windows) into logic-based programs, ensuring that the underlying semantics are preserved when moving between execution environments.
The Boundaries of First-Order Rewritability
Can every temporal semantic query be rewritten into standard SQL? Unfortunately, no. The boundary between what can be rewritten and what cannot depends on a property called First-Order (FO) rewritability.
When a query is FO-rewritable, its data complexity is in $AC^0$. In practical terms, this means the time required to answer the query scales exceptionally well with the size of the dataset, matching the complexity of standard SQL evaluation. However, certain combinations of temporal operators and ontological rules can cause the reasoning process to spiral into higher complexity classes (like PTIME or coNP-complete), making pure SQL rewriting impossible.
Researchers have mapped out these "islands of tractability" to find the exact limits of rewriting:
- Acyclicity Conditions: Víctor Gutiérrez-Basulto et al. identified that by enforcing specific acyclicity conditions on temporal description logics (such as $T\mathcal{EL}$), queries remain tractable and can be rewritten into standard SQL or a Datalog variant with a successor function.
- Interval Types: Przemysław Andrzej Wałęga et al. and Stanislav Kikot et al. proved that under event-based semantics, certain fragments of Metric Temporal Logic that utilize only infinite intervals are equivalent to first-order logic. This means they can be executed natively using standard SQL without specialized engines.
- The Challenge of Rigid Symbols: A major roadblock occurs when a logic includes "rigid symbols"—properties or relations that are guaranteed not to change over time. As shown by Stefan Borgwardt, the introduction of rigid symbols significantly increases computational complexity, often preventing first-order query rewriting entirely.
- Periodicity and Precedence: Standard first-order logic cannot natively express concepts like parity or fixed-interval periodicity (e.g., "every second Tuesday"). To overcome this, Alessandro Artale et al. demonstrated that queries can be rewritten into an extended version of first-order logic that includes explicit mathematical predicates for precedence and addition.
Alternative Paths: Materialization and Symbolic Execution
When a temporal logic program falls outside the boundaries of first-order rewritability, executing it purely via backward-chained SQL queries becomes unfeasible. In these scenarios, researchers turn to alternative paradigms like materialization or symbolic execution.
Instead of rewriting the query at runtime, the materialization approach precomputes all possible historical facts up front. This is known as temporal forward-chaining. While forward-chaining in temporal settings can easily loop forever (since time is infinite), Przemyslaw Andrzej Walega et al. identified a class of "finitely materialisable" DatalogMTL programs. These programs guarantee that the precomputation phase will terminate, creating a complete canonical model that allows for rapid, subsequent query answering.
To make these transformations easier to deploy, Jacopo Urbani et al. introduced a multi-step transformation process that converts temporal logic programs with existential rules into standard, non-temporal existential rules by encoding temporal parameters directly into predicates. This allows developers to use existing, highly optimized static reasoning engines for stream processing tasks without modifying the underlying engine kernel.
This concept of separating temporal logic from the underlying execution engine has also influenced machine learning and automated reasoning. For example, Dingmin Wang et al. developed a framework that extracts static rules from time-stamped data and uses a rewriting function to transform them into formal temporal logic expressions, achieving high interpretability in link prediction tasks.
Furthermore, in the domain of Question Answering, Zhuo Chen et al. demonstrated that teaching Large Language Models to translate natural language questions into symbolic temporal logic programs yields massive performance gains. By executing these rewritten programs symbolically rather than relying on high-dimensional vector representations, the system replaces logical fuzziness with a transparent, deterministic sequence of database operations.
Ultimately, whether through direct SQL rewriting, stream transformation, or finite materialization, the goal remains the same: translating high-level temporal abstractions into concrete, executable structures that leverage the raw power of relational data processing.
Key Foundational Work
Enhancing an Extensible Query Optimizer with Support for Multiple Equivalence Types (2001) — contributed 1 concept elements to this direction.
CTL Model Checking for Processing Simple XPath Queries (2004) — contributed 1 concept elements to this direction.