231 points · by maximilianroos · 5y ago · 62 comments
The fact that this is bottom up makes me think that you probably can't go crazy with the number of rules.
For example, if we declare the predicate f/1 dynamic using the standard directive dynamic/1:
:- dynamic(f/1).
then we can assert new clauses for f/1 using the standard predicates asserta/1 and assertz/1: ?- asserta(f(a)), assertz(f(b)).
true.
We then get: ?- f(X).
X = a
; X = b.
Rules can also be asserted: ?- use_module(library(lists)), asserta((f(X):-length(_,X))).
true.
yielding: ?- f(X).
X = 0
; X = 1
; X = 2
; X = 3
; ...
I used Scryer Prolog to run these examples.Dynamic changes to the database have advantages and drawbacks. On the plus side, they let us incorporate new knowledge and rules, and we automatically benefit from built-in features such as argument indexing. However, running the same query twice can lead to different results, changes in the order of goals can affect the meaning of the conjunction, and we can no longer reason about different predicates in isolation. Dynamic changes of the database are said to be side-effects, since they affect the program in a way that is outside the logical interpretation of the given definitions.
I wonder if that is part of the benefits advertised by DDlog, the ability to update a program without having to worry about clause ordering (which one does not have to, with datalog and bottom-up evaluation) or generally side effects.
Very tentatively, I'm guessing that, since datalog programs lack negation, any new facts or rules added to the (extensional) database will update the "output" of the database (its Herbrand model) monotonically, i.e. you'll see new facts comeing out but no facts will "disappear". I'm a bit unsure because the DDlog github is talking about functions and typed datalog and I'm not really sure how that works now.
I don't see much literature out there on it (after the mid-2000s) but it always seemed a conceptually elegant way of doing forward-chaining (and inference) in the way of most rules engines. It only has a few simple rules: simplification (remove data/rules), simpagation and propagation (new data).
The best part is that it embeds very easily in Prolog.
It doesn't actually care much about the number of rules, and more about how much memory it costs to materialize them. There are delta-join techniques to do at least most of that in linear memory (w.r.t. input size) though, and DDlog has at least experimental support for them.
edit: I guess this explains some of the static nature: "A DDlog program is compiled into a Rust library"
But, do not worry: Support for run-time rule updates is in the works and would allow for arbitrary dynamic datalog queries.
I’ve been keeping an eye on work that’s been done to support this internally in Postgres. Looks like it’s still moving along. I guess it’s a pretty tricky puzzle (especially once you add aggregates). Depending on the performance implications, it would sit in a pretty sweet spot for some usecases. For example, at the moment I have a form of materialised view that’s simple too slow to build from scratch. My compromise is to run periodically, figure out which subset of the table needs refreshing and update just those rows.
https://wiki.postgresql.org/wiki/Incremental_View_Maintenanc...
You can feed a CDC to Materialize and either feed the output back to Postgres, or just query it directly in Materialize (via a normal postgres client).
It's very likely that you can cut the typical maintenance latency down below one second. Currently that likely involves using a Kafka broker between the two databases, but feel free to plop in the community Slack [0].
Am in the process of exploring kafka to do something similar, and I think it will work but requires quite a complex setup (compared to an incrementally updated materialized view)
It does integrate with your postgres database, but logical replication target mode is still a TODO. Don't worry though, this only means the change data stream has to go through a Kafka broker. There should be a guide on this (if not, plop in the community Slack and ask).
The license allows for free single-node deployments; the SaaS variant is currently in Beta, so you'll have to ask. On-premise clusters for production usage are "contact sales", but AFAIK due to the low sales volume (turns out, vertical scaling is easy and often enough), so don't let that scare you off.
That "it starts up from a set of known facts" refers to the "bottom-up" evaluation of datalog programs, in contrast with the top-down evaluation in Prolog.
Consider for instance the following database of facts and rules:
grandparent(X,Y):- parent(X,Z), parent(Z,Y). % (1)
parent(maria, kostas).
parent(kostas,yannis).
Prolog's top-down evaluation starts with the "head" of rule (1),
grandparent(X,Y) and proceeds to its "body", (parent(X,Z), parent(Z,Y)) to
instantiate the variables {X,Y,Z} according to the facts in the database and
derive the new fact grandparent(maria,yannis).Datalog's bottom-up evaluation instead starts with the facts in the program: parent(maria,kostas), parent(kostas, yannis) and adds those to the set of immediate consequences of the program (TP is called the "immediate consequence operator"):
TP₀ = {parent(maria,kostas), parent(kostas,yannis)}
Now the "parent" facts in the body of the "grandparent" rule can be instantiated
as follows: (parent(maria,kostas), parent(kostas,yannis)).
And finally the head of the rule can be instantiated to derive the same new atom
as with Prolog's top-down evaluation: TP₁ = TP₀ ∪ {grandparent(maria,yannis)}
The advantage of bottom-up evaluation is decidability, as long as a program is
datalog, which means no function symbols other than constants (which are
function symbols of arity 0, i.e. with 0 arguments) and no negation. For
instance, given the following program with a left-recursive clause, datalog's
bottom-up evaluation terminates: ancestor(X,Y):- parent(X,Y). % (2)
ancestor(X,Y):- ancestor(X,Z), ancestor(Z,Y). % (3)
parent(maria,kostas).
parent(kostas,yannis).
Given this program, datalog's bottom-up evaluation will first add the two facts
of "parent" to the immediate consequences of the program; then instantiate the
fact parent(X,Y) in the body of rule (2) to the two facts of "parent" in the
program; and finally instantiate the head of rule (2), deriving two new facts of
"ancestor": TP₁ = {ancestor(maria,kostas), ancestor(kostas,yannis), parent(maria,kostas),
parent(kostas,yannis)}
Then the two facts of "ancestor" in the body of rule (3) will be
instantiated to the facts derived in the previous step and a new fact derived
from the instantiation of the variables in the head of the rule : TP₂ = TP₀ ∪ {ancestor(maria,yannis)}
And then the program will terminate because there are no more new facts to
derive.By contrast, Prolog's top-down evaluation will first correctly derive the two new "ancestor" facts from the head of rule (2) but then will try to evaluate the first ancestor(X,Z) fact in the body of rule (3). This will take it back to the head of rule (3) and evaluation will be stuck in an infinite recursion.
The trade-off is that bottom-up evaluation cannot handle negation or functions (including the cons function used to construct lists) and so is, in that sense, incomplete, whereas Prolog can do both. For this reason, datalog is preferred as a database language that offers a limited ability for reasoning (certainly richer than what is possible e.g. with SQL, that essentially only allows facts) whereas Prolog is preferred for general-purpose programming.
The DDlog repo includes a Souffle-to-DDlog converter: https://github.com/vmware/differential-datalog/blob/v0.38.0/...
The Souffle converter is somewhat incomplete: https://github.com/vmware/differential-datalog/issues/174
Do these databases ever grow beyond what you can afford to keep in RAM/swap? I'm working towards high-throughput out-of-core support in differential-dataflow, but can't get much actual coding done in my off time. Understanding/concepts/discussions are not affected, so feel free to contact me.
What exactly do you mean with "logic/rule updates" ...in dd I can change/modify the actual dataflow?
Some of the episodes of my OVS Orbit podcast may be relevant:
* Episode 5: nlog, with Teemu Koponen from Styra and Yusheng Wang from VMware (https://ovsorbit.org/#e5)
* Episode 44: Cocoon-2, with Leonid Ryzhyk from VMware Research (https://ovsorbit.org/#e44)
* Episode 58: Toward Leaner, Faster ovn-northd, with Leonid Ryzhyk from VMware Research Group (https://ovsorbit.org/#e58)
I'd known of it a while before seeing a presentation on how one service team used it. They had a stream processing problem where they were beginning to lag the input by up to an hour. With differential datalog the lag dropped to sub-second.
It's seriously cool technology.
1: https://github.com/vmware/differential-datalog/blob/master/d...
We have other internal-to-VMware use cases for DDlog. The OVN use case is the one that's easiest to evangelize since it's all open source.
https://blogs.vmware.com/opensource/2018/07/26/open-source-o...
https://blogs.vmware.com/research/2020/12/07/whats-new-diffe...
It's not really arbitrary structures so much, though you're mostly free in what record type you use in a relation (structs and tagged enums are typical, though).
The incremental part is that you can feed it changes to the input (additions/retractions of facts) and get changes to the outputs back with low latency (you can alternatively just use it to keep an index up-to-date, where you can quickly look up based on a key (like a materialized view in SQL)).
This [0] section in the readme of the underlying incremental dataflow framework may help get the concept across, but feel free to follow up if you're still not seeing the incrementality.
[0]: https://github.com/TimelyDataflow/differential-dataflow#an-e...