Re: [sqlite] User function's alias

2017-11-12 Thread korablev
More strange things:

SELECT x, sequence() AS y FROM t1 WHERE y>0 AND y<99  order by x desc;

9, 2
8, 3
7, 4

SELECT x, 0+sequence() AS y FROM t1 WHERE y>0 AND y<99  order by x desc;

9, 6
8, 6
7, 6

Seems that this optimization a little bit broken...



--
Sent from: http://sqlite.1065341.n5.nabble.com/
___
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] User function's alias

2017-11-12 Thread korablev
Well, behaviour of deterministic function is really strange. 
SELECT x, sequence() AS y FROM t1 WHERE y>0 AND y<99 AND y!=55 AND y NOT IN
(56,57,58) AND y NOT LIKE 'abc%' AND y%10==2 order by x desc
(Example from alias.test, I have declared sequence() with
SQLITE_DETERMINISTIC flag)
Trace for the query above:
...
Function0 001 sequence(0)   00 r[1]=func(r[0])
Integer020  00 r[2]=0
Integer   9930 00 r[3]=99
Integer   5540 00 r[4]=55
String80   130 abc%00 r[13]='abc%'
Function0 00   14 sequence(0)   00 r[14]=func(r[0])
Function0 3   138 like(2)  02 r[8]=func(r[13..14])
Function0 007 sequence(0)  00 r[7]=func(r[0])
...
sequence() is called 3 times in trace(even without branching), instead of
using register r[1] after first call.





--
Sent from: http://sqlite.1065341.n5.nabble.com/
___
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] User function's alias

2017-11-12 Thread korablev
Jens Alfke-2 wrote
> First off, you didn’t register the function as deterministic, so SQLite
> has to assume it can return a different result every time it’s called,
> even with the same arguments. That immediately prevents the kind of
> optimization you wanted.

I guess, it doesn't really matter whether function is deterministic or not.
It is rather about how aliases should work. To prove it I have found
"alias.test" in test folder. There is a comment: 
# Aliases are currently evaluated twice.  We might try to change this
# in the future.  But not now.
return

The test is quite old, but still doesn't work.
Moreover, consider query: SELECT x, rand() as random FROM tab WHERE random >
0.5;
Currently, it would return random numbers which might be less than 0.5 due
to two calls of rand(x).



--
Sent from: http://sqlite.1065341.n5.nabble.com/
___
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] User function's alias

2017-11-10 Thread Jens Alfke


> On Nov 10, 2017, at 9:51 AM, korablev  wrote:
> 
> I have noticed strange behaviour of user functions. Consider following
> example:

In reports like this, it really helps if you can clearly state the situation at 
the start, instead of dumping hundreds of lines of code and output, and 
expecting the reader to figure out what’s going on.

Down at the end:
> So, change_global_var is called 3 times for -- one for inserting and two for
> comparison. I expected that it would call function one time, save result and
> use saved result for comparisons and insertion.

This is what should have been at the top :)

First off, you didn’t register the function as deterministic, so SQLite has to 
assume it can return a different result every time it’s called, even with the 
same arguments. That immediately prevents the kind of optimization you wanted.

As for deterministic functions, I asked a similar question a month or two ago, 
about factoring multiple calls out of a query. You can find list archives and 
read the thread. It doesn’t sound likely to happen.

—Jens
___
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users