You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by kr...@apache.org on 2007/03/19 14:05:49 UTC
svn commit: r519936 - in
/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile:
FromList.java FromSubquery.java OrderByColumn.java SelectNode.java
Author: kristwaa
Date: Mon Mar 19 06:05:48 2007
New Revision: 519936
URL: http://svn.apache.org/viewvc?view=rev&rev=519936
Log:
DERBY-2442: Remove code made redundant by DERBY 681.
Patch contributed by Manish Khettry.
Modified:
db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/FromList.java
db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/FromSubquery.java
db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/OrderByColumn.java
db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/SelectNode.java
Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/FromList.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/FromList.java?view=diff&rev=519936&r1=519935&r2=519936
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/FromList.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/FromList.java Mon Mar 19 06:05:48 2007
@@ -333,31 +333,10 @@
for (int index = 0; index < size; index++)
{
fromTable = (FromTable) elementAt(index);
- fromTable.bindExpressions( makeFromList( fromListParam, fromTable ) );
+ fromTable.bindExpressions(this);
}
}
- /**
- * Construct an appropriate from list for binding an individual
- * table element. Normally, this is just this list. However,
- * for the special wrapper queries which the parser creates for
- * GROUP BY and HAVING clauses, the appropriate list is the
- * outer list passed into us--it will contain the appropriate
- * tables needed to resolve correlated columns.
- */
- private FromList makeFromList( FromList fromListParam, FromTable fromTable )
- {
- if ( fromTable instanceof FromSubquery )
- {
- FromSubquery fromSubquery = (FromSubquery) fromTable;
-
- if ( fromSubquery.generatedForGroupByClause || fromSubquery.generatedForHavingClause )
- { return fromListParam; }
- }
-
- return this;
- }
-
/**
* Bind the result columns of the ResultSetNodes in this FromList when there is no
* base table to bind them to. This is useful for SELECT statements,
Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/FromSubquery.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/FromSubquery.java?view=diff&rev=519936&r1=519935&r2=519936
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/FromSubquery.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/FromSubquery.java Mon Mar 19 06:05:48 2007
@@ -21,12 +21,8 @@
package org.apache.derby.impl.sql.compile;
-import org.apache.derby.iapi.reference.SQLState;
-
-import org.apache.derby.iapi.services.context.ContextManager;
import org.apache.derby.iapi.error.StandardException;
-import org.apache.derby.iapi.sql.compile.CompilerContext;
import org.apache.derby.iapi.sql.compile.C_NodeTypes;
import org.apache.derby.iapi.sql.dictionary.DataDictionary;
@@ -34,7 +30,6 @@
import org.apache.derby.iapi.util.JBitSet;
-import java.util.Properties;
/**
* A FromSubquery represents a subquery in the FROM list of a DML statement.
@@ -48,8 +43,6 @@
*/
public class FromSubquery extends FromTable
{
- boolean generatedForGroupByClause;
- boolean generatedForHavingClause;
ResultSetNode subquery;
/**
@@ -82,10 +75,7 @@
{
if (SanityManager.DEBUG)
{
- return
- "generatedForGroupByClause: " + generatedForGroupByClause + "\n" +
- "generatedForHavingClause: " + generatedForHavingClause + "\n" +
- super.toString();
+ return super.toString();
}
else
{
@@ -123,29 +113,6 @@
return subquery;
}
- /**
- * Mark this FromSubquery as being generated for a GROUP BY clause.
- * (This node represents the SELECT thru GROUP BY clauses. We
- * appear in the FromList of a SelectNode generated to represent
- * the result of the GROUP BY. This allows us to add ResultColumns
- * to the SelectNode for the user's query.
- */
- public void markAsForGroupByClause()
- {
- generatedForGroupByClause = true;
- }
-
- /**
- * Mark this FromSubquery as being generated for a HAVING clause.
- * (This node represents the SELECT thru GROUP BY clauses. We
- * appear in the FromList of a SelectNode generated to represent
- * the actual HAVING clause.
- */
- public void markAsForHavingClause()
- {
- generatedForHavingClause = true;
- }
-
/**
* Determine whether or not the specified name is an exposed name in
* the current query block.
@@ -162,14 +129,7 @@
protected FromTable getFromTableByName(String name, String schemaName, boolean exactMatch)
throws StandardException
{
- if (generatedForGroupByClause || generatedForHavingClause)
- {
- return subquery.getFromTableByName(name, schemaName, exactMatch);
- }
- else
- {
- return super.getFromTableByName(name, schemaName, exactMatch);
- }
+ return super.getFromTableByName(name, schemaName, exactMatch);
}
/**
@@ -249,15 +209,10 @@
FromList nestedFromList;
/* From subqueries cannot be correlated, so we pass an empty FromList
- * to subquery.bindExpressions() and .bindResultColumns(). However,
- * the parser rewrites queries which have GROUP BY and HAVING clauses.
- * For these rewritten pseudo-subqueries, we need to pass in the outer FromList
- * which contains correlated tables.
+ * to subquery.bindExpressions() and .bindResultColumns()
*/
- if ( generatedForGroupByClause || generatedForHavingClause )
- { nestedFromList = fromListParam; }
- else { nestedFromList = emptyFromList; }
+ nestedFromList = emptyFromList;
subquery.bindExpressions(nestedFromList);
subquery.bindResultColumns(nestedFromList);
@@ -323,85 +278,12 @@
columnsTableName = columnReference.getTableName();
- /* We have 5 cases here:
- * 1. ColumnReference was generated to replace an aggregate.
- * (We are the wrapper for a HAVING clause and the ColumnReference
- * was generated to reference the aggregate which was pushed down into
- * the SELECT list in the user's query.)
- * Just do what you would expect. Try to resolve the
- * ColumnReference against our RCL if the ColumnReference is unqualified
- * or if it is qualified with our exposed name.
- * 2. We are the wrapper for a GROUP BY and a HAVING clause and
- * either the ColumnReference is qualified or it is in
- * the HAVING clause. For example:
- * select a from t1 group by a having t1.a = 1
- * select a as asdf from t1 group by a having a = 1
- * We need to match against the underlying FromList and then find
- * the grandparent ResultColumn in our RCL so that we return a
- * ResultColumn from the correct ResultSetNode. It is okay not to
- * find a matching grandparent node. In fact, this is how we ensure
- * the correct semantics for ColumnReferences in the HAVING clause
- * (which must be bound against the GROUP BY list.)
- * 3. We are the wrapper for a HAVING clause without a GROUP BY and
- * the ColumnReference is from the HAVING clause. ColumnReferences
- * are invalid in this case, so we return null.
- * 4. We are the wrapper for a GROUP BY with no HAVING. This has
- * to be a separate case because of #5 and the following query:
- * select * from (select c1 from t1) t, (select c1 from t1) tt
- * group by t1.c1, tt.c1
- * (The correlation names are lost in the generated FromSuquery.)
- * 5. Everything else - do what you would expect. Try to resolve the
- * ColumnReference against our RCL if the ColumnReference is unqualified
- * or if it is qualified with our exposed name.
- */
+ // post 681, 1 may be no longer needed. 5 is the default case
+ // now but what happens if the condition is false? Investigate.
if (columnReference.getGeneratedToReplaceAggregate()) // 1
{
resultColumn = resultColumns.getResultColumn(columnReference.getColumnName());
}
- else if (generatedForGroupByClause && generatedForHavingClause &&
- (columnsTableName != null ||
- columnReference.getClause() != ValueNode.IN_SELECT_LIST)) // 2
- {
- if (SanityManager.DEBUG)
- {
- SanityManager.ASSERT(correlationName == null,
- "correlationName expected to be null");
- SanityManager.ASSERT(subquery instanceof SelectNode,
- "subquery expected to be instanceof SelectNode, not " +
- subquery.getClass().getName());
- }
-
- SelectNode select = (SelectNode) subquery;
-
- resultColumn = select.getFromList().bindColumnReference(columnReference);
-
- /* Find and return the matching RC from our RCL.
- * (Not an error if no match found. Let ColumnReference deal with it.
- */
- if (resultColumn != null)
- {
- /* Is there a matching resultColumn in the subquery's RCL? */
- resultColumn = subquery.getResultColumns().findParentResultColumn(
- resultColumn);
- if (resultColumn != null)
- {
- /* Is there a matching resultColumn in our RCL? */
- resultColumn = resultColumns.findParentResultColumn(
- resultColumn);
- }
- }
- }
- else if ((generatedForHavingClause && ! generatedForGroupByClause) // 3
- && (columnReference.getClause() != ValueNode.IN_SELECT_LIST) )
- {
- resultColumn = null;
- }
- else if (generatedForGroupByClause) // 4
- {
- resultColumn = resultColumns.getResultColumn(
- columnsTableName,
- columnReference.getColumnName());
- }
else if (columnsTableName == null || columnsTableName.equals(correlationName)) // 5?
{
resultColumn = resultColumns.getAtMostOneResultColumn(columnReference, correlationName, false);
@@ -677,14 +559,8 @@
*/
TableName tableName;
- if (correlationName == null && generatedForGroupByClause)
- {
- tableName = makeTableName(null, resultColumn.getTableName());
- }
- else
- {
- tableName = exposedName;
- }
+ tableName = exposedName;
+
valueNode = (ValueNode) getNodeFactory().getNode(
C_NodeTypes.COLUMN_REFERENCE,
columnName,
Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/OrderByColumn.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/OrderByColumn.java?view=diff&rev=519936&r1=519935&r2=519936
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/OrderByColumn.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/OrderByColumn.java Mon Mar 19 06:05:48 2007
@@ -197,34 +197,6 @@
{
ResultColumnList targetCols = target.getResultColumns();
- // If the target is generated for a select node then we must also pull the order by column
- // into the select list of the subquery.
- if((target instanceof SelectNode) && ((SelectNode) target).getGeneratedForGroupbyClause())
- {
- if( SanityManager.DEBUG)
- SanityManager.ASSERT( target.getFromList().size() == 1
- && (target.getFromList().elementAt(0) instanceof FromSubquery)
- && targetCols.size() == 1
- && targetCols.getResultColumn(1) instanceof AllResultColumn,
- "Unexpected structure of selectNode generated for a group by clause");
-
- ResultSetNode subquery = ((FromSubquery) target.getFromList().elementAt(0)).getSubquery();
- pullUpOrderByColumn( subquery);
- if( resultCol == null) // The order by column is referenced by number
- return;
-
- // ResultCol is in the subquery's ResultColumnList. We have to transform this OrderByColumn
- // so that it refers to the column added to the subquery. We assume that the select list
- // in the top level target is a (generated) AllResultColumn node, so the this order by expression
- // does not have to be pulled into the the top level ResultColumnList. Just change this
- // OrderByColumn to be a reference to the added column. We cannot use an integer column
- // number because the subquery can have a '*' in its select list, causing the column
- // number to change when the '*' is expanded.
- resultCol = null;
- targetCols.copyOrderBySelect( subquery.getResultColumns());
- return;
- }
-
if(expression instanceof ColumnReference){
ColumnReference cr = (ColumnReference) expression;
Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/SelectNode.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/SelectNode.java?view=diff&rev=519936&r1=519935&r2=519936
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/SelectNode.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/SelectNode.java Mon Mar 19 06:05:48 2007
@@ -112,9 +112,6 @@
private boolean orderByAndDistinctMerged;
- private boolean generatedForGroupByClause;
- private boolean generatedForHavingClause;
-
/* Copy of fromList prior to generating join tree */
private FromList preJoinFL;
@@ -159,8 +156,6 @@
(groupByList != null ? groupByList.toString() : "null") + "\n" +
"orderByList: " +
(orderByList != null ? orderByList.toString() : "null") + "\n" +
- "generatedForGroupByClause: " +generatedForGroupByClause +"\n" +
- "generatedForHavingClause: " + generatedForHavingClause + "\n" +
super.toString();
}
else
@@ -190,32 +185,6 @@
}
/**
- * Mark this SelectNode as being generated for a GROUP BY clause.
- */
- public void markAsForGroupByClause()
- {
- generatedForGroupByClause = true;
- }
-
- /**
- * Return whether or not this SelectNode was generated for a GROUP BY clause.
- *
- * @return boolean Whether or not this SelectNode was generated for a GROUP BY clause.
- */
- public boolean getGeneratedForGroupbyClause()
- {
- return generatedForGroupByClause;
- }
-
- /**
- * Mark this SelectNode as being generated for a HAVING clause.
- */
- public void markAsForHavingClause()
- {
- generatedForHavingClause = true;
- }
-
- /**
* Prints the sub-nodes of this object. See QueryTreeNode.java for
* how tree printing is supposed to work.
*
@@ -364,24 +333,10 @@
return selectAggregates;
case ValueNode.IN_WHERE_CLAUSE:
- if (generatedForHavingClause)
- {
- return null;
- }
- else
- {
- return whereAggregates;
- }
+ return whereAggregates;
case ValueNode.IN_HAVING_CLAUSE:
- if (generatedForHavingClause)
- {
- return whereAggregates;
- }
- else
- {
- return null;
- }
+ return null;
default:
if (SanityManager.DEBUG)
@@ -542,8 +497,7 @@
** in the WHERE clause at all.
** Note: a similar check is made in JoinNode.
*/
- if ((whereAggregates.size() > 0) &&
- !generatedForHavingClause)
+ if (whereAggregates.size() > 0)
{
throw StandardException.newException(SQLState.LANG_NO_AGGREGATES_IN_WHERE_CLAUSE);
}
@@ -781,14 +735,6 @@
return;
}
- /* Select * always okay when SelectNode generated to wrap
- * GROUP BY or HAVING.
- */
- if (generatedForGroupByClause || generatedForHavingClause)
- {
- return;
- }
-
/* Select * currently only valid for EXISTS/NOT EXISTS.
* NOT EXISTS does not appear prior to preprocessing.
*/
@@ -1255,7 +1201,7 @@
}
/* Don't flatten if selectNode contains a group by or having clause */
- if ((groupByList != null) || generatedForHavingClause)
+ if ((groupByList != null) || (havingClause != null))
{
return false;
}
@@ -1933,7 +1879,7 @@
return false;
}
- if (groupByList != null || generatedForHavingClause)
+ if (groupByList != null || havingClause != null)
{
return false;
}