ReQL: pluses and minuses of pipeline-style queries

Idan Arye via Digitalmars-d digitalmars-d at puremagic.com
Sun Apr 26 03:10:56 PDT 2015


On Saturday, 25 April 2015 at 05:16:21 UTC, Andrei Alexandrescu 
wrote:
> Found this on reddit a few days ago: 
> http://rob.conery.io/2015/04/17/rethinkdb-2-0-is-amazing/
>
> A good discussion of the pros and cons of pipeline-style 
> queries (the ReQL query language reminiscent of D's 
> algorithms/ranges) vs. classic SQL.
>
>
> Andrei

I think his example of composability is targeting the exact 
examples where ReQL is composable and SQL isn't. It's easy enough 
to create an opposite example: let's say that our initial query 
is this:

select vendor.name from vendor
inner join catalog on vendor.id = catalog.vendor_id
inner join details on details.catalog_id = catalog.id

r.db("music").table("catalog").map(function(album){
   return {
     artist : album("vendor")("name")
   }
})

If we want to add the `type_id = 2` filter, in SQL it's a matter 
of appending a line:

select vendor.name from vendor
inner join catalog on vendor.id = catalog.vendor_id
inner join details on details.catalog_id = catalog.id
where details.media_type_id = 2;

But in ReQL you'd have to insert the filter in the middle:

r.db("music").table("catalog").filter(function(album){
   return album("details").filter(function(track){
     return track("media_type_id").eq(2)
   })
}).map(function(album){
   return {
     artist : album("vendor")("name")
   }
})

So, composablity seems to be just a matter of where the part you 
want to add happens to be placed in the query's structure...


At this point one might claim that in the SQL case we had to 
modify a string while in the ReQL case we could simply use 
methods of the existing query object - but that's because the 
ReQL query is wrapped for us(the actual format sent to the server 
is not the one written in the source code) while the SQL 
isn't(OK, that's a lie - the SQL query is compiled to some more 
efficient format. But that happens in the driver/server, so the 
bindings don't have to do it). In reality SQL is also wrapped, 
either by entirely new languages(ORMs like Hibernate) or by query 
builders that let you use SQL almost directly, and simply make it 
more convenient(I had good experience with
PetaPoco(http://www.toptensoftware.com/petapoco/), for example). 
If you use these wrappers you get the same composability as 
ReQL(modify query by invoking methods of existing query objects).


More information about the Digitalmars-d mailing list