Skip to content

count() behavior not consistent with query result involving secondary index, OR secondary indexes broken somehow #1796

Closed

Description

@neumino

Opened by @srh here: rethinkdb/docs#160

This behavior is reported:

<divad12>     query = r.table('table').order_by(r.desc('field1'), index=r.desc('field2'))
<divad12>     print query.count().run(r_conn())  # 4644
<divad12>     results = list(query.limit(5000).run(r_conn()))
<divad12>     print len(results)  # 1011

Without using the secondary index, we don't get weird behavior.

<divad12>     query = r.table('table').order_by(r.desc('field1'), r.desc('field2'))
<divad12>     print query.count().run(r_conn())  # 4644
<divad12>     results = list(query.limit(5000).run(r_conn()))
<divad12>     print len(results)  # 4644

And apparently use of r.desc affects the numbers!

<divad12>     query = r.table('table').order_by('field1', index='field2')
<divad12>     print query.count().run(r_conn())  # 4644
<divad12>     results = list(query.limit(5000).run(r_conn()))
<divad12>     print len(results)  # 4560

Removing the use of .limit(5000) reportedly does not affect the outcome.

Observed with rethinkdb 1.11.2 (CLANG 4.2 (clang-425.0.28)) (OS X).


A little more information:
I can reproduce this on linux, and with the JavaScript driver too.

I tried a few variants of this query

r.db('test').table('plugins').orderBy('vimorg_rating', {index:'github_stars'})

Results

r.db('test').table('plugins').orderBy('vimorg_rating', {index:'github_stars'}) // 4560 docs
r.db('test').table('plugins').orderBy('vimorg_rating', {index:'github_stars'}).count() // 4644

r.db('test').table('plugins').orderBy('id', {index:'github_stars'}) // 4560 docs
r.db('test').table('plugins').orderBy('id', {index:'github_stars'}).count() // 4644

r.db('test').table('plugins').orderBy('vimorg_rating', {index:'id'}) // 4644 docs
r.db('test').table('plugins').orderBy('vimorg_rating', {index:'id'}).count() // 4644

r.db('test').table('plugins').orderBy('vimorg_rating', {index:'name'}) // 4373 docs
r.db('test').table('plugins').orderBy('vimorg_rating', {index:'name'}).count() // 4644

The data set is on newton /home/michel/bugs/2013.12.23

Activity

  1. coffeemug commented on Dec 23, 2013

    @coffeemug
    Contributor

    This might be a consequence of #1032, but at first glance I can't tell how that might be the case. It seems more likely that it's a bug.

  2. divad12 commented on Dec 23, 2013

    @divad12

    @coffeemug The data set I sent to @neumino does not have any null or missing values for the github_stars and vimorg_rating fields.

  3. coffeemug commented on Dec 23, 2013

    @coffeemug
    Contributor

    @divad12 -- ok, thanks for chiming in. This looks like a pretty bad bug and we'll push a fix ASAP (but unfortunately like after the holidays, since many people are off).

  4. coffeemug commented on Dec 23, 2013

    @coffeemug
    Contributor

    Also, sorry you ran into this!

  5. divad12 commented on Dec 23, 2013

    @divad12

    @coffeemug Cool, thanks for acknowledging it. That's alright, just not using the secondary index works well enough for me for now.

    Have a good holidays!

  6. jdoliner commented on Dec 24, 2013

    @jdoliner
    Contributor

    I'd be interested to know if this is present in 1.10 and 1.09. The streaming code was recently rewritten which could be to blame for this. Also I'm not 100% certain how this query gets executed, I'm assuming it we're not smart enough to ignore the in memory part of the order_by but maybe I'm wrong.

  7. ghost assigned on Jan 2, 2014
  8. mlucy commented on Jan 2, 2014

    @mlucy
    Member

    Assigning to myself because the #1 candidate for the problem is my recent streaming changes.

  9. mlucy commented on Jan 4, 2014

    @mlucy
    Member

    Working on this now.

  10. mlucy commented on Jan 4, 2014

    @mlucy
    Member

    So, this turns out to be something super dumb that I did. The class reader_t in datum_stream.cc has a member variable finished that we use to determine when to send SUCCESS_SEQUENCE instead of SUCCESS_PARTIAL in the protobuf Response. (It's also used for other logic internally in reader_t.) The problem is that this member variable doesn't mean "the stream is exhausted", it means "the shards are exhausted". Normally these are identical, because we try not to keep things hanging around in memory, but in the specific case of a an order_by on both an indexed and non-indexed field we do some clever shenanigans to get the sorting right and the two meanings diverge.

    I think we can fix this by changing that one predicate (and renaming the variable to be less confusing).

    I'm going to work on writing a unit test to reproduce this in our test system, which is arguably the more challenging part of this issue <.< .

  11. mlucy commented on Jan 4, 2014

    @mlucy
    Member

    Also, our performance on this workload is atrocious; our throughput is something like 1000 rows/sec in debug mode.

  12. mlucy commented on Jan 4, 2014

    @mlucy
    Member

    This is in review 1102 by @jdoliner .

  13. coffeemug commented on Jan 4, 2014

    @coffeemug
    Contributor

    @mlucy -- would you mind opening an issue for performance? (I could do it, but you have more info on what you observed). For example, is the performance bad just for unindexed orderBy or both?

  14. mlucy commented on Jan 8, 2014

    @mlucy
    Member

    @coffeemug -- opened in #1828 .

  15. mlucy commented on Jan 8, 2014

    @mlucy
    Member

    This is in next and cherry-picked into v1.11.x. @AtnNn -- could we do a point release for this when you get back?

  16. AtnNn commented on Jan 8, 2014

    @AtnNn
    Member

    Yes, we can do a point-release tomorrow or Friday. I will confer with my coffee mug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions