[Cubicweb] CWEP-0002 - RQL rewriting

Léa Capgen lea.capgen at logilab.fr
Fri Dec 13 17:11:12 CET 2013


Hi Cubicweb users,

Here is the first hints about allowing users rewriting RQL.

Léa,


Rationale
==========


Logilab has been thinking about rules and RQL rewriting for years, but 
never had the time. Using rules in database queries is not a new topic, 
that dates back to the 70s (read about Datalog_ for example).

.. _Datalog: http://en.wikipedia.org/wiki/Datalog

With rules and RQL rewriting, we could have syntactic sugar for almost 
free and have more flexible schemas that define relations and attributes 
that we can chose not to materialize at the SQL table level.

Here are a few examples of use cases. A more complete document can be 
found here:

http://hg.logilab.org/users/lcapgen/cw_rules/file/44522d5906d4/cw-regles-utilisations.rst

Proposal
========

Relation rewriting
--------------------------

Instead of::

   Any A,B WHERE C is Contribution, C contributor A, C manifestation B,
                 C role R, R name "illustrator"

we would like to write::

   Any A,B WHERE A illustrator_of B

after adding a rule to the schema as follows:

.. sourcecode:: python

     class illustrator_of(RelationDefinition):
         subject = 'Person'
         object = 'Manifestation'
         rule  = ('C is Contribution, C contributor S, C manifestation O,'
                  'C role R, R name "illustrator"')



Computed attribute
----------------------------

Instead of::

   Any SUM(SA) GROUPBY S WHERE P works_for S, P salary SA

we would like to write::

   Any A WHERE S total_salary A

after adding to the schema a rule for the computed attribute as follows:

.. sourcecode:: python

     class Company(EntityType):
         name = String()
         total_salary = Int(computed=('Any SUM(SA) GROUPBY S WHERE P 
works_for S, P salary SA'))



Map RDF to Yams
-------------------------

Given the following XY equivalences:

.. sourcecode:: python

    xy.add_equivalence('CWUser', 'foaf:Person')
    xy.add_equivalence('Person', 'foaf:Person')

when writing::

     Any S WHERE S is foaf:Person

we would like the query to be rewritten as::

     Any A WHERE A is IN(Person, User)


Implementation
===============

A first implementation is available here:

   http://hg.logilab.org/review/rql/rev/3a74699faa12

   http://hg.logilab.org/review/yams/rev/501931830b3a

   http://hg.logilab.org/review/cubicweb/rev/972a53c416a8


Discussion
==========

Please comment. Known limitations can be found here:

http://hg.logilab.org/users/lcapgen/cw_rules/file/44522d5906d4/cw-regles-limitations.rst 




More information about the Cubicweb mailing list