PRQL 是用于转换数据的现代化语言,也是简单、强大、管道化的 SQL 替代品。
PRQL 全称是 Pipelined Relational Query Language,读作 "Prequel"(正好和 SQL/Sequel 相反)。跟 SQL 类似,PRQL 是可读和具有声明性的;但与 SQL 不同的是,它支持变量和函数等抽象概念,以及 PRQL 是线性转化的:每一行查询都在转换前一行的结果。
示例
from tracks
filter artist == "Bob Marley" # Each line transforms the previous result
aggregate { # `aggregate` reduces each column to a value
plays = sum plays,
longest = max length,
shortest = min length, # Trailing commas are allowed
}
from employees
filter start_date > @2021-01-01 # Clear date syntax
derive { # `derive` adds columns / variables
gross_salary = salary + (tax ?? 0), # Terse coalesce
gross_cost = gross_salary + benefits_cost, # Variables can use other variables
}
filter gross_cost > 0
group {title, country} ( # `group` runs a pipeline over each group
aggregate { # `aggregate` reduces each group to a value
average gross_salary,
sum_gross_cost = sum gross_cost, # `=` sets a column name
}
)
filter sum_gross_cost > 100_000 # `filter` replaces both of SQL's `WHERE` & `HAVING`
derive id = f"{title}_{country}" # F-strings like Python
derive country_code = s"LEFT(country, 2)" # S-strings allow using SQL as an escape hatch
sort {sum_gross_cost, -country} # `-country` means descending order
take 1..20 # Range expressions (also valid here as `take 20`)
评论