Logica:谷歌新发布的编程语言

周末读 Data Engineering Weekly 发现谷歌在四月二十一号的时候 Google Open Source Blog 发表了一篇文章 Logica: organizing your data queries, making them universally reusable and fun 介绍了其谷歌公司内部的一种崭新的开源逻辑编程语言 Logica。

We present Logica, a novel open source Logic Programming language.

先从文章看看 Logica 是什么吧。

诞生

Logica 来源于 Yedalog(一种由 Google 较早开发的语言),它是一种类似于 Datalog 的逻辑编程语言。

A successor to Yedalog (a language developed at Google earlier) it is a Datalog-like logic programming language.

Yedalog 可以参考谷歌在2015年写的论文 Yedalog: Exploring Knowledge at Scale ,其摘要放在了下面。

We introduce Yedalog, a declarative programming language that allows programmers to mix data-parallel pipelines and computation seamlessly in a single language … Yedalog extends Datalog, incorporating not only computational features from logic programming, but also features for working with data structured as nested records. Yedalog programs can run both on a single machine, and distributed across a cluster in batch and interactive modes, allowing programmers to mix different modes of execution easily.

Logica 介绍

Logica 可被编译成 SQL 语言,并且运行在 Google BigQuery 上(当然,也可以运行在PostgreSQL和SQLite的测试环境)。

Logica code compiles to SQL and runs on Google BigQuery (with experimental support for PostgreSQL and SQLite)

Logica 相比于 SQL ,更加简洁和可复用。

supports the clean and reusable abstraction mechanisms that SQL lacks.

Logica 支持模块和导入,也可以在交互式 Python notebook 中使用。

It supports modules and imports, it can be used from an interactive Python notebook and it even makes testing your queries natural and easy.

设计 Logica 的原因

为了解决 SQL 存在的问题,而 SQL 的问题简而言之一句话,太不抽象了。

Constructing statements from long chains of English words (which are often capitalized to keep the old-fashioned COBOL spirit of the 70s alive!) can be very verbose—a single query spanning hundreds of lines is a routine occurrence. The main flaw of SQL, however, lies in its very limited support for abstraction.

Logica 的设计者认为“好的程序设计应该是可测试的、可理解的、可重用的小型逻辑,并给出名称并将其组织到程序包中,这些程序包随后可用于构造更多有用的逻辑。SQL 做不到这个。尽管开发者可以将某些重复的计算封装到视图和函数中,但是它们的语法和支持在实现中可能有所不同,但通常不存在包和导入的概念,并且不可能进行更高级的构造。”

Good programming is about creating small, understandable, reusable pieces of logic that can be tested, given names, and organized into packages which can later be used to construct more useful pieces of logic. SQL resists this workflow. Although you can encapsulate certain repeated computations into views and functions, the syntax and support for these can vary among implementations, the notions of packages and imports are generally nonexistent, and higher-level constructions (e.g. passing a function to a function) are impossible.

因此Logica 的设计者认为,SQL 的不抽象的特性“导致了人为的冗长的查询,复制粘贴的代码块,以及最终导致无法维护的,未结构化的 SQL 代码库。”同时这也导致了很难“对 SQL 代码进行测试”。

This inherent resistance to decomposition of logic into bite-sized pieces is what leads into the contrived, lengthy queries, the copy-pasted chunks of code and, eventually, unmaintainable, unstructured (note the irony) SQL codebases. To make things worse, SQL code is rarely tested, because “testing SQL queries” sounds rather esoteric to most engineers, at best. Because of that, a number of alternative query languages and libraries have been developed. Of those, systems based on logic programming perhaps come the closest to addressing SQL’s limitations.

Logica 的优势

Logica 语言通过使用数学命题逻辑的语法而不是自然英语语言来解决 SQL 问题。形式逻辑语言是由数学家专门设计的,目的是使表达复杂的语句更容易,并且比自然语言更适合于此目的。Logica 进一步扩展了经典的 Logic 编程语法,尤其是在聚合方面。

Logic programming languages solve problems of SQL by using syntax of mathematical propositional logic rather than natural English language. The language of formal logic was designed by mathematicians specifically to make expression of complex statements easier and suits this purpose much better than natural language. Logica extends classical Logic programming syntax further, most notably with aggregation…

因此,可以认为 Logica 的命名来自 逻辑 聚合。

Logica = Logic Aggregation.

Logica 与 SQL 语言的对比

  • 简单的 SELECT 语句

Logica

MagicNumber(x:) :-

  x in [2, 3, 5];

等价 SQL

SELECT 2 AS x

UNION ALL

SELECT 3 AS x

UNION ALL

SELECT 5 AS x;
  • 过滤语句

Logica

MagicComment(comment_text::-

 `comments`(user_id:comment_text:),

 user_id == 5;

等价 SQL

SELECT comment_text FROM comments WHERE user_id = 5;

个人评价

Logica 语言宣称要取代 SQL 语言,并且认为 SQL 不够抽象,不足以表达更复杂的逻辑。是的,这个确实是 SQL 语言存在的问题,但是 SQL 的核心是为了要做一种极度简单的语言,避免一般编程语言的复杂性,让使用者聚焦于要做什么,而不用关注要怎么做。

要是 SQL 语言的使用者知道底层的计算引擎怎么做,为什么不直接使用正宗的编程语言去描述,反而要用半吊子 Logica 语言呢?就是因为大部分的 SQL 语言使用者不需要知道底层的计算引擎怎么做,才会使用 SQL 语言,告诉底层的计算引擎要做什么,剩下的交给计算引擎实现即可。

我觉得写这篇文章的作者可能是一个典型的程序员吧,所以才认为 SQL 语言不够完美。然而,大部分 SQL 的使用者都不是程序员,他们更关注业务的逻辑,而不是计算的逻辑。

总的来说,这篇文章更像是“鸡同鸭讲”。

内容出处:,

声明:本网站所收集的部分公开资料来源于互联网,转载的目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。如果您发现网站上有侵犯您的知识产权的作品,请与我们取得联系,我们会及时修改或删除。文章链接:http://www.yixao.com/tech/23275.html

发表评论

登录后才能评论