From 8041ecb342298fc86beb5a78e512221f56beab61 Mon Sep 17 00:00:00 2001 From: jackfoxy Date: Sat, 15 Oct 2022 16:09:57 -0700 Subject: [PATCH] select --- docs/query.md | 3 +- urql/lib/parse.hoon | 92 +++++++---- urql/tests/lib/select.hoon | 323 +++++++++++++++++++++++++++++++++++++ 3 files changed, 386 insertions(+), 32 deletions(-) create mode 100644 urql/tests/lib/select.hoon diff --git a/docs/query.md b/docs/query.md index cd53097..9620a8b 100644 --- a/docs/query.md +++ b/docs/query.md @@ -15,7 +15,6 @@ SELECT [ TOP ] [ BOTTOM ] [ DISTINCT ] | { { [] | }.* | { | } [ [ AS ] ] - | = { | } | } [ ,...n ] } @@ -26,7 +25,7 @@ SELECT [ TOP ] [ BOTTOM ] [ DISTINCT ] ] ] [ INTO ] -[ ORDER BY { | } [ ,...n ] ] +[ ORDER BY { | } [ ASC | DESC ] [ ,...n ] ] [ { UNION | COMBINE | EXCEPT diff --git a/urql/lib/parse.hoon b/urql/lib/parse.hoon index 73010e4..babe6e3 100644 --- a/urql/lib/parse.hoon +++ b/urql/lib/parse.hoon @@ -409,6 +409,10 @@ (cold %drop-column ;~(plug whitespace (jester 'drop') whitespace (jester 'column'))) face-list == +++ parse-datum ~+ ;~ pose + ;~(pose ;~(pfix whitespace parse-qualified-column) parse-qualified-column) + ;~(pose ;~(pfix whitespace parse-value-literal) parse-value-literal) + == :: :: indices :: @@ -827,10 +831,6 @@ ;~(plug whitespace (jester 'outer')) ;~(plug whitespace (jester 'then')) == -++ parse-datum ~+ ;~ pose - ;~(pose ;~(pfix whitespace parse-qualified-column) parse-qualified-column) - ;~(pose ;~(pfix whitespace parse-value-literal) parse-value-literal) - == ++ predicate-part ;~ pose :: value-literal-list ;~(pose ;~(pfix whitespace parse-operator) parse-operator) @@ -841,19 +841,15 @@ :: parse scalar :: ++ get-datum ~+ ;~ pose - ;~(sfix ;~(pfix whitespace parse-qualified-column) whitespace) - ;~(sfix ;~(pfix whitespace parse-value-literal) whitespace) - ;~(pfix whitespace parse-qualified-column) - ;~(pfix whitespace parse-value-literal) ;~(sfix parse-qualified-column whitespace) ;~(sfix parse-value-literal whitespace) - parse-qualified-column - parse-value-literal + ;~(sfix parse-datum whitespace) + parse-datum == -++ cook-if - |= parsed=* - ^- if-then-else:ast - (if-then-else:ast %if-then-else -.parsed +>-.parsed +>+>-.parsed) +::++ cook-if +:: |= parsed=* +:: ^- if-then-else:ast +:: (if-then-else:ast %if-then-else -.parsed +>-.parsed +>+>-.parsed) ++ parse-if ;~ plug parse-predicate ;~(pfix whitespace (cold %then (jester 'then'))) @@ -946,7 +942,38 @@ :: :: select :: -++ select-columns (easy ~) +++ select-stop ;~ pose + ;~(plug whitespace (jester 'group')) + ;~(plug whitespace (jester 'into')) + ;~(plug whitespace (jester 'order')) + ;~(plug whitespace (jester 'union')) + ;~(plug whitespace (jester 'combine')) + ;~(plug whitespace (jester 'except')) + ;~(plug whitespace (jester 'intersect')) + ;~(plug whitespace (jester 'divided')) + == +++ select-column ~+ ;~ pose + ;~(sfix ;~(pfix whitespace ;~(plug parse-alias (cold %all ;~(plug dot tar)))) whitespace) + ;~(pfix whitespace ;~(plug parse-alias (cold %all ;~(plug dot tar)))) + ;~(sfix ;~(plug parse-alias (cold %all ;~(plug dot tar))) whitespace) + ;~(plug parse-alias (cold %all ;~(plug dot tar))) + :: + ;~(sfix ;~(pfix whitespace ;~(plug parse-qualified-object (cold %all ;~(plug dot tar)))) whitespace) + ;~(pfix whitespace ;~(plug parse-qualified-object (cold %all ;~(plug dot tar)))) + ;~(sfix ;~(plug parse-qualified-object (cold %all ;~(plug dot tar))) whitespace) + ;~(plug parse-qualified-object (cold %all ;~(plug dot tar))) + :: + ;~(sfix ;~(plug get-datum (cold %as (jester 'as')) ;~(pfix whitespace alias)) whitespace) + ;~(plug get-datum (cold %as (jester 'as')) ;~(pfix whitespace alias)) + :: + get-datum + :: + (cold %all ;~(plug whitespace tar whitespace)) + (cold %all ;~(plug whitespace tar)) + (cold %all ;~(plug tar whitespace)) + (cold %all tar) + == +++ select-columns (full (more com select-column)) ++ select-top-bottom-distinct ;~ plug (cold %select ;~(plug whitespace (jester 'select'))) (cold %top ;~(plug whitespace (jester 'top'))) @@ -954,7 +981,7 @@ (cold %bottom ;~(plug whitespace (jester 'bottom'))) ;~(pfix whitespace dem) (cold %distinct ;~(plug whitespace (jester 'distinct'))) - ;~(pose (cold %all tar) select-columns) + select-columns == ++ select-top-bottom ;~ plug (cold %select ;~(plug whitespace (jester 'select'))) @@ -962,47 +989,52 @@ ;~(pfix whitespace dem) (cold %bottom ;~(plug whitespace (jester 'bottom'))) ;~(pfix whitespace dem) - ;~(pose (cold %all tar) select-columns) + ;~(less ;~(plug whitespace (jester 'distinct')) select-columns) == ++ select-top-distinct ;~ plug (cold %select ;~(plug whitespace (jester 'select'))) (cold %top ;~(plug whitespace (jester 'top'))) ;~(pfix whitespace dem) (cold %distinct ;~(plug whitespace (jester 'distinct'))) - ;~(pose (cold %all tar) select-columns) + select-columns == ++ select-top ;~ plug (cold %select ;~(plug whitespace (jester 'select'))) (cold %top ;~(plug whitespace (jester 'top'))) - ;~(pose (cold %all tar) select-columns) + ;~(pfix whitespace dem) + ;~(less ;~(plug whitespace (jester 'distinct')) select-columns) == - ++ select-bottom-distinct ;~ plug (cold %select ;~(plug whitespace (jester 'select'))) (cold %bottom ;~(plug whitespace (jester 'bottom'))) ;~(pfix whitespace dem) (cold %distinct ;~(plug whitespace (jester 'distinct'))) - ;~(pose (cold %all tar) select-columns) + select-columns == ++ select-bottom ;~ plug (cold %select ;~(plug whitespace (jester 'select'))) (cold %bottom ;~(plug whitespace (jester 'bottom'))) ;~(pfix whitespace dem) - ;~(pose (cold %all tar) select-columns) + ;~(less ;~(plug whitespace (jester 'distinct')) select-columns) == ++ select-distinct ;~ plug (cold %select ;~(plug whitespace (jester 'select'))) (cold %distinct ;~(plug whitespace (jester 'distinct'))) - ;~(pose (cold %all tar) select-columns) + select-columns + == +++ simple-select ;~ plug + (cold %select ;~(plug whitespace (jester 'select'))) + ;~(less ;~(plug whitespace (jester 'distinct')) select-columns) == ++ parse-select ;~ pose - select-top-bottom-distinct - select-top-bottom - select-top-distinct - select-top - select-bottom-distinct - select-bottom - select-distinct + ;~(less select-stop select-top-bottom-distinct) + ;~(less select-stop select-top-bottom) + ;~(less select-stop select-top-distinct) + ;~(less select-stop select-top) + ;~(less select-stop select-bottom-distinct) + ;~(less select-stop select-bottom) + ;~(less select-stop select-distinct) + ;~(less select-stop simple-select) == :: :: parse urQL command diff --git a/urql/tests/lib/select.hoon b/urql/tests/lib/select.hoon new file mode 100644 index 0000000..542164e --- /dev/null +++ b/urql/tests/lib/select.hoon @@ -0,0 +1,323 @@ +/+ parse, *test +|% +:: +:: select +:: +++ simple-columns ~[[%qualified-column qualifier=[%qualified-object ship=~ database='UNKNOWN' namespace='COLUMN-OR-CTE' name='x1'] column='x1' alias=~] [%qualified-column qualifier=[%qualified-object ship=~ database='db' namespace='ns' name='table'] column='col1' alias=~] [%qualified-column qualifier=[%qualified-object ship=~ database='UNKNOWN' namespace='COLUMN' name='table-alias'] column='name' alias=~] [%qualified-column qualifier=[%qualified-object ship=~ database='db' namespace='dbo' name='table'] column='col2' alias=~] [%qualified-column qualifier=[%qualified-object ship=~ database='UNKNOWN' namespace='COLUMN' name='T1'] column='foo' alias=~] [%ud 1] [%p 0] [%t 'cord']] +++ aliased-columns-1 ~[[[%qualified-column qualifier=[%qualified-object ship=~ database='UNKNOWN' namespace='COLUMN-OR-CTE' name='x1'] column='x1' alias=~] %as %foo] [[%qualified-column qualifier=[%qualified-object ship=~ database='db' namespace='ns' name='table'] column='col1' alias=~] %as %foo2] [[%qualified-column qualifier=[%qualified-object ship=~ database='UNKNOWN' namespace='COLUMN' name='table-alias'] column='name' alias=~] %as %bar] [[%qualified-column qualifier=[%qualified-object ship=~ database='db' namespace='dbo' name='table'] column='col2' alias=~] %as %bar2] [[%ud 1] %as %foobar] [[%p 0] %as 'F1'] [[%t 'cord'] %as 'BAR3']] +++ mixed-all ~[[[%qualified-object ship=~ database='db' namespace='dbo' name='t1'] %all] [[%qualified-column qualifier=[%qualified-object ship=~ database='UNKNOWN' namespace='COLUMN-OR-CTE' name='foo'] column='foo' alias=~] %as 125.762.588.864.358] [%qualified-column qualifier=[%qualified-object ship=~ database='UNKNOWN' namespace='COLUMN-OR-CTE' name='bar'] column='bar' alias=~] %all [p='T2' q=%all]] +:: +:: star select top, bottom, distinct, trailing whitespace +++ test-select-01 + =/ select "select top 10 bottom 10 distinct * " + %+ expect-eq + !> [%select %top 10 %bottom 10 %distinct [%all ~]] + !> (wonk (parse-select:parse [[1 1] select])) +:: +:: star select top, bottom, distinct +++ test-select-02 + =/ select "select top 10 bottom 10 distinct *" + %+ expect-eq + !> [%select %top 10 %bottom 10 %distinct [%all ~]] + !> (wonk (parse-select:parse [[1 1] select])) +:: +:: star select top bottom +++ test-select-03 + =/ select "select top 10 bottom 10 *" + %+ expect-eq + !> [%select %top 10 %bottom 10 [%all ~]] + !> (wonk (parse-select:parse [[1 1] select])) +:: +:: star select top, distinct, trailing whitespace +++ test-select-04 + =/ select "select top 10 distinct * " + %+ expect-eq + !> [%select %top 10 %distinct [%all ~]] + !> (wonk (parse-select:parse [[1 1] select])) +:: +:: star select top, distinct +++ test-select-05 + =/ select "select top 10 distinct *" + %+ expect-eq + !> [%select %top 10 %distinct [%all ~]] + !> (wonk (parse-select:parse [[1 1] select])) +:: +:: star select top, trailing whitespace +++ test-select-06 + =/ select "select top 10 * " + %+ expect-eq + !> [%select %top 10 [%all ~]] + !> (wonk (parse-select:parse [[1 1] select])) +:: +:: star select top +++ test-select-07 + =/ select "select top 10 *" + %+ expect-eq + !> [%select %top 10 [%all ~]] + !> (wonk (parse-select:parse [[1 1] select])) +:: +:: star select, trailing whitespace +++ test-select-08 + =/ select "select * " + %+ expect-eq + !> [%select [%all ~]] + !> (wonk (parse-select:parse [[1 1] select])) +:: +:: star select +++ test-select-09 + =/ select "select *" + %+ expect-eq + !> [%select [%all ~]] + !> (wonk (parse-select:parse [[1 1] select])) +:: +:: star select bottom, distinct, trailing whitespace +++ test-select-10 + =/ select "select bottom 10 distinct * " + %+ expect-eq + !> [%select %bottom 10 %distinct [%all ~]] + !> (wonk (parse-select:parse [[1 1] select])) +:: +:: star select bottom, distinct +++ test-select-11 + =/ select "select bottom 10 distinct *" + %+ expect-eq + !> [%select %bottom 10 %distinct [%all ~]] + !> (wonk (parse-select:parse [[1 1] select])) +:: +:: star select bottom, trailing whitespace +++ test-select-12 + =/ select "select bottom 10 * " + %+ expect-eq + !> [%select %bottom 10 [%all ~]] + !> (wonk (parse-select:parse [[1 1] select])) +:: +:: star select bottom +++ test-select-13 + =/ select "select bottom 10 *" + %+ expect-eq + !> [%select %bottom 10 [%all ~]] + !> (wonk (parse-select:parse [[1 1] select])) +:: +:: star select distinct, trailing whitespace +++ test-select-14 + =/ select "select distinct * " + %+ expect-eq + !> [%select %distinct [%all ~]] + !> (wonk (parse-select:parse [[1 1] select])) +:: +:: star select distinct +++ test-select-15 + =/ select "select distinct *" + %+ expect-eq + !> [%select %distinct [%all ~]] + !> (wonk (parse-select:parse [[1 1] select])) +:: +:: fail top, bottom, distinct, no column selection, trailing whitespace +++ test-fail-select-16 + =/ select "select top 10 bottom 10 distinct " + %- expect-fail + |. (wonk (parse-select:parse [[1 1] select])) +:: +:: fail top, bottom, distinct, no bottom parameter, trailing whitespace +++ test-fail-select-17 + =/ select "select top 10 bottom distinct * " + %- expect-fail + |. (wonk (parse-select:parse [[1 1] select])) +:: +:: fail top, bottom, distinct, no bottom parameter +++ test-fail-select-18 + =/ select "select top 10 bottom distinct *" + %- expect-fail + |. (wonk (parse-select:parse [[1 1] select])) +:: +:: fail top, bottom, distinct, no top parameter, trailing whitespace +++ test-fail-select-19 + =/ select "select top bottom 10 distinct * " + %- expect-fail + |. (wonk (parse-select:parse [[1 1] select])) +:: +:: fail top, bottom, distinct, no top parameter +++ test-fail-select-20 + =/ select "select top bottom 10 distinct *" + %- expect-fail + |. (wonk (parse-select:parse [[1 1] select])) +:: +:: fail top, bottom, no column selection, trailing whitespace +++ test-fail-select-21 + =/ select "select top 10 bottom 10 " + %- expect-fail + |. (wonk (parse-select:parse [[1 1] select])) +:: +:: fail top, bottom, no bottom parameter, trailing whitespace +++ test-fail-select-22 + =/ select "select top 10 bottom * " + %- expect-fail + |. (wonk (parse-select:parse [[1 1] select])) +:: +:: fail top, bottom, no bottom parameter +++ test-fail-select-23 + =/ select "select top 10 bottom *" + %- expect-fail + |. (wonk (parse-select:parse [[1 1] select])) +:: +:: fail top, no column selection, trailing whitespace +++ test-fail-select-24 + =/ select "select top 10 " + %- expect-fail + |. (wonk (parse-select:parse [[1 1] select])) +:: +:: fail top, distinct, no column selection, trailing whitespace +++ test-fail-select-25 + =/ select "select top 10 distinct " + %- expect-fail + |. (wonk (parse-select:parse [[1 1] select])) +:: +:: fail top, distinct, no top parameter, trailing whitespace +++ test-fail-select-26 + =/ select "select top distinct * " + %- expect-fail + |. (wonk (parse-select:parse [[1 1] select])) +:: +:: fail top, distinct, no top parameter +++ test-fail-select-27 + =/ select "select top distinct *" + %- expect-fail + |. (wonk (parse-select:parse [[1 1] select])) +:: +:: fail no column selection, trailing whitespace +++ test-fail-select-28 + =/ select "select " + %- expect-fail + |. (wonk (parse-select:parse [[1 1] select])) +:: +:: fail bottom, distinct, no column selection, trailing whitespace +++ test-fail-select-29 + =/ select "select bottom 10 distinct " + %- expect-fail + |. (wonk (parse-select:parse [[1 1] select])) +:: +:: fail bottom, distinct, no bottom parameter, trailing whitespace +++ test-fail-select-30 + =/ select "select bottom distinct * " + %- expect-fail + |. (wonk (parse-select:parse [[1 1] select])) +:: +:: fail bottom, distinct, no bottom parameter +++ test-fail-select-31 + =/ select "select bottom distinct *" + %- expect-fail + |. (wonk (parse-select:parse [[1 1] select])) +:: +:: fail bottom, no column selection, trailing whitespace +++ test-fail-select-32 + =/ select "select bottom 10 " + %- expect-fail + |. (wonk (parse-select:parse [[1 1] select])) +:: +:: fail distinct, no column selection, trailing whitespace +++ test-fail-select-33 + =/ select "select distinct " + %- expect-fail + |. (wonk (parse-select:parse [[1 1] select])) +:: +:: select top, bottom, distinct, simple columns +++ test-select-34 + =/ select "select top 10 bottom 10 distinct ". +" x1, db.ns.table.col1, table-alias.name, db..table.col2, T1.foo, 1, ~zod, 'cord'" + %+ expect-eq + !> [%select %top 10 %bottom 10 %distinct [simple-columns]] + !> (wonk (parse-select:parse [[1 1] select])) +:: +:: select top, bottom, distinct, simple columns, trailing space, no internal space +++ test-select-35 + =/ select "select top 10 bottom 10 distinct x1,db.ns.table.col1,table-alias.name,db..table.col2,T1.foo,1,~zod,'cord' " + %+ expect-eq + !> [%select %top 10 %bottom 10 %distinct [simple-columns]] + !> (wonk (parse-select:parse [[1 1] select])) +:: +:: aliased format 1 columns +++ test-select-36 + =/ select "select x1 as foo , db.ns.table.col1 as foo2 , table-alias.name as bar , db..table.col2 as bar2 , 1 as foobar , ~zod as F1 , 'cord' as BAR3 " + %+ expect-eq + !> [%select [aliased-columns-1]] + !> (wonk (parse-select:parse [[1 1] select])) +:: +:: aliased format 1 columns, no whitespace +++ test-select-37 + =/ select "select x1 as foo,db.ns.table.col1 as foo2,table-alias.name as bar,db..table.col2 as bar2,1 as foobar,~zod as F1,'cord' as BAR3" + %+ expect-eq + !> [%select [aliased-columns-1]] + !> (wonk (parse-select:parse [[1 1] select])) +:: +:: mixed all, object all, object alias all, column, aliased column +++ test-select-38 + =/ select "select db..t1.* , foo as foobar , bar , * , T2.* " + %+ expect-eq + !> [%select [mixed-all]] + !> (wonk (parse-select:parse [[1 1] select])) +:: +:: mixed all, object all, object alias all, column, aliased column, no whitespace +++ test-select-39 + =/ select "select db..t1.*,foo as foobar,bar,*,T2.*" + %+ expect-eq + !> [%select [mixed-all]] + !> (wonk (parse-select:parse [[1 1] select])) +-- + +:: for later inclusion in full query + +:: +:: fail top, bottom, distinct, no column selection +::++ test-fail-select- +:: =/ select "select top 10 bottom 10 distinct" +:: %- expect-fail +:: |. (wonk (parse-select:parse [[1 1] select])) + +:: +:: fail top, bottom, no column selection +::++ test-fail-select- +:: =/ select "select top 10 bottom 10" +:: %- expect-fail +:: |. (wonk (parse-select:parse [[1 1] select])) + +:: +:: fail top, no column selection +::++ test-fail-select- +:: =/ select "select top 10" +:: %- expect-fail +:: |. (wonk (parse-select:parse [[1 1] select])) + +:: +:: fail top, distinct, no column selection +::++ test-fail-select- +:: =/ select "select top 10 distinct" +:: %- expect-fail +:: |. (wonk (parse-select:parse [[1 1] select])) + +:: +:: fail no column selection +::++ test-fail-select- +:: =/ select "select" +:: %- expect-fail +:: |. (wonk (parse-select:parse [[1 1] select])) + +:: +:: fail bottom, distinct, no column selection +::++ test-fail-select- +:: =/ select "select bottom 10 distinct" +:: %- expect-fail +:: |. (wonk (parse-select:parse [[1 1] select])) + +:: +:: fail bottom, no column selection +::++ test-fail-select- +:: =/ select "select bottom 10" +:: %- expect-fail +:: |. (wonk (parse-select:parse [[1 1] select])) + +:: +:: fail distinct, no column selection +::++ test-fail-select- +:: =/ select "select distinct" +:: %- expect-fail +:: |. (wonk (parse-select:parse [[1 1] select]))