<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Archiving and Interchange DTD v1.0 20120330//EN" "JATS-archivearticle1.dtd">
<article xmlns:xlink="http://www.w3.org/1999/xlink">
  <front>
    <journal-meta />
    <article-meta>
      <title-group>
        <article-title>Extending Datalog with Analytics in LogicBlox</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Molham Aref</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Benny Kimelfeld?</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
          <xref ref-type="aff" rid="aff1">1</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Emir Pasalic</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Nikolaos Vasiloglou</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>LogiQL Basics</string-name>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>LogicBlox, Inc</institution>
        </aff>
        <aff id="aff1">
          <label>1</label>
          <institution>Technion</institution>
          ,
          <country country="IL">Israel</country>
        </aff>
      </contrib-group>
      <abstract>
        <p>LogicBlox is a database product designed for enterprise software development, combining transactions and analytics. The underying data model is a relational database, and the query language, LogiQL, is an extension of Datalog [13]. As such, LogiQL features a simple and unified syntax for traditional relational manipulation as well as deeper analytics. Moreover, its declarative nature allows for substantial static analysis for optimizing evaluation schemes, parallelization, and incremental maintenance, and it allows for sophisticated transactional management [11]. In this paper, we describe various extensions of Datalog for supporting prescriptive and predictive analytics. These extensions come in the form of mathematical optimization (mixed integer programming), machine-learning capabilities, statistical relational models, and probabilistic programming. Some of these extensions are currently implemented in LogicQL, while others are in either development or planning phases. In this section we briefly (and informally) describe LogiQL. (See [13] for a full description of the language.) The core components of LogiQL are its rules and constraints, which are stated over the predicates of the database schema. A (basic) derivation rule is a standard Datalog rule that defines how new facts are derived in the database. For example, the following rules define Anc as the transitive closure of the predicate Par.</p>
      </abstract>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>Introduction</title>
      <p>Par(x; y)</p>
      <p>Anc(x; z); Par(z; y)
An integrity constraint does not derive new facts, but rather fires an error when violated.
For example, the rule Anc(x; y) ! x 6= y states that ancestorship is antireflexive, and
the rule</p>
      <p>Par(x; y); Par(x; z) ! y = z
states that every object has at most one parent, that is, in Par the first element is a
key. For interpretability sake, brackets are used to implicitly express key constraints,
as in Par[x] = y. Derivation rules and constraints syntactically differ in the direction
of the implication arrow. The logical language for specifying the right-hand-side of
? Taub Fellow – supported by the Taub Foundation
rules, as well as both sides of constraints, allows arbitrary propositional formulas with
numerical operations and comparisons (while safety conditions, such as stratification,
may be imposed to bound complexity).</p>
      <p>Predicate-to-Predicate (P2P) rules are used for deriving whole relations over tuples.
An example of such a rule is the aggregate P2P rule, such as the following one that sums
up the salaries for each employee.</p>
      <p>Annual[e] = a
agg
a = sum(s)</p>
      <p>Salary(e; 2014; s)
Such a rule always includes a component of the form func arg , where func is the
type of the predicate rule, and arg is an additional argument that gives specific
arguments for the rule. In the above rule, e is a grouping variable (as it occurs in the head).
This rule derives a fact Annual(e; a) for every e in the first attribute of Salary.
3</p>
    </sec>
    <sec id="sec-2">
      <title>Extensions for Analytics</title>
      <p>We now describe several extensions of the LogicBlox system, for supporting
prescriptive and predictive analytics.
3.1</p>
      <sec id="sec-2-1">
        <title>Mixed Integer Programming (MIP)</title>
        <p>
          MIP is often applied for effectively solving real-world optimization problems that
naturally arise in prescriptive analytics, such as network flow optimization, resource
allocation, and scheduling. Solutions for various classes of mathematical programming
problems can be obtained by deploying specialized, highly optimized solvers (e.g., [
          <xref ref-type="bibr" rid="ref1">12,
1</xref>
          ]). As an example, a warehouse supplies stores s with products i from its available
inventory ai. For each store s and product i there is a demand dis and available inventory
ais. For each product i, let wi denote the quantity of i in the warehouse, and pi denote
its unit price. A set of N trucks delivers commodity, and for simplicity assume that
each truck makes a single warehouse-to-store trip. We need to determine the quantity
qis to ship to each store in order to maximize revenue. In standard MIP notation, the
problem can be phrased as follows. Here, s is a binary variable (with values in f0; 1g)
determining whether a truck should be sent to store s, and mis is the quantity of product
i missing in order to satisfy the demand at store s.
        </p>
        <p>Maximize (X dis
i;s</p>
        <p>mis) pi subject to:
8i; s
wis + qis + mis
dis ; qis</p>
        <p>B
s ;</p>
        <p>X qis0
s0
ai ; X</p>
        <p>s0
s0</p>
        <p>N
8i; s
mis
0 ; qis
0 ; s 2 f0; 1g
In this program, we assume that a store can hold at most 1000 units of each product.
Observe that the unknown variables are the qis, mis and s (while the others are fixed).</p>
        <p>MIP declaration in LogiQL is done by means of predicates with free second-order
variables, which are essentially unknown functions over predefined domains, except
that they are eventually assigned actual values (by invoking a MIP solver). Moreover,
the objective function (that one wishes to minimize or maximize) is simply an attribute
of a relation. Linear constraints are phrased as LogiQL constraints. Hence, a LogiQL
program with MIP is an ordinary program, with the addition that attributes are marked
as second-order variables or objectives. As an example, the following program
corresponds to the above MIP specification. Here, the second-order variables are m[i; s],
q[i; s] and psi[s], and the objective is Obj(v).</p>
        <p>Obj(v)
v = sum(z)
z = (d[i; s]</p>
        <p>m[i; s]) p[i]
agg
agg
Store(s); Prod(i) ! w[i; s] + q[i; s] + m[i; s]
d[i; s]
TotalTrans[i] = v</p>
        <p>TotalTrans[i] = v ! v
q[i; s] = v1; psi[s] = v2 ! v1
v = sum(z)</p>
        <p>q[i; s] = z
a[i]
v2</p>
        <p>1000</p>
        <sec id="sec-2-1-1">
          <title>TotalTrucks(v)</title>
          <p>agg
v = sum(z)
psi[s] = z
TotalTrucks(v) ! v
m[i; s] = v ! v</p>
        </sec>
        <sec id="sec-2-1-2">
          <title>AvailableTrucks[] 0 ; (psi[s] = 0 or psi[s] = 1)</title>
          <p>
            Observe that the constraints are used in a fashion similar to the stable-model (or
answer-set) semantics [8], except that we also optimize an objective. Other similar
approaches include [
            <xref ref-type="bibr" rid="ref5">5, 10, 16, 14</xref>
            ]. As far as we know, LogicBlox is the first commercial
database system to provide native support for prescriptive analytics.
          </p>
          <p>
            The engine automatically synthesizes the necessary mathematical programming
instances from the program, and invokes a MIP solver (e.g., [
            <xref ref-type="bibr" rid="ref1">12, 1</xref>
            ]). Specifically, we
ground (i.e., eliminates quantifiers in) the problem instance in a manner similar to [15],
and translate the constraints over variable predicates into a representation that can be
consumed by the solver. Then, the solver output is used for populating the value of
marked predicates (turning unknown values into known ones). The evaluation engine
listens to updates in the relevant relations (as part of standard view maintenance), and
invokes the solver when necessary to populate unknown values. The underlying MIP
instance is constructed incrementally. For example, if a store demand changes, then only
the portion of the program that is relevant to that store is replaced in the current MIP
instance (before being re-sent to the solver to obtain an updated solution). We found
that this optimization often leads to considerable reduction in execution cost.
3.2
          </p>
        </sec>
      </sec>
      <sec id="sec-2-2">
        <title>Machine Learning (ML) Aggregates</title>
        <p>The next extension is predictive analytics by means of a built-in set of ML algorithms.
We use the special predict P2P rule that comes in two modes: learning mode (where a
model is being learned) and evaluation mode (where a model is being applied to make
predictions). We do not give here the formal syntax and semantics for these rules; rather,
we give an illustrative example.</p>
        <p>Suppose that we wish to predict the monthly sales of products in branches. We have
the following predicates:
– Hstr[sku; branch; month]=amount contains historical sales per sku (“stock
keeping unit”) and branch;
– Ftr[sku; branch; name]=value associates with every sku, branch and feature name
a corresponding feature value.</p>
        <p>The following learning rule learns a logistic-regression model for each sku and branch,
and stores the resulting model object (which is handle to a representation of the model)
in the predicate SM[sales; branch] = model.</p>
        <p>SM[s; b] = m
predict
m = logist(vjf )</p>
        <p>Hstr[s; b; t] = v; Ftr[s; b; t; n] = f
And the following evaluation rule evaluates the model to get specific predictions.</p>
        <p>Sales[s; b; t] = v</p>
        <p>predict v = eval(mjf )</p>
        <p>Unknown(s; b; t); SM[s; b] = m; Ftr[s; b; t; n] = f
3.3</p>
      </sec>
      <sec id="sec-2-3">
        <title>Statistical Relational Models</title>
        <p>
          Such models are specified by various mechanisms, including Markov Logic Networks
(MLN) [
          <xref ref-type="bibr" rid="ref6">6</xref>
          ] and Probabilistic Soft Logic (PSL) [
          <xref ref-type="bibr" rid="ref4">4</xref>
          ]. MLNs and PLSs are, intuitively,
logical formalisms that allow for soft rules. The canonical example is
        </p>
        <p>R(x)</p>
        <p>R(y) ; Friends(x; y)
where R describes a person property (e.g., “smokes” or “votes”). This rule states that
whenever x and y are friends, the property R propagates from y to x; this rule should
be taken as a hint on the unknown, and not as rigid truth. While an ordinary Datalog
program specifies a unique extension of the database, soft rules specify a probability
space over such extensions. Intuitively, the probability of a possible extension is
determined by the extent to which the rules are satisfied (where weights of rules are taken
into account). A common practice is to find the most likely extension (i.e. Maximum
APriori, or MAP, inference), such as the most likely votes given partial knowledge about
votes, and use that world as an ordinary database. We make an ongoing effort to support
MLN and PSL within LogicBlox. Our current implementation applies MAP inference
by translation into MIP. In future work we plan to include specialized algorithms to
reduce the execution cost.
3.4</p>
      </sec>
      <sec id="sec-2-4">
        <title>Probabilistic Programming Datalog (PPDL)</title>
        <p>
          Formalisms for specifying general statistical models, such as probabilistic-programming
languages [9], typically consist of two components: a specification of a stochastic
process (the prior), and a specification of observations that restrict the probability space
to a conditional subspace (the posterior). We plan to enhance LogiQL with capabilities
of probabilistic programming, in order to facilitate the design and engineering of ML
solutions. Towards that, we have initiated a theoretical exploration of such an extension.
In a recent paper [
          <xref ref-type="bibr" rid="ref3">3</xref>
          ], we have proposed Probabilistic Programming Datalog (PPDL),
which is a framework that extends LogiQL with convenient mechanisms to include
common numerical probability functions; in particular, conclusions of rules may
contain values drawn from such functions. As a (simplistic) example, assume the relation
Client(ssn; branch; #visits) that associates clients with social security numbers, local
branches, and an average number of visits (per month) in the branch. The rule
        </p>
        <sec id="sec-2-4-1">
          <title>Visits(c; b; Poisson[ ])</title>
        </sec>
        <sec id="sec-2-4-2">
          <title>Client(c; b; )</title>
          <p>associates with the client a random number of visits in the branch, where that number
is drawn from the Poisson distribution with average (parameter) #visits.</p>
          <p>
            The semantics of a program is a probability distribution over the possible outcomes
of the input database with respect to the program; these possible outcomes are minimal
solutions with respect to a related program that involves existentially quantified
variables in conclusions. Observations are naturally incorporated by means of constraints.
We focused on discrete numerical distributions (such as Poisson), but even then the
space of possible outcomes may be uncountable (as a solution can be infinite). We
defined a probability measure over possible outcomes by applying the known concept of
cylinder sets [
            <xref ref-type="bibr" rid="ref2">2</xref>
            ] to a probabilistic chase procedure. This chase is similar to that of data
exchange with tuple-generating dependencies [
            <xref ref-type="bibr" rid="ref7">7</xref>
            ], except that instead of introducing
named nulls, we sample real values from the associated distribution (e.g., Poisson[
            <xref ref-type="bibr" rid="ref5">5</xref>
            ]).
We have shown that the resulting semantics is invariant under different chases.
4
          </p>
        </sec>
      </sec>
    </sec>
    <sec id="sec-3">
      <title>Conclusions</title>
      <p>We described four approaches taken by LogicBlox to extend LogiQL with built-in
analytics. While MIP and ML aggregates are conceptually syntactic bridges between
Datalog and external solvers, statistical relational models, and PPDL feature stronger ties to
Datalog (and naturally require more in-house implementation effort). In future work we
plan to investigate the applicability of our statistical specifications to real-life problems
that arise in our business, as well as their theoretical and system aspects.
8. M. Gelfond and V. Lifschitz. The stable model semantics for logic programming. In Logic
Programming, Proceedings of the Fifth International Conference and Symposium, Seattle,
Washington, August 15-19, 1988 (2 Volumes), pages 1070–1080. MIT Press, 1988.
9. N. D. Goodman. The principles and practice of probabilistic programming. In POPL, 2013.
10. S. Greco, C. Molinaro, I. Trubitsyna, and E. Zumpano. NP datalog: A logic language for
expressing search and optimization problems. TPLP, 10(2):125–166, 2010.
11. T. J. Green, M. Aref, and G. Karvounarakis. LogicBlox, platform and language: A tutorial.</p>
      <p>In Int Conf on Datalog in Academia and Industry, 2012.
12. I. Gurobi Optimization. Gurobi optimizer reference manual, 2015.
13. T. Halpin and S. Rugaber. LogiQL: A Query Language for Smart Databases. CRC Press,
2014.
14. A. Meliou and D. Suciu. Tiresias: The database oracle for how-to queries. In SIGMOD,
pages 337–348, 2012.
15. F. Niu, C. Re´, A. Doan, and J. W. Shavlik. Tuffy: Scaling up statistical inference in markov
logic networks using an rdbms. PVLDB, 4(6):373–384, 2011.
16. T. E. Sheard. Painless programming combining reduction and search: Design principles for
embedding decision procedures in high-level languages. In ICFP, pages 89–102, 2012.</p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          1.
          <string-name>
            <given-names>T.</given-names>
            <surname>Achterberg</surname>
          </string-name>
          . Scip:
          <article-title>Solving constraint integer programs</article-title>
          .
          <source>Mathematical Programming Computation</source>
          ,
          <volume>1</volume>
          (
          <issue>1</issue>
          ),
          <year>2009</year>
          . http://mpc.zib.de/index.php/MPC/article/view/4.
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          2.
          <string-name>
            <given-names>R. B.</given-names>
            <surname>Ash</surname>
          </string-name>
          and
          <string-name>
            <given-names>C.</given-names>
            <surname>Doleans-Dade</surname>
          </string-name>
          .
          <article-title>Probability &amp; Measure Theory</article-title>
          . Harcourt Academic Press,
          <year>2000</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          3.
          <string-name>
            <given-names>V.</given-names>
            <surname>Barany</surname>
          </string-name>
          , B. t. Cate,
          <string-name>
            <given-names>B.</given-names>
            <surname>Kimelfeld</surname>
          </string-name>
          ,
          <string-name>
            <given-names>D.</given-names>
            <surname>Olteanu</surname>
          </string-name>
          , and
          <string-name>
            <given-names>Z.</given-names>
            <surname>Vagena</surname>
          </string-name>
          .
          <article-title>Declarative statistical modeling with datalog</article-title>
          .
          <source>arXiv preprint arXiv:1412.2221</source>
          ,
          <year>2014</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          4. M. Bro¨cheler, L. Mihalkova, and
          <string-name>
            <given-names>L.</given-names>
            <surname>Getoor</surname>
          </string-name>
          .
          <article-title>Probabilistic similarity logic</article-title>
          .
          <source>In UAI</source>
          ,
          <year>2010</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          5.
          <string-name>
            <given-names>M.</given-names>
            <surname>Cadoli</surname>
          </string-name>
          ,
          <string-name>
            <given-names>G.</given-names>
            <surname>Ianni</surname>
          </string-name>
          ,
          <string-name>
            <given-names>L.</given-names>
            <surname>Palopoli</surname>
          </string-name>
          ,
          <string-name>
            <given-names>A.</given-names>
            <surname>Schaerf</surname>
          </string-name>
          , and
          <string-name>
            <given-names>D.</given-names>
            <surname>Vasile</surname>
          </string-name>
          .
          <article-title>Np-spec: an executable specification language for solving all problems in np</article-title>
          .
          <source>Computer Languages</source>
          ,
          <volume>26</volume>
          (
          <issue>2</issue>
          ):
          <fpage>165</fpage>
          -
          <lpage>195</lpage>
          ,
          <year>2000</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          6.
          <string-name>
            <given-names>P.</given-names>
            <surname>Domingos</surname>
          </string-name>
          and
          <string-name>
            <given-names>D.</given-names>
            <surname>Lowd</surname>
          </string-name>
          .
          <source>Markov Logic: An Interface Layer for Artificial Intelligence. Synthesis Lectures on AI and Machine Learning</source>
          . Morgan &amp; Claypool Publishers,
          <year>2009</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref7">
        <mixed-citation>
          7.
          <string-name>
            <given-names>R.</given-names>
            <surname>Fagin</surname>
          </string-name>
          ,
          <string-name>
            <given-names>P. G.</given-names>
            <surname>Kolaitis</surname>
          </string-name>
          ,
          <string-name>
            <given-names>R. J.</given-names>
            <surname>Miller</surname>
          </string-name>
          , and
          <string-name>
            <given-names>L.</given-names>
            <surname>Popa</surname>
          </string-name>
          .
          <article-title>Data exchange: Semantics and query answering</article-title>
          .
          <source>In ICDT</source>
          , volume
          <volume>2572</volume>
          <source>of LNCS</source>
          . Springer,
          <year>2003</year>
          .
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>