protected SelectStatement buildAggCommand(SelectStatement in) { SelectStatement out = CopyVisitor.copy(in); // we're going to build // select Name, max(Engine), max(Version), max(Row_format), // sum(Rows), avg(Avg_row_length), sum(Data_length), // max(Max_data_length), sum(Index_length), // sum(Data_free), max(Auto_increment), min(Create_time), // max(Update_time), max(Check_time), // max(Collation), null /*Checksum*/, max(Create_options), max(Comment) from temp1 group by Name // eventually we will build another temp table of rewrite info (visible name, auto_inc values) // and join // but for now this is good enough List<ExpressionNode> proj = new ArrayList<ExpressionNode>(); ExpressionAlias nameColumn = null; for (ExpressionNode en : out.getProjection()) { ExpressionAlias ea = (ExpressionAlias) en; ColumnInstance ci = (ColumnInstance) ea.getTarget(); String cn = ci.getColumn().getName().getUnquotedName().get(); FunctionCall fc = null; if (maxedColumns.contains(cn)) { fc = new FunctionCall(FunctionName.makeMax(), ci); } else if (summedColumns.contains(cn)) { fc = new FunctionCall(FunctionName.makeSum(), ci); } else if ("Name".equals(cn)) { fc = null; nameColumn = ea; } else if ("Avg_row_length".equals(cn)) { fc = new FunctionCall( FunctionName.makeRound(), new FunctionCall(FunctionName.makeAvg(), ci)); } else if ("Create_time".equals(cn)) { fc = new FunctionCall(FunctionName.makeMin(), ci); } else if ("Checksum".equals(cn)) { fc = null; ea = new ExpressionAlias( LiteralExpression.makeNullLiteral(), new NameAlias(ci.getColumn().getName().getUnqualified()), false); } else { throw new SchemaException(Pass.PLANNER, "Unknown show status column: " + cn); } if (fc != null) { ea = new ExpressionAlias( fc, new NameAlias(ci.getColumn().getName().getUnqualified()), false); } proj.add(ea); } out.setProjection(proj); SortingSpecification ngb = new SortingSpecification(nameColumn.buildAliasInstance(), true); ngb.setOrdering(Boolean.FALSE); out.getGroupBysEdge().add(ngb); return out; }
public static SelectStatement filterEntryProjection(SelectStatement in, PartitionEntry jre) throws PEException { PartitionEntry actual = jre.getActualEntry(); SelectStatement expecting = null; if (actual instanceof OriginalPartitionEntry) expecting = ((OriginalPartitionEntry) actual).getChildCopy(); else expecting = actual.getJoinQuery(null); ListSet<ColumnKey> ec = new ListSet<ColumnKey>(); for (ExpressionNode en : expecting.getProjection()) { ExpressionNode targ = ExpressionUtils.getTarget(en); if (targ instanceof ColumnInstance) { ColumnKey was = ((ColumnInstance) targ).getColumnKey(); ColumnKey isnow = in.getMapper().copyColumnKeyForward(was); if (isnow == null) { throw new SchemaException(Pass.PLANNER, "Lost column during lookup table join"); } ec.add(isnow); } else if (targ instanceof FunctionCall) { ExpressionNode exn = targ; RewriteKey rk = in.getMapper().mapExpressionToColumn(exn); while (rk == null && (exn.getParent() instanceof ExpressionNode)) { exn = (ExpressionNode) exn.getParent(); rk = in.getMapper().mapExpressionToColumn(exn); } if (rk != null) { ec.add((ColumnKey) rk); } } } for (Iterator<ExpressionNode> iter = in.getProjectionEdge().iterator(); iter.hasNext(); ) { ExpressionNode en = ExpressionUtils.getTarget(iter.next()); if (en instanceof ColumnInstance) { ColumnKey ck = ((ColumnInstance) en).getColumnKey(); if (!ec.contains(ck)) iter.remove(); } } return in; }