RethinkDB “IS NULL” in a where clause filter

I find the syntax for RethinkDB filtering a little unintuitive – if this was actually Javascript, you might expect to see “row[‘Query’]”. I expect this allows them to instrument the queries more easily, although considering they are supposedly translating this into an intermediate language, this doesn’t seem necessary.

To do equality, non-equality, and so on, there are several functions available (ne / eq / gt / lt). I suspect that the reason for this is that the getter function for a column always returns an object – so it can’t be “equal” to null. This is likely a Builder pattern that lets them lazy-execute results; if this database goes the direction of Oracle’s optimizer it will also provide opportunities for query rewriting.

The key is, using Javascript operators will be syntactically correct, but will not work. Unfortunately the RethinkDB editor does not warn you about this, or auto-correct them.

r.db('performance')
.table('query_timings')
.filter(
  function(d){ 
    return d('Query').ne(null) 
  })

Hopefully in the future we’ll also see ES6 functions, to make this more compact.

Interested in JavaScript? I send out weekly, personalized emails with articles and conference talks. Click here to see an example and subscribe.

2 replies
  1. Jorge
    Jorge says:

    Hi Gary,

    I agree with you that the syntax for JS can seem a bit unintuitive at first. It would be nice to do `row[‘Query’]`. You can actually do this in Python+Ruby because they have operator overloading. In the JS driver, you basically can’t do this because this query gets serialized and sent to the server. Those functions are not executed on the client. The driver could, for example, turn the function into a string `toString()` and then try to parse it, but (as you can imagine) this is a bit hacky.

    I agree it’d be good to have the editor correct this :).

    If you want some ES6 features, you should take a look at rethinkdb-dash, which has a lot of these.

    Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *