1 21 22 package org.apache.derby.impl.sql.compile; 23 24 import org.apache.derby.iapi.sql.compile.Visitable; 25 import org.apache.derby.iapi.sql.compile.Visitor; 26 27 import org.apache.derby.iapi.reference.SQLState; 28 import org.apache.derby.iapi.error.StandardException; 29 30 43 public class VerifyAggregateExpressionsVisitor implements Visitor 44 { 45 private GroupByList groupByList; 46 47 public VerifyAggregateExpressionsVisitor(GroupByList groupByList) 48 { 49 this.groupByList = groupByList; 50 } 51 52 53 59 72 public Visitable visit(Visitable node) 73 throws StandardException 74 { 75 if (node instanceof ColumnReference) 76 { 77 ColumnReference cr = (ColumnReference)node; 78 79 if (groupByList == null) 80 { 81 throw StandardException.newException(SQLState.LANG_INVALID_COL_REF_NON_GROUPED_SELECT_LIST, cr.getSQLColumnName()); 82 } 83 84 if (groupByList.findGroupingColumn(cr) == null) 85 { 86 throw StandardException.newException(SQLState.LANG_INVALID_GROUPED_SELECT_LIST); 87 } 88 } 89 90 99 else if (node instanceof SubqueryNode) 100 { 101 SubqueryNode subq = (SubqueryNode)node; 102 103 if ((subq.getSubqueryType() != SubqueryNode.EXPRESSION_SUBQUERY) || 104 subq.hasCorrelatedCRs()) 105 { 106 throw StandardException.newException( (groupByList == null) ? 107 SQLState.LANG_INVALID_NON_GROUPED_SELECT_LIST : 108 SQLState.LANG_INVALID_GROUPED_SELECT_LIST); 109 } 110 111 115 HasNodeVisitor visitor = new HasNodeVisitor(AggregateNode.class); 116 subq.accept(visitor); 117 if (visitor.hasNode()) 118 { 119 throw StandardException.newException( (groupByList == null) ? 120 SQLState.LANG_INVALID_NON_GROUPED_SELECT_LIST : 121 SQLState.LANG_INVALID_GROUPED_SELECT_LIST); 122 } 123 } else if (node instanceof JavaToSQLValueNode) 124 { 125 throw StandardException.newException( (groupByList == null) ? 128 SQLState.LANG_INVALID_NON_GROUPED_SELECT_LIST : 129 SQLState.LANG_INVALID_GROUPED_SELECT_LIST); 130 } 131 132 return node; 133 } 134 135 144 public boolean skipChildren(Visitable node) throws StandardException 145 { 146 return ((node instanceof AggregateNode) || 147 (node instanceof SubqueryNode) || 148 (node instanceof ValueNode && 149 groupByList != null 150 && groupByList.findGroupingColumn((ValueNode)node) != null)); 151 } 152 153 public boolean stopTraversal() 154 { 155 return false; 156 } 157 } 158 | Popular Tags |