Re: Correct behavior of ItemQuery?

[email protected] Fri, 06 Feb 2009 13:42:16 -0000
Newsgroups gmane.comp.python.quotient.dev
Message-ID <20090206134216.12555.613994370.divmod.xquotient.4267@weber.divmod.com>
On 11:00 am, [email protected] wrote:
>I got result like this:
>
>  $ python foo.py
>  result.count() -> 100
>  len(list(result)) -> 10
>
>Why result.count() does not return 10? Is this correct behavior?

10 would indeed be the correct behavior.  It should be fixed.

The reason for this strangeness is the fairly literal-minded way that 
axiom translates your queries.  Consider:

    sqlite> create table foo (bar integer);
    sqlite> insert into foo values (1);
    sqlite> insert into foo values (2);
    sqlite> insert into foo values (3);
    sqlite> insert into foo values (4);
    sqlite> select * from foo limit 2;
    1
    2
    sqlite> select count(*) from foo limit 2;
    4

Axiom does put the 'limit 2' on there when counting, but sqlite 
interprets that to mean 'limit the number of results from count(*) to 
2', not 'stop counting after 2'.  Since count() only ever produces one 
result, this is not useful.

While I was experimenting I noticed that this seems to do the right 
thing:

    sqlite> select count(*) from (select * from foo limit 2);
    2

I seem to remember that I tried this and it definitely *didn't* work at 
some point in the past, but it seems fine now.  Perhaps you could write 
a patch which would fix the SQL generation for count() to do this 
instead?  (Assuming it's reasonably efficient, I haven't yet checked 
that it doesn't do anything too stupid like allocate and hold on to the 
entire result set in memory.)