前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >评谷歌新发布的编程语言:Logica

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

作者头像
哒呵呵
发布2021-05-13 16:43:54
7820
发布2021-05-13 16:43:54
举报
文章被收录于专栏:鸿的学习笔记

周末读 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

代码语言:javascript
复制
MagicNumber(x:) :-

  x in [2, 3, 5];

等价 SQL

代码语言:javascript
复制
SELECT 2 AS x

UNION ALL

SELECT 3 AS x

UNION ALL

SELECT 5 AS x;
  • 过滤语句

Logica

代码语言:javascript
复制
MagicComment(comment_text:) :-

 `comments`(user_id:, comment_text:),

 user_id == 5;

等价 SQL

代码语言:javascript
复制
SELECT comment_text FROM comments WHERE user_id = 5;
个人评价

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

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

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

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

参考链接

https://opensource.googleblog.com/2021/04/logica-organizing-your-data-queries.html?m=1

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-05-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 鸿的笔记 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 诞生
  • Logica 介绍
  • 设计 Logica 的原因
  • Logica 的优势
  • Logica 与 SQL 语言的对比
  • 个人评价
    • 参考链接
    相关产品与服务
    数据库
    云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档