Issue Details (XML | Word | Printable)

Key: DERBY-84
Type: Improvement Improvement
Status: Resolved Resolved
Resolution: Won't Fix
Priority: Minor Minor
Assignee: Bryan Pendleton
Reporter: Bob Gibson
Votes: 2
Watchers: 1
Operations

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

Column aliasing could simplify queries

Created: 28/Nov/04 03:25 PM   Updated: 01/Jul/09 12:34 AM
Return to search
Component/s: SQL
Affects Version/s: 10.0.2.0
Fix Version/s: None

Time Tracking:
Not Specified

File Attachments:
  Size
File Licensed for inclusion in ASF works simpleTest.diff 2008-05-14 04:24 AM Bryan Pendleton 2 kB
Issue Links:
Duplicate
 
Reference
 

Resolution Date: 16/May/08 07:25 PM


 Description  « Hide
Currently, one can not use an alias to simplify queries. For example, being able to alias "LongDescriptiveColumnName" AS LDCN would allow one to use the alias elsewhere in the query, e.g., the WHERE clause:

SELECT LongDescriptiveColumnName AS LDCN FROM MyTable WHERE LDCN LIKE '%testing%';

The current result is a message like:

ERROR 42X04: Column 'LDCN' is not in any table in the FROM list or it appears within a join specification and is outside the scope of the join specification or it appears in a HAVING clause and is not in the GROUP BY list. If this is a CREATE or ALTER TABLE statement then 'LDCN' is not a column in the target table.

 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Micah Spears added a comment - 13/Jan/05 07:37 AM
This issue is hindering Hibernate support. HQL queries using 'group by' and 'having' clauses fail in cloudscape because hibernate uses column aliases. An example is below.

HQL: select p.rxId from gov.va.med.predemo.local.pharmacy.db.pharmacy.hibernate.PatientPrescriptionDO as p, p.prescriptionStatusLogs as s group by p having max(s.statusId) = :status

SQL: select patientpre0_.RX_ID as x0_0_ from Pharmacy.PATIENT_PRESCRIPTION patientpre0_ inner join Pharmacy.PRESCRIPTION_STATUS_LOG prescripti1_ on patientpre0_.RX_ID=prescripti1_.RX_ID group by patientpre0_.RX_ID having (max(prescripti1_.STATUS_ID)=? )

Message: com.ibm.db2.jcc.c.SqlException: Column 'PATIENTPRE0_.X0_0_' is not in any table in the FROM list or it appears within a join specification and is outside the scope of the join specification or it appears in a HAVING clause and is not in the GROUP BY list. If this is a CREATE or ALTER TABLE statement then 'PATIENTPRE0_.X0_0_' is not a column in the target table.


If I remove 'patientpre0_.RX_ID as x0_0_', the query runs as expected.

Jack Klebanoff added a comment - 30/Apr/05 02:43 AM
The problem described by Bob Gibson in the main body of this report and the problem described by Micah Spears in his comment of 12/Jan/05 seem different to me.

This bug report complains that Derby does _not_ support the use of column aliases in WHERE clauses.

The problem described in the 12/Jan/05 comment is that Derby _requires_ the use of column aliases in an ORDER BY clause for columns that have aliases. The comment complains that Derby should, but currently does not, allow the original column names in an ORDER BY clause if the column has an alias. I think that the 12/Jan/05 comment is a duplicate of Derby-127, but this bug report (Derby-84) is different.

Shreyas Kaushik added a comment - 02/Jun/05 02:34 PM
Without the where clause this works, looks like a binding issue

ij> connect 'jdbc:derby:test';
ij> drop table test;
0 rows inserted/updated/deleted
ij> create table test(i int, longcolname varchar(10));
0 rows inserted/updated/deleted
ij> insert into test values(1,'abcd');
1 row inserted/updated/deleted
ij> select longcolname as lc from test where lc like '%ab%';
ERROR 42X04: Column 'LC' is not in any table in the FROM list or it appears within a join specification and is outside the scope of the join specification or it appears in a HAVING clause and is not in the GROUP BY list. If this is a CREATE or ALTER TABLE statement then 'LC' is not a column in the target table.
ij> select longcolname as lc from test;
LC
----------
abcd

1 row selected

Jack Klebanoff added a comment - 03/Jun/05 12:23 AM
I suspect that this is not a bug, that Derby is behaving correctly here according to the SQL standard.

The alias in the select list names the column in the virtual table produced by the SELECT. However Derby requires that where clause column references refer to columns in tables, views, or sub-queries listed in the FROM clause. In the bug report example LDCN is not a column in in any from list table, so Derby issues an error message when LDCN is used in the where clause.

My reading of the SQL2003 spec indicates that the Derby behavior follows the SQL standard. See the standard's discussion of identifier chains. The spec differentiates between identifiers used in ORDER BY clauses and identifiers used elsewhere. The spec says that (column) identifiers in a ORDER BY clause should be bound to the column names defined in the select list, but that other (column) identifiers should be bound to columns in tables in the FROM list.


Shreyas Kaushik added a comment - 03/Jun/05 01:03 PM
So, can this be closed ? Should the hibernate group be informed about the way they generate queries to take care of this ?
BTW have a look at this link, http://www.hibernate.org/80.392.html

Alex Miller added a comment - 16/Feb/06 02:00 AM
I agree that this matches the SQL spec. The ORDER BY column is conceptually executed after the SELECT clause and works on the output names defined in the SELECT clause. All other clauses are logically evaluated *before* the SELECT clause and thus cannot use the output column names defined by the SELECT clause. Of course, many dbs are more tolerant about what can occur, particularly about what can occur in the ORDER BY.

Andrew McIntyre made changes - 11/Sep/07 05:39 PM
Field Original Value New Value
Link This issue relates to DERBY-3066 [ DERBY-3066 ]
Thomas Nielsen made changes - 14/Sep/07 06:43 AM
Link This issue is duplicated by DERBY-3066 [ DERBY-3066 ]
Andrew McIntyre made changes - 13/May/08 05:07 PM
Link This issue relates to DERBY-2457 [ DERBY-2457 ]
Bryan Pendleton made changes - 13/May/08 10:35 PM
Assignee Bryan Pendleton [ bryanpendleton ]
Bryan Pendleton added a comment - 13/May/08 10:36 PM
I intend to add a few more tests to the test suite, to demonstrate the
current handling of column aliases in WHERE clauses, and then to
resolve this issue as "won't fix".

See the discussion in DERBY-2457 for some additional comments
regarding the reasoning behind this proposal.

Bryan Pendleton added a comment - 14/May/08 04:24 AM
simpleTest.diff contains a simple addition to select.sql to
demonstrate the current (believed to be standards-compliant)
behavior of Derby with respect to column aliases in the WHERE clause.

I propose to commit this change and resolve this issue as won't-fix,
with the reasoning that we intend to preserve the standards-compliant
behavior in this area for now.

Bryan Pendleton made changes - 14/May/08 04:24 AM
Attachment simpleTest.diff [ 12382010 ]
Bryan Pendleton added a comment - 16/May/08 03:16 AM
Committed the simple test to the trunk as revision 656929.

Bryan Pendleton added a comment - 16/May/08 07:25 PM
For the time being, we intend to preserve Derby's current behavior,
as we believe it to be standards-compliant, and adhering to the
SQL standard seems important for this case.

Bryan Pendleton made changes - 16/May/08 07:25 PM
Resolution Won't Fix [ 2 ]
Status Open [ 1 ] Resolved [ 5 ]
Dag H. Wanvik made changes - 01/Jul/09 12:34 AM
Issue Type New Feature [ 2 ] Improvement [ 4 ]