Skip to content

Commit

Permalink
feat!: Rename switch to case (#2036)
Browse files Browse the repository at this point in the history
* feat: Rename `switch` to `case`

In case `switch` is confusing, this switches `switch` for `case`. Hopefully the switch won't lead to a case of confusion; or we can case the decision again.
  • Loading branch information
max-sixty authored Mar 7, 2023
1 parent 47ec070 commit f04baf2
Show file tree
Hide file tree
Showing 34 changed files with 97 additions and 93 deletions.
16 changes: 9 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
- `and` now has a higher precedence than `or` (of same reason as the previous
point).
- Dates, times and timestamps have stricter parsing rules.
- `let`, `func`, `prql`, `switch` are now treated as keywords.
- `let`, `func`, `prql`, `case` are now treated as keywords.
- Float literals without fraction part are not allowed anymore (`1.`).
- Add a `--format` option to `prqlc parse` which can return the AST in YAML
(@max-sixty, #1962)
Expand All @@ -28,6 +28,8 @@
`@2020-01-01T13:19:55-0800` (@max-sixty, #1991).
- Add `std.upper` and `std.lower` functions for changing string casing
(@Jelenkee, #2019).
- Rename the experimental `switch` to `case` given it more closely matches the
traditional semantics of `case`. (@max-sixty, #2036)

**Fixes**:

Expand Down Expand Up @@ -214,7 +216,7 @@ This release has 74 commits from 12 contributors. Selected changes:

## 0.4.0 — 2022-01-15

0.4.0 brings lots of new features including `switch`, `select ![]` and numbers
0.4.0 brings lots of new features including `case`, `select ![]` and numbers
with underscores. We have initial (unpublished) bindings to Elixir. And there's
the usual improvements to fixes & documentation (only a minority are listed
below in this release).
Expand All @@ -229,12 +231,12 @@ below in this release).
[tables docs](https://prql-lang.org/book/queries/variables.html) for details.

- _Experimental:_ The
[`switch`](https://prql-lang.org/book/language-features/switch.html) function
sets a variable to a value based on one of several expressions (@aljazerzen,
[`case`](https://prql-lang.org/book/language-features/case.html) function sets
a variable to a value based on one of several expressions (@aljazerzen,
#1278).

```prql
derive var = switch [
derive var = case [
score <= 10 -> "low",
score <= 30 -> "medium",
score <= 70 -> "high",
Expand All @@ -258,8 +260,8 @@ below in this release).
```

Check out the
[`switch` docs](https://prql-lang.org/book/language-features/switch.html) for
more details.
[`case` docs](https://prql-lang.org/book/language-features/case.html) for more
details.

- _Experimental:_ Columns can be excluded by name with `select` (@aljazerzen,
#1329)
Expand Down
2 changes: 1 addition & 1 deletion book/highlight-prql.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ formatting = function (hljs) {
"union",
"window",
];
const BUILTIN_FUNCTIONS = ["switch", "in", "as"];
const BUILTIN_FUNCTIONS = ["case", "in", "as"];
const KEYWORDS = ["func", "let", "prql"];
return {
name: "PRQL",
Expand Down
2 changes: 1 addition & 1 deletion book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
- [Strings](./language-features/strings.md)
- [S-strings](./language-features/s-strings.md)
- [F-strings](./language-features/f-strings.md)
- [Switch](./language-features/switch.md)
- [Case](./language-features/case.md)
- [Target & Version](./language-features/target.md)

- [Bindings](./bindings/README.md)
Expand Down
26 changes: 13 additions & 13 deletions book/src/language-features/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
The pages of this section describe how PRQL handles various aspects of the
language.

| Feature | Purpose |
| ---------------- | ------------------------------------------------------------------------- |
| Coalesce | [Handle potentially NULL values](./coalesce.md) |
| Dates & times | [Handle dates and times](./dates-and-times.md) |
| Distinct | [Equivalent of SQL `DISTINCT`](./distinct.md) |
| Null handling | [Handle `NULL` values](./null.md) |
| Ranges | [Syntax for all forms of ranges](./ranges.md) |
| Regex | [Handle regular expressions](./regex.md) |
| Strings | [Rules for creating strings](./strings.md) |
| S-strings | [Insert SQL directly into a query with an S-string](./s-strings.md) |
| F-strings | [Combine several column's data with F-strings](./f-strings.md) |
| Switch | [Create a new column based on the contents of other columns](./switch.md) |
| Target & Version | [Specify a target SQL engine and PRQL version](./target.md) |
| Feature | Purpose |
| ---------------- | ----------------------------------------------------------------------- |
| Coalesce | [Handle potentially NULL values](./coalesce.md) |
| Dates & times | [Handle dates and times](./dates-and-times.md) |
| Distinct | [Equivalent of SQL `DISTINCT`](./distinct.md) |
| Null handling | [Handle `NULL` values](./null.md) |
| Ranges | [Syntax for all forms of ranges](./ranges.md) |
| Regex | [Handle regular expressions](./regex.md) |
| Strings | [Rules for creating strings](./strings.md) |
| S-strings | [Insert SQL directly into a query with an S-string](./s-strings.md) |
| F-strings | [Combine several column's data with F-strings](./f-strings.md) |
| Case | [Create a new column based on the contents of other columns](./case.md) |
| Target & Version | [Specify a target SQL engine and PRQL version](./target.md) |
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Switch
# Case

```admonish note
`switch` is currently experimental and may change behavior in the near future
`case` is currently experimental and may change behavior in the near future
```

PRQL uses `switch` for both SQL's `CASE` and `IF` statements. Here's an example:
PRQL uses `case` for both SQL's `CASE` and `IF` statements. Here's an example:

```prql_no_fmt
from employees
derive distance = switch [
derive distance = case [
city == "Calgary" -> 0,
city == "Edmonton" -> 300,
]
Expand All @@ -19,7 +19,7 @@ If no condition is met, the value takes a `null` value. To set a default, use a

```prql_no_fmt
from employees
derive distance = switch [
derive distance = case [
city == "Calgary" -> 0,
city == "Edmonton" -> 300,
true -> "Unknown",
Expand Down
4 changes: 2 additions & 2 deletions book/src/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,9 @@ At the moment, PRQL uses only four keywords:
- `prql`
- `let`
- `func`
- `switch`
- `case`

To use these names as columns or relations, use backticks: `` `switch` ``.
To use these names as columns or relations, use backticks: `` `case` ``.

It may seem that transforms are also keywords, but they are normal function
within std namespace:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Can't yet format & compile

from employees
derive distance = switch [
derive distance = case [
city == "Calgary" -> 0,
city == "Edmonton" -> 300,
]
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Can't yet format & compile

from employees
derive distance = switch [
derive distance = case [
city == "Calgary" -> 0,
city == "Edmonton" -> 300,
true -> "Unknown",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
source: book/tests/snapshot.rs
expression: "# Can't yet format & compile\n\nfrom employees\nderive distance = case [\n city == \"Calgary\" -> 0,\n city == \"Edmonton\" -> 300,\n]\n"
input_file: book/tests/prql/language-features/case-0.prql
---
SELECT
*,
CASE
WHEN city = 'Calgary' THEN 0
WHEN city = 'Edmonton' THEN 300
ELSE NULL
END AS distance
FROM
employees

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
source: book/tests/snapshot.rs
expression: "# Can't yet format & compile\n\nfrom employees\nderive distance = case [\n city == \"Calgary\" -> 0,\n city == \"Edmonton\" -> 300,\n true -> \"Unknown\",\n]\n"
input_file: book/tests/prql/language-features/case-1.prql
---
SELECT
*,
CASE
WHEN city = 'Calgary' THEN 0
WHEN city = 'Edmonton' THEN 300
ELSE 'Unknown'
END AS distance
FROM
employees

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion book/theme/highlight.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion playground/src/workbench/prql-syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const TRANSFORMS = [
"union",
"window",
];
const BUILTIN_FUNCTIONS = ["switch"]; // "in", "as"
const BUILTIN_FUNCTIONS = ["case"]; // "in", "as"
const KEYWORDS = ["func", "let", "prql"];
const LITERALS = ["null", "true", "false"];
const OPERATORS = ["and", "or"]; // "not"
Expand Down
6 changes: 3 additions & 3 deletions prql-compiler/src/ast/pl/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub enum ExprKind {
TransformCall(TransformCall),
SString(Vec<InterpolateItem>),
FString(Vec<InterpolateItem>),
Switch(Vec<SwitchCase>),
Case(Vec<SwitchCase>),
BuiltInFunction {
name: String,
args: Vec<Expr>,
Expand Down Expand Up @@ -573,8 +573,8 @@ impl Display for Expr {
ExprKind::Literal(literal) => {
write!(f, "{}", literal)?;
}
ExprKind::Switch(cases) => {
f.write_str("switch [\n")?;
ExprKind::Case(cases) => {
f.write_str("case [\n")?;
for case in cases {
writeln!(f, " {} => {}", case.condition, case.value)?;
}
Expand Down
2 changes: 1 addition & 1 deletion prql-compiler/src/ast/pl/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ pub fn fold_expr_kind<T: ?Sized + AstFold>(fold: &mut T, expr_kind: ExprKind) ->
.map(|x| fold.fold_interpolate_item(x))
.try_collect()?,
),
Switch(cases) => Switch(fold_cases(fold, cases)?),
Case(cases) => Case(fold_cases(fold, cases)?),

FuncCall(func_call) => FuncCall(fold.fold_func_call(func_call)?),
Closure(closure) => Closure(Box::new(fold.fold_closure(*closure)?)),
Expand Down
2 changes: 1 addition & 1 deletion prql-compiler/src/ast/rq/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub enum ExprKind {

// TODO: convert this into built-in function
FString(Vec<InterpolateItem<Expr>>),
Switch(Vec<SwitchCase<Expr>>),
Case(Vec<SwitchCase<Expr>>),

BuiltInFunction {
name: String,
Expand Down
2 changes: 1 addition & 1 deletion prql-compiler/src/ast/rq/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ pub fn fold_expr_kind<F: ?Sized + RqFold>(fold: &mut F, kind: ExprKind) -> Resul

ExprKind::SString(items) => ExprKind::SString(fold_interpolate_items(fold, items)?),
ExprKind::FString(items) => ExprKind::FString(fold_interpolate_items(fold, items)?),
ExprKind::Switch(cases) => ExprKind::Switch(
ExprKind::Case(cases) => ExprKind::Case(
cases
.into_iter()
.map(|c| fold_switch_case(fold, c))
Expand Down
6 changes: 3 additions & 3 deletions prql-compiler/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub fn expr() -> impl Parser<Token, Expr, Error = Simple<Token>> + Clone {
}
});

let switch = keyword("switch")
let case = keyword("case")
.ignore_then(
func_call(expr.clone())
.then_ignore(just(Token::Arrow))
Expand All @@ -84,7 +84,7 @@ pub fn expr() -> impl Parser<Token, Expr, Error = Simple<Token>> + Clone {
.then_ignore(new_line().repeated())
.delimited_by(ctrl('['), ctrl(']')),
)
.map(ExprKind::Switch);
.map(ExprKind::Case);

let param = select! { Token::Param(id) => ExprKind::Param(id) };

Expand All @@ -94,7 +94,7 @@ pub fn expr() -> impl Parser<Token, Expr, Error = Simple<Token>> + Clone {
pipeline,
interpolation,
ident_kind,
switch,
case,
param,
))
.map_with_span(into_expr)
Expand Down
2 changes: 1 addition & 1 deletion prql-compiler/src/parser/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub fn lexer() -> impl Parser<char, Vec<(Token, std::ops::Range<usize>)>, Error

let ident = ident_part().map(Token::Ident);

let keyword = choice((just("func"), just("let"), just("switch"), just("prql")))
let keyword = choice((just("func"), just("let"), just("case"), just("prql")))
.then_ignore(end_expr())
.map(|x| x.to_string())
.map(Token::Keyword);
Expand Down
6 changes: 3 additions & 3 deletions prql-compiler/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2188,13 +2188,13 @@ join s=salaries [==id]
}

#[test]
fn test_switch() {
assert_yaml_snapshot!(parse_expr(r#"switch [
fn test_case() {
assert_yaml_snapshot!(parse_expr(r#"case [
nickname != null -> nickname,
true -> null
]"#).unwrap(), @r###"
---
Switch:
Case:
- condition:
Binary:
left:
Expand Down
2 changes: 1 addition & 1 deletion prql-compiler/src/semantic/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ impl Lowerer {
pl::ExprKind::FString(items) => {
rq::ExprKind::FString(self.lower_interpolations(items)?)
}
pl::ExprKind::Switch(cases) => rq::ExprKind::Switch(
pl::ExprKind::Case(cases) => rq::ExprKind::Case(
cases
.into_iter()
.map(|case| -> Result<_> {
Expand Down
4 changes: 2 additions & 2 deletions prql-compiler/src/semantic/static_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ fn eval(kind: ExprKind) -> ExprKind {
}
}

ExprKind::Switch(items) => {
ExprKind::Case(items) => {
let mut res = Vec::with_capacity(items.len());
for item in items {
if let ExprKind::Literal(Literal::Boolean(condition)) = item.condition.kind {
Expand Down Expand Up @@ -160,7 +160,7 @@ fn eval(kind: ExprKind) -> ExprKind {
}
}

ExprKind::Switch(res)
ExprKind::Case(res)
}

k => k,
Expand Down
2 changes: 1 addition & 1 deletion prql-compiler/src/sql/anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ pub fn infer_complexity(compute: &Compute) -> Complexity {

pub fn infer_complexity_expr(expr: &Expr) -> Complexity {
match &expr.kind {
rq::ExprKind::Switch(_) => Complexity::NonGroup,
rq::ExprKind::Case(_) => Complexity::NonGroup,
rq::ExprKind::Binary { left, right, .. } => {
Complexity::max(infer_complexity_expr(left), infer_complexity_expr(right))
}
Expand Down
2 changes: 1 addition & 1 deletion prql-compiler/src/sql/gen_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub(super) fn translate_expr_kind(item: ExprKind, ctx: &mut Context) -> Result<s
ExprKind::Param(id) => sql_ast::Expr::Identifier(sql_ast::Ident::new(format!("${id}"))),
ExprKind::FString(f_string_items) => translate_fstring(f_string_items, ctx)?,
ExprKind::Literal(l) => translate_literal(l, ctx)?,
ExprKind::Switch(mut cases) => {
ExprKind::Case(mut cases) => {
let default = cases
.last()
.filter(|last| {
Expand Down
Loading

0 comments on commit f04baf2

Please sign in to comment.