arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2012.13198cs.DB
收藏

用二值逻辑处理 SQL 空值

Handling SQL Nulls with Two-Valued Logic

Leonid Libkin, Liat Peterfreund

AI总结:

本文证明 SQL 可在保留空值和不损失表达能力的前提下改用标准二值逻辑,将未知合并为假,并给出向标准 SQL 的高效翻译方法及新优化规则。

摘要:

SQL 的设计基于三值逻辑(3VL),而非人们熟悉的布尔逻辑。3VL 在真和假之外增加了一个真值“未知”来处理空值。3VL 被视为 SQL 表达能力不可或缺的一部分,但同时也因查询行为不符合直觉以及成为程序员出错的来源而备受批评。我们表明,与普遍持有的观点相反,SQL 本可以基于标准布尔逻辑来设计,既不会损失表达能力,也不必放弃空值。该方法本身遵循 SQL 的求值方式,即只保留 WHERE 中条件求值为真的元组。我们证明,将“未知”与“假”合并可以得到一个表达能力相同的 SQL 版本,且不使用第三个真值。在二值语义下编写的查询可以高效地翻译成标准 SQL,从而可在任何现有的关系数据库管理系统(RDBMS)上执行。这些结果覆盖了 SQL 1999 标准的核心内容:带有子查询和 IN/EXISTS/ANY/ALL 条件的 SELECT-FROM-WHERE-GROUP BY-HAVING 查询,以及递归查询。我们还研究了二值 SQL 所带来的新优化规则,并表明对于许多查询——包括 TPC-H 和 TPC-DS 等基准测试中的大多数查询——三值版本和二值版本之间没有差异。

英文摘要:

The design of SQL is based on a three-valued logic (3VL), rather than the familiar Boolean logic. 3VL adds a truth value unknown to true and false to handle nulls. Viewed as indispensable for SQL expressiveness, it is at the same time much criticized for unintuitive behavior of queries and being a source of programmer mistakes. We show that, contrary to the widely held view, SQL could have been designed based on the standard Boolean logic, without any loss of expressiveness and without giving up nulls. The approach itself follows SQL's evaluation, which only retains tuples for which conditions in WHERE evaluate to true. We show that conflating unknown with false leads to an equally expressive version of SQL that does not use the third truth value. Queries written under the two-valued semantics can be efficiently translated into the standard SQL and thus executed on any existing RDBMS. These results cover the core of the SQL 1999 Standard: SELECT-FROM-WHERE-GROUP BY-HAVING queries extended with subqueries and IN/EXISTS/ANY/ALL conditions, and recursive queries. We also investigate new optimization rules enabled by the two-valued SQL, and show that for many queries, including most of those found in benchmarks such as TPC-H and TPC-DS, there is no difference between three- and two-valued versions.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑