Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adjust how aliases are formatted #4750

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -50,7 +50,7 @@ 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
sum_gross_cost=(sum gross_cost), # `=` sets a column name
}
)
filter sum_gross_cost > 100_000 # `filter` replaces both of SQL's `WHERE` & `HAVING`
12 changes: 12 additions & 0 deletions prqlc/prqlc-parser/src/parser/pr/expr.rs
Original file line number Diff line number Diff line change
@@ -97,6 +97,18 @@
doc_comment: None,
}
}

/// Whether it contains spaces between top-level items.
/// So, for example `sum foo` would be true, but `[foo, bar]` would be
/// false, since the array is self-contained.
pub fn is_multiple_items(&self) -> bool {
match self {
ExprKind::Binary(_) => true,
ExprKind::Func(_) => true,

Check warning on line 107 in prqlc/prqlc-parser/src/parser/pr/expr.rs

Codecov / codecov/patch

prqlc/prqlc-parser/src/parser/pr/expr.rs#L107

Added line #L107 was not covered by tests
ExprKind::FuncCall(func_call) if !func_call.args.is_empty() => true,
_ => false,
}
}
}

#[derive(Debug, EnumAsInner, PartialEq, Clone, Serialize, Deserialize, JsonSchema)]
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: prqlc/prqlc-parser/src/test.rs
expression: "parse_single(r#\"\nfrom employees\nfilter country == \"USA\" # Each line transforms the previous result.\nderive { # This adds columns / variables.\n gross_salary = salary + payroll_tax,\n gross_cost = gross_salary + benefits_cost # Variables can use other variables.\n}\nfilter gross_cost > 0\ngroup {title, country} ( # For each group use a nested pipeline\n aggregate { # Aggregate each group to a single row\n average salary,\n average gross_salary,\n sum salary,\n sum gross_salary,\n average gross_cost,\n sum_gross_cost = sum gross_cost,\n ct = count salary,\n }\n)\nsort sum_gross_cost\nfilter ct > 200\ntake 20\n \"#).unwrap()"
expression: "parse_source(r#\"\nfrom employees\nfilter country == \"USA\" # Each line transforms the previous result.\nderive { # This adds columns / variables.\n gross_salary = salary + payroll_tax,\n gross_cost = gross_salary + benefits_cost # Variables can use other variables.\n}\nfilter gross_cost > 0\ngroup {title, country} ( # For each group use a nested pipeline\n aggregate { # Aggregate each group to a single row\n average salary,\n average gross_salary,\n sum salary,\n sum gross_salary,\n average gross_cost,\n sum_gross_cost=(sum gross_cost),\n ct=(count salary),\n }\n)\nsort sum_gross_cost\nfilter ct > 200\ntake 20\n \"#).unwrap()"
---
- VarDef:
kind: Main
@@ -136,20 +136,20 @@ expression: "parse_single(r#\"\nfrom employees\nfilter country == \"USA\"
- FuncCall:
name:
Ident: sum
span: "0:625-628"
span: "0:624-627"
args:
- Ident: gross_cost
span: "0:629-639"
span: "0:625-639"
span: "0:628-638"
span: "0:623-639"
alias: sum_gross_cost
- FuncCall:
name:
Ident: count
span: "0:650-655"
span: "0:649-654"
args:
- Ident: salary
span: "0:656-662"
span: "0:650-662"
span: "0:655-661"
span: "0:648-662"
alias: ct
span: "0:424-667"
span: "0:414-667"
4 changes: 2 additions & 2 deletions prqlc/prqlc-parser/src/test.rs
Original file line number Diff line number Diff line change
@@ -108,8 +108,8 @@ group {title, country} ( # For each group use a nested pipel
sum salary,
sum gross_salary,
average gross_cost,
sum_gross_cost = sum gross_cost,
ct = count salary,
sum_gross_cost=(sum gross_cost),
ct=(count salary),
}
)
sort sum_gross_cost
4 changes: 2 additions & 2 deletions prqlc/prqlc/examples/compile-files/queries/variables.prql
Original file line number Diff line number Diff line change
@@ -12,8 +12,8 @@ group {title, country} ( # For each group use a nested pipel
sum salary,
sum gross_salary,
average gross_cost,
sum_gross_cost = sum gross_cost,
ct = count salary,
sum_gross_cost=(sum gross_cost),
ct=(count salary),
}
)
sort sum_gross_cost
23 changes: 14 additions & 9 deletions prqlc/prqlc/src/codegen/ast.rs
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@

if let Some(alias) = &self.alias {
r += opt.consume(alias)?;
r += opt.consume(" = ")?;
r += opt.consume("=")?;
opt.unbound_expr = false;
}

@@ -48,6 +48,12 @@
}

fn needs_parenthesis(this: &pr::Expr, opt: &WriteOpt) -> bool {
// If we have an alias, we use parentheses if we contain multiple items, so
// we get `a=(b + c)` instead of `a=b + c`.
if this.alias.is_some() && this.kind.is_multiple_items() {
return true;
}

if opt.unbound_expr && can_bind_left(&this.kind) {
return true;
}
@@ -474,7 +480,7 @@
r += opt.consume(&format!("type {}", type_def.name))?;

if let Some(ty) = &type_def.value {
r += opt.consume(" = ")?;
r += opt.consume("=")?;
r += &ty.kind.write(opt)?;
}
r += "\n";
@@ -493,7 +499,7 @@
r += "import ";
if let Some(alias) = &import_def.alias {
r += &write_ident_part(alias);
r += " = ";
r += "=";

Check warning on line 502 in prqlc/prqlc/src/codegen/ast.rs

Codecov / codecov/patch

prqlc/prqlc/src/codegen/ast.rs#L502

Added line #L502 was not covered by tests
}
r += &import_def.name.write(opt)?;
r += "\n";
@@ -614,7 +620,7 @@
fn test_unary() {
assert_is_formatted(r#"sort {-duration}"#);

assert_is_formatted(r#"select a = -b"#);
assert_is_formatted(r#"select a=-b"#);
assert_is_formatted(r#"join `project-bar.dataset.table` (==col_bax)"#);
}

@@ -638,9 +644,8 @@
fn test_simple() {
assert_is_formatted(
r#"
aggregate average_country_salary = (
average salary
)"#,
aggregate average_country_salary=(average salary)
"#,
);
}

@@ -654,8 +659,8 @@
sum salary,
sum gross_salary,
average gross_cost,
sum_gross_cost = sum gross_cost,
ct = count salary,
sum_gross_cost=(sum gross_cost),
ct=(count salary),
})"#,
);
}
4 changes: 2 additions & 2 deletions prqlc/prqlc/src/codegen/types.rs
Original file line number Diff line number Diff line change
@@ -101,7 +101,7 @@ impl WriteSource for pr::TyTupleField {

if let Some(name) = name {
r += name;
r += " = ";
r += "=";
}
if let Some(expr) = expr {
r += &expr.write(opt)?;
@@ -121,7 +121,7 @@ impl WriteSource for UnionVariant<'_> {
let mut r = String::new();
if let Some(name) = &self.0 {
r += name;
r += " = ";
r += "=";
}
opt.consume_width(r.len() as u16);
r += &self.1.write(opt)?;
8 changes: 4 additions & 4 deletions prqlc/prqlc/src/semantic/eval.rs
Original file line number Diff line number Diff line change
@@ -482,7 +482,7 @@ mod test {
assert_snapshot!(eval(r"
{{a_a = 4, a_b = false}, b = 2.1 + 3.6, c = [false, true, false]}
").unwrap(),
@"{{a_a = 4, a_b = false}, b = 5.7, c = [false, true, false]}"
@"{{a_a=4, a_b=false}, b=5.7, c=[false, true, false]}"
);
}

@@ -507,7 +507,7 @@ mod test {
std.derive {d = 42}
std.filter c
").unwrap(),
@"[{c = true, 7, d = 42}, {c = true, 14, d = 42}]"
@"[{c=true, 7, d=42}, {c=true, 14, d=42}]"
);
}

@@ -521,7 +521,7 @@ mod test {
]
std.window {d = std.sum b}
").unwrap(),
@"[{d = 4}, {d = 9}, {d = 17}]"
@"[{d=4}, {d=9}, {d=17}]"
);
}

@@ -535,7 +535,7 @@ mod test {
]
std.columnar {g = std.lag b}
").unwrap(),
@"[{g = null}, {g = 4}, {g = 5}]"
@"[{g=null}, {g=4}, {g=5}]"
);
}
}
4 changes: 2 additions & 2 deletions prqlc/prqlc/tests/integration/project/Project.prql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
let favorite_artists = [
{artist_id = 120, last_listen = @2023-05-18},
{artist_id = 7, last_listen = @2023-05-16},
{artist_id=120, last_listen=@2023-05-18},
{artist_id=7, last_listen=@2023-05-16},
]

favorite_artists
16 changes: 8 additions & 8 deletions prqlc/prqlc/tests/integration/resolving.rs
Original file line number Diff line number Diff line change
@@ -31,7 +31,7 @@ fn resolve_basic_01() {
from x
select {a, b}
"#).unwrap(), @r###"
let main <[{a = ?, b = ?}]> = `(Select ...)`
let main <[{a=?, b=?}]> = `(Select ...)`
"###)
}

@@ -53,7 +53,7 @@ fn resolve_types_01() {
assert_snapshot!(resolve(r#"
type A = int || int
"#).unwrap(), @r###"
type A = int
type A=int
"###)
}

@@ -62,7 +62,7 @@ fn resolve_types_02() {
assert_snapshot!(resolve(r#"
type A = int || {}
"#).unwrap(), @r###"
type A = int || {}
type A=int || {}
"###)
}

@@ -71,7 +71,7 @@ fn resolve_types_03() {
assert_snapshot!(resolve(r#"
type A = {a = int, bool} || {b = text, float}
"#).unwrap(), @r###"
type A = {a = int, bool, b = text, float}
type A={a=int, bool, b=text, float}
"###)
}

@@ -87,9 +87,9 @@ fn resolve_types_04() {
"#,
)
.unwrap(), @r###"
type Status = (
Unpaid = float ||
{reason = text, cancelled_at = timestamp} ||
type Status=(
Unpaid=float ||
{reason=text, cancelled_at=timestamp} ||
)
"###);
}
@@ -103,7 +103,7 @@ fn resolve_types_05() {
"#,
)
.unwrap(), @r###"
type A = null
type A=null
"###);
}

Original file line number Diff line number Diff line change
@@ -5,11 +5,10 @@ input_file: prqlc/prqlc/tests/integration/queries/aggregation.prql
---
from tracks
filter genre_id == 100
derive empty_name = name == ""
derive empty_name=(name == "")
aggregate {
sum track_id,
concat_array name,
all empty_name,
any empty_name,
}

Original file line number Diff line number Diff line change
@@ -4,33 +4,27 @@ expression: "# mssql:test\nfrom [\n { id = 1, x_int = 13, x_float = 13.0, k
input_file: prqlc/prqlc/tests/integration/queries/arithmetic.prql
---
from [
{id=1, x_int=13, x_float=13, k_int=5, k_float=5},
{
id = 1,
x_int = 13,
x_float = 13,
k_int = 5,
k_float = 5,
id=2,
x_int=-13,
x_float=-13,
k_int=5,
k_float=5,
},
{
id = 2,
x_int = -13,
x_float = -13,
k_int = 5,
k_float = 5,
id=3,
x_int=13,
x_float=13,
k_int=-5,
k_float=-5,
},
{
id = 3,
x_int = 13,
x_float = 13,
k_int = -5,
k_float = -5,
},
{
id = 4,
x_int = -13,
x_float = -13,
k_int = -5,
k_float = -5,
id=4,
x_int=-13,
x_float=-13,
k_int=-5,
k_float=-5,
},
]
select {
@@ -39,18 +33,17 @@ select {
x_int / k_float,
x_float / k_int,
x_float / k_float,
q_ii = x_int // k_int,
q_if = x_int // k_float,
q_fi = x_float // k_int,
q_ff = x_float // k_float,
r_ii = x_int % k_int,
r_if = x_int % k_float,
r_fi = x_float % k_int,
r_ff = x_float % k_float,
q_ii=(x_int // k_int),
q_if=(x_int // k_float),
q_fi=(x_float // k_int),
q_ff=(x_float // k_float),
r_ii=(x_int % k_int),
r_if=(x_int % k_float),
r_fi=(x_float % k_int),
r_ff=(x_float % k_float),
(q_ii * k_int + r_ii | math.round 0),
(q_if * k_float + r_if | math.round 0),
(q_fi * k_int + r_fi | math.round 0),
(q_ff * k_float + r_ff | math.round 0),
}
sort id

Original file line number Diff line number Diff line change
@@ -5,6 +5,5 @@ input_file: prqlc/prqlc/tests/integration/queries/cast.prql
---
from tracks
sort {-bytes}
select {name, bin = (album_id | as REAL) * 99}
select {name, bin=((album_id | as REAL) * 99)}
take 20

Original file line number Diff line number Diff line change
@@ -8,5 +8,4 @@ take 10
filter true
take 20
filter true
select d = 10

select d=10
Original file line number Diff line number Diff line change
@@ -6,17 +6,22 @@ input_file: prqlc/prqlc/tests/integration/queries/date_to_text.prql
from invoices
take 20
select {
d1 = (invoice_date | date.to_text "%Y/%m/%d"),
d2 = (invoice_date | date.to_text "%F"),
d3 = (invoice_date | date.to_text "%D"),
d4 = (invoice_date | date.to_text "%H:%M:%S.%f"),
d5 = (invoice_date | date.to_text "%r"),
d6 = (invoice_date | date.to_text "%A %B %-d %Y"),
d7 = (invoice_date | date.to_text "%a, %-d %b %Y at %I:%M:%S %p"),
d8 = (invoice_date | date.to_text "%+"),
d9 = (invoice_date | date.to_text "%-d/%-m/%y"),
d10 = (invoice_date | date.to_text "%-Hh %Mmin"),
d11 = (invoice_date | date.to_text ""%M'%S"""),
d12 = (invoice_date | date.to_text "100%% in %d days"),
d1=(invoice_date | date.to_text "%Y/%m/%d"),
d2=(invoice_date | date.to_text "%F"),
d3=(invoice_date | date.to_text "%D"),
d4=(invoice_date | date.to_text "%H:%M:%S.%f"),
d5=(invoice_date | date.to_text "%r"),
d6=(invoice_date | date.to_text "%A %B %-d %Y"),
d7=(
invoice_date
date.to_text "%a, %-d %b %Y at %I:%M:%S %p"
),
d8=(invoice_date | date.to_text "%+"),
d9=(invoice_date | date.to_text "%-d/%-m/%y"),
d10=(invoice_date | date.to_text "%-Hh %Mmin"),
d11=(invoice_date | date.to_text ""%M'%S"""),
d12=(
invoice_date
date.to_text "100%% in %d days"
),
}

Original file line number Diff line number Diff line change
@@ -5,10 +5,9 @@ input_file: prqlc/prqlc/tests/integration/queries/genre_counts.prql
---
let genre_count = (
from genres
aggregate {a = count name}
aggregate {a=(count name)}
)

from genre_count
filter a > 0
select a = -a

select a=-a
Original file line number Diff line number Diff line change
@@ -3,12 +3,11 @@ source: prqlc/prqlc/tests/integration/queries.rs
expression: "# mssql:test\nfrom a=albums\ntake 10\njoin tracks (==album_id)\ngroup {a.album_id, a.title} (aggregate price = (sum tracks.unit_price | math.round 2))\nsort album_id\n"
input_file: prqlc/prqlc/tests/integration/queries/group_all.prql
---
from a = albums
from a=albums
take 10
join tracks (==album_id)
group {a.album_id, a.title} (aggregate price = (
group {a.album_id, a.title} (aggregate price=(
sum tracks.unit_price
math.round 2
))
sort album_id

Original file line number Diff line number Diff line change
@@ -4,9 +4,8 @@ expression: "# mssql:test\nfrom tracks\nderive d = album_id + 1\ngroup d (\n
input_file: prqlc/prqlc/tests/integration/queries/group_sort.prql
---
from tracks
derive d = album_id + 1
group d (aggregate {n1 = (track_id | sum)})
derive d=(album_id + 1)
group d (aggregate {n1=(track_id | sum)})
sort d
take 10
select {d1 = d, n1}

select {d1=d, n1}
Original file line number Diff line number Diff line change
@@ -3,28 +3,28 @@ source: prqlc/prqlc/tests/integration/queries.rs
expression: "# clickhouse:skip (clickhouse doesn't have lag function)\n\n#! Calculate a number of metrics about the sales of tracks in each city.\nfrom i=invoices\njoin ii=invoice_items (==invoice_id)\nderive {\n city = i.billing_city,\n street = i.billing_address,\n}\ngroup {city, street} (\n derive total = ii.unit_price * ii.quantity\n aggregate {\n num_orders = count_distinct i.invoice_id,\n num_tracks = sum ii.quantity,\n total_price = sum total,\n }\n)\ngroup {city} (\n sort street\n window expanding:true (\n derive {running_total_num_tracks = sum num_tracks}\n )\n)\nsort {city, street}\nderive {num_tracks_last_week = lag 7 num_tracks}\nselect {\n city,\n street,\n num_orders,\n num_tracks,\n running_total_num_tracks,\n num_tracks_last_week\n}\ntake 20\n"
input_file: prqlc/prqlc/tests/integration/queries/invoice_totals.prql
---
from i = invoices
join ii = invoice_items (==invoice_id)
from i=invoices
join ii=invoice_items (==invoice_id)
derive {
city = i.billing_city,
street = i.billing_address,
city=i.billing_city,
street=i.billing_address,
}
group {city, street} (
derive total = ii.unit_price * ii.quantity
derive total=(ii.unit_price * ii.quantity)
aggregate {
num_orders = count_distinct i.invoice_id,
num_tracks = sum ii.quantity,
total_price = sum total,
num_orders=(count_distinct i.invoice_id),
num_tracks=(sum ii.quantity),
total_price=(sum total),
}
)
group {city} (
sort street
window expanding:true (derive {
running_total_num_tracks = sum num_tracks,
running_total_num_tracks=(sum num_tracks),
})
)
sort {city, street}
derive {num_tracks_last_week = lag 7 num_tracks}
derive {num_tracks_last_week=(lag 7 num_tracks)}
select {
city,
street,
@@ -34,4 +34,3 @@ select {
num_tracks_last_week,
}
take 20

Original file line number Diff line number Diff line change
@@ -3,9 +3,8 @@ source: prqlc/prqlc/tests/integration/queries.rs
expression: "# clickhouse:skip (DB::Exception: Syntax error)\n# glaredb:skip (DataFusion does not support recursive CTEs https://github.com/apache/arrow-datafusion/issues/462)\nfrom [{n = 1}]\nselect n = n - 2\nloop (filter n < 4 | select n = n + 1)\nselect n = n * 2\nsort n\n"
input_file: prqlc/prqlc/tests/integration/queries/loop_01.prql
---
from [{n = 1}]
select n = n - 2
loop (filter n < 4 | select n = n + 1)
select n = n * 2
from [{n=1}]
select n=(n - 2)
loop (filter n < 4 | select n=(n + 1))
select n=(n * 2)
sort n

Original file line number Diff line number Diff line change
@@ -6,47 +6,43 @@ input_file: prqlc/prqlc/tests/integration/queries/math_module.prql
from invoices
take 5
select {
total_original = (total | math.round 2),
total_x = (
total_original=(total | math.round 2),
total_x=(
math.pi - total
math.round 2
math.abs
),
total_floor = math.floor total,
total_ceil = math.ceil total,
total_log10 = (math.log10 total | math.round 3),
total_log2 = (math.log 2 total | math.round 3),
total_sqrt = (math.sqrt total | math.round 3),
total_ln = (
total_floor=(math.floor total),
total_ceil=(math.ceil total),
total_log10=(math.log10 total | math.round 3),
total_log2=(math.log 2 total | math.round 3),
total_sqrt=(math.sqrt total | math.round 3),
total_ln=(
math.ln total
math.exp
math.round 2
),
total_cos = (
total_cos=(
math.cos total
math.acos
math.round 2
),
total_sin = (
total_sin=(
math.sin total
math.asin
math.round 2
),
total_tan = (
total_tan=(
math.tan total
math.atan
math.round 2
),
total_deg = (
total_deg=(
total
math.degrees
math.radians
math.round 2
),
total_square = (
total
math.pow 2
math.round 2
),
total_square_op = (total ** 2 | math.round 2),
total_square=(total | math.pow 2 | math.round 2),
total_square_op=(total ** 2 | math.round 2),
}
Original file line number Diff line number Diff line change
@@ -4,12 +4,11 @@ expression: "# mssql:test\nlet distinct = rel -> (from t = _param.rel | group {t
input_file: prqlc/prqlc/tests/integration/queries/set_ops_remove.prql
---
let distinct = func rel -> (
from t = _param.rel
from t=_param.rel
group {t.*} (take 1)
)

from_text format:json '{ "columns": ["a"], "data": [[1], [2], [2], [3]] }'
distinct
remove (from_text format:json '{ "columns": ["a"], "data": [[1], [2]] }')
sort a

Original file line number Diff line number Diff line change
@@ -3,9 +3,8 @@ source: prqlc/prqlc/tests/integration/queries.rs
expression: "# mssql:test\nfrom e=employees\nfilter first_name != \"Mitchell\"\nsort {first_name, last_name}\n\n# joining may use HashMerge, which can undo ORDER BY\njoin manager=employees side:left (e.reports_to == manager.employee_id)\n\nselect {e.first_name, e.last_name, manager.first_name}\n"
input_file: prqlc/prqlc/tests/integration/queries/sort.prql
---
from e = employees
from e=employees
filter first_name != "Mitchell"
sort {first_name, last_name}
join side:left manager = employees e.reports_to == manager.employee_id
join side:left manager=employees e.reports_to == manager.employee_id
select {e.first_name, e.last_name, manager.first_name}

Original file line number Diff line number Diff line change
@@ -5,10 +5,9 @@ input_file: prqlc/prqlc/tests/integration/queries/switch.prql
---
from tracks
sort milliseconds
select display = case [
select display=case [
composer != null => composer,
genre_id < 17 => "no composer",
true => f"unknown composer",
]
take 10

Original file line number Diff line number Diff line change
@@ -6,15 +6,15 @@ input_file: prqlc/prqlc/tests/integration/queries/text_module.prql
from albums
select {
title,
title_and_spaces = f" {title} ",
low = (title | text.lower),
up = (title | text.upper),
ltrimmed = (title | text.ltrim),
rtrimmed = (title | text.rtrim),
trimmed = (title | text.trim),
len = (title | text.length),
subs = (title | text.extract 2 5),
replace = (title | text.replace "al" "PIKA"),
title_and_spaces=f" {title} ",
low=(title | text.lower),
up=(title | text.upper),
ltrimmed=(title | text.ltrim),
rtrimmed=(title | text.rtrim),
trimmed=(title | text.trim),
len=(title | text.length),
subs=(title | text.extract 2 5),
replace=(title | text.replace "al" "PIKA"),
}
sort {title}
filter (title | text.starts_with "Black") || (
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
---
source: prqlc/prqlc/tests/integration/queries.rs
expression: "# mssql:skip Conversion(\"cannot interpret I64(Some(1)) as an i32 value\")', connection.rs:200:34\n# duckdb:skip problems with DISTINCT ON (duckdb internal error: [with INPUT_TYPE = int; RESULT_TYPE = unsigned char]: Assertion `min_val <= input' failed.)\n# clickhouse:skip problems with DISTINCT ON\n# postgres:skip problems with DISTINCT ON\nfrom tracks\ngroup genre_id (\n sort milliseconds\n derive {\n num = row_number this,\n total = count this,\n last_val = last track_id,\n }\n take 10\n)\nsort {genre_id, milliseconds}\nselect {track_id, genre_id, num, total, last_val}\nfilter genre_id >= 22\n"
expression: "# mssql:skip Conversion(\"cannot interpret I64(Some(1)) as an i32 value\")', connection.rs:200:34\n# duckdb:skip problems with DISTINCT ON (duckdb internal error: [with INPUT_TYPE = int; RESULT_TYPE = unsigned char]: Assertion `min_val <= input' failed.)\n# clickhouse:skip problems with DISTINCT ON\n# postgres:skip problems with DISTINCT ON\n# glaredb:skip — TODO: started raising an error on 2024-05-20, from https://github.com/PRQL/prql/actions/runs/9154902656/job/25198160283:\n # ERROR: This feature is not implemented: Unsupported ast node in sqltorel:\n # Substring { expr: Identifier(Ident { value: \"title\", quote_style: None }),\n # substring_from: Some(Value(Number(\"2\", false))), substring_for:\n # Some(Value(Number(\"5\", false))), special: true }\nfrom tracks\ngroup genre_id (\n sort milliseconds\n derive {\n num = row_number this,\n total = count this,\n last_val = last track_id,\n }\n take 10\n)\nsort {genre_id, milliseconds}\nselect {track_id, genre_id, num, total, last_val}\nfilter genre_id >= 22\n"
input_file: prqlc/prqlc/tests/integration/queries/window.prql
---
from tracks
group genre_id (
sort milliseconds
derive {
num = row_number this,
total = count this,
last_val = last track_id,
num=(row_number this),
total=(count this),
last_val=(last track_id),
}
take 10
)
sort {genre_id, milliseconds}
select {track_id, genre_id, num, total, last_val}
filter genre_id >= 22

4 changes: 2 additions & 2 deletions prqlc/prqlc/tests/integration/sql.rs
Original file line number Diff line number Diff line change
@@ -2836,8 +2836,8 @@ aggregate { # `by` are the columns to group by.
average gross_salary,
sum gross_salary,
average gross_cost,
sum_gross_cost = sum gross_cost,
ct = count salary,
sum_gross_cost=(sum gross_cost),
ct=(count salary),
}
)
sort sum_gross_cost
2 changes: 1 addition & 1 deletion web/book/src/README.md
Original file line number Diff line number Diff line change
@@ -49,7 +49,7 @@ 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
sum_gross_cost=(sum gross_cost), # `=` sets a column name
}
)
filter sum_gross_cost > 100_000 # `filter` replaces both of SQL's `WHERE` & `HAVING`
2 changes: 1 addition & 1 deletion web/book/src/reference/stdlib/transforms/aggregate.md
Original file line number Diff line number Diff line change
@@ -32,7 +32,7 @@ from employees
group {title, country} (
aggregate {
average salary,
ct = count salary,
ct=(count salary),
}
)
```
2 changes: 1 addition & 1 deletion web/book/src/reference/syntax/pipes.md
Original file line number Diff line number Diff line change
@@ -66,7 +66,7 @@ from employees
group {title, country} (
aggregate {
average salary,
ct = count salary,
ct=(count salary),
}
)
```
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: web/book/tests/documentation/book.rs
expression: "from employees\nfilter start_date > @2021-01-01 # Clear date syntax\nderive { # `derive` adds columns / variables\n gross_salary = salary + (tax ?? 0), # Terse coalesce\n gross_cost = gross_salary + benefits, # Variables can use other variables\n}\nfilter gross_cost > 0\ngroup {title, country} ( # `group` runs a pipeline over each group\n aggregate { # `aggregate` reduces each group to a value\n average gross_salary,\n sum_gross_cost = sum gross_cost, # `=` sets a column name\n }\n)\nfilter sum_gross_cost > 100_000 # `filter` replaces both of SQL's `WHERE` & `HAVING`\nderive id = f\"{title}_{country}\" # F-strings like Python\nderive country_code = s\"LEFT(country, 2)\" # S-strings permit SQL as an escape hatch\nsort {sum_gross_cost, -country} # `-country` means descending order\ntake 1..20 # Range expressions (also valid as `take 20`)\n"
expression: "from employees\nfilter start_date > @2021-01-01 # Clear date syntax\nderive { # `derive` adds columns / variables\n gross_salary = salary + (tax ?? 0), # Terse coalesce\n gross_cost = gross_salary + benefits, # Variables can use other variables\n}\nfilter gross_cost > 0\ngroup {title, country} ( # `group` runs a pipeline over each group\n aggregate { # `aggregate` reduces each group to a value\n average gross_salary,\n sum_gross_cost=(sum gross_cost), # `=` sets a column name\n }\n)\nfilter sum_gross_cost > 100_000 # `filter` replaces both of SQL's `WHERE` & `HAVING`\nderive id = f\"{title}_{country}\" # F-strings like Python\nderive country_code = s\"LEFT(country, 2)\" # S-strings permit SQL as an escape hatch\nsort {sum_gross_cost, -country} # `-country` means descending order\ntake 1..20 # Range expressions (also valid as `take 20`)\n"
---
WITH table_1 AS (
SELECT
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: web/book/tests/documentation/book.rs
expression: "from employees\ngroup {title, country} (\n aggregate {\n average salary,\n ct = count salary,\n }\n)\n"
expression: "from employees\ngroup {title, country} (\n aggregate {\n average salary,\n ct=(count salary),\n }\n)\n"
---
SELECT
title,
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: web/book/tests/documentation/book.rs
expression: "from employees\ngroup {title, country} (\n aggregate {\n average salary,\n ct = count salary,\n }\n)\n"
expression: "from employees\ngroup {title, country} (\n aggregate {\n average salary,\n ct=(count salary),\n }\n)\n"
---
SELECT
title,
2 changes: 1 addition & 1 deletion web/website/content/posts/2022-05-19-examples.md
Original file line number Diff line number Diff line change
@@ -67,7 +67,7 @@ group {title, country} ( # `group` runs a pipeline over eac
average gross_salary,
sum gross_salary,
average gross_cost,
sum_gross_cost = sum gross_cost, # `=` sets a column name.
sum_gross_cost=(sum gross_cost), # `=` sets a column name.
ct = count this,
}
)