Skip to content

Commit

Permalink
Added aliases for cmd inspect flags; added aliases for order_by and g…
Browse files Browse the repository at this point in the history
…roup_by (#340)
  • Loading branch information
neilotoole authored Nov 22, 2023
1 parent f0d83cd commit 880c817
Show file tree
Hide file tree
Showing 12 changed files with 316 additions and 286 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,21 @@ Breaking changes are annotated with ☢️, and alpha/beta features with 🐥.
526 221.55
148 216.54
```
- [`sq inspect`](https://sq.io/docs/cmd/inspect): added flag shorthand `-C` for `--catalogs` and `-S` for `--schemata`.

- [SLQ](https://sq.io/docs/concepts#slq) [`group_by`](https://sq.io/docs/query#group_by) function
now has a synonym `gb`, and [`order_by`](https://sq.io/docs/query#order_by) now has synonym `ob`.
These synonyms are experimental. The motivation is to reduce typing, especially the underscore (`_`)
in both function names, but it's not clear that the loss of clarity is worth it. Maybe synonyms
`group` and `order` might be better? Feedback welcome.

```shell
# Previously
$ sq '.payment | .customer_id, sum(.amount) | group_by(.customer_id) | order_by(.customer_id)'

# Now
$ sq '.payment | .customer_id, sum(.amount) | gb(.customer_id) | ob(.customer_id)'
```

## [v0.45.0] - 2023-11-21

Expand Down
4 changes: 2 additions & 2 deletions cli/cmd_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ formats both show extensive detail.`,

cmd.Flags().BoolP(flag.InspectOverview, flag.InspectOverviewShort, false, flag.InspectOverviewUsage)
cmd.Flags().BoolP(flag.InspectDBProps, flag.InspectDBPropsShort, false, flag.InspectDBPropsUsage)
cmd.Flags().Bool(flag.InspectCatalogs, false, flag.InspectCatalogsUsage)
cmd.Flags().Bool(flag.InspectSchemata, false, flag.InspectSchemataUsage)
cmd.Flags().BoolP(flag.InspectCatalogs, flag.InspectCatalogsShort, false, flag.InspectCatalogsUsage)
cmd.Flags().BoolP(flag.InspectSchemata, flag.InspectSchemataShort, false, flag.InspectSchemataUsage)

cmd.MarkFlagsMutuallyExclusive(flag.InspectOverview, flag.InspectDBProps, flag.InspectCatalogs, flag.InspectSchemata)

Expand Down
2 changes: 2 additions & 0 deletions cli/flag/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,11 @@ const (
InspectDBPropsUsage = "Show DB properties only"

InspectCatalogs = "catalogs"
InspectCatalogsShort = "C"
InspectCatalogsUsage = "List catalogs only"

InspectSchemata = "schemata"
InspectSchemataShort = "S"
InspectSchemataUsage = "List schemas (in current catalog) only"

LogEnabled = "log"
Expand Down
8 changes: 5 additions & 3 deletions grammar/SLQ.g4
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ where: WHERE LPAR (expr)? RPAR;

/*
group_by
-------
--------
The 'group_by' construct implments the SQL "GROUP BY" clause.
Expand All @@ -151,9 +151,10 @@ The 'group_by' construct implments the SQL "GROUP BY" clause.
Syonyms:
- 'group_by' for jq interoperability.
https://stedolan.github.io/jq/manual/v1.6/#group_by(path_expression)
- 'gb' for brevity.
*/

GROUP_BY: 'group_by';
GROUP_BY: 'group_by' | 'gb';
groupByTerm: selector | func;
groupBy: GROUP_BY '(' groupByTerm (',' groupByTerm)* ')';

Expand Down Expand Up @@ -188,13 +189,14 @@ Synonyms:
- 'sort_by' for jq interoperability.
https://stedolan.github.io/jq/manual/v1.6/#sort,sort_by(path_expression)
- 'ob' for brevity.
We do not implement a 'sort' synonym for the jq 'sort' function, because SQL
results are inherently sorted. Although perhaps it should be implemented
as a no-op.
*/

ORDER_BY: 'order_by' | 'sort_by';
ORDER_BY: 'order_by' | 'sort_by' | 'ob';
orderByTerm: selector ('+' | '-')?;
orderBy: ORDER_BY '(' orderByTerm (',' orderByTerm)* ')';

Expand Down
2 changes: 1 addition & 1 deletion libsq/ast/internal/slq/SLQ.interp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ null
null
null
null
'group_by'
null
'having'
null
null
Expand Down
1 change: 0 additions & 1 deletion libsq/ast/internal/slq/SLQ.tokens
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ LINECOMMENT=54
'&&'=22
'~'=23
'!'=24
'group_by'=28
'having'=29
'null'=33
'('=36
Expand Down
4 changes: 2 additions & 2 deletions libsq/ast/internal/slq/SLQLexer.interp

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion libsq/ast/internal/slq/SLQLexer.tokens
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ LINECOMMENT=54
'&&'=22
'~'=23
'!'=24
'group_by'=28
'having'=29
'null'=33
'('=36
Expand Down
547 changes: 274 additions & 273 deletions libsq/ast/internal/slq/slq_lexer.go

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions libsq/ast/internal/slq/slq_parser.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions libsq/query_groupby_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ func TestQuery_groupby(t *testing.T) {
override: driverMap{mysql.Type: "SELECT `customer_id`, sum(`amount`) AS `sum(.amount)` FROM `payment` GROUP BY `customer_id`"},
wantRecCount: 599,
},
{
name: "alias-gb",
in: `@sakila | .payment | .customer_id, sum(.amount) | gb(.customer_id)`,
wantSQL: `SELECT "customer_id", sum("amount") AS "sum(.amount)" FROM "payment" GROUP BY "customer_id"`,
override: driverMap{mysql.Type: "SELECT `customer_id`, sum(`amount`) AS `sum(.amount)` FROM `payment` GROUP BY `customer_id`"},
wantRecCount: 599,
},
{
name: "group_by/multiple_terms",
in: `@sakila | .payment | .customer_id, .staff_id, sum(.amount) | group_by(.customer_id, .staff_id)`,
Expand Down
7 changes: 7 additions & 0 deletions libsq/query_orderby_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ func TestQuery_orderby(t *testing.T) {
override: driverMap{mysql.Type: "SELECT * FROM `actor` ORDER BY `first_name`"},
wantRecCount: sakila.TblActorCount,
},
{
name: "alias-ob",
in: `@sakila | .actor | ob(.first_name)`,
wantSQL: `SELECT * FROM "actor" ORDER BY "first_name"`,
override: driverMap{mysql.Type: "SELECT * FROM `actor` ORDER BY `first_name`"},
wantRecCount: sakila.TblActorCount,
},
{
name: "order_by/single-element-table-selector",
in: `@sakila | .actor | order_by(.actor.first_name)`,
Expand Down

0 comments on commit 880c817

Please sign in to comment.