Abstract
Relational Database Management Systems (RDBMS) are foundational to modern software, yet their adherence to the ISO SQL standard is often inconsistent. This is largely because the standard is documented in natural language, leading to varied interpretations and implementation errors. We introduce SEMCONT, a testing framework that translates SQL semantics into an executable Prolog-based reference engine. By serving as a "golden standard" oracle, SEMCONT identified 19 bugs and 13 inconsistencies across six major database engines, including MySQL, TiDB, and SQLite, significantly outperforming existing differential testing methods.
Introduction
The SQL specification (ISO/IEC 9075) serves as the definitive guide for RDBMS behavior. However, its natural language format makes it non-executable and prone to misinterpretation by developers. Existing testing methodologies, such as differential testing (comparing two databases) or metamorphic testing (checking logical equivalence), often fail when all tested systems share the same incorrect implementation or when the specification itself is ambiguous.
As shown in Figure 1, discrepancies can arise in basic operations like bitwise logic or string concatenation with NULL values. Without a formal, executable reference, these "silent" logic errors can persist undetected.
Methodology: SEMCONT
SEMCONT bridges the gap between natural language specifications and automated testing through a three-step process:
- Formal Semantics: The researchers manually defined denotational semantics for 138 SQL keywords and features based on the ISO standard.
- Executable Prolog Specification: SQL’s declarative nature makes Prolog an ideal choice for a reference engine. The Prolog implementation handles complex SQL behaviors, such as bag semantics and three-valued NULL logic, more naturally than imperative languages.
- Coverage-Guided Testing: To ensure comprehensive evaluation, SEMCONT employs three coverage criteria: Keyword Coverage, Rule Coverage, and Composite Rule Coverage.
Figure 2 illustrates how SQL clauses are mapped to logic programming rules, allowing SEMCONT to compute the "correct" result for any given query according to the formalized standard.
Evaluation and Results
SEMCONT was evaluated against six popular RDBMSs: MySQL, PostgreSQL, TiDB, SQLite, DuckDB, and OceanBase. Over 18 million test cases were executed, leading to the discovery of 19 bugs and 13 inconsistencies.
Key findings include:
- Superior Bug Discovery: In a 6-hour head-to-head comparison, SEMCONT found 16 bugs and 9 inconsistencies, while state-of-the-art tools like SQLancer failed to find any new bugs in the same timeframe.
- Efficiency: The tool identifies a bug approximately every 67 minutes.
- Semantic Awareness: Unlike traditional "syntax-aware" fuzzers, SEMCONT's "semantic-aware" approach allows it to detect deep logical errors that do not necessarily cause crashes but return incorrect data.
Discussion: Bugs vs. Inconsistencies
The study distinguishes between clear bugs (violations of documented specs) and inconsistencies (divergent behaviors in "under-specified" areas). For instance, the standard may not explicitly define bitwise operations on negative numbers, leading different engines to adopt different, sometimes conflicting, behaviors. SEMCONT highlights these areas, providing valuable feedback to both database developers and standards committees.
Conclusion and Future Work
SEMCONT demonstrates the power of using a formal, executable specification to validate complex software systems. Future work aims to expand the tool's scope beyond Data Query Language (DQL) to include Data Definition (DDL) and Data Manipulation (DML) languages. Additionally, researchers are exploring the use of Large Language Models (LLMs) to automate the translation of natural language specifications into formal logic rules, reducing the manual effort required for standard formalization.
Summary updated