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
Show file tree
Hide file tree
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
Expand Up @@ -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`
Expand Down
12 changes: 12 additions & 0 deletions prqlc/prqlc-parser/src/parser/pr/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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

View check run for this annotation

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)]
Expand Down
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
Expand Down Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions prqlc/prqlc-parser/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions prqlc/prqlc/examples/compile-files/queries/variables.prql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 14 additions & 9 deletions prqlc/prqlc/src/codegen/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

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

Expand All @@ -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;
}
Expand Down Expand Up @@ -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";
Expand All @@ -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

View check run for this annotation

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";
Expand Down Expand Up @@ -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)"#);
}

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

Expand All @@ -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),
})"#,
);
}
Expand Down
4 changes: 2 additions & 2 deletions prqlc/prqlc/src/codegen/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand All @@ -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)?;
Expand Down
8 changes: 4 additions & 4 deletions prqlc/prqlc/src/semantic/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]}"
);
}

Expand All @@ -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}]"
);
}

Expand All @@ -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}]"
);
}

Expand All @@ -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
Expand Down
16 changes: 8 additions & 8 deletions prqlc/prqlc/tests/integration/resolving.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ...)`
"###)
}

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

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

Expand All @@ -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}
"###)
}

Expand All @@ -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} ||
)
"###);
}
Expand All @@ -103,7 +103,7 @@ fn resolve_types_05() {
"#,
)
.unwrap(), @r###"
type A = null
type A=null
"###);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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 {
Expand All @@ -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
Expand Up @@ -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
Expand Up @@ -8,5 +8,4 @@ take 10
filter true
take 20
filter true
select d = 10

select d=10
Loading