I suspect that the udf() function is called once to build the record and once 
again to build the parameter list for the trigger program.

-----Ursprüngliche Nachricht-----
Von: sqlite-users [mailto:sqlite-users-boun...@mailinglists.sqlite.org] Im 
Auftrag von no...@null.net
Gesendet: Dienstag, 28. November 2017 15:04
An: SQLite mailing list <sqlite-users@mailinglists.sqlite.org>
Betreff: [EXTERNAL] Re: [sqlite] Possible User Defined Function (UDF) Bug?

Here is a trimmed-down test case for my issue:

        CREATE TABLE d (
                id INTEGER NOT NULL PRIMARY KEY
        );


        CREATE TRIGGER
                bi_d
        BEFORE INSERT ON
                d
        FOR EACH ROW
        BEGIN
                select 1;
        END;


        INSERT INTO
                d
        VALUES(
                udf(1)
        );

VDBE Program Listing:
   0 Init             0   20    0               00 Start at 20
   1 OpenWrite        0    2    0 1             00 root=2 iDb=0; d
   2 Function0        1    5    3 udf(1)        01 r[3]=func(r[5])
   3 NotNull          3    5    0               00 if r[3]!=NULL goto 5
   4 Integer         -1    3    0               00 r[3]=-1
   5 MustBeInt        3    0    0               00
   6 Function0        1    6    4 udf(1)        01 r[4]=func(r[6])
   7 Copy             4    7    0               00 r[7]=r[4]
   8 Affinity         4    1    0 D             00 affinity(r[4])
   9 Program          1   19    8 program       00 Call: bi_d.default
  10 Copy             7    1    0               00 r[1]=r[7]
  11 NotNull          1   13    0               00 if r[1]!=NULL goto 13
  12 NewRowid         0    1    0               00 r[1]=rowid
  13 MustBeInt        1    0    0               00
  14 SoftNull         2    0    0               00 r[2]=NULL
  15 NotExists        0   17    1               00 intkey=r[1]
  16 Halt          1555    2    0 d.id          02
  17 MakeRecord       2    1    9 D             00 r[9]=mkrec(r[2])
  18 Insert           0    9    1 d             31 intkey=r[1] data=r[9]
  19 Halt             0    0    0               00
  20 Transaction      0    1    2 0             01 usesStmtJournal=1
  21 TableLock        0    2    1 d             00 iDb=0 root=2 write=1
  22 Integer          1    5    0               00 r[5]=1
  23 Integer          1    6    0               00 r[6]=1
  24 Goto             0    1    0               00

Note the two calls to Function0 in the above. If you create a udf() function 
that prints a message you will see that it does in fact get called twice.

I only see this happening when all three of the following statements are true:

    - The UDF is used in a VALUES() statement
    - The destination table "d" has a PRIMARY KEY
    - There is a BEFORE INSERT trigger on "d"

--
Mark Lawrence
_______________________________________________
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users


___________________________________________
 Gunter Hick | Software Engineer | Scientific Games International GmbH | 
Klitschgasse 2-4, A-1130 Vienna | FN 157284 a, HG Wien, DVR: 0430013 | (O) +43 
1 80100 - 0

May be privileged. May be confidential. Please delete if not the addressee.
_______________________________________________
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to