Commits

Kirill Simonov committed c9f5888

Added `guard()` macro.

Comments (0)

Files changed (9)

src/htsql/core/tr/bind.py

         if name is not None:
             scope = DefineBinding(scope, name, None, recipe, self.syntax)
         condition = self.state.bind(self.syntax.larm, scope=scope)
+        condition = ImplicitCastBinding(condition, coerce(BooleanDomain()),
+                                        condition.syntax)
         return AttachBinding(self.state.scope, seed, [], condition, self.syntax)
 
 

src/htsql/core/tr/fn/bind.py

         DateTimeDomain, EnumDomain, ListDomain, RecordDomain)
 from ...syn.syntax import (NumberSyntax, IntegerSyntax, StringSyntax,
         IdentifierSyntax, ComposeSyntax, ApplySyntax, OperatorSyntax,
-        PrefixSyntax, GroupSyntax)
+        PrefixSyntax, GroupSyntax, ReferenceSyntax)
 from ..binding import (LiteralBinding, SortBinding, SieveBinding,
         FormulaBinding, CastBinding, ImplicitCastBinding, WrappingBinding,
         TitleBinding, DirectionBinding, QuotientBinding, AssignmentBinding,
         DefineBinding, DefineReferenceBinding, SelectionBinding, HomeBinding,
         RescopingBinding, CoverBinding, ForkBinding, ClipBinding,
-        SegmentBinding, QueryBinding, Binding, BindingRecipe, ComplementRecipe,
-        KernelRecipe, SubstitutionRecipe, ClosedRecipe)
+        SegmentBinding, QueryBinding, AliasBinding, Binding, BindingRecipe,
+        ComplementRecipe, KernelRecipe, SubstitutionRecipe, ClosedRecipe)
 from ..bind import BindByName, BindingState
 from ...error import Error, translate_guard
 from ..coerce import coerce
         KeepPolaritySig, ReversePolaritySig, RoundSig, RoundToSig, TruncSig,
         TruncToSig, LengthSig, ContainsSig, ExistsSig, CountSig, MinMaxSig,
         SumSig, AvgSig, AggregateSig, QuantifySig, DefineSig, GivenSig,
-        SelectSig, LinkSig, TopSig)
+        SelectSig, LinkSig, TopSig, GuardSig)
 import sys
 
 
         return TitleBinding(base, title, self.syntax)
 
 
+class BindGuard(BindMacro):
+
+    call('guard')
+    signature = GuardSig
+
+    def expand(self, reference, consequent, alternative=None):
+        if not isinstance(reference, ReferenceSyntax):
+            with translate_guard(reference):
+                raise Error("Function '%s' expects a reference"
+                            " as the first argument"
+                            % self.name.encode('utf-8'))
+        value = self.state.bind(reference)
+        if isinstance(value, AliasBinding):
+            value = value.base
+        if isinstance(value, LiteralBinding):
+            if value.value is None:
+                value = None
+            elif isinstance(value.domain, UntypedDomain) and not value.value:
+                value = None
+        if value is not None:
+            return self.state.bind(consequent)
+        elif alternative is not None:
+            return self.state.bind(alternative)
+        else:
+            return self.state.scope
+
+
 #class BindSieve(BindMacro):
 #
 #    call('?')

src/htsql/core/tr/fn/signature.py

     ]
 
 
+class GuardSig(Signature):
+
+    slots = [
+            Slot('reference'),
+            Slot('consequent'),
+            Slot('alternative', is_mandatory=False),
+    ]
+
+
 class IfSig(Signature):
 
     slots = [

test/input/translation.yaml

                           num_dept := count(department))
                   .match('art'){num_dept}
       skip: true
+    # Guards
+    - uri: /school.guard($c, filter(campus=$c)) :given $c:=null
+    - uri: /school.guard($c, filter(campus=$c)) :given $c:=''
+    - uri: /school.guard($c, filter(campus=$c)) :given $c:='north'
+    - uri: /school.guard($c, filter(campus=$c)) :given $c:={'north','south'}
+    - uri: /school.guard($c, filter(campus=$c), filter(campus='old')) :given $c:=null
+    - uri: /school.guard($c, filter(campus=$c), filter(campus='old')) :given $c:='north'
+    - uri: /school.guard($c, filter(campus=$c))
+      expect: 400
+    - uri: /school.guard(!is_null($c), filter(campus=$c)) :given $c:='north'
+      expect: 400
 
   - title: Projections
     tests:

test/output/mssql.yaml

               bind error: parameterized definition is not allowed for an in-segment assignment:
                   /school():=school{code, name}?exists(department)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+          - uri: /school.guard($c, filter(campus=$c)) :given $c:=null
+            status: 200 OK
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            - [Vary, Accept]
+            body: |2
+               | school                                        |
+               +------+-------------------------------+--------+
+               | code | name                          | campus |
+              -+------+-------------------------------+--------+-
+               | art  | School of Art & Design        | old    |
+               | bus  | School of Business            | south  |
+               | edu  | College of Education          | old    |
+               | eng  | School of Engineering         | north  |
+               | la   | School of Arts and Humanities | old    |
+               | mus  | School of Music & Dance       | south  |
+               | ns   | School of Natural Sciences    | old    |
+               | ph   | Public Honorariums            |        |
+               | sc   | School of Continuing Studies  |        |
+
+               ----
+               /school.guard($c,filter(campus=$c)) :given $c:=null
+               SELECT [school].[code],
+                      [school].[name],
+                      [school].[campus]
+               FROM [ad].[school]
+               ORDER BY 1 ASC
+          - uri: /school.guard($c, filter(campus=$c)) :given $c:=''
+            status: 200 OK
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            - [Vary, Accept]
+            body: |2
+               | school                                        |
+               +------+-------------------------------+--------+
+               | code | name                          | campus |
+              -+------+-------------------------------+--------+-
+               | art  | School of Art & Design        | old    |
+               | bus  | School of Business            | south  |
+               | edu  | College of Education          | old    |
+               | eng  | School of Engineering         | north  |
+               | la   | School of Arts and Humanities | old    |
+               | mus  | School of Music & Dance       | south  |
+               | ns   | School of Natural Sciences    | old    |
+               | ph   | Public Honorariums            |        |
+               | sc   | School of Continuing Studies  |        |
+
+               ----
+               /school.guard($c,filter(campus=$c)) :given $c:=''
+               SELECT [school].[code],
+                      [school].[name],
+                      [school].[campus]
+               FROM [ad].[school]
+               ORDER BY 1 ASC
+          - uri: /school.guard($c, filter(campus=$c)) :given $c:='north'
+            status: 200 OK
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            - [Vary, Accept]
+            body: |2
+               | school                                |
+               +------+-----------------------+--------+
+               | code | name                  | campus |
+              -+------+-----------------------+--------+-
+               | eng  | School of Engineering | north  |
+
+               ----
+               /school.guard($c,filter(campus=$c)) :given $c:='north'
+               SELECT [school].[code],
+                      [school].[name],
+                      [school].[campus]
+               FROM [ad].[school]
+               WHERE ([school].[campus] = 'north')
+               ORDER BY 1 ASC
+          - uri: /school.guard($c, filter(campus=$c)) :given $c:={'north','south'}
+            status: 200 OK
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            - [Vary, Accept]
+            body: |2
+               | school                                  |
+               +------+-------------------------+--------+
+               | code | name                    | campus |
+              -+------+-------------------------+--------+-
+               | bus  | School of Business      | south  |
+               | eng  | School of Engineering   | north  |
+               | mus  | School of Music & Dance | south  |
+
+               ----
+               /school.guard($c,filter(campus=$c)) :given $c:={'north','south'}
+               SELECT [school].[code],
+                      [school].[name],
+                      [school].[campus]
+               FROM [ad].[school]
+               WHERE ([school].[campus] IN ('north', 'south'))
+               ORDER BY 1 ASC
+          - uri: /school.guard($c, filter(campus=$c), filter(campus='old')) :given
+              $c:=null
+            status: 200 OK
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            - [Vary, Accept]
+            body: |2
+               | school                                        |
+               +------+-------------------------------+--------+
+               | code | name                          | campus |
+              -+------+-------------------------------+--------+-
+               | art  | School of Art & Design        | old    |
+               | edu  | College of Education          | old    |
+               | la   | School of Arts and Humanities | old    |
+               | ns   | School of Natural Sciences    | old    |
+
+               ----
+               /school.guard($c,filter(campus=$c),filter(campus='old')) :given $c:=null
+               SELECT [school].[code],
+                      [school].[name],
+                      [school].[campus]
+               FROM [ad].[school]
+               WHERE ([school].[campus] = 'old')
+               ORDER BY 1 ASC
+          - uri: /school.guard($c, filter(campus=$c), filter(campus='old')) :given
+              $c:='north'
+            status: 200 OK
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            - [Vary, Accept]
+            body: |2
+               | school                                |
+               +------+-----------------------+--------+
+               | code | name                  | campus |
+              -+------+-----------------------+--------+-
+               | eng  | School of Engineering | north  |
+
+               ----
+               /school.guard($c,filter(campus=$c),filter(campus='old')) :given $c:='north'
+               SELECT [school].[code],
+                      [school].[name],
+                      [school].[campus]
+               FROM [ad].[school]
+               WHERE ([school].[campus] = 'north')
+               ORDER BY 1 ASC
+          - uri: /school.guard($c, filter(campus=$c))
+            status: 400 Bad Request
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            body: |
+              Found unknown reference:
+                  $c
+              While translating:
+                  /school.guard($c, filter(campus=$c))
+                                ^^
+          - uri: /school.guard(!is_null($c), filter(campus=$c)) :given $c:='north'
+            status: 400 Bad Request
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            body: |
+              Function 'guard' expects a reference as the first argument
+              While translating:
+                  /school.guard(!is_null($c), filter(campus=$c)) :given $c:='north'
+                                ^^^^^^^^^^^^
         - id: projections
           tests:
           - uri: /distinct(program{degree})

test/output/mysql.yaml

               bind error: parameterized definition is not allowed for an in-segment assignment:
                   /school():=school{code, name}?exists(department)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+          - uri: /school.guard($c, filter(campus=$c)) :given $c:=null
+            status: 200 OK
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            - [Vary, Accept]
+            body: |2
+               | school                                        |
+               +------+-------------------------------+--------+
+               | code | name                          | campus |
+              -+------+-------------------------------+--------+-
+               | art  | School of Art & Design        | old    |
+               | bus  | School of Business            | south  |
+               | edu  | College of Education          | old    |
+               | eng  | School of Engineering         | north  |
+               | la   | School of Arts and Humanities | old    |
+               | mus  | School of Music & Dance       | south  |
+               | ns   | School of Natural Sciences    | old    |
+               | ph   | Public Honorariums            |        |
+               | sc   | School of Continuing Studies  |        |
+
+               ----
+               /school.guard($c,filter(campus=$c)) :given $c:=null
+               SELECT `school`.`code`,
+                      `school`.`name`,
+                      `school`.`campus`
+               FROM `school`
+               ORDER BY 1 ASC
+          - uri: /school.guard($c, filter(campus=$c)) :given $c:=''
+            status: 200 OK
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            - [Vary, Accept]
+            body: |2
+               | school                                        |
+               +------+-------------------------------+--------+
+               | code | name                          | campus |
+              -+------+-------------------------------+--------+-
+               | art  | School of Art & Design        | old    |
+               | bus  | School of Business            | south  |
+               | edu  | College of Education          | old    |
+               | eng  | School of Engineering         | north  |
+               | la   | School of Arts and Humanities | old    |
+               | mus  | School of Music & Dance       | south  |
+               | ns   | School of Natural Sciences    | old    |
+               | ph   | Public Honorariums            |        |
+               | sc   | School of Continuing Studies  |        |
+
+               ----
+               /school.guard($c,filter(campus=$c)) :given $c:=''
+               SELECT `school`.`code`,
+                      `school`.`name`,
+                      `school`.`campus`
+               FROM `school`
+               ORDER BY 1 ASC
+          - uri: /school.guard($c, filter(campus=$c)) :given $c:='north'
+            status: 200 OK
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            - [Vary, Accept]
+            body: |2
+               | school                                |
+               +------+-----------------------+--------+
+               | code | name                  | campus |
+              -+------+-----------------------+--------+-
+               | eng  | School of Engineering | north  |
+
+               ----
+               /school.guard($c,filter(campus=$c)) :given $c:='north'
+               SELECT `school`.`code`,
+                      `school`.`name`,
+                      `school`.`campus`
+               FROM `school`
+               WHERE (`school`.`campus` = 'north')
+               ORDER BY 1 ASC
+          - uri: /school.guard($c, filter(campus=$c)) :given $c:={'north','south'}
+            status: 200 OK
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            - [Vary, Accept]
+            body: |2
+               | school                                  |
+               +------+-------------------------+--------+
+               | code | name                    | campus |
+              -+------+-------------------------+--------+-
+               | bus  | School of Business      | south  |
+               | eng  | School of Engineering   | north  |
+               | mus  | School of Music & Dance | south  |
+
+               ----
+               /school.guard($c,filter(campus=$c)) :given $c:={'north','south'}
+               SELECT `school`.`code`,
+                      `school`.`name`,
+                      `school`.`campus`
+               FROM `school`
+               WHERE (`school`.`campus` IN ('north', 'south'))
+               ORDER BY 1 ASC
+          - uri: /school.guard($c, filter(campus=$c), filter(campus='old')) :given
+              $c:=null
+            status: 200 OK
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            - [Vary, Accept]
+            body: |2
+               | school                                        |
+               +------+-------------------------------+--------+
+               | code | name                          | campus |
+              -+------+-------------------------------+--------+-
+               | art  | School of Art & Design        | old    |
+               | edu  | College of Education          | old    |
+               | la   | School of Arts and Humanities | old    |
+               | ns   | School of Natural Sciences    | old    |
+
+               ----
+               /school.guard($c,filter(campus=$c),filter(campus='old')) :given $c:=null
+               SELECT `school`.`code`,
+                      `school`.`name`,
+                      `school`.`campus`
+               FROM `school`
+               WHERE (`school`.`campus` = 'old')
+               ORDER BY 1 ASC
+          - uri: /school.guard($c, filter(campus=$c), filter(campus='old')) :given
+              $c:='north'
+            status: 200 OK
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            - [Vary, Accept]
+            body: |2
+               | school                                |
+               +------+-----------------------+--------+
+               | code | name                  | campus |
+              -+------+-----------------------+--------+-
+               | eng  | School of Engineering | north  |
+
+               ----
+               /school.guard($c,filter(campus=$c),filter(campus='old')) :given $c:='north'
+               SELECT `school`.`code`,
+                      `school`.`name`,
+                      `school`.`campus`
+               FROM `school`
+               WHERE (`school`.`campus` = 'north')
+               ORDER BY 1 ASC
+          - uri: /school.guard($c, filter(campus=$c))
+            status: 400 Bad Request
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            body: |
+              Found unknown reference:
+                  $c
+              While translating:
+                  /school.guard($c, filter(campus=$c))
+                                ^^
+          - uri: /school.guard(!is_null($c), filter(campus=$c)) :given $c:='north'
+            status: 400 Bad Request
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            body: |
+              Function 'guard' expects a reference as the first argument
+              While translating:
+                  /school.guard(!is_null($c), filter(campus=$c)) :given $c:='north'
+                                ^^^^^^^^^^^^
         - id: projections
           tests:
           - uri: /distinct(program{degree})

test/output/oracle.yaml

               bind error: parameterized definition is not allowed for an in-segment assignment:
                   /school():=school{code, name}?exists(department)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+          - uri: /school.guard($c, filter(campus=$c)) :given $c:=null
+            status: 200 OK
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            - [Vary, Accept]
+            body: |2
+               | school                                        |
+               +------+-------------------------------+--------+
+               | code | name                          | campus |
+              -+------+-------------------------------+--------+-
+               | art  | School of Art & Design        | old    |
+               | bus  | School of Business            | south  |
+               | edu  | College of Education          | old    |
+               | eng  | School of Engineering         | north  |
+               | la   | School of Arts and Humanities | old    |
+               | mus  | School of Music & Dance       | south  |
+               | ns   | School of Natural Sciences    | old    |
+               | ph   | Public Honorariums            |        |
+               | sc   | School of Continuing Studies  |        |
+
+               ----
+               /school.guard($c,filter(campus=$c)) :given $c:=null
+               SELECT "SCHOOL"."CODE",
+                      "SCHOOL"."NAME",
+                      "SCHOOL"."CAMPUS"
+               FROM "SCHOOL"
+               ORDER BY 1 ASC
+          - uri: /school.guard($c, filter(campus=$c)) :given $c:=''
+            status: 200 OK
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            - [Vary, Accept]
+            body: |2
+               | school                                        |
+               +------+-------------------------------+--------+
+               | code | name                          | campus |
+              -+------+-------------------------------+--------+-
+               | art  | School of Art & Design        | old    |
+               | bus  | School of Business            | south  |
+               | edu  | College of Education          | old    |
+               | eng  | School of Engineering         | north  |
+               | la   | School of Arts and Humanities | old    |
+               | mus  | School of Music & Dance       | south  |
+               | ns   | School of Natural Sciences    | old    |
+               | ph   | Public Honorariums            |        |
+               | sc   | School of Continuing Studies  |        |
+
+               ----
+               /school.guard($c,filter(campus=$c)) :given $c:=''
+               SELECT "SCHOOL"."CODE",
+                      "SCHOOL"."NAME",
+                      "SCHOOL"."CAMPUS"
+               FROM "SCHOOL"
+               ORDER BY 1 ASC
+          - uri: /school.guard($c, filter(campus=$c)) :given $c:='north'
+            status: 200 OK
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            - [Vary, Accept]
+            body: |2
+               | school                                |
+               +------+-----------------------+--------+
+               | code | name                  | campus |
+              -+------+-----------------------+--------+-
+               | eng  | School of Engineering | north  |
+
+               ----
+               /school.guard($c,filter(campus=$c)) :given $c:='north'
+               SELECT "SCHOOL"."CODE",
+                      "SCHOOL"."NAME",
+                      "SCHOOL"."CAMPUS"
+               FROM "SCHOOL"
+               WHERE ("SCHOOL"."CAMPUS" = 'north')
+               ORDER BY 1 ASC
+          - uri: /school.guard($c, filter(campus=$c)) :given $c:={'north','south'}
+            status: 200 OK
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            - [Vary, Accept]
+            body: |2
+               | school                                  |
+               +------+-------------------------+--------+
+               | code | name                    | campus |
+              -+------+-------------------------+--------+-
+               | bus  | School of Business      | south  |
+               | eng  | School of Engineering   | north  |
+               | mus  | School of Music & Dance | south  |
+
+               ----
+               /school.guard($c,filter(campus=$c)) :given $c:={'north','south'}
+               SELECT "SCHOOL"."CODE",
+                      "SCHOOL"."NAME",
+                      "SCHOOL"."CAMPUS"
+               FROM "SCHOOL"
+               WHERE ("SCHOOL"."CAMPUS" IN ('north', 'south'))
+               ORDER BY 1 ASC
+          - uri: /school.guard($c, filter(campus=$c), filter(campus='old')) :given
+              $c:=null
+            status: 200 OK
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            - [Vary, Accept]
+            body: |2
+               | school                                        |
+               +------+-------------------------------+--------+
+               | code | name                          | campus |
+              -+------+-------------------------------+--------+-
+               | art  | School of Art & Design        | old    |
+               | edu  | College of Education          | old    |
+               | la   | School of Arts and Humanities | old    |
+               | ns   | School of Natural Sciences    | old    |
+
+               ----
+               /school.guard($c,filter(campus=$c),filter(campus='old')) :given $c:=null
+               SELECT "SCHOOL"."CODE",
+                      "SCHOOL"."NAME",
+                      "SCHOOL"."CAMPUS"
+               FROM "SCHOOL"
+               WHERE ("SCHOOL"."CAMPUS" = 'old')
+               ORDER BY 1 ASC
+          - uri: /school.guard($c, filter(campus=$c), filter(campus='old')) :given
+              $c:='north'
+            status: 200 OK
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            - [Vary, Accept]
+            body: |2
+               | school                                |
+               +------+-----------------------+--------+
+               | code | name                  | campus |
+              -+------+-----------------------+--------+-
+               | eng  | School of Engineering | north  |
+
+               ----
+               /school.guard($c,filter(campus=$c),filter(campus='old')) :given $c:='north'
+               SELECT "SCHOOL"."CODE",
+                      "SCHOOL"."NAME",
+                      "SCHOOL"."CAMPUS"
+               FROM "SCHOOL"
+               WHERE ("SCHOOL"."CAMPUS" = 'north')
+               ORDER BY 1 ASC
+          - uri: /school.guard($c, filter(campus=$c))
+            status: 400 Bad Request
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            body: |
+              Found unknown reference:
+                  $c
+              While translating:
+                  /school.guard($c, filter(campus=$c))
+                                ^^
+          - uri: /school.guard(!is_null($c), filter(campus=$c)) :given $c:='north'
+            status: 400 Bad Request
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            body: |
+              Function 'guard' expects a reference as the first argument
+              While translating:
+                  /school.guard(!is_null($c), filter(campus=$c)) :given $c:='north'
+                                ^^^^^^^^^^^^
         - id: projections
           tests:
           - uri: /distinct(program{degree})

test/output/pgsql.yaml

               bind error: parameterized definition is not allowed for an in-segment assignment:
                   /school():=school{code, name}?exists(department)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+          - uri: /school.guard($c, filter(campus=$c)) :given $c:=null
+            status: 200 OK
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            - [Vary, Accept]
+            body: |2
+               | school                                        |
+               +------+-------------------------------+--------+
+               | code | name                          | campus |
+              -+------+-------------------------------+--------+-
+               | art  | School of Art & Design        | old    |
+               | bus  | School of Business            | south  |
+               | edu  | College of Education          | old    |
+               | eng  | School of Engineering         | north  |
+               | la   | School of Arts and Humanities | old    |
+               | mus  | School of Music & Dance       | south  |
+               | ns   | School of Natural Sciences    | old    |
+               | ph   | Public Honorariums            |        |
+               | sc   | School of Continuing Studies  |        |
+
+               ----
+               /school.guard($c,filter(campus=$c)) :given $c:=null
+               SELECT "school"."code",
+                      "school"."name",
+                      "school"."campus"
+               FROM "ad"."school"
+               ORDER BY 1 ASC
+          - uri: /school.guard($c, filter(campus=$c)) :given $c:=''
+            status: 200 OK
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            - [Vary, Accept]
+            body: |2
+               | school                                        |
+               +------+-------------------------------+--------+
+               | code | name                          | campus |
+              -+------+-------------------------------+--------+-
+               | art  | School of Art & Design        | old    |
+               | bus  | School of Business            | south  |
+               | edu  | College of Education          | old    |
+               | eng  | School of Engineering         | north  |
+               | la   | School of Arts and Humanities | old    |
+               | mus  | School of Music & Dance       | south  |
+               | ns   | School of Natural Sciences    | old    |
+               | ph   | Public Honorariums            |        |
+               | sc   | School of Continuing Studies  |        |
+
+               ----
+               /school.guard($c,filter(campus=$c)) :given $c:=''
+               SELECT "school"."code",
+                      "school"."name",
+                      "school"."campus"
+               FROM "ad"."school"
+               ORDER BY 1 ASC
+          - uri: /school.guard($c, filter(campus=$c)) :given $c:='north'
+            status: 200 OK
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            - [Vary, Accept]
+            body: |2
+               | school                                |
+               +------+-----------------------+--------+
+               | code | name                  | campus |
+              -+------+-----------------------+--------+-
+               | eng  | School of Engineering | north  |
+
+               ----
+               /school.guard($c,filter(campus=$c)) :given $c:='north'
+               SELECT "school"."code",
+                      "school"."name",
+                      "school"."campus"
+               FROM "ad"."school"
+               WHERE ("school"."campus" = 'north')
+               ORDER BY 1 ASC
+          - uri: /school.guard($c, filter(campus=$c)) :given $c:={'north','south'}
+            status: 200 OK
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            - [Vary, Accept]
+            body: |2
+               | school                                  |
+               +------+-------------------------+--------+
+               | code | name                    | campus |
+              -+------+-------------------------+--------+-
+               | bus  | School of Business      | south  |
+               | eng  | School of Engineering   | north  |
+               | mus  | School of Music & Dance | south  |
+
+               ----
+               /school.guard($c,filter(campus=$c)) :given $c:={'north','south'}
+               SELECT "school"."code",
+                      "school"."name",
+                      "school"."campus"
+               FROM "ad"."school"
+               WHERE ("school"."campus" IN ('north', 'south'))
+               ORDER BY 1 ASC
+          - uri: /school.guard($c, filter(campus=$c), filter(campus='old')) :given
+              $c:=null
+            status: 200 OK
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            - [Vary, Accept]
+            body: |2
+               | school                                        |
+               +------+-------------------------------+--------+
+               | code | name                          | campus |
+              -+------+-------------------------------+--------+-
+               | art  | School of Art & Design        | old    |
+               | edu  | College of Education          | old    |
+               | la   | School of Arts and Humanities | old    |
+               | ns   | School of Natural Sciences    | old    |
+
+               ----
+               /school.guard($c,filter(campus=$c),filter(campus='old')) :given $c:=null
+               SELECT "school"."code",
+                      "school"."name",
+                      "school"."campus"
+               FROM "ad"."school"
+               WHERE ("school"."campus" = 'old')
+               ORDER BY 1 ASC
+          - uri: /school.guard($c, filter(campus=$c), filter(campus='old')) :given
+              $c:='north'
+            status: 200 OK
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            - [Vary, Accept]
+            body: |2
+               | school                                |
+               +------+-----------------------+--------+
+               | code | name                  | campus |
+              -+------+-----------------------+--------+-
+               | eng  | School of Engineering | north  |
+
+               ----
+               /school.guard($c,filter(campus=$c),filter(campus='old')) :given $c:='north'
+               SELECT "school"."code",
+                      "school"."name",
+                      "school"."campus"
+               FROM "ad"."school"
+               WHERE ("school"."campus" = 'north')
+               ORDER BY 1 ASC
+          - uri: /school.guard($c, filter(campus=$c))
+            status: 400 Bad Request
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            body: |
+              Found unknown reference:
+                  $c
+              While translating:
+                  /school.guard($c, filter(campus=$c))
+                                ^^
+          - uri: /school.guard(!is_null($c), filter(campus=$c)) :given $c:='north'
+            status: 400 Bad Request
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            body: |
+              Function 'guard' expects a reference as the first argument
+              While translating:
+                  /school.guard(!is_null($c), filter(campus=$c)) :given $c:='north'
+                                ^^^^^^^^^^^^
         - id: projections
           tests:
           - uri: /distinct(program{degree})

test/output/sqlite.yaml

               bind error: parameterized definition is not allowed for an in-segment assignment:
                   /school():=school{code, name}?exists(department)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+          - uri: /school.guard($c, filter(campus=$c)) :given $c:=null
+            status: 200 OK
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            - [Vary, Accept]
+            body: |2
+               | school                                        |
+               +------+-------------------------------+--------+
+               | code | name                          | campus |
+              -+------+-------------------------------+--------+-
+               | art  | School of Art & Design        | old    |
+               | bus  | School of Business            | south  |
+               | edu  | College of Education          | old    |
+               | eng  | School of Engineering         | north  |
+               | la   | School of Arts and Humanities | old    |
+               | mus  | School of Music & Dance       | south  |
+               | ns   | School of Natural Sciences    | old    |
+               | ph   | Public Honorariums            |        |
+               | sc   | School of Continuing Studies  |        |
+
+               ----
+               /school.guard($c,filter(campus=$c)) :given $c:=null
+               SELECT "school"."code",
+                      "school"."name",
+                      "school"."campus"
+               FROM "school"
+               ORDER BY 1 ASC
+          - uri: /school.guard($c, filter(campus=$c)) :given $c:=''
+            status: 200 OK
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            - [Vary, Accept]
+            body: |2
+               | school                                        |
+               +------+-------------------------------+--------+
+               | code | name                          | campus |
+              -+------+-------------------------------+--------+-
+               | art  | School of Art & Design        | old    |
+               | bus  | School of Business            | south  |
+               | edu  | College of Education          | old    |
+               | eng  | School of Engineering         | north  |
+               | la   | School of Arts and Humanities | old    |
+               | mus  | School of Music & Dance       | south  |
+               | ns   | School of Natural Sciences    | old    |
+               | ph   | Public Honorariums            |        |
+               | sc   | School of Continuing Studies  |        |
+
+               ----
+               /school.guard($c,filter(campus=$c)) :given $c:=''
+               SELECT "school"."code",
+                      "school"."name",
+                      "school"."campus"
+               FROM "school"
+               ORDER BY 1 ASC
+          - uri: /school.guard($c, filter(campus=$c)) :given $c:='north'
+            status: 200 OK
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            - [Vary, Accept]
+            body: |2
+               | school                                |
+               +------+-----------------------+--------+
+               | code | name                  | campus |
+              -+------+-----------------------+--------+-
+               | eng  | School of Engineering | north  |
+
+               ----
+               /school.guard($c,filter(campus=$c)) :given $c:='north'
+               SELECT "school"."code",
+                      "school"."name",
+                      "school"."campus"
+               FROM "school"
+               WHERE ("school"."campus" = 'north')
+               ORDER BY 1 ASC
+          - uri: /school.guard($c, filter(campus=$c)) :given $c:={'north','south'}
+            status: 200 OK
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            - [Vary, Accept]
+            body: |2
+               | school                                  |
+               +------+-------------------------+--------+
+               | code | name                    | campus |
+              -+------+-------------------------+--------+-
+               | bus  | School of Business      | south  |
+               | eng  | School of Engineering   | north  |
+               | mus  | School of Music & Dance | south  |
+
+               ----
+               /school.guard($c,filter(campus=$c)) :given $c:={'north','south'}
+               SELECT "school"."code",
+                      "school"."name",
+                      "school"."campus"
+               FROM "school"
+               WHERE ("school"."campus" IN ('north', 'south'))
+               ORDER BY 1 ASC
+          - uri: /school.guard($c, filter(campus=$c), filter(campus='old')) :given
+              $c:=null
+            status: 200 OK
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            - [Vary, Accept]
+            body: |2
+               | school                                        |
+               +------+-------------------------------+--------+
+               | code | name                          | campus |
+              -+------+-------------------------------+--------+-
+               | art  | School of Art & Design        | old    |
+               | edu  | College of Education          | old    |
+               | la   | School of Arts and Humanities | old    |
+               | ns   | School of Natural Sciences    | old    |
+
+               ----
+               /school.guard($c,filter(campus=$c),filter(campus='old')) :given $c:=null
+               SELECT "school"."code",
+                      "school"."name",
+                      "school"."campus"
+               FROM "school"
+               WHERE ("school"."campus" = 'old')
+               ORDER BY 1 ASC
+          - uri: /school.guard($c, filter(campus=$c), filter(campus='old')) :given
+              $c:='north'
+            status: 200 OK
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            - [Vary, Accept]
+            body: |2
+               | school                                |
+               +------+-----------------------+--------+
+               | code | name                  | campus |
+              -+------+-----------------------+--------+-
+               | eng  | School of Engineering | north  |
+
+               ----
+               /school.guard($c,filter(campus=$c),filter(campus='old')) :given $c:='north'
+               SELECT "school"."code",
+                      "school"."name",
+                      "school"."campus"
+               FROM "school"
+               WHERE ("school"."campus" = 'north')
+               ORDER BY 1 ASC
+          - uri: /school.guard($c, filter(campus=$c))
+            status: 400 Bad Request
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            body: |
+              Found unknown reference:
+                  $c
+              While translating:
+                  /school.guard($c, filter(campus=$c))
+                                ^^
+          - uri: /school.guard(!is_null($c), filter(campus=$c)) :given $c:='north'
+            status: 400 Bad Request
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            body: |
+              Function 'guard' expects a reference as the first argument
+              While translating:
+                  /school.guard(!is_null($c), filter(campus=$c)) :given $c:='north'
+                                ^^^^^^^^^^^^
         - id: projections
           tests:
           - uri: /distinct(program{degree})