fix predicate

This commit is contained in:
jackfoxy 2022-10-30 19:52:27 -07:00
parent e19c15667f
commit af65a41dab
6 changed files with 111 additions and 59 deletions

View File

@ -1,3 +1,5 @@
# ALTER INDEX
```
ALTER INDEX [ <db-qualifer> ]{ <index-name> }
ON { <table-name> | <view-name> }
@ -10,22 +12,25 @@ Discussion:
### _______________________________
# ALTER NAMESPACE
```
ALTER NAMESPACE [ <database-name>. ]<namespace-name>
TRANSFER { TABLE | VIEW } [ <db-qualifer> ]{ <table-name> | <view-name> }
```
Discussion:
The namespace *sys* cannot be altered, nor can objects be transferred out of it.
The namespace *sys* cannot be altered, nor can objects be transferred in or out of it.
### _______________________________
# ALTER PROCEDURE
```
ALTER { PROC | PROCEDURE }
[<db-qualifer>]<procedure-name>
[ { #<parameter-name> <data-type> } ] [ ,...n ]
AS { sql_statement [ ;...n ] }
AS { <urql command>; | *hoon } [ ;...n ]
```
Discussion:
@ -33,6 +38,7 @@ TBD
### _______________________________
# ALTER TABLE
```
ALTER TABLE [ <db-qualifer> ]{ <table-name> }
@ -40,7 +46,7 @@ ALTER TABLE [ <db-qualifer> ]{ <table-name> }
| ADD COLUMN ( { <column-name> <aura> } [ ,... n ] )
| DROP COLUMN ( { <column-name> } [ ,... n ] )
| ADD FOREIGN KEY <foreign-key-name> (<column-name> [ ,... n ])
REFERENCES [<namespace-name>.]<table-name> ( <column-name> [ ,... n ])
REFERENCES [<namespace-name>.]<table-name> (<column-name> [ ,... n ])
[ ON DELETE { NO ACTION | CASCADE } ]
[ ON UPDATE { NO ACTION | CASCADE } ]
[ ,... n ]
@ -55,19 +61,20 @@ DROP FOREIGN KEY fk-1, fk-2
### _______________________________
# ALTER TRIGGER
```
ALTER TRIGGER { [ <db-qualifer> ]{ <trigger-name> } | ALL ]
ON { SERVER | <database.name> | <table-name> | <view-name> }
[ ENABLE | DISABLE ]
```
TBD
Discussion:
Not for initial release.
TBD
### _______________________________
# ALTER VIEW
```
ALTER VIEW [ <db-qualifer> ]{ <view-name> }

View File

@ -1,4 +1,8 @@
`CREATE DATABASE <database-name>`
# CREATE DATABASE
```
CREATE DATABASE <database-name>
```
Example:
`CREATE DATABASE my-database`
@ -8,6 +12,7 @@ Discussion:
### _______________________________
# CREATE INDEX
```
CREATE [ UNIQUE ] [ NONCLUSTERED | CLUSTERED ] INDEX <index-name>
@ -30,6 +35,7 @@ The `UNIQUE` option is not available for views.
### _______________________________
# CREATE NAMESPACE
`CREATE NAMESPACE [<database-name>.]<namespace-name>`
@ -38,12 +44,13 @@ Example:
### _______________________________
# CREATE PROCEDURE
```
CREATE { PROC | PROCEDURE }
[<db-qualifer>]<procedure-name>
[ { #<parameter-name> <data-type> } ] [ ,...n ]
AS { sql_statement [ ;...n ] }
AS { <urql command>; | *hoon } [ ;...n ]
```
Discussion:
@ -52,6 +59,7 @@ Cannot be used to create database.
### _______________________________
# CREATE TABLE
```
CREATE TABLE
@ -82,6 +90,7 @@ Discussion:
### _______________________________
# CREATE TRIGGER
```
CREATE TRIGGER [ <db-qualifer> ]<trigger-name>
@ -95,6 +104,7 @@ Not for initial release.
### _______________________________
# CREATE TYPE
`CREATE TYPE <type-name>`
TBD
@ -105,6 +115,7 @@ Possibly part of initial or early release.
### _______________________________
# CREATE VIEW
`CREATE VIEW [ <db-qualifer> ]<view-name> AS <query>`

View File

@ -1,5 +1,5 @@
```
[ WITH <common-table-expression> ]
[ WITH (<query>) AS <alias> [ ,...n ] ]
DELETE [ FROM ] [ <ship-qualifer> ]<table-name>
[ WHERE <predicate>
```
@ -31,13 +31,13 @@ The `VALUES` or `<query>` must provide data for all columns in the expected orde
Tables in the namespace *sys* cannot be inserted into.
`DEFAULT` is the bunt of the column type.
Cord values are represented in single quotes 'this is a cord'.
Escape single quotes with double backslash thusly 'this is a cor\\'d'.
Escape single quotes with double backslash thusly `'this is a cor\\'d'`.
### _______________________________
```
[ WITH <common-table-expression> ]
[ WITH (<query>) AS <alias> [ ,...n ] ]
MERGE [ INTO ] [ <ship-qualifer> ]<target-table-name> [ [ AS ] <alias> ]
USING [ <ship-qualifer> ]<table-source-name> [ [ AS ] <alias> ]
ON <predicate>
@ -77,7 +77,7 @@ Tables in the namespace *sys* cannot be truncated.
```
[WITH <common-table-expression> ]
[WITH (<query>) AS <alias> [ ,...n ] ]
UPDATE [ FROM ] [ <ship-qualifer> ]<table-name>
SET { <column-name> = { <scalar-expression> | DEFAULT | ~ }
[ WHERE <predicate> ]

View File

@ -1,40 +1,54 @@
# Query
```
<query> ::=
[WITH (<query>) AS <alias> [ ,...n ] ]
[ { ]
FROM [ <ship-qualifer> ]<table-view> [ [AS] <alias> ]
[ { { JOIN | LEFT JOIN | RIGHT JOIN | OUTER JOIN [ALL] }
[ <ship-qualifer> ]<table-view> [ [AS] <alias> ]
ON <predicate>
} [ ...n ]
| CROSS JOIN
[ <ship-qualifer> ]<table-view> [ [AS] <alias> ]
]
# Query
[ { SCALAR <scalar-name> [ AS ] <scalar-function> } [ ...n ] ]
[ WHERE <predicate> ]
SELECT [ TOP <n> ] [ BOTTOM <n> ] [ DISTINCT ]
{ *
| {
{ [<ship-qualifer>]<table-view> | <alias> }.*
| { <qualified-column> | <constant> } [ [ AS ] <column-alias> ]
| <scalar-name>
| <aggregate-name>( { <column> | <scalar-name> } )
} [ ,...n ]
{ * | { { [<ship-qualifer>]<table-view> | <alias> }.*
| { <qualified-column> | <constant> } [ [ AS ] <column-alias> ]
| <scalar-name>
| <aggregate-name>( { <column> | <scalar-name> } )
} [ ,...n ]
}
[ GROUP BY { <qualified-column> | <column-alias> | <column-ordinal> } [ ,...n ]
[ HAVING <predicate> ]
]
[ ORDER BY { { <qualified-column> | <column-alias> | <column-ordinal> } [ ASC | DESC ] } [ ,...n ] ]
[ HAVING <predicate> ] ]
[ ORDER BY { { <qualified-column> | <column-alias> | <column-ordinal> }
[ ASC | DESC ] } [ ,...n ] ]
[ INTO <table> ]
[ { UNION
| COMBINE
| EXCEPT
| INTERSECT
| DIVIDED BY [ WITH REMAINDER ]
}
<query> ] [ } ] [ ...n ]
```
Cross database joins are allowed, but not cross ship joins.
`SELECT ... INTO` targets an existing table not otherwise in the query.
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 unless otherwise qualified by brackets `{ ... }`.
```
<predicate> ::=
{ [ NOT ] <predicate> | ( <simple-predicate> ) }
[ { { AND | OR } [ NOT ] { <predicate> | ( <simple-predicate> ) }
{ [ NOT ] <predicate> | [ ( ] <simple-predicate> [ ) ] }
[ { { AND | OR } [ NOT ] { <predicate> | [ ( ] <simple-predicate> [ ) ] }
[ ...n ]
]
```
@ -45,33 +59,46 @@ SELECT [ TOP <n> ] [ BOTTOM <n> ] [ DISTINCT ]
| expression [ NOT ] BETWEEN expression [ AND ] expression
| expression IS [ NOT ] DISTINCT FROM expression
| expression [ NOT ] IN
{ <cte-one-column-query> | ( <value> ,...n ) }
| expression <inequality-operator> { ALL | ANY} ( <cte-one-column-query> )
| [ NOT ] EXISTS { <column-value> | <cte-one-column-query> } }
{ <cte one column query> | ( <value> ,...n ) }
| expression <inequality operator> { ALL | ANY} ( <cte one column query> )
| [ NOT ] EXISTS { <column value> | <cte one column query> } }
```
`DISTINCT FROM` is like equals `=` except comparing two `NOT EXISTS` yields false.
```
<scalar-function> ::=
IF <predicate> THEN { <expression> | <scalar-function> } ELSE { <expression> | <scalar-function> } ENDIF
IF <predicate> THEN { <expression> | <scalar-function> }
ELSE { <expression> | <scalar-function> } ENDIF
| CASE <expression>
WHEN { <expression> | <predicate> } THEN { <expression> | <scalar-function> } [ ...n ]
WHEN { <expression> | <predicate> }
THEN { <expression> | <scalar-function> } [ ...n ]
[ ELSE { <expression> | <scalar-function> } ]
END
| COALESCE ( <expression> [ ,...n ] )
| BEGIN <arithmetic on expressions and scalar functions> END
| *hoon (TBD)
```
If a `CASE` expression uses `<predicate>`, the expected boolean (or loobean) logic applies.
If it uses `<expression>` `@`0 is treated as false and any other value as true (not loobean).
`COALESCE` returns the first `<expression>` in the list that exists where not existing occurs when selected `<expression>` value is not returned due to `LEFT` or `RIGHT JOIN` not matching.
```
<expression> ::=
{
constant
| <column>
{ <column>
| <scalar-function>
| <scalar-query>
| <aggregate-name>( { <column> | <scalar-name> } )
}
```
```
`<column> ::=
{ [ <qualified-column>
| <column-alias>
| <constant> }
```
```
<binary-operator> ::=
{ = | <> | != | > | >= | !> | < | <= | !< }
@ -81,27 +108,3 @@ SELECT [ TOP <n> ] [ BOTTOM <n> ] [ DISTINCT ]
<qualified-column> ::=
[ [ <ship-qualifer> ]<table-view> | <alias> } ].<column-name>
```
```
`<column> ::=
{ [ <qualified-column>
| <column-alias>
| <constant> }
```
Discussion:
Not shown in diagrams, parentheses distinguish order of operations for binary conjunctions `AND` and `OR`.
Set operators apply the previous result set to the next query unless otherwise qualified by parentheses.
`ORDER BY` is not recommended in Common Table Experessions (CTE, WITH clause) or in any query joined by set operators prior to the last of the queries, except when `TOP` or `BOTTOM` is specified.
`SELECT INTO` targets an existing table not otherwise in the query.
`COALESCE` returns the first `<expression>` in the list that does not evaluate to `~` (in the case of unit) or not in the selected `<expression>` due to `LEFT` or `RIGHT JOIN`.
If a `CASE WHEN` expression is a `<predicate>`, the expected boolean (or loobean) logic applies. If it is a <expression> atom value 0 is treated as false and any other value as true (not loobean).
Cross database joins are allowed, but not cross ship joins.
`DISTINCT FROM` is like equals, `=`, except comparing two nulls will yield false.

View File

@ -327,7 +327,10 @@
?: ?=([[@ @ [@ %~] @] @ @ [@ %~] @] a) :: <type> no action, <type> no action
~
!!
++ end-or-next-command ~+ (cold %end-command ;~(pose ;~(plug whitespace mic) whitespace mic))
++ end-or-next-command ;~ plug
(cold %end-command ;~(pose ;~(plug whitespace mic) whitespace mic))
(easy ~)
==
++ alias
%+ cook
|=(a=tape (rap 3 ^-((list ,@) a)))
@ -787,7 +790,9 @@
?: =(-.b.a %pal)
?: =(+>-.b.a %par)
:: stand-alone tree
?: =((lent b.a) 3) ^$(b.a +>+.b.a, resolved [+<.b.a resolved])
?: =((lent b.a) 3) ^$(b.a +>+.b.a, resolved [+<.b.a resolved])
?: ?&((gth (lent resolved) 1) =(-.resolved %pal)) $(b.a +>+.b.a, working-tree +<.b.a)
?: =((lent b.a) 4) $(b.a ~, working-tree +<.b.a)
$(b.a +>+>+.b.a, working-tree [+>+<.b.a +<.b.a +>+>-.b.a])
$(b.a +>.b.a, working-tree [+>-.b.a +<.b.a +>+<.b.a])
?: =(-.b.a %par)
@ -821,7 +826,7 @@
?: =(%pal +<.b.a) $(b.a +>+>.b.a, working-tree [%and working-tree +>-.b.a])
$(b.a +>.b.a, working-tree [%and working-tree +<.b.a])
:: can only be tree on first time
$(b.a +.b.a, working-tree -.b.a)
$(b.a +.b.a, working-tree -.b.a)
++ cook-predicate
::
:: 1. resolve operators into trees
@ -1146,13 +1151,38 @@
==
++ parse-query ;~ plug
parse-object-and-joins
(star parse-scalar)
(stag %scalars (star parse-scalar))
;~(pfix whitespace ;~(plug (cold %where (jester 'where')) parse-predicate))
parse-select
parse-group-by
parse-order-by
end-or-next-command
==
++ produce-joins
|= a=* ::(list *)
=/ joins=(list joined-object:ast) ~
^- (list joined-object:ast)
|-
?: =(a ~) (flop joins)
?: ?=(joined-object:ast -.a) $(joins [-.a joins], a +.a)
::(crash "cannot produce join from {<-.a>}")
!!
++ produce-from
|= a=* ::(list *)
^- from:ast
?: =(%query-object -<.a) ::?&(=(%query-object -<.a) (gth (lent a) 0))
?: =(+.a ~) (from:ast %from -.a ~)
(from:ast -.a (produce-joins +.a))
::(crash "cannot produce query-object from {<-.a>}")
!!
++ cook-query
|= parsed=(list *)
=| from=from:ast
|-
?: =(-.parsed %query) $(from (produce-from +<.parsed), parsed +>.parsed)
!!
++ parse-revoke ;~ plug
:: permission
;~(pfix whitespace ;~(pose (jester 'adminread') (jester 'readonly') (jester 'readwrite') (jester 'all')))

View File

@ -140,6 +140,7 @@
==
+$ from
$:
%from
object=query-object
joins=(list joined-object)
==