# Patent

## Title
Incremental query evaluation

## Abstract

A system and method for implementing incremental evaluation that is advantageous over nonincremental evaluation. The system and method incrementally maintain each individual aggregate value, for non-nested queries, using a constant amount of work.

## Background
CROSS REFERENCE TO RELATED APPLICATIONS
This application is the U.S. national phase entry under 35 U.S.C. §371 of International Patent Application No. PCT/US10/39982 filed Jun. 25, 2010, entitled INCREMENTAL QUERY EVALUATION, which in turn claims priority from U.S. Provisional Application Ser. No. 61/220,256, filed Jun. 25, 2009, entitled AGGRESSIVE COMPILATION FRAMEWORK FOR CONTINUOUS QUERY PROCESSING ON UPDATE STREAMS, both of which are incorporated by reference herein in their entirety for all purposes.


STATEMENT REGARDING FEDERALLY SPONSORED RESEARCH OR DEVELOPMENT
This invention was made with Government support under Grant Number FA9550-06-1-0111 awarded by the Air Force Office of Scientific Research. The United States Government has certain rights in the invention.


BACKGROUND
The system and method of the present embodiment relate generally to processing queries using incremental processing techniques.
Modern relational databases focus almost exclusively on providing flexible and extensible querying capabilities in dynamic environments, supporting both schema and data evolution. As a consequence, today's database management systems are centered around highly flexible interactive query processors, with their plan interpreters and other runtime components including schedulers and optimizers. However, a large fraction of the world's query workloads are fixed and embedded into database application programs. In these applications, queries are ossified during the development cycle, with developers converging to a choice of schema, query structure and parameters. Once hardened, queries are deployed into production environments, and reused numerous times, executing non-interactively. Other stream processing engines and relational databases provide a development environment for authoring stream processing applications. Data stream processing addresses the problem of processing standing queries over a limited form of database, namely sliding windows on sequential data. A window restricts the set of data to be processed to a very specific subset, typically a recent period of time. Incremental view maintenance on the other hand considers queries on a general database, that can be modified in any manner, and does not need to consist of only recent data. However the limitation of view maintenance lies in the class of queries supported, especially in terms of nested aggregates, and subqueries, and has been shown to have problems scaling to handle large volumes of changing data. Thus neither of these solutions is sufficient for financial, monitoring, and database applications, among others.
Most databases evolve incrementally, through changes that are small compared to the overall database size. For queries that are asked repeatedly, memoization of the query result can substantially reduce the amount of work needed to re-evaluate the query after moderate changes to the database. Using stored query results or auxiliary data structures to this effect is known as incremental view maintenance. A key notion in incremental view maintenance is that of a delta query. A change (“update”, which captures both insertions and deletions) to database D is denoted by u. The updated database is denoted by D+u, where + is a generalization of the union operation of relational algebra. A delta query ΔuQ depends on both D and u and expresses the change to the result of query Q as D is updated to D+u:

Q(D+u)=Q(D)+ΔuQ(D).

Evaluating ΔuQ(D) and using the result to update a materialized representation of Q(D) can be faster than recomputing Q(D+u) from scratch, because ΔQ is a simpler query than Q. Queries for which this is not true, e.g., queries with aggregates nested into conditions, may not be included in incremental view maintenance studies. It may not be clear that incremental view maintenance is more efficient than nonincremental query evaluation. If a query language L with multiset semantics is closed under joins (such as SQL), the image of L under taking deltas is the full language L: Given an arbitrary query Q0εL, there is another query QεL and a single-tuple insertion u such that ΔuQ=Q0. If u is an arbitrary single-tuple insertion into a relation R that does not occur in Q0, then Q is Q0×πØR, which suggests that incremental view maintenance is not advantageous over nonincremental query evaluation. A system and method are needed that implement incremental evaluation that is advantageous over nonincremental evaluation. A system and method are further needed for incrementally maintaining each individual aggregate value, for non-nested queries, using a constant amount of work per data item or data value incrementally maintained. A system and method are still further needed to develop automated trading algorithms having reduced development time and better throughput and lower latency for queries over level II (i.e. orderbook) data. A system and method are even still further needed for a query processing framework. This framework could be used, for example, but not limited to, for monitoring applications that could enable uniformly using the core of a high-level declarative language such as SQL across the multiple heterogeneous computing platforms present in such applications, by transforming to low-level code and customizing such code to execute on embedded devices such sensor motes, and cell phones.
SUMMARY
The needs set forth above as well as further and other needs and advantages are addressed by the embodiments set forth below. The present embodiment solves the problem of processing standing queries over a rapidly, and arbitrarily changing database, using incremental processing techniques.
The present embodiment provides incremental view maintenance based on an algebraic perspective. In the present embodiment, a ring of databases can be used as the foundation of a query calculus that allows expression of aggregate queries. The query calculus can inherit properties of the ring, such as having a normal form of polynomials and being closed under computing inverses and delta queries. The k-th delta of a polynomial query of degree k without nesting becomes, in the present embodiment, a function of the update, not of the database. Using the present embodiment, expensive query operators such as joins can be eliminated from programs that perform incremental view maintenance. The main result is that, for non-nested queries, each individual aggregate value can be incrementally maintained using a constant amount of work. This is not possible for nonincremental evaluation.
The present embodiment is related to both data stream processing, and incremental view maintenance. As described, the delta of a query is a query itself. Subsequently, a kth delta of a query, also called a query of the kth level, and an input specification is a specification of level zero.
The system and method of the present embodiment can recursively apply compilation to transform delta forms such as, for example, queries, to simpler and simpler queries, by considering combinations of base relation deltas. Recursive compilation in the present embodiment completes with queries that can be represented as simple procedural code statements. The system and method of the present embodiment can maintain each delta form encountered as a map datastructure, essentially a group-by aggregate index derived from applying aggregate distributivity properties together with join-graph decompositions. The procedural code generated by recursively compiling the delta forms maps can incrementally maintain the maps. The system and method can be enabled by an exemplary map algebra that can be used to represent and reason about queries and map datastructures, and can be used to perform recursive compilation through a set of transformation rules defined in the map algebra.
The present embodiment can include a framework by which SQL aggregate queries can be compiled into C++ code for continuous, incremental view maintenance. The SQL query compilation framework of the present embodiment can generate native code that can incrementally and continuously maintain aggregate views, for example, but not limited to, in main memory, using aggressive delta processing techniques. The system and method of the present embodiment can recursively compile queries into incremental view maintenance code; that is, instead of expressing and evaluating increments to queries as queries, the system and method of the present embodiment recursively compute increments to these increments, and so on, which can eliminate all queries. The system and method of the present embodiment can include, but are not limited to including, an aggressive compilation mechanism for transforming SQL queries to low-level code. Aggressive compilation refers to recursive compilation of queries into code. View maintenance algorithms typically consider a type of modification (insert, update, or delete) to a view, and derive a query to process this modification. However, this is only a single level of incremental computation, and the job of view maintenance overall still relies on queries. On the contrary, the system and method of the present embodiment recursively apply compilation on these resulting queries, until all queries are eliminated, leaving low-level code. Characteristics of aggressive compilation can include, but are not limited to including, aggressive inlining and rule-based transformation of a SQL query plan to produce event-handling functions capable of processing queries and producing results. The system and method of the present embodiment produce one event handling function for each type of modification event (insert, update, or delete) on any input relation to the query. Aggressive compilation can replace query plans (i.e. graphs) with event handling functions. Query graphs typically include dataflow between operators, which in turn hold queues and data structures as state to support their computation. The system and method of the present embodiment can transform and inline the operators, completely eliminating queues, and can determine which data structures to maintain to support the query, in its entirety, and in particular for each type of modification event, rather than at the individual operator level. In the system and method of the present embodiment, data structures can be reused across different modification events.
For a better understanding of the present embodiments, together with other and further embodiments thereof, reference is made to the accompanying drawings and detailed description.



DESCRIPTION OF THE SEVERAL VIEWS OF THE DRAWING
FIG. 1 is a table of results from recursive memoization of deltas for f(x)=3x2;
FIG. 2 is a table of a trace of map q that changes during a sequence of insertions and deletions;
FIG. 3 is a schematic block diagram of the system of the present embodiment; and
FIG. 4 is a flowchart of the method of the present embodiment.



DETAILED DESCRIPTION
The present embodiments are now described more fully hereinafter with reference to the accompanying drawings. The following configuration description is presented for illustrative purposes only. Any computer configuration and architecture satisfying the speed and interface requirements herein described may be suitable for implementing the system and method of the present embodiments.
The present embodiment can be described with reference to function ƒ: A→B such that (A, +) is an additive semigroup (i.e, set A is closed under a binary operation +) and (B, +) is an additive group (i.e., a semigroup with an additive identity 0B and an additive inverse). For an uεA, function Δuƒ:A→B is defined as

Δuƒ:xƒ(x+u)−ƒ(x).

Δuƒ(x) is the amount of change that happens to the value of ƒ(x) as the value of x is changed to x+u (“updated by u”).

Function Δjƒ of j-th deltas for j≧0 is defined as

A×Uj→B 

<x,u1, . . . ,uj>(Δuj(Δuj−1 . . . (Δutƒ) . . . ))(x).

where Δ0ƒ is ƒ. xεA is considered current. x can be updated with increments from a set of possible updates U⊂A and to obtain ƒ(x) without having to recompute it “from scratch” after each change to x. ƒ has the property that there is a kεN such that Δkƒ is 0B on all inputs. If 0≦j≦k and {right arrow over (u)}εUj, the values Δjƒ(x,{right arrow over (u)}) for the current x are known. If additions and subtractions can be performed cheaply, but it is expensive to compute Δjƒ(xnew,{right arrow over (u)}) from xnew (j≧0). On update event xnew:=xcur+u, compute

Δjƒ(xnew,{right arrow over (u)}):=Δjƒ(xcurr,{right arrow over (u)})+Δj+1ƒ(xcurr,{right arrow over (u)},u),

for all 0≦j<k and {right arrow over (u)}εUj. This requires only additions of memorized Δjƒ values. If the sums are computed in order of increasing j, double-buffering may not be necessary but the values can be updated in place, i.e., Δjƒ(x,{right arrow over (u)})+=Δj+1ƒ(x,{right arrow over (u)},u). None of these values have to be recomputed from their (function) definitions. For example, if ƒ(x)=3x2,




  
    
      
        
          
            Δ
            ⁢
            
              
            
            ⁢
            
              f
              ⁡
              
                (
                
                  x
                  ,
                  
                    u
                    1
                  
                
                )
              
            
          
          =
          
          ⁢
          
            
              f
              ⁡
              
                (
                
                  x
                  +
                  
                    u
                    1
                  
                
                )
              
            
            -
            
              f
              ⁡
              
                (
                x
                )
              
            
          
        
      
    
    
      
        
          =
          
          ⁢
          
            
              3
              ⁢
              
                
                  (
                  
                    x
                    +
                    
                      u
                      1
                    
                  
                  )
                
                2
              
            
            -
            
              3
              ⁢
              
                x
                2
              
            
          
        
      
    
    
      
        
          =
          
          ⁢
          
            
              6
              ⁢
              
                u
                1
              
              ⁢
              x
            
            +
            
              3
              ⁢
              
                
                  u
                  1
                  2
                
                .
              
            
          
        
      
    
  




  
    
      
        
          
            
              Δ
              2
            
            ⁢
            
              f
              ⁡
              
                (
                
                  x
                  ,
                  
                    u
                    1
                  
                  ,
                  
                    u
                    2
                  
                
                )
              
            
          
          =
          
          ⁢
          
            
              (
              
                Δ
                ⁢
                
                  
                
                ⁢
                
                  f
                  ⁡
                  
                    (
                    
                      
                        x
                        +
                        
                          u
                          2
                        
                      
                      ,
                      
                        u
                        1
                      
                    
                    )
                  
                
              
              )
            
            -
            
              Δ
              ⁢
              
                
              
              ⁢
              
                f
                ⁡
                
                  (
                  
                    x
                    ,
                    
                      u
                      1
                    
                  
                  )
                
              
            
          
        
      
    
    
      
        
          =
          
          ⁢
          
            
              6
              ⁢
              
                
                  u
                  1
                
                ⁡
                
                  (
                  
                    x
                    +
                    
                      u
                      2
                    
                  
                  )
                
              
            
            +
            
              3
              ⁢
              
                u
                1
                2
              
            
            -
            
              (
              
                
                  6
                  ⁢
                  
                    u
                    1
                  
                  ⁢
                  x
                
                +
                
                  3
                  ⁢
                  
                    u
                    1
                    2
                  
                
              
              )
            
          
        
      
    
    
      
        
          =
          
          ⁢
          
            6
            ⁢
            
              u
              1
            
            ⁢
            
              
                u
                2
              
              .
            
          
        
      
    
  




  
    
      
        
          
            
              Δ
              3
            
            ⁢
            
              f
              ⁡
              
                (
                
                  x
                  ,
                  
                    u
                    1
                  
                  ,
                  
                    u
                    2
                  
                  ,
                  
                    u
                    3
                  
                
                )
              
            
          
          =
          
          ⁢
          
            
              (
              
                
                  Δ
                  2
                
                ⁢
                
                  f
                  ⁡
                  
                    (
                    
                      
                        x
                        +
                        
                          u
                          3
                        
                      
                      ,
                      
                        u
                        1
                      
                      ,
                      
                        u
                        2
                      
                    
                    )
                  
                
              
              )
            
            -
            
              
                Δ
                2
              
              ⁢
              
                f
                ⁡
                
                  (
                  
                    x
                    ,
                    
                      u
                      1
                    
                    ,
                    
                      u
                      2
                    
                  
                  )
                
              
            
          
        
      
    
    
      
        
          =
          
          ⁢
          
            
              6
              ⁢
              
                u
                1
              
              ⁢
              
                u
                2
              
            
            -
            
              6
              ⁢
              
                u
                1
              
              ⁢
              
                u
                2
              
            
          
        
      
    
    
      
        
          =
          
          ⁢
          0.
        
      
    
  



The set of possible updates is U=±1, seven function values are memorized, and values are computed for them from the definitions of ƒ, Δƒ, and Δ2ƒ. |U|0+|U|1+|U|2=7 values are computed from the definitions only once, for the initial x value, and additions of the current seven values are performed. FIG. 1 shows values for x=0, . . . , 4. At any point in time (for any current x value), one row of this table, with seven values, can be held in memory, and x is incremented or decremented by one, which corresponds to changing the memorized row to its predecessor or successor in the table of FIG. 1. To perform this replacement, one other memorized value is added to each memorized value. For example, if x=3 and x is incremented by 1. Then ƒ(x)+=21=48, Δtƒ(x,+1)+=6=27, Δ1ƒ(x, −1)+=−6=−15, and Δ2ƒ(x,•,•)+=0. This memoization technique can be converted to an incremental view maintenance algorithm as follows. The functions ƒ:A→B can be queries, and A can be a domain of databases. For this analogy to hold, the query language L must have two properties: the query language L has to be closed under taking deltas, that is, for each query of L, a delta query has to be expressible in L; L has to support the addition of a query and its delta; and for each query, there must be a kεN such that the k-th delta query returns constantly the empty database.
Given a query ƒ, this technique memorizes a hierarchy of materialized views representing the functions

<u1, . . . ,uj>Δjƒ(x,u1, . . . ,uj)

for 0≦j≦k. x is the current database. The (active) domains of these functions are finite, and their graphs can be captured by group-by queries, grouping the results by the tuples <u1, . . . , uj>. For example, the following SQL query over unary relation R with schema A and multiset semantics can be stated as

Q(R)=select count(*) from R r1,R r2 where r1.A=r2.A 

The delta queries for single-tuple insertions (denoted +R(a)) and deletions (denoted −R(a)) are

ΔQ(R,±R(a))=1±2*

(select count(*) from R where R.A=a)




  
    
      Δ
      2
    
    ⁢
    
      Q
      ⁡
      
        (
        
          R
          ,
          
            
              ±
              1
            
            ⁢
            
              R
              ⁡
              
                (
                
                  a
                  1
                
                )
              
            
          
          ,
          
            
              ±
              2
            
            ⁢
            
              R
              ⁡
              
                (
                
                  a
                  2
                
                )
              
            
          
        
        )
      
    
  
  =
  
    {
    
      
        
          
            
              ±
              1
            
            ⁢
            
              
                ±
                2
              
              ⁢
              2
            
          
        
        
          ...
        
        
          
            
              a
              1
            
            =
            
              a
              2
            
          
        
      
      
        
          0
        
        
          ...
        
        
          otherwise
        
      
    
  




and Δ′Q(R,•,•,•)=0.

The above definitions give Q(Ø)=0 and ΔQ(Ø, ±R(·))=1.














ΔQ(R, •)













Update
R
Q(R)
+R(c)
−R(c)
+R(d)
−R(d)

















Ø
0
1
1
1
1


+R(c)
{c}
1
3
−1
1
1


+R(c)
{c, c}
4
5
−3
1
1


+R(d)
{c, c, d}
5
5
−3
3
−1


+R(c)
{c, c, c, d}
10
7
−5
1
−1


−R(d)
{c, c, c}
9
7
−5
1
1


+R(c)
{c, c, c, c}
16
9
−7
1
1


−R(c)
{c, c, c}
9
7
−5
1
1









In the above table, Δ2Q is omitted for space reasons. It is constant (does not depend on the database), and

Δ2Q(R,+R(a),+R(a))=Δ2Q(R,−R(a),−R(a))=2,

Δ2Q(R,+R(a),−R(a))=Δ2Q(R,−R(a),+R(a))=−2,

and Δ2Q(R,±1R(a1),±2R(a2))=0 for a1≠a2.

Queries are evaluated and the database is accessed to initialize the memorized values. After that, the update rule is used

Δjƒ(xnew,{right arrow over (u)}):=Δjƒ(xcurr,{right arrow over (u)})+Δj+1ƒ(xcurr,{right arrow over (u)},u),

to apply an update u. In this incremental view maintenance technique delta processing is performed recursively along with materializing view Q and using ΔQ to update it. ΔQ is materialized, incrementally maintained using Δ2Q, the result is materialized, and so on.

The query language that is required for the present embodiment encompasses a large class of SQL (aggregate) queries. The algebraic structure of a ring of databases of the present invention includes an addition operation that generalizes relational union, and a multiplication operation that generalizes the natural join. The elements of the ring are generalizations of multi-set relational databases in which tuples can have negative multiplicities (to model deletions). There are further generalizations shown to be necessary in the present embodiment, in addition to a full additive inverse (necessary for delta processing) and distributivity (to yield polynomials). The ring is the basis of the definition of an expressive aggregate query calculus, AGCA, which inherits these key properties. For an AGCA query Q without nested aggregates, Act exists and is structurally strictly simpler than Q. The k-th delta of an AGCA query of degree k without nested aggregates has degree 0; a query of degree 0 only depends on the update but not on the database. Thus, the recursive delta processing technique of the present embodiment is applicable to AGCA. Each query can be incrementally maintained by a hierarchy of materialized views, requiring only basic summations of values in adjacent layers of the view hierarchy to keep all views up to date. Only a single arithmetic operation is required per single-tuple update and per value maintained in this hierarchy of views. For the purpose of aggregate query processing, these values are numerical. In practice, such values can be represented using fixed-size memory words.
The present embodiment can maintain finite active domains of the memorized functions, and can extend these when updates require it. More fundamentally, a j-th delta is a function of a j-tuple of update tuples, which means that its domain and the, for example, tabular representation of the memorized function may become large. The present embodiment can employ query factorization and simplification of queries, which can allow the creation of small representations of the materialized views. The present embodiment includes a method for compiling queries to a simple low-level language which can be, but is not limited to being, related to the programming language C. The present embodiment can eliminate all joins and evaluate queries without resorting to classical query operators. The compiler of the present embodiment produces, for example, source code or object code, that can be edited or simply linked in with existing client application code as, for example, an embeddable query processor.
The notations ƒ:xv, ƒ(x):=v, and ƒ:={xvxεdom(ƒ)} are used interchangeably to define functions, with a preference for the latter when the domain dom(ƒ) might otherwise remain unclear. ƒ|D denotes the restriction of the domain of ƒ to D, i.e. ƒ|D:={(xv)εƒxεD}. A function ƒ: A→B is called surjective if for all bεB, there is an aεA such that ƒ(a)=b.
A semigroup is a pair (A, °) of a base set A and a binary total function ∘:A×A→A (“the operation”) such that a is associative, that is, for all a, b, cεA, (a◯b)∘c=a∘(b∘c). A semigroup is called commutative if a∘b=b∘a for all a, bεA. A monoid (A,∘,e) is a semigroup that has neutral element eεA, that is, a∘e=e∘a=a for all aεA. A monoid is called a group if for each aεA there is an inverse element a−1εA such that a∘a−1=a−1∘a=e.
A ring over base set A is a tuple (A,+,*,0) with two operations + and * (called addition and multiplication, respectively) such that (A,+,0) is a commutative group, (A,*) is a semigroup, and + and * are distributive, that is, a*(b+c)=a*b+a*c and (a+b)*c=a*c+b*c for all a, b, cεA. A ring with identity (A,+,*,0,1) is a ring in which (A,*,1) is a monoid. A ring is called commutative if * is commutative. For example, the integers Z and the rational numbers Q form commutative rings with identity (Z,+,*,0,1) and (Q,+,*,0,1). The natural numbers N do not form a ring because there is no additive inverse; for example, there is no natural number x such that 1+x=0.
Neutral elements 0 and 1 are named by analogy and are not necessarily numbers. For a group with an operation +, −a denotes a−1 and the shortcut a−b denotes a+(−b). When the operations + and * are clear from the context, the name of the base set is used herein to denote the ring (e.g., Z for (Z,+,*,0,1)). In a monoid, there is a unique identity element and in a group (A,∘,e), there is a unique inverse element a−1 for each element aεA. Thus, in particular, a ring is uniquely determined by its base set and its operations + and *.
A is a commutative ring such as the integers Z, the rational numbers Q, or the real numbers R. A structure (ARel,+,*,0,1) (or just ARel, for short) of generalized multiset relations denotes collections of tuples with multiplicities from A and possibly differing schemas. The operations + and * are generalizations of multiset union and natural join, respectively, to total functions (i.e., applicable to any pair of elements of ARel). The schema polymorphism of tuples in generalized multiset relations serves the purpose of accommodating such total operator definitions.
A (typed) tuple {right arrow over (t)} is a partial function from a vocabulary of column names dom({right arrow over (t)}) to data values (that is, {right arrow over (t)} is not just a tuple of values but has an associated schema of its own). In the present description, all tuples are typed, and Tup denotes the set of all typed tuples. {{right arrow over (t)}} denotes construct a singleton relation with schema sch({{right arrow over (t)}})=dom({right arrow over (t)}) from {right arrow over (t)} and the classical natural join operator  is used on such single tons. The set of all singletons is denoted by Sng (i.e., Sng:={{{right arrow over (t)}}|{right arrow over (t)}εTup}). For a commutative ring A, a generalized multiset relation (gmr) is a function R: Tup→A such that R({right arrow over (t)})≠0 for at most a finite number of tuples {right arrow over (t)}. The set of all such functions is denoted by ARel. Such a function indicates the multiplicity with which each tuple of Tup occurs in the gmr. Tuples can have negative multiplicities. The operations + and * of ARel are defined as follows.



  
    
      For
      ⁢
      
        
      
      ⁢
      R
    
    ,
    
      S
      ∈
      
        A
        Rel
      
    
    ,
    
      

    
    ⁢
    
      R
      +
      
        S
        :
        
          
            x
            ->
          
          ↦
          
            (
            
              
                R
                (
                
                  x
                  ->
                
                )
              
              +
              
                S
                (
                
                  x
                  ->
                
                )
              
            
            )
          
        
      
    
  




  
    R
    *
    
      S
      :
      
        
          x
          ->
        
        ↦
        
          
            ∑
            
              
                {
                
                  x
                  _
                
                }
              
              =
              
                
                  {
                  
                    a
                    _
                  
                  }
                
                ⁢
                                ⁢
                
                  {
                  
                    b
                    _
                  
                  }
                
              
            
          
          ⁢
          
            
              R
              (
              
                a
                ->
              
              )
            
            *
            
              S
              (
              
                b
                ->
              
              )
            
          
        
      
    
  



It follows:




  
    
      (
      
        -
        R
      
      )
    
    :
    
      
        x
        ->
      
      ↦
      
        (
        
          -
          
            R
            ⁡
            
              (
              
                x
                ->
              
              )
            
          
        
        )
      
    
  




  
    1
    :
    
      
        x
        ->
      
      ↦
      
        {
        
          
            
              
                
                  1
                
                
                  ...
                
                
                  
                    
                      x
                      ->
                    
                    =
                    
                      〈
                      
                        
                      
                      〉
                    
                  
                
              
              
                
                  0
                
                
                  ...
                
                
                  
                    
                      x
                      ->
                    
                    ≠
                    
                      〈
                      
                        
                      
                      〉
                    
                  
                
              
            
            ⁢
            
              

            
            ⁢
            0
          
          :
          
            
              x
              ->
            
            ↦
            0
          
        
      
    
  



On classical multiset relations (where all multiplicities are ≧0 and all tuples with multiplicity>0 have the same schema), * is the usual multiset natural join operation.
Given the three gmrs of ARel




  
    
      
        
          
            R
          
          
            A
          
          
            B
          
          
            
              
            
          
          
            
              
            
          
          
            S
          
          
            C
          
          
            
              
            
          
          
            T
          
          
            B
          
          
            C
          
          
            
              
            
          
          
            
              
            
          
        
        
          
            
              
            
          
          
            
              a
              1
            
          
          
            
              
            
          
          
            ↦
          
          
            
              r
              1
            
          
          
            
              
            
          
          
            c
          
          
            ↦
          
          
            s
          
          
            
              
            
          
          
            c
          
          
            ↦
          
          
            
              t
              1
            
          
        
        
          
            
              
            
          
          
            
              a
              2
            
          
          
            b
          
          
            ↦
          
          
            
              r
              2
            
          
          
            
              
            
          
          
            
              
            
          
          
            
              
            
          
          
            
              
            
          
          
            b
          
          
            c
          
          
            ↦
          
          
            
              t
              2
            
          
        
      
    
    
      
        Example
        ⁢
        
          
        
        ⁢
        1
      
    
  




over column name vocabulary Σ={A,B,C} and value domain adorn={a1,a2,b,c}. For example, in multiset relation R, two tuples of different schema have a multiplicity other than 0. These two tuples can be specified as partial functions Σ→adorn: {Aa1} and {Aa2; Bb}.

Then S+T and R*(S+T) are as follows:




  
    
      
        
          S
          +
          T
        
      
      
        B
      
      
        C
      
      
        
          
        
      
    
    
      
        
          
        
      
      
        
          
        
      
      
        c
      
      
        
          ❘
          
            ->
            
              s
              +
              
                t
                l
              
            
          
        
      
    
    
      
        
          
        
      
      
        b
      
      
        c
      
      
        
          ❘
          
            ->
            
              t
              2
            
          
        
      
    
  




  
    
      
        
          R
          *
          
            (
            
              S
              +
              T
            
            )
          
        
      
      
        A
      
      
        B
      
      
        C
      
      
        
          
        
      
      
        
          
        
      
    
    
      
        
          
        
      
      
        
          a
          1
        
      
      
        
          
        
      
      
        c
      
      
        ↦
      
      
        
          
            r
            1
          
          *
          
            (
            
              s
              +
              
                t
                1
              
            
            )
          
        
      
    
    
      
        
          
        
      
      
        
          a
          1
        
      
      
        b
      
      
        c
      
      
        ↦
      
      
        
          
            r
            1
          
          *
          
            t
            2
          
        
      
    
    
      
        
          
        
      
      
        
          a
          2
        
      
      
        b
      
      
        c
      
      
        ↦
      
      
        
          
            
              r
              2
            
            *
            
              (
              
                s
                +
                
                  t
                  1
                
              
              )
            
          
          +
          
            
              r
              2
            
            *
            
              t
              2
            
          
        
      
    
  



The missing values should not be taken as SQL null values, and * is not an outer join. (ARel,+,*,0,1) is a commutative ring with identity.

AGCA defines two forms of query expressions, formulae and terms. Formulae evaluate to elements of the ring (ARel,+,*,0,1) of gmrs. Terms evaluate to elements of the ring (A,+,*,0,1). Terms and formulae are connected mutually recursively, creating a powerful language for expressing aggregate queries. Both formulae and terms, and thus the overall query language, inherit the key properties of polynomial rings in that they have an additive inverse, a normal point of polynomial expressions, and admit a form of factorization. These properties are the basis of delta processing and incremental query evaluation in subsequent sections. Formulae are expressions built from atomic formulae using +, −, and *. The atomic formulae are true, false, relational atoms R({right arrow over (x)}) where {right arrow over (x)} is a tuple of variables, and atomic conditions of the form t θ 0 comparing term t with 0 using comparison operations θ from =, ≠, >, ≧, <, and ≦. AGCA terms are built from variables, built-in function calls (constants are functions with zero arguments), and aggregate sums (Sum) using addition, its inverse, and multiplication. Built-in functions compute their result entirely based on their input terms, not accessing the database. The abstract syntax of formulae φ and terms t (given variables x, relation names R, comparison operators θ, and constants/builtin functions ƒ) can be given by the EBNF

φ::−φ*φ|φ+φ|−φ|true|false|R({right arrow over (x)})|tθ0

t::−t*t|t+t|−t|ƒ(t*)|x|Sum(t,φ)

xθy can be written as (x−y)θ0. Formulae φ*ψ*π can be read as (φ*ψ)*π. The formal semantics of AGCA is given by mutually recursive functions [[•]]F(•,•) and [[•]]T(•,•) for formulae and terms, respectively. Given database A and a bound variable tuple {right arrow over (b)}(i.e., a function that maps each bound variable to a value), [[φ]]F(A,{right arrow over (b)}) evaluates to an element of ARel and [[t]]T(A,{right arrow over (b)}) evaluates to a value from A. The semantics of AGCA formulae is defined as follows.





  
    
      
        
          
        
        ⁢
        
          
            
              
                [
                
                  [
                  
                    φ
                    +
                    ψ
                  
                  ]
                
                ]
              
              F
            
            ⁢
            
              (
              
                A
                ,
                
                  b
                  ->
                
              
              )
            
          
          :=
          
            
              
                
                  
                    [
                    
                      [
                      φ
                      ]
                    
                    ]
                  
                  F
                
                ⁢
                
                  (
                  
                    A
                    ,
                    
                      b
                      ->
                    
                  
                  )
                
              
              ⁢
              
                +
                
                  A
                  Rel
                
              
              ⁢
              
                
                  
                    [
                    
                      [
                      ψ
                      ]
                    
                    ]
                  
                  F
                
                ⁢
                
                  
                    
                      (
                      
                        A
                        ,
                        
                          b
                          ->
                        
                      
                      )
                    
                    ⁢
                    
                      

                    
                    ⁢
                    
                      
                    
                    [
                    
                      [
                      
                        -
                        φ
                      
                      ]
                    
                    ]
                  
                  F
                
                ⁢
                
                  (
                  
                    A
                    ,
                    
                      b
                      ->
                    
                  
                  )
                
              
            
            :=
            
              
                
                  
                    -
                    
                      A
                      Rel
                    
                  
                  ⁢
                  
                    
                      [
                      
                        [
                        φ
                        ]
                      
                      ]
                    
                    F
                  
                
                ⁢
                
                  
                    
                      (
                      
                        A
                        ,
                        
                          b
                          ->
                        
                      
                      )
                    
                    ⁢
                    
                      

                    
                    ⁢
                    
                      
                    
                    [
                    
                      [
                      
                        φ
                        *
                        ψ
                      
                      ]
                    
                    ]
                  
                  F
                
                ⁢
                
                  (
                  
                    A
                    ,
                    
                      b
                      ->
                    
                  
                  )
                
              
              :=
              
                
                  
                    [
                    
                      [
                      φ
                      ]
                    
                    ]
                  
                  F
                
                ⁢
                
                  (
                  
                    A
                    ,
                    
                      b
                      ->
                    
                  
                  )
                
                ⁢
                
                  *
                  
                    A
                    Rel
                  
                
                ⁢
                
                  
                    [
                    
                      [
                      ψ
                      ]
                    
                    ]
                  
                  F
                
                ⁢
                
                  (
                  
                    A
                    ,
                    
                      b
                      ->
                    
                  
                  )
                
              
            
          
        
      
    
    
      
        
      
    
  
  
    
      
        
          
        
        ⁢
        
          
            
              
                [
                
                  [
                  true
                  ]
                
                ]
              
              F
            
            ⁢
            
              (
              
                A
                ,
                
                  b
                  ->
                
              
              )
            
          
          :=
          
            
              
                
                  
                    1
                    
                      A
                      Rel
                    
                  
                  ⁢
                  
                    

                  
                  ⁢
                  
                    
                  
                  [
                  
                    [
                    false
                    ]
                  
                  ]
                
                F
              
              ⁢
              
                (
                
                  A
                  ,
                  
                    b
                    ->
                  
                
                )
              
            
            :=
            
              0
              
                A
                Rel
              
            
          
        
      
    
    
      
        
      
    
  
  
    
      
        
          
            
              [
              
                [
                
                  φ
                  *
                  
                    (
                    
                      
                        x
                        -
                        y
                      
                      =
                      0
                    
                    )
                  
                
                ]
              
              ]
            
            F
          
          ⁢
          
            (
            
              A
              ,
              
                b
                ->
              
            
            )
          
        
        :=
        
          
            x
            ->
          
          ↦
          
            {
            
              
                
                  
                    
                      
                        
                          [
                          
                            [
                            φ
                            ]
                          
                          ]
                        
                        F
                      
                      ⁢
                      
                        (
                        
                          A
                          ,
                          
                            b
                            ->
                          
                        
                        )
                      
                      ⁢
                      
                        (
                        
                          x
                          ->
                        
                        )
                      
                    
                    +
                  
                
                
                  
                    
                  
                
                
                  
                    
                  
                
              
              
                
                  
                    
                      
                        
                          [
                          
                            [
                            φ
                            ]
                          
                          ]
                        
                        F
                      
                      ⁢
                      
                        (
                        
                          A
                          ,
                          
                            b
                            ->
                          
                        
                        )
                      
                      ⁢
                      
                        (
                        
                          
                            x
                            ->
                          
                          ⁢
                          
                            ❘
                            
                              
                                dom
                                (
                                
                                  x
                                  ->
                                
                                )
                              
                              ⁢
                              \
                              ⁢
                              
                                {
                                y
                                }
                              
                            
                          
                        
                        )
                      
                    
                    +
                  
                
                
                  
                    
                  
                
                
                  
                    
                  
                
              
              
                
                  
                    
                      
                        [
                        
                          [
                          φ
                          ]
                        
                        ]
                      
                      F
                    
                    ⁢
                    
                      (
                      
                        A
                        ,
                        
                          b
                          ->
                        
                      
                      )
                    
                    ⁢
                    
                      (
                      
                        
                          x
                          ->
                        
                        ⁢
                        
                          ❘
                          
                            
                              dom
                              ⁡
                              
                                (
                                
                                  x
                                  ->
                                
                                )
                              
                            
                            ⁢
                            \
                            ⁢
                            
                              {
                              x
                              }
                            
                          
                        
                      
                      )
                    
                  
                
                
                  ...
                
                
                  
                    
                      
                        [
                        
                          [
                          t
                          ]
                        
                        ]
                      
                      T
                    
                    ⁢
                    
                      (
                      
                        A
                        ,
                        
                          
                            b
                            ->
                          
                          ⋃
                          
                            x
                            ->
                          
                        
                      
                      )
                    
                    ⁢
                    θ0
                  
                
              
              
                
                  
                    
                  
                
                
                  
                    
                  
                
                
                  
                    
                      and
                      ⁢
                      
                        
                      
                      ⁢
                      
                        {
                        
                          b
                          ->
                        
                        }
                      
                      ⁢
                                            ⁢
                      
                        {
                        
                          x
                          ->
                        
                        }
                      
                    
                    ≠
                    Ø
                  
                
              
              
                
                  0
                
                
                  ...
                
                
                  otherwise
                
              
            
          
        
      
    
    
      
        
      
    
  
  
    
      
        
          
            
              
                
                  
                    [
                    
                      [
                      
                        φ
                        *
                        
                          (
                          
                            t
                            ⁢
                            
                              
                            
                            ⁢
                            θ0
                          
                          )
                        
                      
                      ]
                    
                    ]
                  
                  F
                
                ⁢
                
                  (
                  
                    A
                    ,
                    
                      b
                      ->
                    
                  
                  )
                
              
              :=
              
                (
                
                  
                
                ⁢
                
                  
                    
                      *
                      t
                      ⁢
                      
                        
                      
                      ⁢
                      θ0
                      ⁢
                      
                        
                      
                      ⁢
                      is
                      ⁢
                      
                        
                      
                      ⁢
                      not
                      ⁢
                      
                        
                      
                      ⁢
                      of
                      ⁢
                      
                        
                      
                      ⁢
                      form
                      ⁢
                      
                        
                      
                      ⁢
                      x
                    
                    -
                    y
                  
                  =
                  0
                
              
            
            ⁢
            *)
          
          ⁢
          
            x
            ->
          
        
        ↦
        
          {
          
            
              
                
                  
                    
                      [
                      
                        [
                        φ
                        ]
                      
                      ]
                    
                    F
                  
                  ⁢
                  
                    (
                    
                      A
                      ,
                      
                        b
                        →
                      
                    
                    )
                  
                  ⁢
                  
                    (
                    
                      x
                      →
                    
                    )
                  
                
              
              
                ...
              
              
                
                  
                    
                      [
                      
                        [
                        t
                        ]
                      
                      ]
                    
                    T
                  
                  ⁢
                  
                    (
                    
                      A
                      ,
                      
                        
                          b
                          ->
                        
                        ⋃
                        
                          x
                          ->
                        
                      
                    
                    )
                  
                  ⁢
                  θ0
                
              
            
            
              
                
                  
                
              
              
                
                  
                
              
              
                
                  
                    and
                    ⁢
                    
                      
                    
                    ⁢
                    
                      {
                      
                        b
                        ->
                      
                      }
                    
                    ⁢
                                        ⁢
                    
                      {
                      
                        x
                        ->
                      
                      }
                    
                  
                  ≠
                  Ø
                
              
            
            
              
                0
              
              
                ...
              
              
                otherwise
              
            
          
        
      
    
    
      
        
      
    
  
  
    
      
        
          
            
              [
              
                [
                
                  R
                  ⁡
                  
                    (
                    
                      
                        x
                        1
                      
                      ,
                      ...
                      ⁢
                      
                        
                      
                      ,
                      
                        x
                        k
                      
                    
                    )
                  
                
                ]
              
              ]
            
            F
          
          ⁢
          
            (
            
              A
              ,
              
                b
                ->
              
            
            )
          
        
        :=
        
          
            x
            ->
          
          ↦
          
            {
            
              
                
                  
                    
                      R
                      A
                    
                    ⁡
                    
                      (
                      
                        y
                        ->
                      
                      )
                    
                  
                
                
                  ...
                
                
                  
                    
                      
                        
                          {
                          
                            b
                            ->
                          
                          }
                        
                        ⁢
                                                ⁢
                        
                          {
                          
                            x
                            ->
                          
                          }
                        
                      
                      ≠
                      Ø
                    
                    ,
                  
                
              
              
                
                  
                    
                  
                
                
                  
                    
                  
                
                
                  
                    
                      
                        dom
                        ⁡
                        
                          (
                          
                            x
                            ->
                          
                          )
                        
                      
                      =
                      
                        {
                        
                          
                            x
                            1
                          
                          ,
                          ...
                          ⁢
                          
                            
                          
                          ,
                          
                            x
                            k
                          
                        
                        }
                      
                    
                    ,
                  
                
              
              
                
                  
                    
                  
                
                
                  
                    
                  
                
                
                  
                    
                      
                        sch
                        ⁡
                        
                          (
                          R
                          )
                        
                      
                      =
                      
                        〈
                        
                          
                            A
                            1
                          
                          ,
                          ...
                          ⁢
                          
                            
                          
                          ,
                          
                            A
                            k
                          
                        
                        〉
                      
                    
                    ,
                    and
                  
                
              
              
                
                  
                    
                  
                
                
                  
                    
                  
                
                
                  
                    
                      y
                      ->
                    
                    =
                    
                      {
                      
                        
                          
                            A
                            i
                          
                          ↦
                          
                            
                              x
                              ->
                            
                            ⁡
                            
                              (
                              
                                x
                                i
                              
                              )
                            
                          
                        
                        ❘
                        
                          1
                          ≤
                          i
                          ≤
                          k
                        
                      
                      }
                    
                  
                
              
              
                
                  0
                
                
                  ...
                
                
                  
                    otherwise
                    .
                  
                
              
            
          
        
      
    
    
      
        
      
    
  




Typed tuples are functions: {{right arrow over (b)}}  {{right arrow over (x)}}≠Ø asserts that the typed tuples {right arrow over (b)} and {right arrow over (x)} are consistent, that is, for all yεdom({right arrow over (b)})∩dom({right arrow over (x)}), {right arrow over (b)}(y)={right arrow over (x)}(y). The union {right arrow over (b)}∩{right arrow over (x)} extends one typed tuple by the other. The definition of [[R(x1, . . . ,xk)]]F supports column renaming. For example, if R is a database relation




  
    
      
        
          R
          A
        
      
      
        a
      
      
        b
      
      
        
          
        
      
      
        
          
        
      
    
    
      
        
          
        
      
      
        
          a
          1
        
      
      
        
          b
          1
        
      
      
        ↦
      
      
        
          r
          1
        
      
    
    
      
        
          
        
      
      
        
          a
          2
        
      
      
        
          b
          2
        
      
      
        ↦
      
      
        
          r
          2
        
      
    
  




  then




  
    
      
        
          
            
              [
              
                [
                
                  R
                  ⁡
                  
                    (
                    
                      x
                      ,
                      y
                    
                    )
                  
                
                ]
              
              ]
            
            F
          
          ⁢
          
            (
            
              A
              ,
              
                {
                
                  y
                  ↦
                  
                    b
                    1
                  
                
                }
              
            
            )
          
        
      
      
        x
      
      
        y
      
      
        
          
        
      
      
        
          
        
      
    
    
      
        
          
        
      
      
        
          a
          1
        
      
      
        
          b
          1
        
      
      
        ↦
      
      
        
          r
          1
        
      
    
  



The query renames the columns (a,b) to (x,y) and selects on y since it is a bound variable.

For example, if a1, . . . ,a5εA=Z and



  
    
      
        
          
            
              [
              
                [
                φ
                ]
              
              ]
            
            F
          
          ⁢
          
            (
            
              A
              ,
              Ø
            
            )
          
        
      
      
        x
      
      
        y
      
      
        
          
        
      
      
        
          
        
      
    
    
      
        
          
        
      
      
        1
      
      
        
          
        
      
      
        ↦
      
      
        
          a
          1
        
      
    
    
      
        
          
        
      
      
        
          
        
      
      
        1
      
      
        ↦
      
      
        
          a
          2
        
      
    
    
      
        
          
        
      
      
        1
      
      
        1
      
      
        ↦
      
      
        
          a
          3
        
      
    
    
      
        
          
        
      
      
        1
      
      
        2
      
      
        ↦
      
      
        
          a
          4
        
      
    
  




  then




  
    
      
        
          
            
              [
              
                [
                
                  φ
                  *
                  
                    (
                    
                      x
                      <
                      y
                    
                    )
                  
                
                ]
              
              ]
            
            F
          
          ⁢
          
            (
            
              A
              ,
              Ø
            
            )
          
        
      
      
        x
      
      
        y
      
      
        
          
        
      
      
        
          
        
      
    
    
      
        
          
        
      
      
        1
      
      
        2
      
      
        ↦
      
      
        
          a
          4
        
      
    
  




  
    
      
        
          
            
              [
              
                [
                
                  φ
                  *
                  
                    (
                    
                      x
                      =
                      y
                    
                    )
                  
                
                ]
              
              ]
            
            F
          
          ⁢
          
            (
            
              A
              ,
              Ø
            
            )
          
        
      
      
        x
      
      
        y
      
      
        
          
        
      
      
        
          
        
      
    
    
      
        
          
        
      
      
        1
      
      
        1
      
      
        ↦
      
      
        
          
            a
            1
          
          +
          
            a
            2
          
          +
          
            a
            3
          
        
      
    
  



For example, for {right arrow over (x)}={x1; y1} and {right arrow over (b)}Ø, [[x−y]]T(A,{right arrow over (b)}∪{right arrow over (x)})=[[x]]T(A,{right arrow over (b)}∪{right arrow over (x)})−[[y]]T(A,{right arrow over (b)}∪{right arrow over (x)})=1−1=0 and {{right arrow over (b)}}{{right arrow over (x)}}={{right arrow over (x)}}, thus

[[φ*(x=y)]]F(A,{right arrow over (b)})({right arrow over (x)})=[[φ]]F(A,{right arrow over (b)})({x1})+[[φ]]F(A,{right arrow over (b)})({y1})+[[φ]]F(A,{right arrow over (b)})({right arrow over (x)})=a1+a2+a3.

The semantics of AGCA terms is defined as





  
    
      
        
          
        
        ⁢
        
          
            
              
                [
                
                  [
                  x
                  ]
                
                ]
              
              T
            
            ⁢
            
              (
              
                A
                ,
                
                  b
                  ->
                
              
              )
            
          
          :=
          
            {
            
              
                
                  
                    
                      b
                      ->
                    
                    ⁡
                    
                      (
                      x
                      )
                    
                  
                
                
                  ...
                
                
                  
                    x
                    ∈
                    
                      dom
                      (
                      
                        b
                        ->
                      
                      )
                    
                  
                
              
              
                
                  fail
                
                
                  ...
                
                
                  otherwise
                
              
            
          
        
      
    
    
      
        
      
    
  
  
    
      
        
          
        
        ⁢
        
          
            
              
                [
                
                  [
                  
                    f
                    ⁡
                    
                      (
                      
                        
                          t
                          1
                        
                        ,
                        ...
                        ⁢
                        
                          
                        
                        ,
                        
                          t
                          k
                        
                      
                      )
                    
                  
                  ]
                
                ]
              
              T
            
            ⁢
            
              (
              
                A
                ,
                
                  b
                  ->
                
              
              )
            
          
          :=
          
            f
            (
            
              
                
                  
                    [
                    
                      [
                      
                        t
                        1
                      
                      ]
                    
                    ]
                  
                  T
                
                ⁢
                
                  (
                  
                    A
                    ,
                    
                      b
                      ->
                    
                  
                  )
                
              
              ,
              ...
              ⁢
              
                
              
              ,
              
                
                  
                    [
                    
                      [
                      
                        t
                        k
                      
                      ]
                    
                    ]
                  
                  T
                
                ⁢
                
                  (
                  
                    A
                    ,
                    
                      b
                      ->
                    
                  
                  )
                
              
            
            )
          
        
      
    
    
      
        
      
    
  
  
    
      
        
          
            
              [
              
                [
                
                  Sum
                  ⁡
                  
                    (
                    
                      t
                      ,
                      φ
                    
                    )
                  
                
                ]
              
              ]
            
            T
          
          ⁢
          
            (
            
              A
              ,
              
                b
                ->
              
            
            )
          
        
        :=
        
          
            ∑
            
              
                
                  (
                  
                    
                      x
                      ->
                    
                    ↦
                    a
                  
                  )
                
                ∈
                
                  
                    
                      [
                      
                        [
                        φ
                        ]
                      
                      ]
                    
                    F
                  
                  ⁢
                  
                    (
                    
                      A
                      ,
                      
                        b
                        ->
                      
                    
                    )
                  
                
              
              ,
              
                
                  
                    {
                    
                      b
                      ->
                    
                    }
                  
                  ⁢
                                    ⁢
                  
                    {
                    
                      x
                      ->
                    
                    }
                  
                
                ≠
                Ø
              
            
          
          ⁢
          
            a
            ⁢
            
              *
              A
            
            ⁢
            
              
                [
                
                  [
                  t
                  ]
                
                ]
              
              T
            
            ⁢
            
              (
              
                A
                ,
                
                  
                    b
                    ->
                  
                  ⋃
                  
                    x
                    ->
                  
                
              
              )
            
          
        
      
    
    
      
        
      
    
  




Take +, −, and * as binary built-in functions ƒ(+A, −A, and *A) and constants as nullary built-in functions and the definition is complete. For example, if gmr R of the previous example is,





  
    
      
        
          
            [
            
              [
              
                Sum
                ⁡
                
                  (
                  
                    
                      3
                      *
                      x
                    
                    ,
                    
                      R
                      ⁡
                      
                        (
                        
                          x
                          ,
                          y
                        
                        )
                      
                    
                  
                  )
                
              
              ]
            
            ]
          
          ⁢
          
            (
            
              A
              ,
              Ø
            
            )
          
        
        =
        
        ⁢
        
          
            
              r
              1
            
            *
            
              
                [
                
                  [
                  
                    3
                    *
                    x
                  
                  ]
                
                ]
              
              T
            
            ⁢
            
              (
              
                A
                ,
                
                  {
                  
                    x
                    ↦
                    
                      a
                      1
                    
                  
                  }
                
              
              )
            
          
          +
        
      
    
  
  
    
      
        
        ⁢
        
          
            r
            2
          
          *
          
            
              [
              
                [
                
                  3
                  *
                  x
                
                ]
              
              ]
            
            T
          
          ⁢
          
            (
            
              A
              ,
              
                {
                
                  x
                  ↦
                  
                    a
                    2
                  
                
                }
              
            
            )
          
        
      
    
  
  
    
      
        =
        
        ⁢
        
          
            
              r
              1
            
            *
            3
            *
            
              a
              1
            
          
          +
          
            
              r
              2
            
            *
            3
            *
            
              
                a
                2
              
              .
            
          
        
      
    
  




Note that [[•]]F maps to elements of ARel; in particular, the image elements are functions Tup→A that are nonzero on at most a finite number of inputs. This is ensured by syntactically requiring each condition to be part of a product φ*(tθ0) with another formula φ that guards its domain. (In the case that a condition tθ0 is not the second argument of a multiplication, read it as formula “true*(tθ0)”.) The evaluation of variables x using [[x]]T can fail if they are not bound at evaluation time. Illegal queries can be excluded from AGCA. A definition of range-restriction completely analogous to that for relational calculus, with A and v replaced by * and +, respectively, allows to statically verify that such runtime failures do not happen. A SQL aggregate query

    SELECT {right arrow over (b)}, SUM(t) FROM R1 r11, R1 r12, . . . , R2 r21, . . .
    WHERE φ GROUP BY {right arrow over (b)}
    is expressed in AGCA as

Sum(t,R1({right arrow over (x)}11)*R1({right arrow over (x)}12)* . . . *R2({right arrow over (x)}21)* . . . *φ)

with bound variables {right arrow over (b)}. While Sum(•,•) computes exactly one number, we can think of an SQL aggregate query with group by clause as a function {right arrow over (b)}[[Sum(•,•)]](A,{right arrow over (b)}) that maps each group to its aggregate value. For example, relation C(cid, nation) stores the ids and nationalities of customers. The SQL query

    SELECT C1.cid, SUM(1) FROM C C1, C C2
    WHERE C1.nation=C2.nation
    GROUP BY C1.cid;
    asks, for each cid, for the number of customers of the same nation (including the customer identified by cid). This query translates to AGCA as

Sum(1,C(c1,n1)*C(c2,n2)*(n1=n2))

with bound variable c1. A language of expressions can be constructed from values (“constants”) and the operations + and * of a ring A, plus variables. If these expressions evaluate to elements of a multivariate polynomial ring in the natural way, turning an expression into a polynomial, that is, a sum of flat products (the products are also known as monomials), means applying distributivity repeatedly until the result is a polynomial. This can be combined with simplification operations based on the 1 and 0-elements and the additive inverse, i.e., α*1 maps to α,α*0 maps to 0, α+0 maps to α, α+(−α) maps to 0, −(−α)=α, and (−α)*β=−(α*β). Such an algorithm for computing and simplifying expressions over a ring is applicable to AGCA formulae and terms. In particular, the operation * is distributive with + despite its sideways information passing semantics. For arbitrary terms s and t and formulae φ and ψ, Sum terms can be simplified using the following equations (to be applied by replacing a left by a right hand side expression):

Sum(s+t,φ)=Sum(s,φ)+Sum(t,φ)

Sum(t,φ+ψ)=Sum(t,φ)+Sum(t,ψ)

Sum(−t,φ)=−Sum(t,φ)

Sum(t,−φ)=−Sum(t,φ)

Sum(t,true)=t 

Sum(t,false)=0

Sum(0,φ)=0.



A term that contains neither + nor − anywhere is referred to as recursively monomial. The following result is based on an algorithm that rewrites an input term in a bottom-up pass using the above rules. Each AGCA term is equivalent to a finite sum±t1±t2 . . . ±tm where t1, . . . , tm are recursively monomial terms. For example, Sum(t, (−φ)*((−ψ)+π)) simplifies to the recursively monomial Sum(t,φ*ψ)−Sum(t,φ*π). When e is either a formula or a term, vars(e) be the set of all variables occurring in e. Factorization employs the equivalence

Sum(s*t,φ*ψ)=Sum(s,φ)*Sum(t,ψ)

which is true if (vars(s)∪vars(φ))∩(vars(t)∪vars(ψ))=Ø. A monomial aggregate term can be maximally factorized in linear time in its size. For example, the term Sum(5*x*Sum(1,R(y,z))*w,R(x,y)*R(v,w)) factorizes as Sum(5,true)*Sum(x*Sum(1,R(y,z)),R(x,y))*Sum(w,R(v,w)). Recursive factorization, given term Sum(t,φ), recursively, for example, from the bottom-up, factorizes the aggregate terms in t before applying factorization as just described to Sum(t,φ) itself. Unneeded variables can be eliminated from queries, variables that are neither bound nor do they occur in relation atoms, but obtain their values through condition atoms that equate them with other variables. For a AGCA term Sum(t,φ) and a set of bound variables B, then there is an equivalent term Sum(t′,v) such that each variable in ψ either occurs in a relational atom R({right arrow over (x)}) of ψ or in B. Given an arbitrary monomial formula φ with variables vars(φ) and a set of bound variables B, there is a monomial formula ψ with variables vars(ψ) such that each variable in vars(ψ) either occurs in a relational atom R({right arrow over (x)}) of ψ or in B and ∃(vars(φ)−B)φ is equivalent to ∃(vars(ψ)−B)ψ. Variables are eliminated that occur in constraint atoms xθy but neither in relational atoms nor in B. If φ=Λ(E∪O), where E is the set of equality atoms x=y and O is the set of remaining atoms (either set may be empty), and a set of bound variables, redundant variables are eliminated as follows. For each equivalence class C of the equivalence relation E, an element (i.e., variable) can be distinguished as xC such that, if B∩C≠Ø, xC is an arbitrary element of B∩C; otherwise, it is an arbitrary element of C. A unification mapping Θ can be created that maps each unbound variable y of E to x|y| (where [y] is the equivalence class of y) and is the identity on the bound variables. Variables in O can be substituted using Θ, obtaining O′. If E′=∪{y=x|y||yε((B∩[y])−{x|y|})}, then φ can be replaced by (ΛO′)ΛE′. This can eliminate redundant variables involved in equalities. Variables involved in inequality constraints can occur either in B or in a relational atom, otherwise φ has to be unsafe.

In the case where Sum(t,φ) (where φ is a monomial) and bound variables are defined, variables can be eliminated by first eliminating variables in φ, creating φ′ and Θ. Then variables of t can be substituted that are in the domain of Θ using Θ, obtaining t′. The result, Sum(t′,φ′), is equivalent to Sum(t,φ).
In the language of the present embodiment, NC0C, for incremental update-event processing that admits massive parallelization, state is represented by finite map data structures (associative arrays), mapping tuples to numbers. NC0C can be a restricted imperative language, similar to C both in syntax and semantics. There are at least two differences between NC0C and C. The first is in the initialization of undefined map values, which uses special syntax to account for the fact that NC0C procedures are update triggers for the map data structures. The second is in looping, which is performed over a set of values that is intentionally not made explicit in the syntax of NC0C (for example, it can be a form of constrained structural recursion over the map—and thus can admit parallelization). AGCA queries can be compiled to NC0C programs that perform incremental view maintenance. These compiled programs only use the maps to represent the view and auxiliary data; no database beyond these is accessed. A map (read) access, where m is a map name, is written as m[{right arrow over (x)}]. An NC0C term is an arithmetic expression built from variables, constants, map accesses, functional ifs of the form if φ then t else 0 or, using C syntax, (φ?t:0), and arithmetic operations + and *. A condition φ is a Boolean combination of (in)equalities over variables and constants. An NC0C trigger is of the form

on ±R({right arrow over (x)}{right arrow over (y)}){s1; . . . ;sk}

where ± indicates insertion respectively deletion, R is a relation name, {right arrow over (x)}{right arrow over (y)} are variables (the trigger arguments), and s1, . . . , sk are NC0C statements of the form

for each {right arrow over (z)} do m[{right arrow over (x)}{right arrow over (z)}]<tinit>±=t 

where {right arrow over (z)} are variables distinct from {right arrow over (x)}{right arrow over (y)}, t is an NC0C term, and tinit is an NC0C term without map accesses that uses only variables from {right arrow over (x)}{right arrow over (z)}, called the initializer of m[{right arrow over (x)}{right arrow over (z)}]. If tinit is 0, it can be omitted and m[{right arrow over (x)}{right arrow over (z)}] can be written rather than m[{right arrow over (x)}{right arrow over (z)}]<0>. If m1[{right arrow over (v)}1], . . . , mk[{right arrow over (v)}k] are the map accesses in the right-hand side term t, then m, m1, . . . , mk must be pairwise distinct and the variables in {right arrow over (v)}1, . . . , {right arrow over (v)}k must be a nonoverlapping subsets of the variables in {right arrow over (x)}{right arrow over (y)}{right arrow over (z)}. Statements of the form (1) can be abbreviated with {right arrow over (z)}=< > as

m[{right arrow over (x)}]<tinit>±t. 

An NC0C program can include of a set of triggers, one for each update event ±R. The semantics of NC0C terms can be, for example, the same as in C. A statement of form (1) can perform the following for each valuation θ of the variables {right arrow over (z)} (extending the valuation of variables {right arrow over (x)}{right arrow over (y)} passed to the trigger via its arguments) such that all map accesses in right-hand side t are defined. If m[{right arrow over (x)}{right arrow over (z)}] is undefined, it can be initialized with tinit. Then, unconditionally, m[{right arrow over (x)}{right arrow over (z)}]+=t can be executed. An “on ±R({right arrow over (x)}{right arrow over (y)})” trigger fires if the update is of the form ±R({right arrow over (x)}{right arrow over (y)}) and executes the statements s1; . . . ; sk of its body sequentially (as in C). Compilation algorithms can create multiple triggers for the same update event. However, NC0C programs do not have cyclic dependencies between triggers: there is no trigger that reads one map that the other updates and vice-versa (there is a hierarchy of maps). Assuming without loss of generality that the argument variable tuples of distinct triggers for the same update event are the same, a topological sort of the triggers can be performed that assures that no map is read after it is written, and their bodies can be concatenated according to this sort to obtain a single trigger per update event. An exemplary NC0C on-insert trigger follows:















on +C(cid, nation) {



q[cid] += q1[nation];



foreach cid2 do q[cid2] += q2[cid2, nation];



q[cid] += 1;



q1[nation] += 1;



q2[cid, nation] += 1



}









The initializers are all 0, and are thus omitted. The trigger on −C is obtained by changing += to −= everywhere in the above trigger except for the third statement (q[cid]+=1), which remains unchanged.
FIG. 2 is a table of a trace of map q that changes during a sequence of insertions and deletions. The Δq[x] columns indicate the changes made to q[x] on each update. Methods for compiling AGCA queries to exemplary language NC0C can be given. AGCA sum terms excluding nested aggregates and inequality join conditions (i.e., involving two variables, e.g. x<y; non-join conditions such as x>5 are permitted) are known herein as primitive AGCA terms. NC0C initializers can be, but are not required to be, constants so as going to the database to compute is not necessary. An AGCA term t=Sum(t0,φ) is called constraints-only if φ does not contain relational atoms R({right arrow over (x)}). When φ contains only bound variables, t can be a (functional) if-statement “if φ then t0 else 0” or, using C syntax, “φ?t0:0”. MakeC(t,B) can be a function that turns t into the corresponding functional if-statement after performing variable elimination using bound variables B. In the present embodiment there is a compilation method for AGCA terms Sum(t, φ) that does not contain nested aggregates, i.e., neither t nor φ contain Sum terms. In the present embodiment, any primitive AGCA term can be compiled into an NC0C program that incrementally maintains it. To create on-insert (+R) and on-delete (−R) triggers that incrementally maintain map q[{right arrow over (b)}] of Sum term t, the following method can be executed as Compile0(q, {right arrow over (b)}, t):














Compile0( q , {right arrow over (b)} , t ) outputs an NC0C program



  begin



  for each relation R in the schema, ± in {+,−} do



   {right arrow over (a)} := turn sch(R) into a list of new variable names;



   tinit := [[t]]T(Ø,{right arrow over (a)}{right arrow over (b)});



   t′ :=  t ;



  if t′ is constraints only then t′′ := MakeC( t′ , {right arrow over (a)}{right arrow over (b)} )



  else t′′ := q±R[{right arrow over (a)}{right arrow over (b)}]; Compile0( q±R, {right arrow over (a)}{right arrow over (b)} , t′ ) end if;



  output on ±R({right arrow over (a)}) {foreach {right arrow over (b)} do q[{right arrow over (b)}] < tinit > += t′′ }



end









q±R is a new map name for an auxiliary materialized view. When {right arrow over (b)} is the empty tuple, “for each {right arrow over (b)} do” can be omitted from the NC0C statement. The method of the present embodiment takes an aggregate query t=Sum(t0, φ) with bound variables {right arrow over (b)} and defines a map q[{right arrow over (b)}] for it, representing a materialized view of the query. The method can create a trigger for each possible update event ±R which specifies how to update q[{right arrow over (b)}] when this event occurs. To create a trigger, the method can compute the delta t′ of the query, and can create a new map q±R representing a materialized view of the delta. The statement increments q[{right arrow over (b)}] by q±R[•], and uses the result of evaluating term t on the empty database as the initializer for q[{right arrow over (b)}]. In particular, on Sum terms t that are not constraints-only, tinit=0. The new map q±R is incrementally maintained as well. To do this, the method recursively calls itself and terminates because the delta t′ eventually reaches degree 0 (i.e., is constraints-only). In this case no new map is created for it but t′ (turned into an NC0C term using MakeC) itself is used as the right-hand side of the NC0C statement. For example, if q[ ]=Sum(1,R(x)*S(x)), then

Δ±R(u)q=±Sum(1,(x=u)*S(x))=:qR[u]

Δ±S(v)q=±Sum(1,R(x)*(x=v))=:qS[v]

Δ±S(v)qR[u]=±Sum(1,(x=u)*(x=v))

Δ±R(u)qS[v]=±Sum(1,(x=u)*(x=v))

If MakeC(Sum(1,(x=u)*(x=v)))=if (u−v) then 1 else 0. Compile0 produced the NC0C insert triggers

on +R(u){q[ ]+=qR[u]}

on +R(u){for each v do

qS[v]+=if(u=v) then 1 else 0}

on +S(v){q[ ]+=qS[v]}

on +S(v){for each u 

do qR[u]+=if(u=v) then 1 else 0}

The delete triggers are obtained from the insert triggers by replacing all occurrences of + by −,

To loop over fewer variables, aggregates can be extracted. For a term t and its set B of bound variables, the function Extract (t, B) can replace each maximal subterm s for t that is of the form Sum(•,•) but is not constraints-only, by a map access m[{right arrow over (x)}]. m is a new name and {right arrow over (x)} are those variables of B that occur in s, turned into an arbitrarily ordered tuple. The results of Extract is a pair (t′, Θ) of the remainder term t′ and a mapping Θ from map accesses m[{right arrow over (x)}] to extracted subterms s which could be used to undo the extraction. That is, t′ is constraints-only and t′ with its map accesses substituted using Θ is t. For example, if t is the term Sum(x*Sum(v,R(v,z)),y=z)*Sum(u,R(x,u)). extract (t,{x,y}) returns the pair (t′,θ) including term t′=Sum(x*m1[z]<θ(m1[z])>,y=z)*m2[x])<θ(m2[x])> and the mapping

θ={m1[z]Sum(v,R(v,z));m2[x]Sum(u,R(x,u))}.

If ψ is a constraints-only term in which all variables are bound and t0 is a term in which all variables are bound, then

Sum(t,φ,ψ)=Sum(Sum(t,φ),ψ)

t0*Sum(t,ψ)=Sum(t0*tψ)

Thus, given a recursively monomial term, ψ can be lifted to the top. For example,

LiftIfs(Sum(1,C(c2,n)*(c1=c)),{c1,c,n})=

Sum(Sum(1,C(c2,n)),c1=c).

To address further auxiliary functions, Simplify (t, B), given an aggregate term t and a set of bound variables B,

    turns t into an equivalent sum of (inverses of) recursive monomials,
    recursively factorizes each of the result monomials,
    eliminates all variables others than those in B or a relational atom, and
    performs LiftIfs(•, B).

The result t±1t1 . . . ±ntn is equivalent to t and the t, are recursive monomials involving only variables in B.

ElimLV is a function that takes an NC0C statement

foreach {right arrow over (x)}{right arrow over (y)} do q[{right arrow over (x)}{right arrow over (y)}]+=if {right arrow over (x)}={right arrow over (z)} then t else 0

and simplifies it to the equivalent statement

foreach {right arrow over (y)} do q[{right arrow over (z)}{right arrow over (y)}]+=t. 

The method Compile(m, {right arrow over (b)}, t), outputing an NC0C program, can be invoked in a similar way to Compile0 except that the delta of the query is first simplified and the non-constraints-only aggregates are extracted.

















Compile( m , {right arrow over (b)} , t )



begin



for each relation R in the schema, ±0 in {+,−} dō



 {right arrow over (a)} := turn sch(R) into a list of new variable names;



 tinit := [[t]]T(Ø,{right arrow over (a)}{right arrow over (b)});



 t′ :=  t ;



 ( ±1t1...±ntn, Θ ) := Extract(Simplify( t′ , {right arrow over (a)}{right arrow over (b)} ), {right arrow over (a)}{right arrow over (b)} );



 foreach i from 1 to n



   si := (foreach {right arrow over (b)} do m[{right arrow over (b)}] < tinit > (±i)= MakeC( ti, {right arrow over (a)}{right arrow over (b)} ));



   output on ±0R({right arrow over (a)}) { ElimLV(si) };



 foreach (m’[{right arrow over (x)}]  t′′) in Θ do Compile( m′,{right arrow over (x)},t′′ );



end









The result is a sum of constraints-only recursive monomials with map accesses. Each of the recursive monomials becomes a separates statement because, using Liftlfs and ElimLV, the loop variables are removed, and each of the statements (monomials) can loop over a different subset of the argument variables of the map representing the query (the remaining variables are substituted by constants). Given a primitive term t and bound variables {right arrow over (x)} by which results are to be grouped, the output of Compile(m, {right arrow over (x)}, t) is an NC0C program that can maintain query t in map m[{right arrow over (x)}] under inserts and deletes. For example, Compile can simplify the two foreach-loop statements of the triggers given previously to qS[u]+=1 and qR[v]+=1. The resulting triggers have no loops and run in constant sequential time. For example, an AGCA query compilation can be invoked as follows:

Compile(q,{c1},Sum(1,C(c1,n1)*C(c2,n2)*(n1=n2))).

A sum of (inverses of) recursive monomials equivalent to Δ±C(c,n)q[c1] has been previously computed. Factorization on that query is the identy. Eliminating variables with variables (c1,c,n) bound yields ±Sum(1,(c1=c)*C(c2,n))±Sum(1,C(c1,n))+Sum(1,c1=c). The result of if-lifting for the first of the three monomials is

LiftIfs(Sum(1,C(c2,n)*(c2=c)),{c1,c,n})=

sum(sum(1,C(c2,n)),c1=c).

For the other two monomials, variables elimination is the identity.

Extract(Simplify(Δ±C(c,n)q[c1,{c1,c,n}),{c1,c,n})

yields (±t1±t2+t3,Θ) where t1=Sum(q1[n],c1=c),t2=q2[c1,n],t3=Sum(1,c1=c), and





  Θ
  =
  
    {
    
      
        
          
            q
            ⁢
            
              
            
            ⁢
            
              1
              ⁡
              
                [
                n
                ]
              
            
          
        
        
          ↦
        
        
          
            Sum
            ⁡
            
              (
              
                1
                ,
                
                  C
                  ⁡
                  
                    (
                    
                      
                        c
                        2
                      
                      ,
                      n
                    
                    )
                  
                
              
              )
            
          
        
      
      
        
          
            q
            ⁢
            
              
            
            ⁢
            
              2
              ⁡
              
                [
                
                  
                    c
                    1
                  
                  ,
                  n
                
                ]
              
            
          
        
        
          ↦
        
        
          
            
              Sum
              ⁡
              
                (
                
                  1
                  ,
                  
                    C
                    ⁡
                    
                      (
                      
                        
                          c
                          1
                        
                        ,
                        n
                      
                      )
                    
                  
                
                )
              
            
            .
          
        
      
    
    }
  




The statements s1, s2, s3 are

    foreach c1 do q[c1]±=if c1=c then q1[n] else 0;
    foreach c1 do q[c1]±=q2[c1, n];
    foreach c1 do q[c1]±=if c1=c then 1 else 0
    ElimLV can simplify these statements, and the resulting triggers are

on ±C(c,n){q[c]±=q1[n] else 0}

on ±C(c,n){foreach c1 do q[c1]±=q2[c1,n]}

on ±C(c,n){q[c]±=1}

q1 and q2 can be compiled. Since

Δ±C(c′,n′)q1[n]=Δ±C(c′,n′)q2[c,n]=±1,

The compiled NC0C program after merging triggers as described previously is

















on +C(cid, nation) {



q[cid] += q1[nation];



foreach cid2 do q[cid2] += q2[cid2, nation];



q[cid] += 1;



q1[nation] += 1;



q2[cid, nation] += 1



}









Compilable AGCA terms are those without nested aggregates or inequality join conditions. The second requirement of that definition can be replaced by the requirement to exclude terms that are unsafe if the set of bound variables is set to Ø or where this condition can become true for a k-th delta. For example, the query q[ ]=Sum(1,R(x)*S(y*(x<y)) can be excluded because its delis m[y]=Sum(1,R(x)*(x<y)) is safe for bound variable y, but unsafe for the empty set of bound variables. On an insertion into R, it is not known for which y values the map m should be updated. If it a global, immutable active domain is assumed, it is not necessary to compute initial values, and the compilation methods can be applicable to all Sum terms with simple conditions, i.e. for which taking deltas simplifies the query structure. For primitive queries, for the initialization of a map value, m[{right arrow over (a)}]<tinit>, tinit can be evaluated on the empty database, and contents of the database are not visible to query tinit or would otherwise have caused initialization of m[{right arrow over (a)}] earlier.
Referring now to FIG. 3, system 100 of the present embodiment can include, but is not limited to including, query compiler 13 which can include, but is not limited to including core data structures 15, a conventional SQL parser 17 such as, for example, GSP's SQL parser, compilation algorithm 19 and query transformation library 21. System 100 receives query 11 and produces query source code 22, which implements a query runtime 23, that is received by conventional source code compiler 27, which produces compiled binary 29. Core data structures 15 can include, but are not limited to including, ring 15A, hypergraph 15B, NC0C abstract syntax tree (AST) 15D, a data structure to describe NC0C programs as presented above, and a standard query AST 15C, a conventional data structure that describes queries as produced by the conventional SQL parser. A hypergraph is a pair (V, E) where V is a set “of nodes” and E is a set of subsets of V. Thus, a hypergraph generalizes a graph in that edges become so-called “hyperedges” (the members of E), which connect possibly more or fewer than two nodes with each other. SQL Parser 17 can receive, but is not limited to receiving, query 11 and information from core data structures 15 to provide initial query AST 18 to compilation algorithm 19. Compilation algorithm 19 can include, but is not limited to including, initializer 19A, elimLV 19B, extract 19C, and MakeC and Output 19D. Compilation algorithm 19 can receive information from core data structures 15, initial query AST 18, and delta queries 20 which have undergone variable elimination and factorization and can provide delta queries 20 to query transformation library 21, and also can provide query source code 22 that implements query runtime 23. Initializer 19A, elimLV 19B, extract 19C, and MakeC and Output 19D have been described in detail previously. Query transformation library 21 can include, but is not limited to including, delta computation 21A and simplify 21B, also described in detail previously. Query transformation library 21 can receive information from core data structures 15, and can provide delta queries 20 to compilation algorithm 19. Query runtime 23 can include, but is not limited to, map definitions 23A, which are source code declarations for maps, and thus materialized views, being maintained by NC0C program 23B as described above, as received in the query source code 22 from compilation algorithm 19, and can be provided to a conventional source code compiler 25 to yield a compiled binary 27.
Referring now to FIG. 4, method 150 executed by query compiler 13 of the present embodiment can include, but is not limited to including, the steps of receiving map name m, map arguments x, and query t, determining 51 relations R in query t, assigning 55 names a to trigger arguments based on the schema of R, if 53 there are more relations R, creating 57 initial value query tinit, for query t based on trigger arguments a and map arguments x, computing 59 delta query t′ based on query t, trigger arguments a and relation R, simplifying 61 delta query t based on trigger arguments a and map arguments x to produce simplified delta monomials tmi, extracting 63 aggregates based on simplified delta monomials tinit, trigger arguments a, and map arguments x to produce monomials t, with substitutions, map names, and defining queries, for each 65 ti creating 67 map update statement si based on map arguments x, map name m, initial value query tinit, delta query t′, and trigger arguments a, eliminating 69 map update loops to produce optimized update statement s′i, based on update statement si and storing 71 optimized update statement s′i, based on trigger arguments a and relation R, and recursively calling 73 query compiler 13 with arguments based on map name m, map arguments x, and delta delta query t′′.
An alternate method for creating code to optimize queries of a database can include, but is not limited to including, the steps of (a) receiving a map name, map arguments, and a query, (b) determining relations in the query, (c) determining trigger arguments from each of the relations, (d) computing an initial value query based on an evaluation of the query, the map arguments, and the trigger arguments, the initial value query being computed by executing the query by a conventional query processor on an empty database, (e) computing a delta query based on the query, the relation, and the trigger arguments, the delta query being computed by conventional delta query computation for incremental view maintenance, (f) computing simplified delta monomials based on the delta query, the trigger arguments, and the map arguments by performing the steps of: (1) computing a polynomial version of the query, (2) applying factorization to each monomial of the polynomial, (3) eliminating from each of the monomials the trigger arguments and the map arguments that can be can be connected by equality to other of the trigger arguments and the map arguments, and (4) creating the simplified delta monomials from monomials in which the trigger arguments and the map arguments do not reference the database, (g) extracting aggregates as the monomials that reference the database based on the simplified delta monomials, the map arguments, and the trigger arguments, (h) computing a map update statement for each of the simplified delta monomials based on the simplified delta monomial, the map arguments, and the trigger arguments, (i) computing optimized update statement by eliminating map update loops from the map update statement for each revised simplified delta monomial based on the map update statement, the optimized update statement being computed by ElimLV, (j) generating code from the optimized update statements based on the relation and the trigger arguments, (k) repeating steps (a)-(k) when there are more of the relations in the query, (l) for each aggregate query in the delta query, recursively applying steps (a)-(m) to the aggregate query, the arguments including the trigger arguments remaining in the aggregate query after simplification, and (m) repeating steps and (a) through (m) when there are more of the aggregates.
Referring primarily to FIG. 4, method 150 can be, in whole or in part, implemented electronically. Signals representing actions taken by elements of system 100 (FIG. 3) can travel over electronic communications media and from node to node in a communications network. Control and data information can be electronically executed and stored on computer-readable media. Method 150 can be implemented to execute on a node in a computer communications network. Common forms of computer-usable media include, for example, a floppy disk, a flexible disk, hard disk, magnetic tape, or any other magnetic medium, a CDROM, any other optical medium, punched cards, paper tape, any other physical medium with patterns of holes, a RAM, a PROM, and EPROM, a FLASH-EPROM, thumb drive, any other memory chip or cartridge, or any other medium from which a computer can read. From a technological standpoint, a signal or carrier wave (such as used for Internet distribution of software) encoded with functional descriptive material is similar to a computer-readable medium encoded with functional descriptive material, in that they both create a functional interrelationship with a computer. In other words, a computer is able to execute the encoded functions, regardless of whether the format is a disk or a signal. System 100 (FIG. 3) can include a communications network that can include at least one node for carrying out method 150. System 100 (FIG. 3) can be a distributed network (such as a network utilizing a distributed system format such as, but not limited to, DCOM or CORBA) where computer data signal (such as, but not limited to, signals over the Internet) traveling over the communications network carrying information capable of causing a computer system in the communications network to practice method 150. The communications network can be a conventional network; the signal is modulated and supplemental data is embedded utilizing conventional means. System 100 (FIG. 3) can include a computer readable medium having instructions embodied therein for the practice of method 150.

## Claims

1. A processor implemented method for incrementally maintaining a representation of the result of a computation using k-th deltas of a specification, the specification being a database query, for k>1, comprising the steps of:
materializing representations of the computation results of multiple levels of the specification;
maintaining the materialized representations concurrently;
maintaining the materialized representation of level k under updates using materialized representations of a level greater than k; and
performing the updates of the representation by the computation whose inputs are a state of the representation, update information related to the updates, and at least one of the representations of the specifications of a level greater than k, the specifications constituting a set, operations on the set of specifications obtaining properties of a ring of databases, the ring of databases being used to define an aggregate query calculus comprising operations on the set;
the properties of the aggregate query calculus including having a full additive inverse and having a property of distributivity;
wherein the steps of materializing representations, maintaining the materialized representations, and performing the updates of the representation are performed by one or more processors executing computer readable code embodied in non-transitory computer usable media.

2. The method of claim 1 further comprising the step of:
compiling the database query.

3. The method of claim 1 further comprising the step of:
performing operations between a plurality of the representations to maintain a hierarchy of the representations and to update the representations.

4. The method of claim 1 further comprising the step of:
performing the computations on the updates by code obtained by compilation of the specification.

5. The method of claim 1 further comprising the step of:
performing the computations on at least one computer by machine code obtained by compilation of the specification.

6. The method of claim 1 further comprising the step of:
performing the computations on hardware generated to execute machine code obtained by compilation of the specification.

7. The method of claim 1 further comprising the step of:
performing the computations on at least one Boolean circuit executing machine code obtained by compilation of the specification.

8. The method of claim 1 wherein the representations of the computation results of specifications are relations.

9. The method of claim 1 wherein the representations of the computation results of specifications are finite functions.

10. The method of claim 1 wherein the representations of the computation results of specifications are hash tables.

11. The method of claim 1 further comprising the step of:
parallelizing incremental view maintenance.

12. A computer node in communications with the Internet for carrying out the method according to claim 1.

13. A communications network in communications with the Internet having a computer node for carrying out the method according to claim 1.

14. A non-transitory computer-readable medium having instructions for carrying out the method according to claim 1.

15. A processor implemented method for creating code to optimize queries of a database comprising the steps of:
(a) receiving a map name, map arguments, and a query;
(b) determining relations in the query;
(c) determining trigger arguments from each of the relations;
(d) computing an initial value query based on an evaluation of the query, the map arguments, and the trigger argument;
(e) computing a delta query based on the query, the relation, and the trigger arguments;
(f) computing simplified delta monomials based on the delta query, the trigger arguments, and the map arguments by performing the steps of:
(1) computing a polynomial version of the query;
(2) applying factorization to each monomial of the polynomial;
(3) eliminating from each of the monomials the trigger arguments and the map arguments that can be connected by equality to other of the trigger arguments and the map arguments; and
(4) creating the simplified delta monomials from monomials in which the trigger arguments and the map arguments do not reference the database;

(g) extracting aggregates as the monomials that reference the database based on the simplified delta monomials, the map arguments, and the trigger arguments;
(h) computing a map update statement for each of the simplified delta monomials based on the simplified delta monomial, the map arguments, and the trigger arguments;
(i) computing optimized update statement by eliminating map update loops from the map update statement for each revised simplified delta monomial based on the map update statement;
(j) generating code from the optimized update statements based on the relation and the trigger arguments;
(k) repeating steps (a)-(k) when there are more of the relations in the query;
(l) for each of the aggregates, recursively applying steps (a)-(l) when there are more of the aggregates, the arguments for said step of recursively applying include the trigger arguments remaining in the aggregate after said step of creating the simplified delta monomials;

wherein steps (a)-(l) are performed by one or more processors executing computer readable code embodied in non-transitory computer usable media.

16. A computer node in communications with the Internet for carrying out the method according to claim 15.

17. A communications network in communications with the Internet having a computer node for carrying out the method according to claim 15.

18. A non-transitory computer-readable medium having instructions for carrying out the method according to claim 15.

19. A system for incrementally maintaining a representation of the result of a computation using k-th deltas of a specification, for k>1, the specification being a database query, the system comprising:
one or more hardware computer components;
non-transitory computer usable media having computer readable code embodied therein, the computer readable code, when executed by the one or more computer components, causing the one or more computer components to:
create materialized representations of the computation results of multiple levels of the specification;
create a first computer program to maintain the materialized representations concurrently;
create a second computer program to maintain the materialized representation of level k under updates using the materialized representations of a level greater than k; and
create a third computer program to perform the updates of the representation by the computation whose inputs are a state of the representation, update information related to the updates, and at least one of the materialized representations of the specifications of a level greater than k; operations on a set of the specifications obtaining properties of a ring of databases, the ring of databases being used to define an aggregate query calculus comprising operations on the set; the properties of the aggregate query calculus including having a full additive inverse and having a property of distributivity; and
create machine code from the first computer program, the second computer program, and the third computer program.