Skip to content

Commit

Permalink
fix!: Update case syntax to use brackets over braces (#3517)
Browse files Browse the repository at this point in the history
Co-authored-by: AaronMoat <[email protected]>
  • Loading branch information
AaronMoat and AaronMoat authored Sep 17, 2023
1 parent 65ac284 commit 33b7171
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

**Language**:

- _Breaking:_ Case syntax now uses brackets `[]` rather than braces `{}`. To
convert previous PRQL queries to this new syntax simply change `case { ... }`
to `case [ ... ]`. (@AaronMoat, #3517)

**Features**:

**Fixes**:
Expand Down
2 changes: 1 addition & 1 deletion crates/prql-compiler/src/codegen/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl WriteSource for ExprKind {
inline: ", ",
line_end: ",",
}
.write_between("{", "}", opt)?;
.write_between("[", "]", opt)?;
Some(r)
}
Param(id) => Some(format!("${id}")),
Expand Down
16 changes: 8 additions & 8 deletions crates/prql-compiler/src/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2908,10 +2908,10 @@ fn test_case() {
assert_display_snapshot!(compile(
r###"
from employees
derive display_name = case {
derive display_name = case [
nickname != null => nickname,
true => f'{first_name} {last_name}'
}
]
"###).unwrap(),
@r###"
SELECT
Expand All @@ -2928,10 +2928,10 @@ fn test_case() {
assert_display_snapshot!(compile(
r###"
from employees
derive display_name = case {
derive display_name = case [
nickname != null => nickname,
first_name != null => f'{first_name} {last_name}'
}
]
"###).unwrap(),
@r###"
SELECT
Expand All @@ -2949,9 +2949,9 @@ fn test_case() {
assert_display_snapshot!(compile(
r###"
from tracks
select category = case {
select category = case [
length > avg_length => 'long'
}
]
group category (aggregate {count this})
"###).unwrap(),
@r###"
Expand Down Expand Up @@ -3002,14 +3002,14 @@ fn test_static_analysis() {
b = !(!(!(!(!(true))))),
a3 = null ?? y,
a3 = case {
a3 = case [
false == true => 1,
7 == 3 => 2,
7 == y => 3,
7.3 == 7.3 => 4,
z => 5,
true => 6
},
],
}
"###).unwrap(),
@r###"
Expand Down
4 changes: 2 additions & 2 deletions crates/prql-compiler/tests/integration/queries/switch.prql
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# mssql:test
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
@@ -1,14 +1,14 @@
---
source: prql-compiler/tests/integration/main.rs
expression: "from tracks\nsort milliseconds\nselect display = case {\n composer != null => composer,\n genre_id < 17 => 'no composer',\n true => f'unknown composer'\n}\ntake 10\n"
expression: "from tracks\nsort milliseconds\nselect display = case [\n composer != null => composer,\n genre_id < 17 => 'no composer',\n true => f'unknown composer'\n]\ntake 10\n"
input_file: prql-compiler/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
@@ -1,6 +1,6 @@
---
source: prql-compiler/tests/integration/main.rs
expression: "from tracks\nsort milliseconds\nselect display = case {\n composer != null => composer,\n genre_id < 17 => 'no composer',\n true => f'unknown composer'\n}\ntake 10\n"
expression: "from tracks\nsort milliseconds\nselect display = case [\n composer != null => composer,\n genre_id < 17 => 'no composer',\n true => f'unknown composer'\n]\ntake 10\n"
input_file: prql-compiler/tests/integration/queries/switch.prql
---
WITH table_0 AS (
Expand Down
2 changes: 1 addition & 1 deletion crates/prql-parser/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pub fn expr() -> impl Parser<Token, Expr, Error = PError> + Clone {
.separated_by(ctrl(','))
.allow_trailing()
.then_ignore(new_line().repeated())
.delimited_by(ctrl('{'), ctrl('}')),
.delimited_by(ctrl('['), ctrl(']')),
)
.map(ExprKind::Case);

Expand Down
4 changes: 2 additions & 2 deletions crates/prql-parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2241,10 +2241,10 @@ join s=salaries (==id)

#[test]
fn test_case() {
assert_yaml_snapshot!(parse_expr(r#"case {
assert_yaml_snapshot!(parse_expr(r#"case [
nickname != null => nickname,
true => null
}"#).unwrap(), @r###"
]"#).unwrap(), @r###"
---
Case:
- condition:
Expand Down
2 changes: 1 addition & 1 deletion web/book/src/reference/syntax/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ A summary of PRQL syntax:
| `#` | [Comments](./comments.md) | `# A comment` |
| `==` | [Self-equality in `join`](../stdlib/transforms/join.md#self-equality-operator) | `join s=salaries (==id)` |
| `->` | [Function definitions](../declarations/functions.md) | `let add = a b -> a + b` |
| `=>` | [Case statement](./case.md) | `case {a==1 => c, a==2 => d }` |
| `=>` | [Case statement](./case.md) | `case [a==1 => c, a==2 => d]` |
| `+`,`-` | [Sort order](../stdlib/transforms/sort.md) | `sort {-amount, +date}` |
| `??` | [Coalesce](./operators.md#coalesce) | `amount ?? 0` |

Expand Down
8 changes: 4 additions & 4 deletions web/book/src/reference/syntax/case.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ associated value. If none of the conditions match, `null` is returned.

```prql
from employees
derive distance = case {
derive distance = case [
city == "Calgary" => 0,
city == "Edmonton" => 300,
}
]
```

To set a default, a `true` condition can be used:

```prql
from employees
derive distance = case {
derive distance = case [
city == "Calgary" => 0,
city == "Edmonton" => 300,
true => "Unknown",
}
]
```
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: web/book/tests/documentation/book.rs
expression: "from employees\nderive distance = case {\n city == \"Calgary\" => 0,\n city == \"Edmonton\" => 300,\n}\n"
expression: "from employees\nderive distance = case [\n city == \"Calgary\" => 0,\n city == \"Edmonton\" => 300,\n]\n"
---
SELECT
*,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: web/book/tests/documentation/book.rs
expression: "from employees\nderive distance = case {\n city == \"Calgary\" => 0,\n city == \"Edmonton\" => 300,\n true => \"Unknown\",\n}\n"
expression: "from employees\nderive distance = case [\n city == \"Calgary\" => 0,\n city == \"Edmonton\" => 300,\n true => \"Unknown\",\n]\n"
---
SELECT
*,
Expand Down

0 comments on commit 33b7171

Please sign in to comment.