This commit is contained in:
jackfoxy 2023-02-14 14:22:54 -08:00
parent 7fc5fbdeed
commit 5d27d92308
4 changed files with 100 additions and 60 deletions

View File

@ -47,15 +47,24 @@
}
]
```
`JOIN` Inner join returns all matching pairs of rows.
`LEFT JOIN` Left inner join returns all rows from the left table not meeting the join condition long with all matching pairs of rows. Missing columns from the right table are `~` (null) filled.
`RIGHT JOIN` Right inner join returns all rows from the right table not meeting the join condition long with all matching pairs of rows. Missing columns from the left table are `~` (null) filled.
`OUTER JOIN` Full outer join returns all rows from both tablea not meeting the join condition long with all matching pairs of rows. Missing columns are `~` (null) filled.
`CROSS JOIN` Cross join is a cartesian join of two tables.
Cross database joins are allowed, but not cross ship joins.
`HAVING <predicate>` any column reference in the predicate must be one of the grouping columns or be contained in an aggregate function.
`SELECT ... INTO` targets an existing table not otherwise in the query, and completes the command.
Do not use `ORDER BY` in Common Table Experessions (CTE, WITH clause) or in any query manipulated by set operators prior to the last of the queries, except when `TOP` or `BOTTOM` is specified.
Set operators apply the previous result set to the next query result unless otherwise qualified by brackets `{ ... }`.
Set operators `UNION`, etc. apply the previous result set to the next query result unless otherwise qualified by brackets `{ ... }`.
`AS OF` defaults to `NOW`
`AS OF <inline-scalar>` Scalar function written inline that returns `<timestamp>`.
```
<predicate> ::=

View File

@ -959,7 +959,6 @@
::
:: select
::
++ select-stop ;~(plug (jester 'order') whitespace (jester 'by'))
++ parse-alias-all (stag %all-columns ;~(sfix parse-alias ;~(plug dot tar)))
++ parse-object-all (stag %all-columns ;~(sfix parse-qualified-object ;~(plug dot tar)))
++ parse-selection ~+ ;~ pose
@ -971,10 +970,13 @@
;~(pose parse-qualified-column parse-value-literal)
(cold %all tar)
==
++ select-column
(ifix [whitespace whitespace] parse-selection)
++ select-column ;~ pose
(ifix [whitespace whitespace] parse-selection)
;~(plug whitespace parse-selection)
parse-selection
==
++ select-columns ;~ pose
(full (more com select-column))
(more com select-column)
select-column
==
++ select-top-bottom-distinct ;~ plug
@ -1028,7 +1030,7 @@
select-bottom-distinct
select-bottom
select-distinct
;~(less select-stop select-columns)
select-columns
==
==
::
@ -1037,7 +1039,6 @@
++ parse-grouping-column (ifix [whitespace whitespace] ;~(pose parse-qualified-column dem))
++ parse-group-by ;~ plug
(cold %group-by ;~(plug whitespace (jester 'group') whitespace (jester 'by')))
:: (cold %group-by ;~(plug (jester 'group') whitespace (jester 'by')))
(more com parse-grouping-column)
==
++ cook-ordering-column
@ -1158,7 +1159,6 @@
=/ order-by=(list ordering-column:ast) ~
|-
::~& "a: {<a>}"
?~ a ~|("cannot parse simple-query {<a>}" !!)
?: =(i.a %query) $(a t.a)
?: =(i.a %end-command)
@ -1243,55 +1243,86 @@
;~(pfix whitespace (more whitespace (ifix [pal par] (more com parse-insert-value))))
end-or-next-command
==
++ parse-query1 ;~ plug
++ parse-query01 ;~ plug
parse-object-and-joins
:: (stag %scalars (star parse-scalar))
;~(pfix whitespace ;~(plug (cold %where (jester 'where')) parse-predicate))
parse-group-by
parse-select
:: parse-order-by
parse-order-by
end-or-next-command
==
++ parse-query2 ;~ plug
++ parse-query02 ;~ plug
parse-object-and-joins
:: (stag %scalars (star parse-scalar))
;~(pfix whitespace ;~(plug (cold %where (jester 'where')) parse-predicate))
parse-group-by
parse-select
end-or-next-command
==
++ parse-query03 ;~ plug
parse-object-and-joins
:: (stag %scalars (star parse-scalar))
;~(pfix whitespace ;~(plug (cold %where (jester 'where')) parse-predicate))
parse-select
:: parse-order-by
parse-order-by
end-or-next-command
==
++ parse-query3 ;~ plug
++ parse-query04 ;~ plug
parse-object-and-joins
:: (stag %scalars (star parse-scalar))
;~(pfix whitespace ;~(plug (cold %where (jester 'where')) parse-predicate))
parse-select
end-or-next-command
==
++ parse-query05 ;~ plug
parse-object-and-joins
:: (stag %scalars (star parse-scalar))
parse-select
:: parse-order-by
parse-order-by
end-or-next-command
==
++ parse-query4 ;~ plug
++ parse-query06 ;~ plug
parse-object-and-joins
:: (stag %scalars (star parse-scalar))
parse-select
end-or-next-command
==
++ parse-query07 ;~ plug
parse-object-and-joins
:: (stag %scalars (star parse-scalar))
parse-group-by
parse-select
:: parse-order-by
parse-order-by
end-or-next-command
==
++ parse-query5 ;~ plug
++ parse-query08 ;~ plug
parse-object-and-joins
:: (stag %scalars (star parse-scalar))
parse-group-by
parse-select
end-or-next-command
==
++ parse-query09 ;~ plug
parse-object-and-joins
parse-select
end-or-next-command
==
++ parse-query9 ;~ plug
++ parse-query10 ;~ plug
parse-select
end-or-next-command
==
++ parse-query ;~ pose
parse-query1
parse-query2
parse-query3
parse-query4
parse-query5
parse-query9
parse-query01
parse-query02
parse-query03
parse-query04
parse-query05
parse-query06
parse-query07
parse-query08
parse-query09
parse-query10
==
++ parse-revoke ;~ plug
:: permission

View File

@ -27,34 +27,34 @@
:: group and order by
::
++ group-by [%group-by ~[[%qualified-column qualifier=[%qualified-object ship=~ database='db' namespace='ns' name='table'] column='col' alias=~] [%qualified-column qualifier=[%qualified-object ship=~ database='UNKNOWN' namespace='COLUMN' name='T1'] column='foo' alias=~] 3 4]]
++ order-by [%order-by ~[[%ordering-column [%qualified-column qualifier=[%qualified-object ship=~ database='db' namespace='ns' name='table'] column='col' alias=~] is-ascending=%.y] [%ordering-column [%qualified-column qualifier=[%qualified-object ship=~ database='UNKNOWN' namespace='COLUMN' name='T1'] column='foo' alias=~] is-ascending=%.n] [%ordering-column 3 is-ascending=%.y] [%ordering-column 4 is-ascending=%.n]]]
++ order-by ~[[%ordering-column [%qualified-column qualifier=[%qualified-object ship=~ database='db' namespace='ns' name='table'] column='col' alias=~] is-ascending=%.y] [%ordering-column [%qualified-column qualifier=[%qualified-object ship=~ database='UNKNOWN' namespace='COLUMN' name='T1'] column='foo' alias=~] is-ascending=%.n] [%ordering-column 3 is-ascending=%.y] [%ordering-column 4 is-ascending=%.n]]
::
:: group by
++ test-group-by-01
=/ select "from foo group by db.ns.table.col , T1.foo , 3 , 4 select *"
%+ expect-eq
!> ~[[%simple-query from-foo [%scalars ~] ~ group-by [%having ~] [%select top=~ bottom=~ distinct=%.n columns=~[all-columns]] ~]]
!> (parse:parse(current-database 'db1') select)
::
:: group by, no whitespace, with predicate
++ test-group-by-02
=/ pred=(tree predicate-component:ast) [%eq t1-foo t2-bar]
=/ select "from foo where T1.foo = T2.bar group by db.ns.table.col,T1.foo,3,4 select *"
%+ expect-eq
!> ~[[%simple-query from-foo [%scalars ~] `pred group-by [%having ~] [%select top=~ bottom=~ distinct=%.n columns=~[all-columns]] ~]]
!> (parse:parse(current-database 'db1') select)
::
:: order by
::++ test-order-by-01
:: =/ select "from foo select * order by db.ns.table.col asc , T1.foo desc , 3 , 4 desc "
::++ test-group-by-01
:: =/ select "from foo group by db.ns.table.col , T1.foo , 3 , 4 select *"
:: %+ expect-eq
:: !> order-by
:: !> ~[[%simple-query from-foo [%scalars ~] ~ group-by [%having ~] [%select top=~ bottom=~ distinct=%.n columns=~[all-columns]] ~]]
:: !> (parse:parse(current-database 'db1') select)
::
:: order by, no whitespace
::++ test-order-by-02
:: =/ select "order by db.ns.table.col aSc,T1.foo desc,3,4 Desc"
:: group by, no whitespace, with predicate
::++ test-group-by-02
:: =/ pred=(tree predicate-component:ast) [%eq t1-foo t2-bar]
:: =/ select "from foo where T1.foo = T2.bar group by db.ns.table.col,T1.foo,3,4 select *"
:: %+ expect-eq
:: !> order-by
:: !> (wonk (parse-order-by:parse [[1 1] select]))
:: !> ~[[%simple-query from-foo [%scalars ~] `pred group-by [%having ~] [%select top=~ bottom=~ distinct=%.n columns=~[all-columns]] ~]]
:: !> (parse:parse(current-database 'db1') select)
::
:: order by
++ test-order-by-01
=/ select "from foo select * order by db.ns.table.col asc , T1.foo desc , 3 , 4 desc "
%+ expect-eq
!> ~[[%simple-query from-foo [%scalars ~] ~ [%group-by ~] [%having ~] [%select top=~ bottom=~ distinct=%.n columns=~[all-columns]] order-by]]
!> (parse:parse(current-database 'db1') select)
::
:: order by, no whitespace
++ test-order-by-02
=/ select "from foo select * order by db.ns.table.col aSc,T1.foo desc,3,4 Desc"
%+ expect-eq
!> ~[[%simple-query from-foo [%scalars ~] ~ [%group-by ~] [%having ~] [%select top=~ bottom=~ distinct=%.n columns=~[all-columns]] order-by]]
!> (parse:parse(current-database 'db1') select)
--

View File

@ -1894,7 +1894,7 @@
:: group and order by
::
++ group-by [%group-by ~[[%qualified-column qualifier=[%qualified-object ship=~ database='db' namespace='ns' name='table'] column='col' alias=~] [%qualified-column qualifier=[%qualified-object ship=~ database='UNKNOWN' namespace='COLUMN' name='T1'] column='foo' alias=~] 3 4]]
++ order-by [%order-by ~[[%ordering-column [%qualified-column qualifier=[%qualified-object ship=~ database='db' namespace='ns' name='table'] column='col' alias=~] is-ascending=%.y] [%ordering-column [%qualified-column qualifier=[%qualified-object ship=~ database='UNKNOWN' namespace='COLUMN' name='T1'] column='foo' alias=~] is-ascending=%.n] [%ordering-column 3 is-ascending=%.y] [%ordering-column 4 is-ascending=%.n]]]
++ order-by ~[[%ordering-column [%qualified-column qualifier=[%qualified-object ship=~ database='db' namespace='ns' name='table'] column='col' alias=~] is-ascending=%.y] [%ordering-column [%qualified-column qualifier=[%qualified-object ship=~ database='UNKNOWN' namespace='COLUMN' name='T1'] column='foo' alias=~] is-ascending=%.n] [%ordering-column 3 is-ascending=%.y] [%ordering-column 4 is-ascending=%.n]]
::
:: group by
++ test-group-by-01
@ -1912,16 +1912,16 @@
!> (parse:parse(current-database 'db1') select)
::
:: order by
::++ test-order-by-01
:: =/ select "from foo select * order by db.ns.table.col asc , T1.foo desc , 3 , 4 desc "
:: %+ expect-eq
:: !> order-by
:: !> (parse:parse(current-database 'db1') select)
++ test-order-by-01
=/ select "from foo select * order by db.ns.table.col asc , T1.foo desc , 3 , 4 desc "
%+ expect-eq
!> ~[[%simple-query from-foo [%scalars ~] ~ [%group-by ~] [%having ~] [%select top=~ bottom=~ distinct=%.n columns=~[all-columns]] order-by]]
!> (parse:parse(current-database 'db1') select)
::
:: order by, no whitespace
::++ test-order-by-02
:: =/ select "order by db.ns.table.col aSc,T1.foo desc,3,4 Desc"
:: %+ expect-eq
:: !> order-by
:: !> (wonk (parse-order-by:parse [[1 1] select]))
++ test-order-by-02
=/ select "from foo select * order by db.ns.table.col aSc,T1.foo desc,3,4 Desc"
%+ expect-eq
!> ~[[%simple-query from-foo [%scalars ~] ~ [%group-by ~] [%having ~] [%select top=~ bottom=~ distinct=%.n columns=~[all-columns]] order-by]]
!> (parse:parse(current-database 'db1') select)
--