11行代码实现更好的 SQL
A better SQL in 11 lines of code

原始链接: https://prela-lang.org/tutorial/

Prela 是由加州大学洛杉矶分校 RePL 开发的一种新型查询语言,它仅通过二元关系(即只有两列的表)来简化数据查询。通过将复杂的多列数据表分解为一系列二元关系,Prela 将数据处理视为函数的组合,从而实现了高度可读且模块化的代码。 与传统的 SQL 不同(SQL 在执行常规任务时往往需要冗长复杂的语法),Prela 使用两个核心操作符:用于组合的 `.select()`(像链条一样链接属性)和用于连接列的 `&`。谓词通过 `.where()` 和 `.eq()` 应用,由于子表达式可作为独立的查询,开发人员可以轻松地将标准 Python 变量作为公用表表达式(CTE)使用。 该语言的优势在于其简洁性:它将关系视为“非确定性函数”,使得复杂的多次连接查询可以用直观、易读的路径来表达,例如 `movie.s(company).s(country)`。本教程展示了通过在 Python 中实现这些核心操作符,用户可以用比 SQL 少得多的样板代码来执行复杂的数据操作,同时保持对过滤、分组和聚合功能的全面支持。

Hacker News | 最新 | 过往 | 评论 | 提问 | 展示 | 招聘 | 提交 | 登录 用 11 行代码实现更好的 SQL (prela-lang.org) 8 分 | remywang | 1 小时前 | 隐藏 | 过往 | 收藏 | 1 条评论 帮助 prathje | 1 分钟前 | [–] 这个概念让我想起了 pandas 中可用的操作。但我不同意那个 20 行的 SQL 语句。那个语句感觉非常冗长,且包含许多多余的条件。一个更好的类比可以是这样: SELECT DISTINCT an.name, t.title FROM keyword k JOIN movie_keyword mk ON mk.keyword_id = k.id JOIN title t ON t.id = mk.movie_id JOIN movie_companies mc ON mc.movie_id = t.id JOIN company_name cn ON cn.id = mc.company_id JOIN cast_info ci ON ci.movie_id = t.id JOIN aka_name an ON an.person_id = ci.person_id WHERE k.keyword = 'character-name-in-title' AND cn.country_code = '[us]'; 回复 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文
Prela Tutorial

Prela is a new query language being developed at UCLA RePL. The language is quite different from SQL, but its key ideas are very simple. In this short tutorial, we will build a toy version of Prela in Python to understand its core principles. By the end of this tutorial, you will know how the following query works:

equivalent query in SQL spans over 20 lines.

The first special thing about Prela is that there are only binary relations, i.e., tables with two columns. That may sound very limiting at first, but it's easy to "binarize" a wide table with multiple columns. Suppose we have a table of movies:

ID title year
646 The Godfather 1972
478 Seven Samurai 1954
583 Casablanca 1942

We can decompose the 3-column table into 3 binary relations, each mapping the row number to the column value:

snip to connect code cells into a notebook-like environment, changes made in one cell are reflected in later cells.

The movie, title, and year relations above represent the ID, title, and year columns of the original table, respectively. Note how the row number comes first in title and year, but second in movie (which is also not called ID). The reason for this will become clear later.

The motivation for focusing on binary relations is that they generalize functions. Functions are powerful because they compose, making them the building blocks of programs. A function maps every input to a unique output, where as a relation can map an input to multiple different outputs. In a sense, a relation can be viewed as a nondeterministic function.

That is all very abstract, so let's go back to our examples. To keep things simple, we will focus on relations mapping every input to exactly one output, i.e., they all happen to be functions. "Calling" a relation then boils down to turning that relation into a dictionary and looking up the value:

CTE with a plain Python variable? Yes! This is possible because Prela queries are made up of operators, and every subexpression is a valid query.

How do we have multiple conditions? A happy accident is that, becuase & joins its arguments, it doubles as logical conjunction once nested inside a .where:

paper for more details. As an excercise, you can try to define the necessary relations so that the snippet at the top runs.

here.

联系我们 contact @ memedata.com