hamidhtc / h2database

Automatically exported from code.google.com/p/h2database
0 stars 0 forks source link

No lazy evaluation in special case #415

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Lazy evaluation works (NEXTVAL is not called):

DROP TABLE IF EXISTS my_table;
DROP SEQUENCE IF EXISTS my_seq;
CREATE SEQUENCE my_seq START WITH 66 INCREMENT BY 1;
CREATE TABLE my_table
(
  id CHAR(1) DEFAULT CHAR(CURRVAL('my_seq')) NOT NULL,
  CHECK(id<='Z' OR NEXTVAL('my_seq')<91)
);
INSERT INTO my_table(id) VALUES('X');
SELECT CURRVAL('my_seq');

Output will be and should be: 65

The id X is <= Z so id<='Z' is TRUE and NEXTVAL('my_seq')<91 is not called. 
Correct behaviour.

Lazy evaluation does not work (NEXTVAL is called):

DROP TABLE IF EXISTS my_table;
DROP SEQUENCE IF EXISTS my_seq;
CREATE SEQUENCE my_seq START WITH 66 INCREMENT BY 1;
CREATE TABLE my_table
(
  id CHAR(1) DEFAULT CHAR(CURRVAL('my_seq')) NOT NULL,
  CHECK(id<>CHAR(CURRVAL('my_seq')) OR NEXTVAL('my_seq')<91)
);
INSERT INTO my_table(id) VALUES('X');
SELECT CURRVAL('my_seq');

Output will be: 66
Output should be: 65

The id X is <> A (current value of my_seq is 65 and 65 is A) so 
id<>CHAR(CURRVAL('my_seq')) is TRUE however NEXTVAL('my_seq')<91 is called 
(misleadingly). Incorrect behaviour.

Original issue reported on code.google.com by DerMais...@gmail.com on 8 Aug 2012 at 12:41

GoogleCodeExporter commented 9 years ago
Hi,

Unlike programming languages such as Java, H2 does not make any guarantee in 
which order the expressions in an OR condition are evaluated. (H2 tries to 
evaluate the faster expression first but this is just an optimization).

Unless the SQL specification mandates in what order expressions are evaluated, 
or unless it is documented in an existing database, I currently don't see this 
as unexpected behavior.

Original comment by thomas.t...@gmail.com on 10 Aug 2012 at 6:52

GoogleCodeExporter commented 9 years ago
OK I understand :) Thanks for your response!

Original comment by DerMais...@gmail.com on 11 Aug 2012 at 12:01