Github user ayingshu commented on a diff in the pull request:
https://github.com/apache/phoenix/pull/39#discussion_r25928196
--- Diff:
phoenix-core/src/main/java/org/apache/phoenix/compile/QueryCompiler.java ---
@@ -147,21 +169,146 @@ public QueryCompiler(PhoenixStatement statement,
SelectStatement select, ColumnR
*/
public QueryPlan compile() throws SQLException{
SelectStatement select = this.select;
- List<Object> binds = statement.getParameters();
+ QueryPlan plan;
+ if (isUnionAll()) {
+ plan = compileUnionAll(select);
+ } else {
+ plan = compileSelect(select);
+ }
+ return plan;
+ }
+
+ public QueryPlan compileSelect(SelectStatement select) throws
SQLException{
+ // SelectStatement select = this.select;
+ List<Object> binds = statement.getParameters();
+ StatementContext context = new StatementContext(statement,
resolver, scan, sequenceManager);
+ ColumnResolver resolver;
+ if (isUnionAll()) {
+ resolver = FromCompiler.getResolverForQuery(select,
statement.getConnection());
+ context = new StatementContext(statement, resolver, new
Scan(), sequenceManager);
+ }
+ if (select.isJoin()) {
+ if (isUnionAll()) {
+ resolver =
FromCompiler.getResolverForQuery(select, statement.getConnection());
+ select = JoinCompiler.optimize(statement,
select, resolver);
+ }
+ else {
+ select = JoinCompiler.optimize(statement, select,
this.resolver);
+ }
+ if (this.select != select) {
+ ColumnResolver resolver1 =
FromCompiler.getResolverForQuery(select, statement.getConnection());
+ context = new StatementContext(statement,
resolver1, scan, sequenceManager);
+ }
+ JoinTable joinTable = JoinCompiler.compile(statement,
select, context.getResolver());
+ return compileJoinQuery(context, binds, joinTable,
false, false, null);
+ } else {
+ return compileSingleQuery(context, select, binds, false,
true);
+ }
+ }
+
+ private void checkForOrderByLimitInUnionAllSelect(SelectStatement
select) throws SQLException {
+ if (select.getOrderBy() != null && !select.getOrderBy().isEmpty())
{
+ throw new
SQLExceptionInfo.Builder(SQLExceptionCode.ORDER_BY_IN_UNIONALL_SELECT_NOT_SUPPORTED).setMessage(".").build().buildException();
+ }
+ if (select.getLimit() != null) {
+ throw new
SQLExceptionInfo.Builder(SQLExceptionCode.LIMIT_IN_UNIONALL_SELECT_NOT_SUPPORTED).setMessage(".").build().buildException();
+ }
+ }
+
+ private PTable createTempTableForUnionAllResultResolver(QueryPlan
plan) throws SQLException {
+ List<PColumn> projectedColumns = new ArrayList<PColumn>();
+ Long scn = statement.getConnection().getSCN();
+ List<PColumnFamily> families =
Collections.<PColumnFamily>emptyList(); // new ArrayList<PColumnFamily>();
+ PTable theTable = new
PTableImpl(statement.getConnection().getTenantId(), "unionAllSchema",
"unionAllTable", scn == null ? HConstants.LATEST_TIMESTAMP : scn, families);
+ PTable table = plan.getTableRef().getTable();
+ for (int i=0; i< plan.getProjector().getColumnCount(); i++) {
+ ColumnProjector colProj =
plan.getProjector().getColumnProjector(i);
+ Expression sourceExpression = colProj.getExpression();
+ PColumnImpl projectedColumn = new
PColumnImpl(PNameFactory.newName(colProj.getName().getBytes()),
table.getDefaultFamilyName(),
+ sourceExpression.getDataType(),
sourceExpression.getMaxLength(), sourceExpression.getScale(),
sourceExpression.isNullable(),
+ i, sourceExpression.getSortOrder(), 50, new byte[0],
true, sourceExpression.toString());
+ projectedColumns.add(projectedColumn);
+ }
+ PTable t = PTableImpl.makePTable(theTable, projectedColumns);
+ return t;
+ }
+
+ private QueryPlan buildTupleProjectPlan(QueryPlan plan) throws
SQLException {
+ List<ExpressionProjector> projectedColumns = new
ArrayList<ExpressionProjector>();
+ PTable tbl = createTempTableForUnionAllResultResolver(plan);
+ for (int i=0; i<tbl.getColumns().size(); i++) {
+ ProjectedColumnExpression expression = new
ProjectedColumnExpression(tbl.getColumns().get(i), tbl.getColumns(), i,
tbl.getColumns().get(i).getExpressionStr());
+ projectedColumns.add(new
ExpressionProjector(tbl.getColumns().get(i).getName().getString(),
tbl.getName().getString(), expression, true));
+ }
+ RowProjector rowProjector = new RowProjector(projectedColumns,
100, true);
+ TupleProjector tupleProjector = new TupleProjector(rowProjector);
+ plan = new TupleProjectionPlan(plan, tupleProjector, null);
+ return plan;
+ }
+ private boolean containOrderBy(SelectStatement select) {
+ if (select.getOrderBy() != null && !select.getOrderBy().isEmpty())
+ return true;
+ else
+ return false;
+ }
+
+ private QueryPlan compileUnionAll(SelectStatement select) throws
SQLException {
+ List<SelectStatement> unionAllSelects = select.getSelects();
+ List<QueryPlan> plans = new ArrayList<QueryPlan>();
StatementContext context = new StatementContext(statement,
resolver, scan, sequenceManager);
- if (select.isJoin()) {
- select = JoinCompiler.optimize(statement, select, resolver);
- if (this.select != select) {
- ColumnResolver resolver =
FromCompiler.getResolverForQuery(select, statement.getConnection());
- context = new StatementContext(statement, resolver, scan,
sequenceManager);
+
+ checkForOrderByLimitInUnionAllSelect(select);
+ QueryPlan plan = compileSelect(select);
+ plan = buildTupleProjectPlan(plan);
+ plans.add(plan);
+ OrderBy orderBy = OrderBy.EMPTY_ORDER_BY;
+ boolean containOrderBy = false;
+ int numSelects = unionAllSelects.size();
+ for (int i=0; i < numSelects; i++ ) {
+ if (i < numSelects-1)
+
checkForOrderByLimitInUnionAllSelect(unionAllSelects.get(i));
+ else if (i == numSelects-1) {
+ containOrderBy = containOrderBy(unionAllSelects.get(i));
}
- JoinTable joinTable = JoinCompiler.compile(statement, select,
context.getResolver());
- return compileJoinQuery(context, binds, joinTable, false,
false, null);
- } else {
- return compileSingleQuery(context, select, binds, false, true);
+ plan = compileSelect(unionAllSelects.get(i));
+ if (containOrderBy) {
+ orderBy = plan.getOrderBy();
+ unionAllSelects.get(i).removeOrderBy();
+ plan = compileSelect(unionAllSelects.get(i));
+ }
+ plan = buildTupleProjectPlan(plan);
+ plans.add(plan);
}
+ checkProjectionNumAndTypes(plans);
+ plan = new UnionPlan(context, plan.getStatement(),
plan.getTableRef(), plan.getProjector(), plan.getLimit(), orderBy,
parallelIteratorFactory, plan.getGroupBy(), plans);
--- End diff --
OrderBy is used by union all plan to get Order by information. Cannot be
null if it has order by clause.
When building the RowProjector, which "select" you referred to here. There
are many selects in union all. Not matter which one you choose, effectively you
pick one of the selects to build the RowProjector. It is the problem I have
mentioned. And that is why I pass down RowProjector to result sets!
RowProjector rowProjector = ProjectorCompiler.compile(context, select,
groupBy, Collections.emptyList());
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---