You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A good way to think of this is r.row("field").eq( null ) being equivalent to r.row.has_fields("field"). The fact that you say eq is an artifact of treatment of nulls which unfortunately we can't do much about (other than undefining eq(null), which worse). We've in general established that we should move towards null being equivalent to non-existence, and while we can't do it in every case and we'll always have inconsistencies like these, it's in general better than the alternative.
You can still index on nulls the same way you'd index on non-existant values (e.g. createIndex('foo', r.row.has_fields('foo').not()) and I think we should keep it this way because the alternative is worse.
I move that we close this issue and treat null as non-existence whenever possible.
So, another option worth thinking about would be to make get_all(null) return all the rows that were excluded from the index. That would fix the unintuitive behavior michel highlighted, allow an easy way to get a useful set of rows, and is consistent with "null means non-existence".
Since when are we moving toward null being equivalent to non existence?
Are we going to have
r.table("foo").update( { field: null} )
equivalent to
r.table("foo").replace( r.row.without("field") )
?
Having .get(null) returns all rows excluded from the index would mean returning more than just null values since we don't index arrays and objects.
So if I want to store if someone answer yes/no/don't know/didn't answer, I will have to use an integer or a string, which is not really cool.
I feel like we are going to add more and more inconsistencies in RQL by having null meaning absence of value... which means making RQL just more and more complicated.
So, another option worth thinking about would be to make get_all(null) return all the rows that were excluded from the index.
How would we implement that?
Are we going to have r.table("foo").update( { field: null} ) equivalent to r.table("foo").replace( r.row.without("field") )? [...]
No -- we agreed to treat that differently, but when getting documents we'll try to treat these as equivalent whenever possible. We're trying to do what's useful at any given time and use the idea of nulls being equivalent to non-existence as an overarching principle whenever possible.
I feel like we are going to add more and more inconsistencies in RQL by having null meaning absence of value... which means making RQL just more and more complicated.
Treatment of non-existence is complicated. Look at SQL -- it's an established standard designed by very smart people, and the treatment of non-existence there is a mess. ReQL is way simpler in comparison, but there will be messy parts. It's a messy problem, and as much as I'd like to find a simple and elegant solution, we've searched and searched and there doesn't seem to be one.
@neumino -- We allow arrays as secondary index values, and I think we allow objects as well. The only things that aren't inserted into an index are things that return null, a non-datum type, or an error. (I think.)
We've been moving toward treating null as non-existence for a few releases now, because users seem to be using it that way. If you look at JSON datasets, some have a variable schema, and some have a consistent schema where non-existent values map to null. This means that in practice about half our users have one thing, and half have the other, and we want to make the queries those two groups need to write as similar as possible. Sometimes we can't do that because the ugliness of doing so is overwhelming, or because we settled on another way of doing it back before we knew what was up and it's too hard to change, but in general we're trying to move in that direction.
There's the additional fact that SQL uses null to mean non-existence, so a lot of people in the DB world expect that.
I just talked to @coffeemug and we end up agreeing that having null in the secondary indexes are better.
I think what lead to the conclusions are the following reasons:
null is different than undefined (in RQL), doing selection.update( { field: null } ) doesn't remove the field, but set the value null
table.filter( r.row("field").eq(null) ) would be the same as table.getAll(null, { index: "field" } )
When creating a secondary index that is the result of a regex matching, people may still want to retrieve results that didn't match
A common use case of using null is to store a boolean about our users (like "is_adult"), and use true/false/null/undefined: null when the user doesn't want to answer and undefined when he hasn't been asked yet. So null is a valid value in this case
In the case of filter( r.row("age").gt(5) ), if the field age to null, we should drop the field not because the field is missing but because we try to compare null to a number.
If people are creating a secondary index on a function like match that can return null, they almost certainly aren't getting the behavior that they want even with this change, because they'll be able to find all things that didn't match but not all things that did match. I don't think we need to worry about supporting that use case because people almost always want to tack a .ne(null) on the end so that both halves are in the index.
I think the number of people using null and non-existence side-by-side in their datasets to mean different things is pretty small. In general I don't think we should try very hard to support that usage (although we should always provide some way to do it), because by doing so we end up making interfaces that treat those two things differently, when most people seem to be using them to indicate the same thing in different datasets rather than different things in the same dataset. We've decided in several other places -- has_fields, for example -- to try to treat these more similarly, and I think all of those arguments still stand.
(As an aside, we don't currently throw or anything when people compare null to a number. This is arguably another place where we're treating it too much like a user value.)
I talked to @neumino today and I changed my mind on this -- I agree with him, I think we should actually index nulls, and I think I came up with a somewhat consistent attempt at formalizing this.
When we talk about null equivalency with non-existence, I think we need to differentiate between two cases -- storing data, and querying data. When storing data I think we shouldn't treat null as equivalent to non-existence, and when querying data I think we should.
We already do this to some degree -- when we save a document, {} isn't equivalent to {a: null}, but when we query it mostly is (for instance, if I try to add a number to a). I think that doing it this way is actually the most useful, and we should continue moving our treatment of nulls and non-existence in this direction. In this context, if an index expression evaluates to null on a row, I think we should still index it and make it available via getAll(null). This is useful because, as @neumino pointed out, if I index on people's age, I expect to be able to say "give me all the users that didn't fill out the form", and the easiest way to express that is with nulls.
As an aside, we don't currently throw or anything when people compare null to a number
I think that in case of filter this is an oversight and we should skip the row, but perhaps we should wait and see what users say when they (inevitably) run into this edge case.
@coffeemug -- let's say you have a table of people who filled out a survery. User A writes {:name => "Slava", :age => nil} to mean "didn't provide age", while user B writes {:name => "Slava"} to indicate the same thing. Now let's say you create a secondary index on age.
Currently, we don't offer any way for either user to get the people who didn't enter their age. If we index null, we solve user A's problem but not user B's. If we say that get_all(null) means "get everything not in the index", we solve both user's problems.
This only doesn't work for users who are using null and non-existence in the same dataset to mean different things, which seems like a rare case to me, and even they would have a workaround (they could just tack a filter on the end).
I see what you mean. I have to think about it a bit more, but a few things immediately jump out at me.
I don't see how we could efficiently implement get_all(null) as "everything not in the index" without actually indexing nulls (and missing attributes). I know we're talking semantics, but when there is no clear implementation path, I think it's important to discuss this too.
I think it would be very confusing for users to say get_all(null) and actually get non-existent rows. I think it's ok to filter out nulls and missing attrs in the same way (e.g. via has_fields), but not ok to select them in the same way. The former seems desirable, the latter seems confusing.
Intuitively, I would very much prefer RethinkDB to solve user A's problem but not user B's in this case. I'm not sure why, but that feels righter to me. I'll try to think of a way to explain/formalize it.
I think I seem to have a really strong intuitive sense for when nulls and missing attrs should be treated similarly and when they should be treated differently, but I can't find an underlying principle for this intuition. (Not to say that this sense is necessarily correct, just that it's strong.)
Here's a very rough set of rules that seem to adequately express my intuition (probably inconsistent and incomplete):
When storing things, treat nulls and missing attrs differently.
When selecting for existence (e.g. age == 20, age != 20), treat nulls and missing attrs in the same way (as non-existence; e.g. filtering for age != 20 when age is null should skip the row)
When selecting for non-existence (e.g. age == null, getAll(null), etc.), treat nulls and missing attrs differently.
When filtering out non-existence (e.g. has_fields('age')), treat nulls and missing attrs in the same way.
A system that adheres to these rules would feel extremely intuitive to me -- I think it would do exactly what I would expect most of the time, would allow me to do most everything I want, and wouldn't feel forced or icky. This is the kind of DB I'd like to actually use (wrt nulls), I think.
If people want to exclude a row from an index, they can still do so by producing a normal error (e.g. with r.error).
Per my comment at rethinkdb/docs#731 (comment), I would rather there be a separate, discrete symbol for the purposes of omitting a value from a secondary index, that isn't conflated with runtime / consistency errors - basically a non-serializable value like the above-proposed r.undefined or r.absent that, like r.minval and r.maxval before it, stands in for the earlier usage of null, coopting null's earlier useful-but-muddled semantics and allowing null to consistently refer to a literal null value.
As an example: if I reach a path in my data that yields an r.error value, I want the message it creates to get sent to a developer pager, as this indicates that something has gone wrong in the data by way of a branch going down an unreachable code path. However, say I want to omit any rows for closed tickets from my sindex of voting options - there's nothing erroneous about the fact that closed tickets exist.
@stuartpb The idea behind allowing null in secondary indexes is that we want an indexed orderBy({index: ...}) or a getAll to behave the same way as its un-indexed equivalents orderBy and filter({a: x}). That will allow us to automatically use indexes if they are available in the future.
We could allow such a special value r.dontIndex as a return value of an index function, as long as we don't allow it to appear in any other result or document.
At the moment this isn't necessary, because we currently swallow errors that happen in secondary index functions anyway, so the effect of using r.error is the same as what that special value would do.
Us not passing up errors in secondary index functions has two reasons at least:
If you create an index over a field that only exists in some documents, you usually don't want the index creation to fail if there's some document in the table that doesn't have that field.
If you update or insert an document such that its new value causes an error in the secondary index function, we currently don't have a great way for reporting that error in the query result. We cannot make the full query fail because a) this would probably be undesired and b) the way secondary index updates are implemented at the time they are evaluated the update might already have been applied to other indexes (including the main tree). We could probably change the latter part by evaluating sindex functions before writing anything, but I'm not sure if we want to.
We could add a new sindex_errors field to the result of write operations where we aggregate all errors that occurred while evaluating the secondary index functions on the changed object. This might be useful for debugging. I'll open a new issue to discuss that.
To clarify: I like the idea of having a special value that can be returned from an index function for omitting a document from the index. It would be a much cleaner replacement for using the error trick. I think it's somewhat orthogonal to the null question though.
r.skip is the name I was originally going to suggest, but I was thinking it might be better to reuse another value like r.absent so there aren't so many single-purpose constants hanging around under r. It's also intuitive that an "undefined" or "absent" index value leads to the row not being indexed. (Also, as @thelinuxlich points out, r.skip sounds like a name that might be better suited to another purpose down the line.)
Anyway, as @danielmewes said, this is orthogonal (though related) to allowing null values in secondary indices, so I'll open another issue for it: #4121
Does it? We're basically trying to run .getAll(null,index='secondary') to select records that do not have a value set for the secondary index. (This is equivalent to one of the scenarios @mlucy mentioned in an early comment).
@coffenbacher At the moment null values don't get indexed (and this issue is about changing that in the future).
There are multiple work-arounds. One is to map a missing value to something other than null, that is not otherwise used in the indexed field(s). For example to false:
Then you can query by .getAll(false, {index: "secondary"}).
Reacted by Raman Gupta, Tihomir Valkanov, coffenbacher, Nicolás Fantone, Blake Embrey, Matt Harrison, thomasmodeneis, Iago, Patricio Merino and Steve Sloan
I'd like to add to this proposal. As far as I currently understand, the plan is to:
start indexing null values
ignore rows that return an "undefined" type using r.absent() or the existing r.literal()
I'd like to also reconsider the behavior of an r.error() (or an implicit error, like operating on the wrong type or not specifying a default) when returned by an index function. I'd propose that a write operation fail if any index returns an error. Likewise, I would propose that creating an index should fail if any documents return such an error.
This would ensure our indices are expecting the correct document structures, and aren't silently missing documents that we would expect to have indexed. This would also have the effect of validating every document in the table.
To ensure backwards-compatibility and ease the process of schema migrations, indexCreate could receive a boolean option of strict to enable this behavior.
Piggy-backing on the same logic, rethinkdb could introduce the concept of "validators" which would function similarly to "strict" indices, but wouldn't actually create the b-trees. This could essentially accomplish #5494, #5879, and #4210, as any json-schema or Joi schema could be compiled into a rql expression and stored as a "validator" expression in the database.
EDIT: after reading my comment, I realized it should probably be its own issue to avoid hijacking this one. I just posted it as #5967.
Activity
coffeemug commentedon Jun 18, 2013
A good way to think of this is
r.row("field").eq( null )being equivalent tor.row.has_fields("field"). The fact that you sayeqis an artifact of treatment of nulls which unfortunately we can't do much about (other than undefiningeq(null), which worse). We've in general established that we should move towardsnullbeing equivalent to non-existence, and while we can't do it in every case and we'll always have inconsistencies like these, it's in general better than the alternative.You can still index on nulls the same way you'd index on non-existant values (e.g.
createIndex('foo', r.row.has_fields('foo').not()) and I think we should keep it this way because the alternative is worse.I move that we close this issue and treat null as non-existence whenever possible.
mlucy commentedon Jun 18, 2013
So, another option worth thinking about would be to make
get_all(null)return all the rows that were excluded from the index. That would fix the unintuitive behavior michel highlighted, allow an easy way to get a useful set of rows, and is consistent with "null means non-existence".neumino commentedon Jun 18, 2013
Since when are we moving toward
nullbeing equivalent to non existence?Are we going to have
equivalent to
?
Having
.get(null)returns all rows excluded from the index would mean returning more than justnullvalues since we don't index arrays and objects.So if I want to store if someone answer yes/no/don't know/didn't answer, I will have to use an integer or a string, which is not really cool.
I feel like we are going to add more and more inconsistencies in RQL by having null meaning absence of value... which means making RQL just more and more complicated.
coffeemug commentedon Jun 18, 2013
How would we implement that?
No -- we agreed to treat that differently, but when getting documents we'll try to treat these as equivalent whenever possible. We're trying to do what's useful at any given time and use the idea of nulls being equivalent to non-existence as an overarching principle whenever possible.
Treatment of non-existence is complicated. Look at SQL -- it's an established standard designed by very smart people, and the treatment of non-existence there is a mess. ReQL is way simpler in comparison, but there will be messy parts. It's a messy problem, and as much as I'd like to find a simple and elegant solution, we've searched and searched and there doesn't seem to be one.
mlucy commentedon Jun 18, 2013
@neumino -- We allow arrays as secondary index values, and I think we allow objects as well. The only things that aren't inserted into an index are things that return
null, a non-datum type, or an error. (I think.)We've been moving toward treating null as non-existence for a few releases now, because users seem to be using it that way. If you look at JSON datasets, some have a variable schema, and some have a consistent schema where non-existent values map to
null. This means that in practice about half our users have one thing, and half have the other, and we want to make the queries those two groups need to write as similar as possible. Sometimes we can't do that because the ugliness of doing so is overwhelming, or because we settled on another way of doing it back before we knew what was up and it's too hard to change, but in general we're trying to move in that direction.There's the additional fact that SQL uses
nullto mean non-existence, so a lot of people in the DB world expect that.mlucy commentedon Jun 18, 2013
@coffeemug -- I don't know much about the sindex implementation, but it doesn't sound that hard. (Famous words, I know.)
neumino commentedon Jun 18, 2013
I just talked to @coffeemug and we end up agreeing that having null in the secondary indexes are better.
I think what lead to the conclusions are the following reasons:
nullis different thanundefined(in RQL), doingselection.update( { field: null } )doesn't remove the field, but set the valuenulltable.filter( r.row("field").eq(null) )would be the same astable.getAll(null, { index: "field" } )nullis to store a boolean about our users (like "is_adult"), and use true/false/null/undefined: null when the user doesn't want to answer and undefined when he hasn't been asked yet. So null is a valid value in this casefilter( r.row("age").gt(5) ), if the field age to null, we should drop the field not because the field is missing but because we try to comparenullto a number.mlucy commentedon Jun 19, 2013
If people are creating a secondary index on a function like
matchthat can returnnull, they almost certainly aren't getting the behavior that they want even with this change, because they'll be able to find all things that didn't match but not all things that did match. I don't think we need to worry about supporting that use case because people almost always want to tack a.ne(null)on the end so that both halves are in the index.I think the number of people using
nulland non-existence side-by-side in their datasets to mean different things is pretty small. In general I don't think we should try very hard to support that usage (although we should always provide some way to do it), because by doing so we end up making interfaces that treat those two things differently, when most people seem to be using them to indicate the same thing in different datasets rather than different things in the same dataset. We've decided in several other places --has_fields, for example -- to try to treat these more similarly, and I think all of those arguments still stand.(As an aside, we don't currently throw or anything when people compare
nullto a number. This is arguably another place where we're treating it too much like a user value.)coffeemug commentedon Jun 19, 2013
I talked to @neumino today and I changed my mind on this -- I agree with him, I think we should actually index nulls, and I think I came up with a somewhat consistent attempt at formalizing this.
When we talk about null equivalency with non-existence, I think we need to differentiate between two cases -- storing data, and querying data. When storing data I think we shouldn't treat null as equivalent to non-existence, and when querying data I think we should.
We already do this to some degree -- when we save a document,
{}isn't equivalent to{a: null}, but when we query it mostly is (for instance, if I try to add a number toa). I think that doing it this way is actually the most useful, and we should continue moving our treatment of nulls and non-existence in this direction. In this context, if an index expression evaluates to null on a row, I think we should still index it and make it available viagetAll(null). This is useful because, as @neumino pointed out, if I index on people's age, I expect to be able to say "give me all the users that didn't fill out the form", and the easiest way to express that is with nulls.I think that in case of
filterthis is an oversight and we should skip the row, but perhaps we should wait and see what users say when they (inevitably) run into this edge case.neumino commentedon Jun 19, 2013
👍 @coffeemug
mlucy commentedon Jun 19, 2013
@coffeemug -- let's say you have a table of people who filled out a survery. User A writes
{:name => "Slava", :age => nil}to mean "didn't provide age", while user B writes{:name => "Slava"}to indicate the same thing. Now let's say you create a secondary index on age.Currently, we don't offer any way for either user to get the people who didn't enter their age. If we index
null, we solve user A's problem but not user B's. If we say thatget_all(null)means "get everything not in the index", we solve both user's problems.This only doesn't work for users who are using
nulland non-existence in the same dataset to mean different things, which seems like a rare case to me, and even they would have a workaround (they could just tack a filter on the end).coffeemug commentedon Jun 19, 2013
I see what you mean. I have to think about it a bit more, but a few things immediately jump out at me.
get_all(null)as "everything not in the index" without actually indexing nulls (and missing attributes). I know we're talking semantics, but when there is no clear implementation path, I think it's important to discuss this too.get_all(null)and actually get non-existent rows. I think it's ok to filter out nulls and missing attrs in the same way (e.g. viahas_fields), but not ok to select them in the same way. The former seems desirable, the latter seems confusing.coffeemug commentedon Jun 19, 2013
I think I seem to have a really strong intuitive sense for when nulls and missing attrs should be treated similarly and when they should be treated differently, but I can't find an underlying principle for this intuition. (Not to say that this sense is necessarily correct, just that it's strong.)
coffeemug commentedon Jun 19, 2013
Here's a very rough set of rules that seem to adequately express my intuition (probably inconsistent and incomplete):
age == 20,age != 20), treat nulls and missing attrs in the same way (as non-existence; e.g. filtering forage != 20when age is null should skip the row)age == null,getAll(null), etc.), treat nulls and missing attrs differently.has_fields('age')), treat nulls and missing attrs in the same way.A system that adheres to these rules would feel extremely intuitive to me -- I think it would do exactly what I would expect most of the time, would allow me to do most everything I want, and wouldn't feel forced or icky. This is the kind of DB I'd like to actually use (wrt nulls), I think.
mlucy commentedon Jun 19, 2013
Actually, another argument in favor of this is that it would make compound indexes more useful if one of the entries could be
null.83 remaining items
stuartpb commentedon Apr 24, 2015
Per my comment at rethinkdb/docs#731 (comment), I would rather there be a separate, discrete symbol for the purposes of omitting a value from a secondary index, that isn't conflated with runtime / consistency errors - basically a non-serializable value like the above-proposed
r.undefinedorr.absentthat, liker.minvalandr.maxvalbefore it, stands in for the earlier usage ofnull, coopting null's earlier useful-but-muddled semantics and allowingnullto consistently refer to a literalnullvalue.As an example: if I reach a path in my data that yields an
r.errorvalue, I want the message it creates to get sent to a developer pager, as this indicates that something has gone wrong in the data by way of a branch going down an unreachable code path. However, say I want to omit any rows for closed tickets from my sindex of voting options - there's nothing erroneous about the fact that closed tickets exist.danielmewes commentedon Apr 24, 2015
@stuartpb The idea behind allowing
nullin secondary indexes is that we want an indexedorderBy({index: ...})or agetAllto behave the same way as its un-indexed equivalentsorderByandfilter({a: x}). That will allow us to automatically use indexes if they are available in the future.We could allow such a special value
r.dontIndexas a return value of an index function, as long as we don't allow it to appear in any other result or document.At the moment this isn't necessary, because we currently swallow errors that happen in secondary index functions anyway, so the effect of using
r.erroris the same as what that special value would do.Us not passing up errors in secondary index functions has two reasons at least:
We could add a new
sindex_errorsfield to the result of write operations where we aggregate all errors that occurred while evaluating the secondary index functions on the changed object. This might be useful for debugging. I'll open a new issue to discuss that.danielmewes commentedon Apr 24, 2015
@stuartpb See #4118 for a discussion on how to handle secondary index errors.
danielmewes commentedon Apr 24, 2015
To clarify: I like the idea of having a special value that can be returned from an index function for omitting a document from the index. It would be a much cleaner replacement for using the error trick. I think it's somewhat orthogonal to the
nullquestion though.coffeemug commentedon Apr 24, 2015
r.skip? 😄thelinuxlich commentedon Apr 24, 2015
r.skip feels like pagination syntax
stuartpb commentedon Apr 24, 2015
r.skipis the name I was originally going to suggest, but I was thinking it might be better to reuse another value liker.absentso there aren't so many single-purpose constants hanging around underr. It's also intuitive that an "undefined" or "absent" index value leads to the row not being indexed. (Also, as @thelinuxlich points out,r.skipsounds like a name that might be better suited to another purpose down the line.)(Edit: oh yeah, there already is a
skipin ReQL.)Another possible name:
r.unindexedstuartpb commentedon Apr 24, 2015
Anyway, as @danielmewes said, this is orthogonal (though related) to allowing
nullvalues in secondary indices, so I'll open another issue for it: #4121coffenbacher commentedon Feb 11, 2016
Is r.absent or r.unindexed still under consideration? This would be really useful in some situations.
danielmewes commentedon Feb 11, 2016
@coffenbacher Yes, though right now you can just return
nullwhich has the same effect.coffenbacher commentedon Feb 11, 2016
Does it? We're basically trying to run .getAll(null,index='secondary') to select records that do not have a value set for the secondary index. (This is equivalent to one of the scenarios @mlucy mentioned in an early comment).
Is there a workaround for that?
danielmewes commentedon Feb 11, 2016
@coffenbacher At the moment
nullvalues don't get indexed (and this issue is about changing that in the future).There are multiple work-arounds. One is to map a missing value to something other than
null, that is not otherwise used in the indexed field(s). For example tofalse:Then you can query by
.getAll(false, {index: "secondary"}).coffenbacher commentedon Feb 11, 2016
Thanks @danielmewes, that solution worked perfectly for us.
mike-marcacci commentedon Jul 17, 2016
I'd like to add to this proposal. As far as I currently understand, the plan is to:
nullvaluesr.absent()or the existingr.literal()I'd like to also reconsider the behavior of an
r.error()(or an implicit error, like operating on the wrong type or not specifying a default) when returned by an index function. I'd propose that a write operation fail if any index returns an error. Likewise, I would propose that creating an index should fail if any documents return such an error.This would ensure our indices are expecting the correct document structures, and aren't silently missing documents that we would expect to have indexed. This would also have the effect of validating every document in the table.
To ensure backwards-compatibility and ease the process of schema migrations,
indexCreatecould receive a boolean option ofstrictto enable this behavior.Piggy-backing on the same logic, rethinkdb could introduce the concept of "validators" which would function similarly to "strict" indices, but wouldn't actually create the b-trees. This could essentially accomplish #5494, #5879, and #4210, as any json-schema or Joi schema could be compiled into a rql expression and stored as a "validator" expression in the database.
EDIT: after reading my comment, I realized it should probably be its own issue to avoid hijacking this one. I just posted it as #5967.