这是今天在办公室发生的事。我没有这样做的计划,但理论上你能写一个SQL编译器吗?乍一看,我觉得它是图灵完备的,尽管对于许多类型的问题来说非常麻烦。

如果它不是图灵完整的,它需要什么才能变成图灵完整的?

注意:我不想做任何事情,比如用SQL编写编译器,我知道这将是一件愚蠢的事情,所以如果我们可以避免这个讨论,我会很感激。


当前回答

https://web.archive.org/web/20110807062050/http://channel9.msdn.com/forums/TechOff/431432-SQL-Turing-Completeness-question

是关于这个话题的讨论。引用:

SQL as such (i.e. the SQL92 standard) is not turing complete. However, many of the languages derived from SQL, such as Oracle's PL/SQL and SQL Server's T-SQL and others are turing complete. PL/SQL and T-SQL certainly qualify as programming languages, whether SQL92 itself qualifies is open for debate. Some people claim that any piece of code that tells a computer what to do qualifies as a programming language; by that definition SQL92 is one, but so is e.g. HTML. The definition is rather vague, and it's imo a pointless thing to argue about.

其他回答

https://web.archive.org/web/20110807062050/http://channel9.msdn.com/forums/TechOff/431432-SQL-Turing-Completeness-question

是关于这个话题的讨论。引用:

SQL as such (i.e. the SQL92 standard) is not turing complete. However, many of the languages derived from SQL, such as Oracle's PL/SQL and SQL Server's T-SQL and others are turing complete. PL/SQL and T-SQL certainly qualify as programming languages, whether SQL92 itself qualifies is open for debate. Some people claim that any piece of code that tells a computer what to do qualifies as a programming language; by that definition SQL92 is one, but so is e.g. HTML. The definition is rather vague, and it's imo a pointless thing to argue about.

严格地说,SQL现在是一种图灵完备的语言,因为最新的SQL标准包含了“持久存储模块”(psm)。简而言之,PSM是Oracle中PL/SQL语言的标准版本(以及当前DBMS的其他类似过程扩展)。

随着这些psm的加入,SQL变得图灵完备

对于什么是图灵完备性以及什么构成了图灵完备语言,公众仍然存在很大的困惑。我写了一篇博客文章,试图澄清这些误解,也就是说,我看了一个关于超级智能计算机的电视节目,它提供了一个非常有用的类比:

https://github.com/ubuvoid/writings/blob/main/miller/miller_is_sql.md

在这个回答中,我将详细阐述SQL的情况。

实际上,我们有足够的信息粗略地看一眼就能确定SQL是图灵完备的。

That's because it has affordances for branching choices ("CASE IF THEN"), and because even in SQL dialects that restrict recursion, it's trivial to write output to a table and re-read it in a subsequent query as though it were a memory register. Therefore, you can implement an interpreter for an assembly language in SQL without thinking very hard about it. If you want to demonstrate this with a minimum of labor, implement a single-instruction-set computer with an operation like 'subleq' (see https://en.wikipedia.org/wiki/One-instruction_set_computer ).

事实上,一些实现尝试沙盒执行SQL查询并没有改变语言的核心功能,它只是在这个问题上贴了一个创可贴,这个问题是善意的“便利”特性往往会像一把刀一样割断。

对于演示,请考虑以下内容:

假设您有一个受执行限制的“非”图灵完整SQL解释器,这种解释器只允许您一次运行单个查询或更新操作,并对该查询施加限制。 现在,假设您可以从shell调用该解释器。 最后,假设您在命令上方键入“while true:”来在shell中执行这个SQL解释器。现在你有了图灵完全执行。

这是一个有意义的区别吗?我认为,如果你能清楚地表达你的语言和图灵完备性之间的唯一区别是没有while循环,那么你解释图灵完备性的框架就不是特别有用。

Non-Turing Complete languages in widespread use tend to describe "inert" data artifacts, and lack affordances for branching paths or explicit procedure invocations. Simply put, if a language has no "verbs" and/or no equivalent to "if then else", you can be sure it's Non-Turing Complete. Examples include interface definition languages (DIANA IDL, CORBA, Proto, Thrift, JSON Typedef, Coral, Ion, etc.), markup languages (XML, HTML, Markdown), data representation languages (JSON, text-format protocol buffers, ascii DIANA representation), and so on.

在软件架构的战略部分对图灵完全执行施加限制以实现关注点分离通常是有用的,而非tc语言提供了实现这些保证的简单而健壮的方法。

一个经典的例子是在HTTP或RPC请求/响应中验证JSON有效负载或其他数据工件的模式,这是一项劳动密集型且容易出错的任务,可以使用IDL和代码模板自动完成。

继续分发藏物。

(编辑2022/04/08:有趣的事实。亚马逊在上述IDL中发明了不止一个,而是两个,同时还使用了Facebook发明的第三个IDL,这一点值得怀疑。请记住,接口定义语言的主要目的是定义接口,因此我希望它们有用于将一个IDL转换为另一个IDL的IDL。我的一位同事把亚马逊的软件基础设施比作巴黎的下水道。)

简单地,执行这个查询:

select * 
from (SELECT 0 union SELECT 1) 
JOIN (SELECT 0 union SELECT 1) 
JOIN (SELECT 0 union SELECT 1)  
JOIN (SELECT 0 union SELECT 1)  
JOIN (SELECT 0 union SELECT 1) 
JOIN (SELECT 0 union SELECT 1)  
JOIN (SELECT 0 union SELECT 1) 
JOIN (SELECT 0 union SELECT 1) 

然后生成8位数字的前256位。 现在使用SELECT, and和or的组合,你可以编写任何8位机器。

正如你所看到的,没有表涉及,只有选择,联合和连接

在我看来,图灵完成了

最初在SQL-86中定义的ANSI选择语句不是图灵完全语句,因为它总是终止(除了递归cte,并且只有在实现支持任意深度递归时)。因此,不可能模拟任何其他图灵机。存储过程是图灵完整的,但这是作弊;-)