Answering FO+MOD queries under updates on bounded degree databases

We investigate the query evaluation problem for fixed queries over fully dynamic databases, where tuples can be inserted or deleted. The task is to design a dynamic algorithm that immediately reports the new result of a fixed query after every database update. We consider queries in first-order logic (FO) and its extension with modulo-counting quantifiers (FO+MOD), and show that they can be efficiently evaluated under updates, provided that the dynamic database does not exceed a certain degree bound. In particular, we construct a data structure that allows to answer a Boolean FO+MOD query and to compute the size of the result of a non-Boolean query within constant time after every database update. Furthermore, after every update we are able to immediately enumerate the new query result with constant delay between the output tuples. The time needed to build the data structure is linear in the size of the database. Our results extend earlier work on the evaluation of first-order queries on static databases of bounded degree and rely on an effective Hanf normal form for FO+MOD recently obtained by Heimberg, Kuske, and Schweikardt (LICS 2016).


Introduction
Query evaluation is a fundamental task in databases, and a vast amount of literature is devoted to the complexity of this problem. In this paper we study query evaluation on relational databases in the "dynamic setting", where the database may be updated by inserting or deleting tuples. In this setting, an evaluation algorithm receives a query ϕ and an initial database D and starts with a preprocessing phase that computes a suitable data structure to represent the result of evaluating ϕ on D. After every database update, the data structure is updated so that it represents the result of evaluating ϕ on the updated database. The data structure shall be designed in such a way that it quickly provides the query result, preferably in constant time (i. e., independent of the database size). We focus on the following flavours of query evaluation.
• Testing: Decide whether a given tuple a is contained in ϕpDq.
• Enumeration: Enumerate ϕpDq with a bounded delay between the output tuples.
Here, as usual, ϕpDq denotes the k-ary relation obtained by evaluating a k-ary query ϕ on a relational database D. For Boolean queries, all three tasks boil down to • Answering: Decide if ϕpDq ‰ H. * This is the full version of the conference contribution [3].
Compared to the dynamic descriptive complexity framework introduced by Patnaik and Immerman [17], which focuses on the expressive power of first-order logic on dynamic databases and has led to a rich body of literature (see [18] for a survey), we are interested in the computational complexity of query evaluation. The query language studied in this paper is FO`MOD, the extension of first-order logic FO with modulo-counting quantifiers of the form D i mod m x ψ, expressing that the number of witnesses x that satisfy ψ is congruent to i modulo m. FO`MOD can be viewed as a subclass of SQL that properly extends the relational algebra.
Following [2], we say that a query evaluation algorithm is efficient if the update time is either constant or at most polylogarithmic (log c n) in the size of the database. As a consequence, efficient query evaluation in the dynamic setting is only possible if the static problem (i.e., the setting without database updates) can be solved in linear or pseudo-linear (n 1`ε ) time. Since this is not always possible, we provide a short overview on known results about first-order query evaluation on static databases and then proceed by discussing our results in the dynamic setting.
First-order query evaluation on static databases. The problem of deciding whether a given database D satisfies a FO-sentence ϕ is AWr˚s-complete (parameterised by ||ϕ||) and it is therefore generally believed that the evaluation problem cannot be solved in time f p||ϕ||q||D|| c for any computable f and constant c (here, ||ϕ|| and ||D|| denote the size of the query and the database, respectively). For this reason, a long line of research focused on increasing classes of sparse instances ranging from databases of bounded degree [19] (where every domain element occurs only in a constant number of tuples in the database) to classes that are nowhere dense [9]. In particular, Boolean first-order queries can be evaluated on classes of databases of bounded degree in linear time f p||ϕ||q||D||, where the constant factor f p||ϕ||q is 3-fold exponential in ||ϕ|| [19,7]. As a matter of fact, Frick and Grohe [7] showed that the 3-fold exponential blow-up in terms of the query size is unavoidable assuming FPT ‰ AWr˚s.
Durand and Grandjean [5] and Kazana and Segoufin [11] considered the task of enumerating the result of a k-ary first-order query on bounded degree databases and showed that after a linear time preprocessing phase the query result can be enumerated with constant delay. This result was later extended to classes of databases of bounded expansion [12]. Kazana and Segoufin [12] also showed that counting the number of result tuples of a k-ary first-order query on databases of bounded expansion (and hence also on databases of bounded degree) can be done in time f p||ϕ||q||D||. In [6] an analogous result was obtained for classes of databases of low degree (i. e., degree at most ||D|| op1q ) and pseudo-linear time f p||ϕ||q||D|| 1`ε ; the paper also presented an algorithm for enumerating the query results with constant delay after pseudo-linear time preprocessing.
Our contribution. We extend the known linear time algorithms for first-order logic on classes of databases of bounded degree to the more expressive query language FO`MOD. Moreover, and more importantly, we lift the tractability to the dynamic setting and show that the result of FO and FO`MOD-queries can be maintained with constant update time. In particular, we obtain the following results. Let ϕ be a fixed k-ary FO`MOD-query and d a fixed degree bound on the databases under consideration. Given an initial database D, we construct in linear time f p||ϕ||, dq||D|| a data structure that can be updated in constant time f p||ϕ||, dq when a tuple is inserted into or deleted from a relation of D. After each update the data structure allows to • immediately answer ϕ on D if ϕ is a Boolean query (Theorem 4.1), • test for a given tuple a whether a P ϕpDq in time Opk 2 q (Theorem 6.1), • immediately output the number of result tuples |ϕpDq| (Theorem 8.1), and • enumerate all tuples pa 1 , . . . , a k q P ϕpDq with Opk 3 q delay (Theorem 9.4).
For fixed d, the parameter function f p||ϕ||, dq is 3-fold exponential in terms of the query size, which is (by Frick and Grohe [7]) optimal assuming FPT ‰ AWr˚s.
Outline. Our dynamic query evaluation algorithm crucially relies on the locality of FO`MOD and in particular an effective Hanf normal form for FO`MOD on databases of bounded degree recently obtained by Heimberg, Kuske, and Schweikardt [10]. After some basic definitions in Section 2 we briefly state their result in Section 3 and obtain a dynamic algorithm for Boolean FO`MOD-queries in Section 4. After some preparations for non-Boolean queries in Section 5, we present the algorithm for testing in Section 6. In Section 7 we reduce the task of counting and enumerating FO`MOD-queries in the dynamic setting to the problem of counting and enumerating independent sets in graphs of bounded degree. We use this reduction to provide efficient dynamic counting and enumeration algorithms in Section 8 and 9, respectively, and we conclude in Section 10.

Preliminaries
We write N for the set of non-negative integers and let N ě1 :" Nzt0u and rns :" t1, . . . , nu for all n P N ě1 . By 2 M we denote the power set of a set M . For a partial function f we write dompf q and codompf q for the domain and the codomain of f , respectively.
Databases. We fix a countably infinite set dom, the domain of potential database entries. Elements in dom are called constants. A schema is a finite set σ of relation symbols, where each R P σ is equipped with a fixed arity arpRq P N ě1 . Let us fix a schema σ " tR 1 , . . . , R |σ| u. A database D of schema σ (σ-db, for short), is of the form D " pR D 1 , . . . , R D |σ| q, where each R D i is a finite subset of dom arpR i q . The active domain adompDq of D is the smallest subset A of dom such that R D i Ď A arpR i q for each R i in σ. The Gaifman graph of a σ-db D is the undirected simple graph G D " pV, Eq with vertex set V :" adompDq, where there is an edge between vertices u and v whenever u ‰ v and there are R P σ and pa 1 , . . . , a arpRq q P R D such that u, v P ta 1 , . . . , a arpRq u. A σ-db D is called connected if its Gaifman graph G D is connected; the connected components of D are the connected components of G D . The degree of a database D is the degree of its Gaifman graph G D , i.e., the maximum number of neighbours of a node of G D . Throughout this paper we fix a number d P N and restrict attention to databases of degree at most d.
Updates. We allow to update a given database of schema σ by inserting or deleting tuples as follows (note that both types of commands may change the database's active domain and the database's degree). A deletion command is of the form delete Rpa 1 , . . . , a r q for R P σ, r " arpRq, and a 1 , . . . , a r P dom. When applied to a σ-db D, it results in the updated σ-db D 1 with R D 1 " R D ztpa 1 , . . . , a r qu and S D 1 " S D for all S P σztRu. An insertion command is of the form insert Rpa 1 , . . . , a r q for R P σ, r " arpRq, and a 1 , . . . , a r P dom. When applied to a σ-db D in the unrestricted setting, it results in the updated σ-db D 1 with R D 1 " R D Y tpa 1 , . . . , a r qu and S D 1 " S D for all S P σztRu. In this paper, we restrict attention to databases of degree at most d. Therefore, when applying an insertion command to a σ-db D of degree ď d, the command is carried out only if the resulting database D 1 still has degree ď d; otherwise D remains unchanged and instead of carrying out the insertion command, an error message is returned.
Queries. We fix a countably infinite set var of variables. We consider the extension FO`MOD of first-order logic FO with modulo-counting quantifiers. For a fixed schema σ, the set FO`MODrσs is built from atomic formulas of the form x 1 "x 2 and Rpx 1 , . . . , x arpRq q, for R P σ and variables x 1 , x 2 , . . . , x arpRq P var, and is closed under Boolean connectives ,^, existential first-order quantifiers Dx, and modulo-counting quantifiers D i mod m x, for a variable x P var and integers i, m P N with m ě 2 and i ă m. The intuitive meaning of a formula of the form D i mod m x ψ is that the number of witnesses x that satisfy ψ is congruent i modulo m. As usual, @x, _, Ñ, Ø will be used as abbreviations when constructing formulas. It will be convenient to add the quantifier D ěm x, for m P N ě1 ; a formula of the form D ěm x ψ expresses that the number of witnesses x which satisfy ψ is ě m. This quantifier is just syntactic sugar an does not increase the expressive power of FO`MOD.
The quantifier rank qrpϕq of a FO`MOD-formula ϕ is the maximum nesting depth of quantifiers that occur in ϕ. By freepϕq we denote the set of all free variables of ϕ, i.e., all variables x that have at least one occurrence in ϕ that is not within a quantifier of the form Dx, D ěm x, or D i mod m x. A sentence is a formula ϕ with freepϕq " H.
An assignment for ϕ in a σ-db D is a partial mapping α from var to adompDq, where freepϕq Ď dompαq. We write pD, αq |ù ϕ to indicate that ϕ is satisfied when evaluated in D with respect to active domain semantics while interpreting every free occurrence of a variable x with the constant αpxq. Recall from [1] that "active domain semantics" means that quantifiers are evaluated with respect to the database's active domain. In particular, pD, αq |ù Dx ψ iff there exists an a P adompDq such that pD, α a x q |ù ψ, where α a x is the assignment α 1 with α 1 pxq " a and α 1 pyq " αpyq for all y P dompαqztxu. Accordingly, pD, αq |ù D ěm x ψ ifˇt a P adompDq : pD, α a x q |ù ψ uˇˇě m, and pD, αq |ù D i mod m x ψ iffˇˇt a P adompDq : pD, α a x q |ù ψ uˇˇ" i mod m . A k-ary FO`MOD query of schema σ is of the form ϕpx 1 , . . . , x k q where k P N, ϕ P FO`MODrσs, and freepϕq Ď tx 1 , . . . , x k u. We will often assume that the tuple px 1 , . . . , x k q is clear from the context and simply write ϕ instead of ϕpx 1 , . . . , x k q and`D, pa 1 , . . . , a k q˘|ù ϕ instead of`D, a 1 ,...,a k x 1 ,...,x k˘| ù ϕ, where a 1 ,...,a k x 1 ,...,x k denotes the assignment α with αpx i q " a i for all i P rks. When evaluated in a σ-db D, the k-ary query ϕpx 1 , . . . , x k q yields the k-ary relation ϕpDq :" pa 1 , . . . , a k q P adompDq k :`D, a 1 ,...,a k Boolean queries are k-ary queries with k " 0. As usual, for Boolean queries we will write ϕpDq " no instead of ϕpDq " H, and ϕpDq " yes instead of ϕpDq ‰ H; and we write D |ù ϕ to indicate that pD, αq |ù ϕ for any assignment α.
Sizes and Cardinalities. The size ||σ|| of a schema σ is the sum of the arities of its relation symbols. The size ||ϕ|| of an FO`MOD query ϕ of schema σ is the length of ϕ when viewed as a word over the alphabet σ Y var Y N Y t ",^, , D, mod , ě , p, q u. For a k-ary query ϕpx 1 , . . . , x k q and a σ-db D, the cardinality of the query result is the number |ϕpDq| of tuples in ϕpDq. The cardinality |D| of a σ-db D is defined as the number of tuples stored in D, i.e., |D| :" ř RPσ |R D |. The size ||D|| of D is defined as ||σ||`|adompDq|`ř RPσ arpRq¨|R D | and corresponds to the size of a reasonable encoding of D.
Dynamic Algorithms for Query Evaluation. We adopt the framework for dynamic algorithms for query evaluation of [2]; the next paragraphs are taken almost verbatim from [2]. Following [4], we use Random Access Machines (RAMs) with Oplog nq word-size and a uniform cost measure to analyse our algorithms. We will assume that the RAM's memory is initialised to 0. In particular, if an algorithm uses an array, we will assume that all array entries are initialised to 0, and this initialisation comes at no cost (in real-world computers this can be achieved by using the lazy array initialisation technique, cf. e.g. [16]). A further assumption is that for every fixed dimension k P N ě1 we have available an unbounded number of k-ary arrays A such that for given pn 1 , . . . , n k q P N k the entry Arn 1 , . . . , n k s at position pn 1 , . . . , n k q can be accessed in constant time. 1 For our purposes it will be convenient to assume that dom " N ě1 .
Our algorithms will take as input a k-ary FO`MOD-query ϕpx 1 , . . . , x k q, a parameter d, and a σ-db D 0 of degree ď d. For all query evaluation problems considered in this paper, we aim at routines preprocess and update which achieve the following.
Upon input of ϕpx 1 , . . . , x k q and D 0 , preprocess builds a data structure D which represents D 0 (and which is designed in such a way that it supports the evaluation of ϕ on D 0 ). Upon input of a command update Rpa 1 , . . . , a r q (with update P tinsert, deleteu), calling update modifies the data structure D such that it represents the updated database D. The preprocessing time t p is the time used for performing preprocess; the update time t u is the time used for performing an update. In this paper, t u will be independent of the size of the current database D. By init we denote the particular case of the routine preprocess upon input of a query ϕpx 1 , . . . , x k q and the empty database D H (where R D H " H for all R P σ). The initialisation time t i is the time used for performing init. In all dynamic algorithms presented in this paper, the preprocess routine for input of ϕpx 1 , . . . , x k q and D 0 will carry out the init routine for ϕpx 1 , . . . , x k q and then perform a sequence of |D 0 | update operations to insert all the tuples of D 0 into the data structure. Consequently, t p " t i`| D 0 |¨t u .
In the following, D will always denote the database that is currently represented by the data structure D.
To solve the enumeration problem under updates, apart from the routines preprocess and update, we aim at a routine enumerate such that calling enumerate invokes an enumeration of all tuples (without repetition) that belong to the query result ϕpDq. The delay t d is the maximum time used during a call of enumerate • until the output of the first tuple (or the end-of-enumeration message EOE, if ϕpDq " H), • between the output of two consecutive tuples, and • between the output of the last tuple and the end-of-enumeration message EOE.
To test if a given tuple belongs to the query result, instead of enumerate we aim at a routine test which upon input of a tuple a P dom k checks whether a P ϕpDq. The testing time t t is the time used for performing a test. To solve the counting problem under updates, instead of enumerate or test we aim at a routine count which outputs the cardinality |ϕpDq| of the query result. The counting time t c is the time used for performing a count. To answer a Boolean query under updates, instead of enumerate, test, or count we aim at a routine answer that produces the answer yes or no of ϕ on D. The answer time t a is the time used for performing answer. Whenever speaking of a dynamic algorithm, we mean an algorithm that has routines preprocess and update and, depending on the problem at hand, at least one of the routines answer, test, count, and enumerate.
Throughout the paper, we often adopt the view of data complexity and suppress factors that may depend on the query ϕ or the degree bound d, but not on the database D. E.g., "linear preprocessing time" means t p ď f pϕ, dq¨||D 0 || and "constant update time" means t u ď f pϕ, dq, for a function f with codomain N. When writing polypnq we mean n Op1q .

Hanf Normal Form for FO+MOD
Our algorithms for evaluating FO`MOD queries rely on a decomposition of FO`MOD queries into Hanf normal form. To describe this normal form, we need some more notation.
Two formulas ϕ and ψ of schema σ are called d-equivalent (in symbols: ϕ " d ψ) if for all σ-dbs D of degree ď d and all assignments α for ϕ and ψ in D we have pD, αq |ù ϕ ðñ pD, αq |ù ψ.
For a σ-db D and a set A Ď adompDq we write DrAs to denote the restriction of D to the domain A, i.e., R DrAs " ta P R D : a P A arpRq u, for all R P σ. For two σ-dbs D and D 1 and two k-tuples a " pa 1 , . . . , a k q and a 1 " pa 1 1 , . . . , a 1 k q of elements in adompDq and adompD 1 q, resp., we write`D, a˘-`D 1 , a 1˘t o indicate that there is an isomorphism 2 π from D to D 1 that maps a i to a 1 i for all i P rks.
The distance dist D pa, bq between two elements a, b P adompDq is the minimal length (i.e., the number of edges) of a path from a to b in D's Gaifman graph G D (if no such path exists, we let dist D pa, bq " 8; note that dist D pa, aq " 0). For r ě 0 and a P adompDq, the r-ball around a in D is the set N D r paq :" tb P adompDq : dist D pa, bq ď ru. For a σ-db D and a tuple a " pa 1 , . . . , a k q we let N D r paq :" The r-neighbourhood around a in D is defined as the σ-db N D r paq :" DrN D r paqs. For r ě 0 and k ě 1, a type τ (over σ) with k centres and radius r (for short: r-type with k centres) is of the form pT, tq, where T is a σ-db, t P adompT q k , and adompT q " N T r ptq. The elements in t are called the centres of τ . For a tuple a P adompDq k , the r-type of a in D is defined as the r-type with k centres`N D r paq, a˘. For a given r-type with k centres τ " pT, tq it is straightforward to construct a first-order formula sph τ pxq (depending on r and τ ) with k free variables x " px 1 , . . . , x k q which expresses that the r-type of x is isomorphic to τ , i.e., for every σ-db D and all a " pa 1 , . . . , a k q P adompDq k we have`D, a˘|ù sph τ pxq ðñ`N D r paq, a˘-`T, t˘. The formula sph τ pxq is called a sphereformula (over σ and x); the numbers r and k are called locality radius and arity, resp., of the sphere-formula.
A Hanf-sentence (over σ) is a sentence of the form D ěm x sph τ pxq or D i mod m x sph τ pxq, where τ is an r-type (over σ) with 1 centre, for some r ě 0. The number r is called locality radius of the Hanf-sentence. A formula in Hanf normal form (over σ) is a Boolean combination 3 of sphereformulas and Hanf-sentences (over σ). The locality radius of a formula ψ in Hanf normal form is the maximum of the locality radii of the Hanf-sentences and the sphere-formulas that occur in ψ. The formula is d-bounded if all types τ that occur in sphere-formulas or Hanf-sentences of ψ are d-bounded, i.e., T is of degree ď d, where τ " pT, tq. Our query evaluation algorithms for FO`MOD rely on the following result by Heimberg, Kuske, and Schweikardt [10].
). There is an algorithm which receives as input a degree bound d P N and a FO`MODrσs-formula ϕ, and constructs a d-equivalent formula ψ in Hanf normal form (over σ) with the same free variables as ϕ. For any d ě 2, the formula ψ is d-bounded and has locality radius ď 4 qrpϕq , and the algorithm's runtime is 2 d 2 Op||ϕ||`||σ||q .
The first step of all our query evaluation algorithms is to use Theorem 3.1 to transform a given query ϕpxq into a d-equivalent query ψpxq in Hanf normal form. The following lemma summarises easy facts that are useful for evaluating the sphere-formulas that occur in ψ.
(b) Given D and a, the r-neighbourhood N D r paq can be computed in time`kd r`1˘O p||σ||q .
(c) N D r pa 1 , a 2 q is connected if and only if dist D pa 1 , a 2 q ď 2r`1.
(e) Let D 1 be a σ-db of degree ď d and let b " pb 1 , . . . , b k q P adompD 1 q.
Proof. Parts (a)-(d) are straightforward. Concerning Part (e), a brute-force approach is to loop through all mappings from N D r paq to N D 1 r pbq that map a i to b i for every i P rks and check whether this mapping is an isomorphism. Each such check can be accomplished in time n Op||σ||q for n :" kd r`1 , and the number of mappings that have to be checked is ď n n . Thus, the isomorphism test is accomplished in time n Opn`||σ||q " pkd r`1 q Op||σ||`kd r`1 q .
The time bound stated in part (e) of Lemma 3.2 is obtained by a brute-force approach. When using Luks' polynomial time isomorphism test for bounded degree graphs [15], the time bound of Lemma 3.2(e) can be improved to`kd r`1˘p olypd||σ||q . However, the asymptotic overall runtime of our algorithms for evaluating FO`MOD-queries won't improve when using Luks algorithm instead of the brute-force isomorphism test of Lemma 3.2(e).

Answering Boolean FO+MOD Queries Under Updates
In [7], Frick and Grohe showed that in the static setting (i.e., without database updates), Boolean

FO-queries ϕ can be answered on databases
|D||. Our first main theorem extends their result to FO`MOD-queries and the dynamic setting.
There is a dynamic algorithm that receives a schema σ, a degree bound d ě 2, a Boolean FO`MODrσs-query ϕ, and a σ-db D 0 of degree ď d, and computes within t p " f pϕ, dq¨||D 0 || preprocessing time a data structure that can be updated in time t u " f pϕ, dq and allows to return the query result ϕpDq with answer time t a " Op1q. The function f pϕ, dq is of . If ϕ is a d-bounded Hanf-sentence of locality radius r, then f pϕ, dq " 2 Op||σ||d 2r`2 q , and the initialisation time is t i " Op||ϕ||q.
Proof. W.l.o.g. we assume that all the symbols of σ occur in ϕ (otherwise, we remove from σ all symbols that do not occur in ϕ). In the preprocessing routine, we first use Theorem 3.1 to transform ϕ into a d-equivalent sentence ψ in Hanf normal form; this takes time 2 d 2 Op||ϕ||q . The sentence ψ is a Boolean combination of d-bounded Hanf-sentences (over σ) of locality radius at most r :" 4 qrpϕq . Let ρ 1 , . . . , ρ s be the list of all types that occur in ψ. Thus, every Hanf-sentence in ψ is of the form D ěk x sph ρ j pxq or D i mod m x sph ρ j pxq for some j P rss and k, i, m P N with k ě 1, m ě 2, and i ă m. For each j P rss let r j be the radius of sph ρ j pxq. Thus, ρ j is an r j -type with 1 centre (over σ).
For each j P rss our data structure will store the number Arjs of all elements a P adompDq whose r j -type is isomorphic to ρ j , i.e., pN D r j paq, aqρ j . The initialisation for the empty database D H lets Arjs " 0 for all j P rss. In addition to the array A, our data structure stores a Boolean value Ans where Ans " ϕpDq is the answer of the Boolean query ϕ on the current database D. This way, the query can be answered in time Op1q by simply outputting Ans. The initialisation for the empty database D H computes Ans as follows. Every Hanfsentence of the form D ěk x sph ρ j pxq in ψ is replaced by the Boolean constant false. Every Hanfsentence of the form D i mod m x sph ρ j pxq is replaced by true if i " 0 and by false otherwise. The resulting formula, a Boolean combination of the Boolean constants true and false, then is evaluated, and we let Ans be the obtained result. The entire initialisation takes time at most . If ϕ is a Hanf-sentence, we even have t i " Op||ϕ||q.
To update our data structure upon a command update Rpa 1 , . . . , a k q, for k " arpRq and update P tinsert, deleteu, we proceed as follows. The idea is to remove from the data structure the information on all the database elements whose r j -neighbourhood (for some j P rss) is affected by the update, and then to recompute the information concerning all these elements on the updated database.
Let D old be the database before the update is received and let D new be the database after the update has been performed. We consider each j P rss. All elements whose r j -neighbourhood might have changed, belong to the set U j :" N D 1 r j paq, where D 1 :" D new if the update command is insert Rpaq, and D 1 :" D old if the update command is delete Rpaq.
To remove the old information from Arjs, we compute for each a P U j the neighbourhood T a :" N D old r j paq, check whether pT a , aqρ j , and if so, decrement the value Arjs. To recompute the new information for Arjs, we compute for all a P U j the neighbourhood T 1 a :" N Dnew r j paq, check whether pT 1 a , aqρ j , and if so, increment the value Arjs. Using Lemma 3.2 we obtain for each j P rss that |U j | ď kd r j`1 . For each a P U j , the neighbourhoods T a and T 1 a can be computed in time`d r j`1˘O p||σ||q , and testing for isomorphism with ρ j can be done in time`d r j`1˘O p||σ||`d r j`1 q . Thus, the update of Arjs is done in time (note that k ď ||σ|| ď ||ϕ|| and r j ď 4 qrpϕq ď 2 Op||ϕ||qq ). After having updated Arjs for each j P rss, we recompute the query answer Ans as follows. Every Hanf-sentence of the form D ěk x sph ρ j pxq in ψ is replaced by the Boolean constant true if Arjs ě k, and by the Boolean constant false otherwise. Every Hanf-sentence of the form D i mod m x sph ρ j pxq is replaced by true if Arjs " i mod m, and by false otherwise. The resulting formula, a Boolean combination of the Boolean constants true and false, then is evaluated, and we let Ans be the obtained result. Thus, recomputing Ans takes time polyp||ψ||q.
In summary, the entire update time is . In case that ϕ is a d-bounded Hanf-sentence of locality radius r, we even have t u " k¨`d r`1˘O p||σ||`d r`1 q ď 2 Op||σ||d 2r`2 q . This completes the proof of Theorem 4.1.
In [7], Frick and Grohe obtained a matching lower bound for answering Boolean FO-queries of schema σ " tEu on databases of degree at most d :" 3 in the static setting. They used the (reasonable) complexity theoretic assumption FPT ‰ AWr˚s and showed that if this assumption is correct, then there is no algorithm that answers Boolean FO-queries ϕ on σ-dbs D of degree ď 3 in time 2 2 2 op||ϕ||q¨p olyp||D||q in the static setting (see Theorem 2 in [7]). As a consequence, the same lower bound holds in the dynamic setting and shows that in Theorem 4.1, the 3-fold exponential dependency on the query size ||ϕ|| cannot be substantially lowered (unless FPT " AWr˚s): Corollary 4.2. Let σ :" tEu and let d :" 3. If FPT ‰ AWr˚s, then there is no dynamic algorithm that receives a Boolean FOrσs-query ϕ and a σ-db D 0 , and computes within t p ď f pϕq¨polyp||D 0 ||q preprocessing time a data structure that can be updated in time t u ď f pϕq and allows to return the query result ϕpDq with answer time t a ď f pϕq, for a function f with f pϕq " 2 2 2 op||ϕ||q .

Technical Lemmas on Types and Spheres Useful for Handling Non-Boolean Queries
For our algorithms for evaluating non-Boolean queries it will be convenient to work with a fixed list of representatives of d-bounded r-types, provided by the following straightforward lemma.
Lemma 5.1. There is an algorithm which upon input of a schema σ, a degree bound d ě 2, a radius r ě 0, and a number k ě 1, computes a list L σ,d r pkq " τ 1 , . . . , τ (for a suitable ě 1) of d-bounded r-types with k centres (over σ), such that for every d-bounded r-type τ with k centres (over σ) there is exactly one i P r s such that ττ i . The algorithm's runtime is 2 pkd r`1 q Op||σ||q . Furthermore, upon input of a d-bounded r-type τ with k centres (over σ), the particular i P r s with ττ i can be computed in time 2 pkd r`1 q Op||σ||q .
Throughout the remainder of this paper, L σ,d r pkq will always denote the list provided by Lemma 5.1. The following lemma will be useful for evaluating Boolean combinations of sphereformulas.
Proof. As a first step, we consider each sphere-formula ζ that occurs in ψ and replace it by a d-equivalent disjunction of sphere-formulas sph τ j pxq with τ j in L σ,d r pkq: if ζ has arity k 1 ď k and radius r 1 ď r and is of the form sph ρ px 1 q with x 1 " x ν 1 , . . . , x ν k 1 for 1 ď ν 1 ă¨¨¨ă ν k 1 ď k and ρ " pS, sq with s " s 1 , . . . , s k 1 , then we replace ζ by the formula ζ 1 :" Ž jPJ sph τ j pxq, where J consists of all those j P r s where for pT, tq " τ j with t " t 1 , . . . , t k and for t 1 :" t ν 1 , . . . , t ν k 1 we have`S, s˘-`T rN T r 1 pt 1 qs, t 1˘. It is straightforward to see that ζ 1 and ζ are d-equivalent.
Let ψ 1 be the formula obtained from ψ by replacing each ζ by ζ 1 . By the Lemmas 5.1 and 3.2, ψ 1 can be constructed in time Op||ψ||¨2 pkd r`1 q Op||σ||q q. Note that ψ 1 is a Boolean combination of formulas sph τ j pxq for j P r s.
In the second step, we repeatedly use de Morgan's law to push all -symbols in ψ 1 directly in front of sphere-formulas. Afterwards, we replace every subformula of the form sph τ j pxq by the d-equivalent formula Ž iPr sztju sph τ i pxq. Let ψ 2 be the formula obtained from ψ 1 by these transformations. Constructing ψ 2 from ψ 1 takes time at most Op||ψ 1 ||q¨2 pkd r`1 q Op||σ||q " Op||ψ||¨2 pkd r`1 q Op||σ||q q. In the third step, we eliminate all the^-symbols in ψ 2 . By the definition of the sphereformulas τ 1 , . . . , τ we have where K is an unsatisfiable formula. Thus, by the distributive law we obtain for all m ě 1 and all I 1 , . . . , I m Ď r s that sph τ i pxq for I :" I 1 X¨¨¨X I m . We repeatedly use this equivalence during a bottom-up traversal of the syntax-tree of ψ 2 to eliminate all the^-symbols in ψ 2 . The resulting formula ψ 3 is obtained in time polynomial in the size of ψ 2 . Furthermore, ψ 3 is of the desired form Ž iPI sph τ i pxq for an I Ď r s. The overall time for constructing ψ 3 and I is polyp||ψ||q¨2 pkd r`1 q Op||σ||q . This completes the proof of Lemma 5.2.
For evaluating a Boolean combination ψpxq of sphere-formulas and Hanf-sentences on a given σ-db D, an obvious approach is to first consider every Hanf-sentence χ that occurs in ψ, to check if D |ù χ, and replace every occurrence of χ in ψ with true (resp., false) if D |ù χ (resp., D * χ). The resulting formula ψ 1 pxq is then transformed into a disjunction ψ 2 pxq :" Ž iPI sph τ i pxq by Lemma 5.2, and the query result ψpDq " ψ 2 pDq is obtained as the union of the query results sph τ i pDq for all i P I.
While this works well in the static setting (i.e., without database updates), in the dynamic setting we have to take care of the fact that database updates might change the status of a Hanf-sentence χ in ψ, i.e., an update operation might turn a database D with D |ù χ into a database D 1 with D 1 * χ (and vice versa). Consequently, the formula ψ 2 pxq that is equivalent to ψpxq on D might be inequivalent to ψpxq on D 1 .
To handle the dynamic setting correctly, at the end of each update step we will use the following lemma (the lemma's proof is an easy consequence of Lemma 5.2). Lemma 5.3. Let σ be a schema. Let s ě 0 and let χ 1 , . . . , χ s be FO`MODrσs-sentences. Let r ě 0, k ě 1, d ě 2, and let L σ,d r pkq " τ 1 , . . . , τ . Let x " px 1 , . . . , x k q be a list of k pairwise distinct variables. For every Boolean combination ψpxq of the sentences χ 1 , . . . , χ s and of d-bounded sphere-formulas of radius at most r (over σ), and for every J Ď rss there is a set I Ď r s such that where ψ J is the formula obtained from ψ by replacing every occurrence of a sentence χ j with true if j P J and with false if j R J (for every j P rss).
Given ψ and J, the set I can be computed in time polyp||ψ||q¨2 pkd r`1 q Op||σ||q .
To evaluate a single sphere-formula sph τ pxq for a given r-type τ with k centres (over σ), it will be useful to decompose τ into its connected components as follows. Let τ " pT, tq with t " pt 1 , . . . , t k q. Consider the Gaifman graph G T of T and let C 1 , . . . , C c be the vertex sets of the c connected components of G T . For each connected component C j of G T , let t j be the subsequence of t consisting of all elements of t that belong to C j , and let k j be the length of t j . Since pT, tq is an r-type with k centres, we have T " N T r ptq, and thus c ď k and k j ě 1 for all j P rcs. To avoid ambiguity, we make sure that the list C 1 , . . . , C c is sorted in such a way that for all j ă j 1 we have i ă i 1 for the smallest i with t i P C j and the smallest i 1 with t i 1 P C j 1 .
For each C j consider the r-type with k j centres ρ j "`T rC j s, t j˘. Let ν j be the unique integer such that ρ j is isomorphic to the ν j -th element in the list L σ,d r pk j q, and let τ j,ν j be the ν j -th element in this list.
It is straightforward to see that the formula sph τ pxq is d-equivalent to the formula conn-sph τ pxq :" ľ jPrcs sph τ j,ν j px j q^ľ where x j is the subsequence of x obtained from x in the same way as t j is obtained from t, and dist k j ,k j 1 ď2r`1 px j , x j 1 q is a formula of schema σ which expresses that for some variable y in x j and some variable y 1 in x j 1 the distance between y and y 1 is ď 2r`1. I.e., for a " pa 1 , . . . , a k j q and b " pb 1 , . . . , b k j 1 q we have pa, bq P dist k j ,k j 1 ď2r`1 pDq ðñ dist D pa; bq ď 2r`1, where dist D pa; bq ď 2r`1 means that dist D pa i , b i 1 q ď 2r`1 for some i P rk j s and i 1 P rk j 1 s.
Using the Lemmas 3.2 and 5.1, the following lemma is straightforward.
Lemma 5.4. There is an algorithm which upon input of a schema σ, numbers r ě 0, k ě 1, and d ě 2, and an r-type τ with k centres (over σ) computes the formula conn-sph τ pxq, along with the corresponding parameters c and k j , ν j , x j , τ j,ν j for all j P rcs. The algorithm's runtime is 2 pkd r`1 q Op||σ||q .
We define the signature of τ to be the tuple sgnpτ q built from the parameters c and k j , ν j , tµ P rks : x µ belongs to x j u˘j Prcs obtained from the above lemma. The signature sgn D paq of a tuple a in a database D (w.r.t. radius r) is defined as sgnpρq for ρ :"`N D r paq, a˘. Note that a P sph τ pDq ðñ sgn D paq " sgnpτ q.

Testing Non-Boolean FO+MOD Queries Under Updates
This section is devoted to the proof of the following theorem. Theorem 6.1. There is a dynamic algorithm that receives a schema σ, a degree bound d ě 2, a k-ary FO`MODrσs-query ϕpxq (for some k P N), and a σ-db D 0 of degree ď d, and computes within t p " f pϕ, dq¨||D 0 || preprocessing time a data structure that can be updated in time t u " f pϕ, dq and allows to test for any input tuple a P dom k whether a P ϕpDq within testing time t t " Opk 2 q. The function f pϕ, dq is of the form 2 d 2 Op||ϕ||q .
For the proof, we use the lemmas provided in Section 5 and the following lemma.
Lemma 6.2. There is a dynamic algorithm that receives a schema σ, a degree bound d ě 2, numbers r ě 0 and k ě 1, an r-type τ with k centres (over σ), and a σ-db D 0 of degree ď d, and computes within t p " 2 pkd r`1 q Op||σ||q¨| |D 0 || preprocessing time a data structure that can be updated in time t u " 2 pkd r`1 q Op||σ||q and allows to test for any input tuple a P dom k whether a P sph τ pDq within testing time t t " Opk 2 q.
Proof. The preprocessing routine starts by using Lemma 5.4 to compute the formula conn-sph τ pxq, along with the according parameters c and k j , ν j , x j , τ j,ν j for each j P rcs. This is done in time 2 pkd r`1 q Op||σ||q . We let sgnpτ q be the signature of τ (defined directly after Lemma 5.4). Recall that conn-sph τ pxq " d sph τ pxq, and recall from equation (2) the precise definition of the formula conn-sph τ pxq. Our data structure will store the following information on the database D: • the set Γ of all tuples b P adompDq k 1 where k 1 ď k and N D r pbq is connected, and • for every j P rcs and every tuple b P Γ of arity k j , the unique number ν b such that ρ b :"`N D r pbq, b˘is isomorphic to the ν b -th element in the list L σ,d r pk j q.
We want to store this information in such a way that for any given tuple b P dom k 1 it can be checked in time Opkq whether b P Γ. To ensure this, we use a k 1 -ary array Γ k 1 that is initialised to 0, and where during update operations the entry Γ k 1 rbs is set to 1 for all b P Γ of arity k 1 . In a similar way we can ensure that for any given j P rcs and any b P Γ of arity k j , the number ν b can be looked up in time Opkq.
The test routine upon input of a tuple a " pa 1 , . . . , a k q proceeds as follows. First, we partition a into a 1 , . . . , a c 1 (for c 1 ď k) such that C j :" N D r pa j q for j P rc 1 s are the connected components of N D r paq. As in the definition of the formula conn-sph τ pxq, we make sure that this list is sorted in such a way that for all j ă j 1 we have i ă i 1 for the smallest i with a i P C j and the smallest i 1 with a i 1 P C j 1 . All of this can be done in time Opk 2 q by first constructing the graph H with vertex set rks and where there is an edge between vertices i and j iff the tuple pa i , a j q belongs to Γ, and then computing the connected components of H.
Afterwards, for each j P rc 1 s we use time Opkq to look up the number ν a j . We then let sgn D paq be the tuple built from c 1 and`|a j |, ν a j , tµ P rks : a µ belongs to a j u˘j Prc 1 s . It is straightforward to see that a P conn-sph τ pDq iff sgn D paq " sgnpτ q. Therefore, the test routine checks whether sgn D paq " sgnpτ q and outputs "yes" if this is the case and "no" otherwise. The entire time used by the test routine is t t " Opk 2 q.
To finish the proof of Lemma 6.2, we have to give further details on the preprocess routine and the update routine. The preprocess routine initialises Γ as the empty set H and then performs |D 0 | update operations to insert all the tuples of D 0 into the data structure. The update routine proceeds as follows.
Let D old be the database before the update is received and let D new be the database after the update has been performed. Let the update command be of the form update Rpa 1 , . . . , a arpRq q. We let r 1 :" r`parpRq´1qp2r`1q. All elements whose r 1 -neighbourhood might have changed belong to the set U :" N D 1 r 1 paq, where D 1 :" D new if the update command is insert Rpaq, and D 1 :" D old if the update command is delete Rpaq.
According to Lemma 3.2(d), all tuples b that have to be inserted into or deleted from Γ are built from elements in U . To update the information stored in our data structure, we loop through all tuples of arity ď k that are built from elements in U .
Using Lemma 3.2, we obtain that |U | ď arpRq¨d r 1`1 . The number of candidate tuples b built from elements in U is at most`arpRq¨d r 1`1˘k`1 . Using the Lemmas 3.2 and 5.1, it is not difficult to see that the entire update time is at most t u " 2 pkd r`1 q Op||σ||q . The initialisation time t i is of the same form, and hence the preprocessing time is as claimed in the lemma. This completes the proof of Lemma 6.2. Theorem 6.1 is now obtained by combining Theorem 3.1, Lemma 6.2, Theorem 4.1, and Lemma 5.3.
Proof of Theorem 6.1. For k " 0, the theorem immediately follows from Theorem 4.1. Consider the case where k ě 1. As in the proof of Theorem 4.1, we assume w.l.o.g. that all the symbols of σ occur in ϕ. We start the preprocessing routine by using Theorem 3.1 to transform ϕpxq into a d-equivalent query ψpxq in Hanf normal form; this takes time 2 d 2 Op||ϕ||q . The formula ψ is a Boolean combination of d-bounded Hanf-sentences and sphere-formulas (over σ) of locality radius at most r :" 4 qrpϕq , and each sphere-formula is of arity at most k. Let χ 1 , . . . , χ s be the list of all Hanf-sentences that occur in ψ.
We use Lemma 5.1 to compute the list L σ,d r pkq " τ 1 , . . . , τ . In parallel for each i P r s, we use the algorithm provided by Lemma 6.2 for τ :" τ i . Furthermore, for each j P rss, we use the algorithm provided by Theorem 4.1 upon input of the Hanf-sentence ϕ :" χ j . In addition to the components used by these dynamic algorithms, our data structure also stores • the set J :" tj P rss : D |ù χ j u, • the particular set I Ď r s provided by Lemma 5.3 for ψpxq and J, and • the set K " tsgnpτ i q : i P Iu, where for each type τ , sgnpτ q is the signature of τ defined directly after Lemma 5.4.
The test routine upon input of a tuple a " pa 1 , . . . , a k q proceeds in the same way as in the proof of Lemma 6.2 to compute in time Opk 2 q the signature sgn D paq of the tuple a. For every i P r s we have a P sph τ i pDq ðñ sgn D paq " sgnpτ i q. Thus, a P ϕpDq ðñ sgn D paq P K. Therefore, the test routine checks whether sgn D paq P K and outputs "yes" if this is the case and "no" otherwise. To ensure that this test can be done in time Opk 2 q, we use an array construction for storing K (similar to the one for storing Γ in the proof of Lemma 6.2).
The update routine runs in parallel the update routines for all the used dynamic data structures. Afterwards, it recomputes J by calling the answer routine for χ j for all j P rss. Then, it uses Lemma 5.3 to recompute I. The set K is then recomputed by applying Lemma 5.4 for τ :" τ i for all i P I. It is straightforward to see that the overall runtime of the update . This completes the proof of Theorem 6.1.

Representing Databases by Coloured Graphs
To obtain dynamic algorithms for counting and enumerating query results, it will be convenient to work with a representation of databases by coloured graphs that is similar to the representation used in [6]. For defining this representation, let us consider a fixed d-bounded r-type τ with k centres (over a schema σ). Use Lemma 5.4 to compute the formula conn-sph τ pxq (for x " px 1 , . . . , x k q) and the according parameters c and k j , ν j , x j , τ j,ν j , and let sgnpτ q be the signature of τ . To keep the notation simple, we assume w.l.o.g. that x 1 " x 1 , . . . , x k 1 , Recall that sph τ pxq is d-equivalent to the formula conn-sph τ pxq :" ľ jPrcs sph τ j,ν j px j q^ľ To count or enumerate the results of the formula sph τ pxq we represent the database D by a c-coloured graph G D . Here, a c-coloured graph G is a database of the particular schema where E is a binary relation symbol and C 1 , . . . , C c are unary relation symbols. We define G D in such a way that the task of counting or enumerating the results of the query sph τ pxq on the database D can be reduced to counting or enumerating the results of the query ϕ c pz 1 , . . . , z c q :" on the c-coloured graph G D . The vertices of G D correspond to tuples over adompDq whose rneighbourhood is connected; a vertex has colour C j if its associated tuple a is in sph τ j,ν j pDq; and an edge between two vertices indicates that dist D pa; bq ď 2r`1, for their associated tuples a and b. The following lemma allows to translate a dynamic algorithm for counting or enumerating the results of the query ϕ c pz 1 , . . . , z c q on c-coloured graphs into a dynamic algorithm for counting or enumerating the result of the query sph τ pxq on D.
Lemma 7.1. Suppose that the counting problem (the enumeration problem) for ϕ c pzq on σ c -dbs of degree at most d 1 can be solved by a dynamic algorithm with initialisation time t i pc, d 1 q, update time t u pc, d 1 q, and counting time t c pc, d 1 q (delay t d pc, d 1 q). Then for every schema σ and every d ě 2 the following holds.
(1) Let r ě 0, k ě 1, τ a d-bounded r-type with k centres, and fix d 1 :" d 2k 2 p2r`1q and r t x :" max k c"1 t x pc, d 1 q for t x P tt i , t u , t c , t d u. The counting problem (the enumeration problem) for sph τ pxq on σ-dbs of degree at most d can be solved by a dynamic algorithm with counting time r t c (delay Op r t d kq), update time t 1 u ď r t u d Opk 2 r`k||σ||q`2Op||σ||k 2 d 2r`2 q , and initialisation time r t i .
(2) The counting problem (the enumeration problem) for k-ary FO`MOD-queries ϕpxq on σdbs of degree at most d can be solved with counting time Op1q (delay Op p t d kq), update time Proof. We prove part (1) by a reduction from conn-sph τ pxq to ϕ c . We use the notation introduced at the beginning of Section 7, and we let τ j :" τ j,ν j for every j P rcs. For a σ-db D we let G D be the σ c -db with C G D j :" t v a : a P adompDq k j with`N D r paq, a˘-τ j u, for all j P rcs, and E G D :" t pv a , v b q P V 2 : dist D pa; bq ď 2r`1 u, where V :" Ť jPrcs C G D j . We will shortly write E and C j instead of E G D and C G D j . Using Lemma 3.2 (and the fact that τ j is connected) we obtain that pv a , v b q P E iff N D r pa, bq is connected. If N D r pa, bq is connected, then b P`N D r`p|a|`|b|´1qp2r`1q pa 1 q˘| b| . It follows that the degree of G D is bounded by d 2k 2 p2r`1q . Furthermore, by the definition of G D and ϕ c we get that pa 1 , . . . , a c q P sph τ pDq ðñ pv a 1 , . . . , v ac q P ϕ c pG D q, for all tuples a 1 , . . . , a c where a j has arity k j for each j P rcs. As a consequence, |sph τ pDq| " |ϕ c pG D q|, and we can therefore use the count routine for ϕ c on G D to count the number of tuples in sph τ pDq. Furthermore, for each tuple pv a 1 , . . . , v ac q P ϕ c pG D q we can compute pa 1 , . . . , a c q in time Opkq. Therefore, given an enumerate routine for ϕ c pG D q with delay t d we can produce an enumeration of sph τ pDq with delay Opt d kq.
It remains to show how to construct and maintain G D when the database D is updated. As initialisation for the empty database D H we just perform the init routine of the dynamic algorithm for ϕ c pzq on σ c -dbs of degree at most d 1 . The update routine of the dynamic algorithm for sph τ pxq on σ-dbs of degree at most d is provided by the following claim.
Claim 7.2. If D new is obtained from D old by one update step, then G Dnew can be obtained from G D old by d Opk 2 r`k||σ||q update steps and additional computing time 2 Op||σ||k 2 d 2r`2 q .
Proof. Let the update command be of the form update Rpa 1 , . . . , a arpRq q with a " pa 1 , . . . , a arpRq q. Let r 1 " r`pk´1qp2r`1q. Let D 1 P tD old , D new u be the database whose relation R contains the tuple a (either before deletion or after insertion). Note that all elements whose r 1 -neighbourhood might have changed, belong to the set U :" N D 1 r 1 paq. For every j P rcs and every tuple b of arity at most k of elements in U , we check whether the r-type`N Dnew r pbq, b˘of b is isomorphic to τ j . Depending on the outcome of this test, we include or exclude v b from the relation C j . Note that it indeed suffices to consider the tuples b built from elements in U : The r-type of some tuple b is changed by the update command only if N D 1 r pbq contains some element from a. Furthermore, we only have to consider tuples b whose r-neighbourhood N D 1 r pbq is connected. Using Lemma 3.2(d), we therefore obtain that each component of b belongs to N D 1 r 1 paq " U . Afterwards, we update the coloured graph's edge relation E: We consider all tuples b and b 1 of arity ď k built from elements in U , and check whether (1) there is a j P rcs such that b P C j , (2) there is a j 1 P rcs such that b 1 P C j 1 , and (3) dist Dnew pb; b 1 q ď 2r`1. If all three checks return the result "yes", then we insert the tuple`v b , v b 1˘into E, otherwise we remove it from E.
For each such b we use Lemma 3.2(e) to check in time 2 Op||σ||k 2 d 2r`2 q whether the r-type of b is isomorphic to τ j , for some j P rcs. In summary, for updating the sets C 1 , . . . , C c we use at most c|U | k`1 ď d Opk 2 r`k||σ||q calls of the update routine of the dynamic algorithm on coloured graphs, and in addition to that we use computation time at most 2 Op||σ||k 2 d 2r`2 q .
By a similar reasoning we obtain that also the edge relation E can be updated by at most d Opk 2 r`k||σ||q calls of the update routine of the dynamic algorithm on coloured graphs and additional computation time at most 2 Op||σ||k 2 d 2r`2 q . For this note that we can use and maintain an additional array that allows us to check, for any a i and b j , in constant time whether dist D pa i , b j q ď 2r`1. This completes the proof of Claim 7.2.
Finally, the preprocess routine of the dynamic algorithm for sph τ pxq proceeds in the obvious way by first calling the init routine for D H and then performing |D 0 | update steps to insert all the tuples of D 0 into the data structure. This completes the proof of part (1) of Lemma 7.1.
We now turn to the proof of part (2) of Lemma 7.1. For k " 0, the result follows immediately from Theorem 4.1. Consider the case where k ě 1. W.l.o.g. we assume that all the symbols of σ occur in ϕ (otherwise, we remove from σ all symbols that do not occur in ϕ). We start the preprocessing routine by using Theorem 3.1 to transform ϕpxq into a d-equivalent query ψpxq in Hanf normal form; this takes time 2 d 2 Op||ϕ||q . The formula ψ is a Boolean combination of d-bounded Hanf-sentences and sphere-formulas (over σ) of locality radius at most r :" 4 qrpϕq , and each sphere-formula is of arity at most k. Note that for d 1 :" d 2k 2 p2r`1q as used in the lemma's part (1), it holds that d 1 " d 2 Op||ϕ||q . Let χ 1 , . . . , χ s be the list of all Hanf-sentences that occur in ψ (recall that s ď 2 d 2 Op||ϕ||q ).
We use Lemma 5.1 to compute the list L σ,d r pkq " τ 1 , . . . , τ (note that ď 2 d 2 Op||ϕ||q ). In parallel for each i P r s, we use the dynamic algorithm for sph τ i pxq provided from the lemma's part (1). Furthermore, for each j P rss, we use the dynamic algorithm provided by Theorem 4.1 upon input of the Hanf-sentence ϕ :" χ j . In addition to the components used by these dynamic algorithms, our data structure also stores • the set J :" tj P rss : D |ù χ j u, • the particular set I Ď r s provided by Lemma 5.3 for ψpxq and J, and • the cardinality n " |ϕpDq| of the query result.
The count routine simply outputs the value n in time Op1q. The enumerate routine runs the enumerate routine on sph τ i pDq for every i P I. Note that this enumerates, without repetition, all tuples in ϕpDq, because by Lemma 5.3, ϕpDq is the union of the sets sph τ i pDq for all i P I, and this is a union of pairwise disjoint sets. The update routine runs in parallel the update routines for all used dynamic data structures. Afterwards, it recomputes J by calling the answer routine for χ j for all j P rss. Then, it uses Lemma 5.3 to recompute I. The number n is then recomputed by letting n " ř iPI n i , where n i is the result of the count routine for τ i . It is straightforwardto verify that the overall runtime of the update routine is bounded by

Counting Results of FO+MOD Queries Under Updates
This section is devoted to the proof of the following theorem.
Theorem 8.1. There is a dynamic algorithm that receives a schema σ, a degree bound d ě 2, a k-ary FO`MODrσs-query ϕpxq (for some k P N), and a σ-db D 0 of degree ď d, and computes within t p " f pϕ, dq¨||D 0 || preprocessing time a data structure that can be updated in time t u " f pϕ, dq and allows to return the cardinality |ϕpDq| of the query result within time Op1q.
The function f pϕ, dq is of the form 2 d 2 Op||ϕ||q .
The theorem follows immediately from Lemma 7.1(2) and the following dynamic counting algorithm for the query ϕ c pzq.
There is a dynamic algorithm that receives a number c ě 1, a degree bound d ě 2, and a σ c -db G 0 of degree ď d, and computes |ϕ c pGq| with d Opc 2 q initialisation time, Op1q counting time, and d Opc 2 q update time.
Note that n 3 " |ϕ c pGq| is the desired size of the query result. Therefore, the count routine can answer in time Op1q by just outputting the number n 3 .
It remains to show how these values can be initialised and updated during updates of G. The initialisation for the empty graph initialises all the values to 0. In the update routine, the values for |C G i | and n 1 can be updated in a straightforward way (using time Opcq). For each K Ď J, the update of |ϕ K pGq| is provided within time d Opc 2 q by the following Claim 8.3. Claim 8.3. For every K Ď J, the cardinality |ϕ K pGq| of a σ c -db G of degree at most d can be updated within time d Opc 2 q after d Opc 2 q¨| G 0 | preprocessing time.
Proof. Consider the directed graph H :" pV, Kq with vertex set V :" rcs and edge set K. Decompose the Gaifman graph of H into its connected components. Let V 1 , . . . , V s be the connected components (for a suitable s ď c). For each i P rss let H i :" HrV i s be the induced subgraph of H on V i . We write K i to denote the set of edges of H i . For every i P rss let i " |V i |, and let tpi, 1q ă tpi, 2q ă¨¨¨ă tpi, i q be the ordered list of the vertices in V i . Consider the query ϕ K i pz tpi,1q , . . . , z tpi, i q q :" Note that ϕ K is the conjunction of the formulas ϕ K i for all i P rss. Since the variables of the formulas ϕ K i for i P rss are pairwise disjoint, we have ϕ K pGq " ϕ K 1 pGqˆ¨¨¨ˆϕ Ks pGq (modulo permutations of the tuples), and thus |ϕ K pGq| " ś iPrss |ϕ K i pGq|. For each i P rss, the value |ϕ K i pGq| can be computed as follows. For every v P adompGq we consider the set S v i :" tpw tpi,1q , . . . , w tpi, i q q P ϕ K i pGq : w tpi,1q " vu. Since the Gaifman graph of H i is connected and has i nodes, it follows that each component of every tuple in S v i is contained in the i -neighbourhood of v in G, and this neighbourhood contains at most d i`1 elements. Therefore, |S v i | ď pd i`1 q i , and using breadth-first search starting from v, the set S v i can be computed in time d Opc 2 q . Note that ϕ K i pGq is the disjoint union of the sets S v i for all v P adompGq. Therefore, |ϕ K i pGq| " ř vPadompGq |S v i |. In our data structure we store for every i P rss and every v P adompGq the number n i,v " |S v i |. Moreover, for every i P rss we store the sum n i " ř vPadompGq n i,v " |ϕ K i pGq|. The initialisation for the empty σ c -db G 0 sets all these values to 0. Whenever the colour of a vertex of G is updated or an edge is inserted or deleted, we update all affected numbers accordingly. Note that a number n i,v changes only if v is in the c-neighbourhood around the updated edge or vertex in the graph G. Hence, for at most 2d c`1 vertices v, the numbers n i,v are affected by an update, and each of them can be updated in time d Opc 2 q . Moreover, for each i P rss, the sum n i can be updated in time Opd c`1 q by subtracting the old value of n i,v and adding the new value of n i,v for each of the at most 2d c`1 relevant vertices v. Finally, it takes time Opcq to compute the updated value |ϕ K pGq| " ś iPrss n i . The overall time used to produce the update is d Opc 2 q .
Once we have available the updated numbers |ϕ K pGq| for all K Ď J, the value n 2 can be computed in time Op|2 J |q ď 2 Opc 2 q . And n 3 is then obtained in time Op1q. Altogether, performing the update routine takes time at most d Opc 2 q . The preprocess routine initialises all values for the empty graph and then uses |G 0 | update steps to insert all the tuples of G 0 into the data structure. This completes the proof of Lemma 8.2.

Enumerating Results of FO+MOD Queries Under Updates
In this section we prove (and afterwards, improve) the following theorem.
Theorem 9.1. There is a dynamic algorithm that receives a schema σ, a degree bound d ě 2, a k-ary FO`MODrσs-query ϕpxq (for some k P N), and a σ-db D 0 of degree ď d, and computes within t p " f pϕ, dq¨||D 0 || preprocessing time a data structure that can be updated in time t u " f pϕ, dq and allows to enumerate ϕpDq with d 2 Op||ϕ||q delay.
The function f pϕ, dq is of the form 2 d 2 Op||ϕ||q .
The theorem follows immediately from Lemma 7.1(2) and the following dynamic enumeration algorithm for the query ϕ c pzq.
Lemma 9.2. There is a dynamic algorithm that receives a number c ě 1, a degree bound d ě 2, and a σ c -db G 0 of degree ď d, and computes within t p " d polypcq¨| G 0 | preprocessing time a data structure that can be updated in time d polypcq and allows to enumerate the query result ϕ c pGq with Opc 3 dq delay.
Proof. For a σ c -db G and a vertex v P adompGq we let N G pvq be the set of all neighbours of v in G. I.e., N G pvq is the set of all w P adompGq such that pv, wq or pw, vq belongs to E G .
The underlying idea of the enumeration procedure is the following greedy strategy. We cycle through all vertices N G pu i q and output pu 1 , . . . , u c q. This strategy does not yet lead to a constant delay enumeration, as there might be vertex tuples pu 1 , . . . , u i q (for i ă c) that do extend to an output tuple pu 1 , . . . , u c q, but where many possible extensions are checked before this output tuple is encountered. We now show how to overcome this problem and describe an enumeration procedure with Opc 3 dq delay and update time d polypcq .
Note that for every J Ď rcs we haveˇˇŤ jPJ N G pu j qˇˇď cd. Hence, if a set C G i contains more than cd elements, we know that every considered tuple has an extension u i P C G i that is not a neighbour of any vertex in the tuple. Let I :" ti P rcs : |C G i | ď cdu be the set of small colour classes in G and to simplify the presentation we assume without loss of generality that I " t1, . . . , su. In our data structure we store the current index set I and the set S :" pu 1 , . . . , u s q P C G 1ˆ¨¨¨ˆC G s : pu j , u j 1 q R E G , for all j ‰ j 1 ( of tuples on the small colours. Note that a tuple pu 1 , . . . , u s q P C G 1ˆ¨¨¨ˆC G s extends to an output tuple pu 1 , . . . , u c q P ϕ c pGq if and only if it is contained in S. We store the current sizes of all colours and this enables us to keep the set I of small colours updated. Moreover, as |S| ď pcdq c , we can update the set S in time d polypcq after every update by a brute-force approach. The enumeration procedure is given in Algorithm 1.
Algorithm 1 Enumeration procedure with delay Opc 3 dq 1: for all pu 1 , . . . , u s q P S do Enumpu 1 , . . . , u s q. 2: Output the end-of-enumeration message EOE. if i " c then output the tuple pu 1 , . . . , u c q. for all u i`1 P C G i`1 do 8: if u i`1 R Ť i j"1 N G pu j q then Enumpu 1 , . . . , u i , u i`1 q.
It is straightforward to see that this procedure enumerates ϕ c pGq. Let us analyse the delay. Since for all i ą s we haveˇˇC G iˇą cd, it follows that every call of Enumpu 1 , . . . , u i q leads to at least one recursive call of Enumpu 1 , . . . , u i , u i`1 q. Furthermore, there are at most cd iterations of the loop in line 7 that do not lead to a recursive call. As every test in line 8 can be done in time Opcq, it follows that the time spans until the first recursive call, between the calls, and after the last call are bounded by Opc 2 dq. As the recursion depth is c, the overall delay between two output tuples is bounded by Opc 3 dq.
By using similar techniques as in [6], we obtain the following improved version of Lemma 9.2 where the delay is independent of the degree bound d.
There is a dynamic algorithm that receives a number c ě 1, a degree bound d ě 2, and a σ c -db G 0 of degree ď d, and computes within t p " d polypcq¨| G 0 | preprocessing time a data structure that can be updated in time d polypcq and allows to enumerate the query result ϕ c pGq with Opc 2 q delay.
Before proving Lemma 9.3, let us first point out that Lemma 9.3 in combination with Lemma 7.1(2) directly improves the delay in Theorem 9.1 from d 2 Op||ϕ||q to Opk 3 q, immediately leading to the following theorem.
Theorem 9.4. There is a dynamic algorithm that receives a schema σ, a degree bound d ě 2, a k-ary FO`MODrσs-query ϕpxq (for some k P N), and a σ-db D 0 of degree ď d, and computes within t p " f pϕ, dq¨||D 0 || preprocessing time a data structure that can be updated in time t u " f pϕ, dq and allows to enumerate ϕpDq with Opk 3 q delay.
The function f pϕ, dq is of the form 2 d 2 Op||ϕ||q .
The rest of the section is devoted to the proof of Lemma 9.3.
Proof of Lemma 9.3. Consider Algorithm 1, which enumerates ϕ c pGq with Opc 3 dq delay. To enumerate the tuples with only Opc 2 q delay, we replace the loop in lines 7-8 by a precomputed "skip" function that allows to iterate through all elements in C G i`1 z Ť i j"1 N G pu j q with Opcq delay. For every i P rcs we store all elements of C G i in a doubly linked list and let void be an auxiliary element that appears at the end of the list. We let first i be the first element in the list and succ i puq the successor of u P C G i . We denote by ď i the linear order induced by this list. We letẼ G be the symmetric closure of E G , i.e.,Ẽ G " E G Y tpv, uq : pu, vq P E G u. For every i P rcs we define the function skip i py, V q :" min ! z P C G i Y tvoidu : y ď i z and for all v P V, pv, zq RẼ G ) , which assigns to every V Ď adompGq with |V | ď c´1, and every y P C G i the next node that is not adjacent to any vertex in V .
Using these functions, our improved enumeration algorithm is given in Algorithm 2. Below, we show that we can access the values skip i py, V q in time Opcq. By the same analysis as given in the proof of Lemma 9.2 it then follows that Algorithm 2 enumerates ϕ c pGq with Opc 2 q delay.
What remains to show is that we can access the values skip i py, V q for all i, y, V in time Opcq and maintain them with d polypcq update time. At first sight, this is not clear at all, because the domain of skip i has size Ωp|adompGq| c q. In what follows, we show that for every y, the number of distinct values that skip i py, V q can take is bounded by d polypcq , and that we can store them in a look-up table with update time d polypcq .
To illustrate the main idea, let us start with a simple example. We want to enumerate ϕ 4 on a coloured graph H with four vertex colours blue, red, yellow, and green (in this order) and analyse the call of Enumpb, r, yq, which is supposed to enumerate all green nodes g i that are not adjacent to any of the nodes b, r, and y. The relevant part of H is depicted in Figure 1.

12:
y Ð skip i`1 psucc i`1 pyq, tu 1 , . . . , u i uq. To do this, we want to know the neighbours of the vertices that we skip (b and r in our example) when looking at g 1 . For this purpose, we define inductively new sorts of edges E 1 4 Ď E 2 4 Ď¨¨¨that connect green vertices g i withẼ-neighbours of skipped vertices. In our example, we first have to skip g 1 , because it isẼ-connected to b and we indicate this by letting E 1 4 be the set of tuples pg i , vq PẼ H (see Figure 2). After realising that even more vertices (g 2 and g 3 ) are excluded by b, the next try would be g 4 . However, this vertex is excluded by itsẼ-neighbour r, so we have to take r into account when computing the skip value for g 1 and indicate this by the E 2 4 -edge pg 1 , rq (see Figure 3). This immediately leads to an inductive definition: E 2 4 contains all pairs of vertices that are already in E 1 4 or connected by a path as shown in Figure 4. The idea outlined above can be formalised as follows. For i, j P rcs, we define inductively the auxiliary edge sets E j i : :" t py, uq : y P C G i and py, uq PẼ G u and E j`1 i :" E j i Y py, uq : there are v, z with py, vq P E j i , pv, zq PẼ G , psucc i pzq, uq PẼ G ( Now we define for every y P C G i the set S y i :" t u : py, uq P E c i u . Note that |S y i | " Opd 2c q. The following claim states that S y i are the only vertices we need to take into account when computing skip i py, V q.
Claim 9.5. For all i ď c, y P C G i Y tvoidu, and V Ď adompGq with |V | ď c´1 it holds that skip i py, V q " skip i py, V X S y i q .
Proof. The proof is identical to the proof of Claim 1 in [6]. For the reader's convenience, we include a proof here. If y " void, the lemma is trivial. Hence assume that y ‰ void and let z :" skip i py, V X S y i q. By definition we have y ď i z ď i skip i py, V q and therefore we have to show z ě i skip i py, V q, which holds if and only if pu, zq RẼ G for all u P V zS y i . If z " y, the claim clearly holds as all E G -neighbours of y are contained in S y i . Hence we have z ą i y and let z 1 ě i y be the predecessor of z, i.e., z " succ i pz 1 q. Now assume for contradiction that there is an u P V zS y i such that (˚) pu, zq PẼ G . Note that since z 1 ă i z " skip i py, V X S y i q, there is a v P V X S y i such that (˚˚) pv, z 1 q PẼ G . In the following we show that (˚˚˚) py, vq P E c´1 i . Note that this finishes the proof of the claim, as by the definition of E c i , the statements (˚), (˚˚), and (˚˚˚) imply that u P S y i , contradicting the assumption that u P V zS y i . To show that py, vq P E c´1 i , let for all j P rcs. Note that V c " V X S y i . Furthermore, if there is a j ă c with V j " V j`1 , then we have V j " V j`1 "¨¨¨" V c " V X S y i .
Since |V | ď c´1 and u P V zS y i , we have |V X S y i | ď c´2. In particular, it holds that V c´1 " V X S y i . Since v P V X S y i , it holds that v P V c´1 and thus py, vq P E c´1 i .
In our dynamic algorithm we maintain an array that allows random access to the values skip i py, S 1 q for all y P C G i and all S 1 Ď S y i of size at most c´1. By Claim 9.5 we can then compute skip i py, V q by first computing S 1 " V X S y i and then looking up skip i py, S 1 q. This can be done in time Opcq. The next claim states that we can efficiently maintain the sets S y i . Claim 9.6. There is a data structure that An obvious future task is to investigate to which extent further query evaluation results that are known for the static setting can be lifted to the dynamic setting. More specifically: Are there efficient dynamic algorithms for evaluating (i.e., answering, testing, counting, or enumerating) results of first-order queries on other sparse classes of databases (e.g. planar, bounded treewidth, bounded expansion, nowhere dense) or databases of low degree, lifting the "static" results accumulated in [12,9,6] to the dynamic setting?