Issue Details (XML | Word | Printable)

Key: DERBY-1967
Type: Bug Bug
Status: Closed Closed
Resolution: Fixed
Priority: Major Major
Assignee: Yip Ng
Reporter: Radu Radutiu
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
Derby

UNION (ALL) contraint violation problem

Created: 17/Oct/06 11:02 AM   Updated: 30/Jun/09 03:55 PM
Return to search
Component/s: SQL
Affects Version/s: 10.1.3.1, 10.2.1.6
Fix Version/s: 10.2.2.0, 10.3.1.4

Time Tracking:
Not Specified

File Attachments:
  Size
Text File Licensed for inclusion in ASF works derby1967-10.2-diff01.txt 2006-10-18 07:32 AM Yip Ng 3 kB
Text File Licensed for inclusion in ASF works derby1967-10.2-stat01.txt 2006-10-18 07:32 AM Yip Ng 0.3 kB
Text File Licensed for inclusion in ASF works derby1967-trunk-diff01.txt 2006-10-17 04:24 PM Yip Ng 3 kB
Text File Licensed for inclusion in ASF works derby1967-trunk-stat01.txt 2006-10-17 04:24 PM Yip Ng 0.3 kB
Environment:
derby v10.1.3.1 and v10.2.1.6 on linux (FC5), jdk 1.5.0_06-b05
and jdk 1.6.0-rc-b99.
Issue Links:
Reference
 

Bug behavior facts: Regression
Resolution Date: 18/Oct/06 02:21 PM


 Description  « Hide
The following simple test case gives an error:

create table a (f1 varchar(10));
create table b (f2 varchar(10));
insert into b values('test');
select nullif('x','x') as f0, f1 from a
   union all
   select nullif('x','x') as f0, nullif('x','x') as f1 from b;

ERROR 23502: Column 'F0' cannot accept a NULL value.
SQLState(23502) vendor code(30000)

However the following works ok:
drop table a;
drop table b;
create table a (f1 int);
create table b (f2 int);
insert into b values(1);
select nullif('x','x') as f0, f1 from a
   union all
   select nullif('x','x') as f0, nullif(1,1) as f1 from b;

The test case is a simplification of a query generated by Hibernate
with the table per class inheritance strategy. Both queries work ok on
MSSQL and PostgreSQL. On Derby only the second query works, the first
one giving a contraint violation.


 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Repository Revision Date User Message
ASF #465122 Wed Oct 18 01:43:14 UTC 2006 bpendleton DERBY-1967: UNION (ALL) constraint violation problem

This patch was contributed by Yip Ng (yipng168@gmail.com)

In the bind phase of ConditionalNode (NULLIF), the CAST node is
generated on top of the untyped NULL and it gets the data type
descriptor(DTD) of the left operand. However, the CAST node should
have DTD where its value can be nullable.

BinaryComparisonOperatorNode bcon = (BinaryComparisonOperatorNode)testCondition;

QueryTreeNode cast = getNodeFactory().getNode(
C_NodeTypes.CAST_NODE,
thenElseList.elementAt(0),
bcon.getLeftOperand().getTypeServices(), <=== not nullable!
getContextManager());

The second query:

select nullif('x','x') as f0, f1 from a
   union all
   select nullif('x','x') as f0, nullif(1,1) as f1 from b;

works because it didn't generate a NormalizedResultSet on top of
the PRN on the right hand side of the union since the datatype and
length matches. So it didn't hit the path where it does additional
checking at execution time.

For the first query:

select nullif('x','x') as f0, f1 from a
   union all
   select nullif('x','x') as f0, nullif('x','x') as f1 from b;

The union result column's length does not match with the right hand
side result column, so it generated a NormalizedResultSet on top of
the RHS of the union. When the system retrieves the row from
NormalizedResultSet at execution time, the normalize method is called
on the DTD and checks if the source is NULL and whether its DTD
is not nullable. In this case, the SQLSTATE 23502 is thrown.
Files Changed
MODIFY /db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/master/union.out
MODIFY /db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/union.sql
MODIFY /db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/ConditionalNode.java

Repository Revision Date User Message
ASF #465256 Wed Oct 18 14:20:44 UTC 2006 bpendleton DERBY-1967: UNION (ALL) constraint violation problem

This patch was contributed by Yip Ng (yipng168@gmail.com)

Merged from the trunk by svn merge -r 465121:465122 ../trunk
Files Changed
MODIFY /db/derby/code/branches/10.2/java/testing/org/apache/derbyTesting/functionTests/tests/lang/union.sql
MODIFY /db/derby/code/branches/10.2/java/engine/org/apache/derby/impl/sql/compile/ConditionalNode.java
MODIFY /db/derby/code/branches/10.2/java/testing/org/apache/derbyTesting/functionTests/master/union.out