[ 
https://issues.apache.org/jira/browse/DERBY-4370?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Knut Anders Hatlen updated DERBY-4370:
--------------------------------------

    Attachment: using.diff

The attached patch enables the USING syntax in the parser.

It appears to do the right thing in the simple queries below. The columns 
specified in the USING clause come first in the result, then all the columns in 
T1 except those in USING, and then all the columns in T2 except those in USING.

ij> create table t1(a int, b int, c int);
0 rows inserted/updated/deleted
ij> insert into t1 values (1,2,3),(2,3,1),(4,4,4);
3 rows inserted/updated/deleted
ij> create table t2(b int, c int, d int);
0 rows inserted/updated/deleted
ij> insert into t2 values (1,2,3),(2,3,1),(5,5,5);
3 rows inserted/updated/deleted
ij> select * from t1 join t2 using (b,c);
B          |C          |A          |D          
-----------------------------------------------
2          |3          |1          |1          

1 row selected
ij> select * from t1 join t2 using (b);
B          |A          |C          |C          |D          
-----------------------------------------------------------
2          |1          |3          |3          |1          

1 row selected
ij> select * from t1 right join t2 using (b,c);
B          |C          |A          |D          
-----------------------------------------------
1          |2          |NULL       |3          
2          |3          |1          |1          
5          |5          |NULL       |5          

3 rows selected

There are some problems that would need to be resolved, though. Some quick 
tests revealed this:

1) NullPointerException if none of the columns in the USING clause are shared 
between the tables that are being joined:

ij> select * from t1 join t2 using (a,d);
ERROR XJ001: Java exception: ': java.lang.NullPointerException'.


2) I'm not sure which behaviour is correct according to the standard, but at 
least this query differs from PostgreSQL and MySQL in which columns to return:

ij> select t1.*, t2.* from t1 join t2 using (b,c);
A          |D          
-----------------------
1          |1          

1 row selected

PostgreSQL and MySQL return more columns (results below from PostgreSQL):

kh160127=# select t1.*, t2.* from t1 join t2 using (b,c);
 a | b | c | b | c | d 
---+---+---+---+---+---
 1 | 2 | 3 | 2 | 3 | 1
(1 row)

> Implement JOIN ... USING syntax
> -------------------------------
>
>                 Key: DERBY-4370
>                 URL: https://issues.apache.org/jira/browse/DERBY-4370
>             Project: Derby
>          Issue Type: Improvement
>          Components: SQL
>    Affects Versions: 10.6.0.0
>            Reporter: Knut Anders Hatlen
>            Priority: Minor
>         Attachments: using.diff
>
>
> The SQL standard defines two ways to specify the join condition in an INNER 
> JOIN or a (LEFT/RIGHT/FULL) OUTER JOIN: with an ON clause or with a USING 
> clause. Derby currently only accepts joins with an ON clause. Internally, 
> Derby has code that supports USING. This code should be enabled to ease the 
> migration to Derby. We must also verify that the implementation adheres to 
> the standard before we enable it.
> Since USING is already a reserved keyword in Derby's parser, enabling the 
> USING syntax should not cause any compatibility issues for existing queries.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to