You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@phoenix.apache.org by ja...@apache.org on 2014/01/27 20:23:41 UTC
[40/51] [partial] Initial commit
http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/c5b80246/src/main/java/org/apache/phoenix/compile/GroupByCompiler.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/phoenix/compile/GroupByCompiler.java b/src/main/java/org/apache/phoenix/compile/GroupByCompiler.java
new file mode 100644
index 0000000..82feeb3
--- /dev/null
+++ b/src/main/java/org/apache/phoenix/compile/GroupByCompiler.java
@@ -0,0 +1,278 @@
+/*
+ * Copyright 2010 The Apache Software Foundation
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.phoenix.compile;
+
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+
+import org.apache.http.annotation.Immutable;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
+import org.apache.phoenix.compile.TrackOrderPreservingExpressionCompiler.Entry;
+import org.apache.phoenix.compile.TrackOrderPreservingExpressionCompiler.Ordering;
+import org.apache.phoenix.coprocessor.GroupedAggregateRegionObserver;
+import org.apache.phoenix.coprocessor.UngroupedAggregateRegionObserver;
+import org.apache.phoenix.exception.SQLExceptionCode;
+import org.apache.phoenix.exception.SQLExceptionInfo;
+import org.apache.phoenix.expression.CoerceExpression;
+import org.apache.phoenix.expression.Expression;
+import org.apache.phoenix.parse.AliasedNode;
+import org.apache.phoenix.parse.ParseNode;
+import org.apache.phoenix.parse.SelectStatement;
+import org.apache.phoenix.schema.PDataType;
+
+
+/**
+ *
+ * Validates GROUP BY clause and builds a {@link GroupBy} instance to encapsulate the
+ * group by expressions.
+ *
+ * @author jtaylor
+ * @since 0.1
+ */
+public class GroupByCompiler {
+ @Immutable
+ public static class GroupBy {
+ private final List<Expression> expressions;
+ private final List<Expression> keyExpressions;
+ private final String scanAttribName;
+ public static final GroupByCompiler.GroupBy EMPTY_GROUP_BY = new GroupBy(new GroupByBuilder());
+
+ private GroupBy(GroupByBuilder builder) {
+ this.expressions = ImmutableList.copyOf(builder.expressions);
+ this.keyExpressions = ImmutableList.copyOf(builder.keyExpressions);
+ this.scanAttribName = builder.scanAttribName;
+ assert(expressions.size() == keyExpressions.size());
+ }
+
+ public List<Expression> getExpressions() {
+ return expressions;
+ }
+
+ public List<Expression> getKeyExpressions() {
+ return keyExpressions;
+ }
+
+ public String getScanAttribName() {
+ return scanAttribName;
+ }
+
+ public boolean isEmpty() {
+ return expressions.isEmpty();
+ }
+
+ public static class GroupByBuilder {
+ private String scanAttribName;
+ private List<Expression> expressions = Collections.emptyList();
+ private List<Expression> keyExpressions = Collections.emptyList();
+
+ public GroupByBuilder() {
+ }
+
+ public GroupByBuilder setScanAttribName(String scanAttribName) {
+ this.scanAttribName = scanAttribName;
+ return this;
+ }
+
+ public GroupByBuilder setExpressions(List<Expression> expressions) {
+ this.expressions = expressions;
+ return this;
+ }
+
+ public GroupByBuilder setKeyExpressions(List<Expression> keyExpressions) {
+ this.keyExpressions = keyExpressions;
+ return this;
+ }
+
+ public GroupBy build() {
+ return new GroupBy(this);
+ }
+ }
+
+ public boolean isOrderPreserving() {
+ return !GroupedAggregateRegionObserver.UNORDERED_GROUP_BY_EXPRESSIONS.equals(scanAttribName);
+ }
+
+ public void explain(List<String> planSteps) {
+ if (scanAttribName != null) {
+ if (UngroupedAggregateRegionObserver.UNGROUPED_AGG.equals(scanAttribName)) {
+ planSteps.add(" SERVER AGGREGATE INTO SINGLE ROW");
+ } else if (GroupedAggregateRegionObserver.UNORDERED_GROUP_BY_EXPRESSIONS.equals(scanAttribName)) {
+ planSteps.add(" SERVER AGGREGATE INTO DISTINCT ROWS BY " + getExpressions());
+ } else {
+ planSteps.add(" SERVER AGGREGATE INTO ORDERED DISTINCT ROWS BY " + getExpressions());
+ }
+ }
+ }
+ }
+
+ /**
+ * Get list of columns in the GROUP BY clause.
+ * @param context query context kept between compilation of different query clauses
+ * @param statement SQL statement being compiled
+ * @return the {@link GroupBy} instance encapsulating the group by clause
+ * @throws ColumnNotFoundException if column name could not be resolved
+ * @throws AmbiguousColumnException if an unaliased column name is ambiguous across multiple tables
+ */
+ public static GroupBy compile(StatementContext context, SelectStatement statement) throws SQLException {
+ List<ParseNode> groupByNodes = statement.getGroupBy();
+ /**
+ * Distinct can use an aggregate plan if there's no group by.
+ * Otherwise, we need to insert a step after the Merge that dedups.
+ * Order by only allowed on columns in the select distinct
+ */
+ if (groupByNodes.isEmpty()) {
+ if (statement.isAggregate()) {
+ return new GroupBy.GroupByBuilder().setScanAttribName(UngroupedAggregateRegionObserver.UNGROUPED_AGG).build();
+ }
+ if (!statement.isDistinct()) {
+ return GroupBy.EMPTY_GROUP_BY;
+ }
+
+ groupByNodes = Lists.newArrayListWithExpectedSize(statement.getSelect().size());
+ for (AliasedNode aliasedNode : statement.getSelect()) {
+ groupByNodes.add(aliasedNode.getNode());
+ }
+ }
+
+ // Accumulate expressions in GROUP BY
+ TrackOrderPreservingExpressionCompiler groupByVisitor =
+ new TrackOrderPreservingExpressionCompiler(context,
+ GroupBy.EMPTY_GROUP_BY, groupByNodes.size(),
+ Ordering.UNORDERED);
+ for (ParseNode node : groupByNodes) {
+ Expression expression = node.accept(groupByVisitor);
+ if (groupByVisitor.isAggregate()) {
+ throw new SQLExceptionInfo.Builder(SQLExceptionCode.AGGREGATE_IN_GROUP_BY)
+ .setMessage(expression.toString()).build().buildException();
+ }
+ groupByVisitor.addEntry(expression);
+ groupByVisitor.reset();
+ }
+
+ List<Entry> groupByEntries = groupByVisitor.getEntries();
+ if (groupByEntries.isEmpty()) {
+ return GroupBy.EMPTY_GROUP_BY;
+ }
+
+ boolean isRowKeyOrderedGrouping = groupByVisitor.isOrderPreserving();
+ List<Expression> expressions = Lists.newArrayListWithCapacity(groupByEntries.size());
+ List<Expression> keyExpressions = expressions;
+ String groupExprAttribName;
+ // This is true if the GROUP BY is composed of only PK columns. We further check here that
+ // there are no "gaps" in the PK columns positions used (i.e. we start with the first PK
+ // column and use each subsequent one in PK order).
+ if (isRowKeyOrderedGrouping) {
+ groupExprAttribName = GroupedAggregateRegionObserver.KEY_ORDERED_GROUP_BY_EXPRESSIONS;
+ for (Entry groupByEntry : groupByEntries) {
+ expressions.add(groupByEntry.getExpression());
+ }
+ } else {
+ /*
+ * Otherwise, our coprocessor needs to collect all distinct groups within a region, sort them, and
+ * hold on to them until the scan completes.
+ */
+ groupExprAttribName = GroupedAggregateRegionObserver.UNORDERED_GROUP_BY_EXPRESSIONS;
+ /*
+ * Put fixed length nullables at the end, so that we can represent null by the absence of the trailing
+ * value in the group by key. If there is more than one, we'll need to convert the ones not at the end
+ * into a Decimal so that we can use an empty byte array as our representation for null (which correctly
+ * maintains the sort order). We convert the Decimal back to the appropriate type (Integer or Long) when
+ * it's retrieved from the result set.
+ *
+ * More specifically, order into the following buckets:
+ * 1) non nullable fixed width
+ * 2) variable width
+ * 3) nullable fixed width
+ * Within each bucket, order based on the column position in the schema. Putting the fixed width values
+ * in the beginning optimizes access to subsequent values.
+ */
+ Collections.sort(groupByEntries, new Comparator<Entry>() {
+ @Override
+ public int compare(Entry o1, Entry o2) {
+ Expression e1 = o1.getExpression();
+ Expression e2 = o2.getExpression();
+ boolean isFixed1 = e1.getDataType().isFixedWidth();
+ boolean isFixed2 = e2.getDataType().isFixedWidth();
+ boolean isFixedNullable1 = e1.isNullable() &&isFixed1;
+ boolean isFixedNullable2 = e2.isNullable() && isFixed2;
+ if (isFixedNullable1 == isFixedNullable2) {
+ if (isFixed1 == isFixed2) {
+ // Not strictly necessary, but forces the order to match the schema
+ // column order (with PK columns before value columns).
+ return o1.getColumnPosition() - o2.getColumnPosition();
+ } else if (isFixed1) {
+ return -1;
+ } else {
+ return 1;
+ }
+ } else if (isFixedNullable1) {
+ return 1;
+ } else {
+ return -1;
+ }
+ }
+ });
+ for (Entry groupByEntry : groupByEntries) {
+ expressions.add(groupByEntry.getExpression());
+ }
+ for (int i = expressions.size()-2; i >= 0; i--) {
+ Expression expression = expressions.get(i);
+ PDataType keyType = getKeyType(expression);
+ if (keyType == expression.getDataType()) {
+ continue;
+ }
+ // Copy expressions only when keyExpressions will be different than expressions
+ if (keyExpressions == expressions) {
+ keyExpressions = new ArrayList<Expression>(expressions);
+ }
+ // Wrap expression in an expression that coerces the expression to the required type..
+ // This is done so that we have a way of expressing null as an empty key when more
+ // than one fixed and nullable types are used in a group by clause
+ keyExpressions.set(i, CoerceExpression.create(expression, keyType));
+ }
+ }
+
+ // Set attribute with serialized expressions for coprocessor
+ // FIXME: what if group by is empty (i.e. only literals)?
+ GroupedAggregateRegionObserver.serializeIntoScan(context.getScan(), groupExprAttribName, keyExpressions);
+ GroupBy groupBy = new GroupBy.GroupByBuilder().setScanAttribName(groupExprAttribName).setExpressions(expressions).setKeyExpressions(keyExpressions).build();
+ return groupBy;
+ }
+
+ private static PDataType getKeyType(Expression expression) {
+ PDataType type = expression.getDataType();
+ if (!expression.isNullable() || !type.isFixedWidth()) {
+ return type;
+ }
+ if (type.isCoercibleTo(PDataType.DECIMAL)) {
+ return PDataType.DECIMAL;
+ }
+ // Should never happen
+ throw new IllegalStateException("Multiple occurrences of type " + type + " may not occur in a GROUP BY clause");
+ }
+
+ private GroupByCompiler() {
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/c5b80246/src/main/java/org/apache/phoenix/compile/HavingCompiler.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/phoenix/compile/HavingCompiler.java b/src/main/java/org/apache/phoenix/compile/HavingCompiler.java
new file mode 100644
index 0000000..3c4a8e2
--- /dev/null
+++ b/src/main/java/org/apache/phoenix/compile/HavingCompiler.java
@@ -0,0 +1,240 @@
+/*
+ * Copyright 2010 The Apache Software Foundation
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.phoenix.compile;
+
+import java.sql.SQLException;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.phoenix.compile.GroupByCompiler.GroupBy;
+import org.apache.phoenix.exception.SQLExceptionCode;
+import org.apache.phoenix.exception.SQLExceptionInfo;
+import org.apache.phoenix.expression.Expression;
+import org.apache.phoenix.expression.LiteralExpression;
+import org.apache.phoenix.parse.AddParseNode;
+import org.apache.phoenix.parse.AndParseNode;
+import org.apache.phoenix.parse.BetweenParseNode;
+import org.apache.phoenix.parse.CaseParseNode;
+import org.apache.phoenix.parse.ColumnParseNode;
+import org.apache.phoenix.parse.ComparisonParseNode;
+import org.apache.phoenix.parse.DivideParseNode;
+import org.apache.phoenix.parse.FunctionParseNode;
+import org.apache.phoenix.parse.IsNullParseNode;
+import org.apache.phoenix.parse.MultiplyParseNode;
+import org.apache.phoenix.parse.OrParseNode;
+import org.apache.phoenix.parse.ParseNode;
+import org.apache.phoenix.parse.SelectStatement;
+import org.apache.phoenix.parse.SelectStatementRewriter;
+import org.apache.phoenix.parse.SubtractParseNode;
+import org.apache.phoenix.parse.TraverseNoParseNodeVisitor;
+import org.apache.phoenix.schema.ColumnRef;
+import org.apache.phoenix.schema.PDataType;
+import org.apache.phoenix.schema.TypeMismatchException;
+
+
+public class HavingCompiler {
+
+ private HavingCompiler() {
+ }
+
+ public static Expression compile(StatementContext context, SelectStatement statement, GroupBy groupBy) throws SQLException {
+ ParseNode having = statement.getHaving();
+ if (having == null) {
+ return null;
+ }
+ ExpressionCompiler expressionBuilder = new ExpressionCompiler(context, groupBy);
+ Expression expression = having.accept(expressionBuilder);
+ if (expression.getDataType() != PDataType.BOOLEAN) {
+ throw new TypeMismatchException(PDataType.BOOLEAN, expression.getDataType(), expression.toString());
+ }
+ if (LiteralExpression.FALSE_EXPRESSION == expression) {
+ context.setScanRanges(ScanRanges.NOTHING);
+ return null;
+ } else if (LiteralExpression.TRUE_EXPRESSION == expression) {
+ return null;
+ }
+ if (!expressionBuilder.isAggregate()) {
+ throw new SQLExceptionInfo.Builder(SQLExceptionCode.ONLY_AGGREGATE_IN_HAVING_CLAUSE).build().buildException();
+ }
+ return expression;
+ }
+
+ public static SelectStatement rewrite(StatementContext context, SelectStatement statement, GroupBy groupBy) throws SQLException {
+ ParseNode having = statement.getHaving();
+ if (having == null) {
+ return statement;
+ }
+ HavingClauseVisitor visitor = new HavingClauseVisitor(context, groupBy);
+ having.accept(visitor);
+ statement = SelectStatementRewriter.moveFromHavingToWhereClause(statement, visitor.getMoveToWhereClauseExpressions());
+ return statement;
+ }
+
+ /**
+ *
+ * Visitor that figures out if an expression can be moved from the HAVING clause to
+ * the WHERE clause, since it's more optimal to pre-filter instead of post-filter.
+ *
+ * The visitor traverses through AND expressions only and into comparison expresssions.
+ * If a comparison expression uses a GROUP BY column and does not use any aggregate
+ * functions, then it's moved. For example, these HAVING expressions would be moved:
+ *
+ * select count(1) from atable group by a_string having a_string = 'foo'
+ * select count(1) from atable group by a_date having round(a_date,'hour') > ?
+ * select count(1) from atable group by a_date,a_string having a_date > ? or a_string = 'a'
+ * select count(1) from atable group by a_string,b_string having a_string = 'a' and b_string = 'b'
+ *
+ * while these would not be moved:
+ *
+ * select count(1) from atable having min(a_integer) < 5
+ * select count(1) from atable group by a_string having count(a_string) >= 1
+ * select count(1) from atable group by a_date,a_string having a_date > ? or min(a_string) = 'a'
+ * select count(1) from atable group by a_date having round(min(a_date),'hour') < ?
+ *
+ * @author jtaylor
+ * @since 0.1
+ */
+ private static class HavingClauseVisitor extends TraverseNoParseNodeVisitor<Void> {
+ private ParseNode topNode = null;
+ private boolean hasNoAggregateFunctions = true;
+ private Boolean hasOnlyAggregateColumns;
+ private final StatementContext context;
+ private final GroupBy groupBy;
+ private final Set<ParseNode> moveToWhereClause = new LinkedHashSet<ParseNode>();
+
+ HavingClauseVisitor(StatementContext context, GroupBy groupBy) {
+ this.context = context;
+ this.groupBy = groupBy;
+ }
+
+ public Set<ParseNode> getMoveToWhereClauseExpressions() {
+ return moveToWhereClause;
+ }
+
+ @Override
+ public boolean visitEnter(AndParseNode node) throws SQLException {
+ return true;
+ }
+
+ @Override
+ public boolean visitEnter(OrParseNode node) throws SQLException {
+ enterBooleanNode(node);
+ return true;
+ }
+
+ @Override
+ public boolean visitEnter(ComparisonParseNode node) throws SQLException {
+ enterBooleanNode(node);
+ return true;
+ }
+
+ @Override
+ public boolean visitEnter(IsNullParseNode node) throws SQLException {
+ enterBooleanNode(node);
+ return true;
+ }
+
+ private void enterBooleanNode(ParseNode node) {
+ if (topNode == null) {
+ topNode = node;
+ }
+ }
+
+ private void leaveBooleanNode(ParseNode node) {
+ if (topNode == node) {
+ if ( hasNoAggregateFunctions && !Boolean.FALSE.equals(hasOnlyAggregateColumns)) {
+ moveToWhereClause.add(node);
+ }
+ hasNoAggregateFunctions = true;
+ hasOnlyAggregateColumns = null;
+ topNode = null;
+ }
+ }
+
+ @Override
+ public Void visitLeave(OrParseNode node, List<Void> l) throws SQLException {
+ leaveBooleanNode(node);
+ return null;
+ }
+
+ @Override
+ public Void visitLeave(ComparisonParseNode node, List<Void> l) throws SQLException {
+ leaveBooleanNode(node);
+ return null;
+ }
+
+ @Override
+ public Void visitLeave(IsNullParseNode node, List<Void> l) throws SQLException {
+ leaveBooleanNode(node);
+ return null;
+ }
+
+ @Override
+ public boolean visitEnter(FunctionParseNode node) throws SQLException {
+ boolean isAggregate = node.isAggregate();
+ this.hasNoAggregateFunctions = this.hasNoAggregateFunctions && !isAggregate;
+ return !isAggregate;
+ }
+
+ @Override
+ public boolean visitEnter(CaseParseNode node) throws SQLException {
+ return true;
+ }
+
+ @Override
+ public Void visit(ColumnParseNode node) throws SQLException {
+ ColumnRef ref = context.getResolver().resolveColumn(node.getSchemaName(), node.getTableName(), node.getName());
+ boolean isAggregateColumn = groupBy.getExpressions().indexOf(ref.newColumnExpression()) >= 0;
+ if (hasOnlyAggregateColumns == null) {
+ hasOnlyAggregateColumns = isAggregateColumn;
+ } else {
+ hasOnlyAggregateColumns &= isAggregateColumn;
+ }
+
+ return null;
+ }
+
+ @Override
+ public boolean visitEnter(SubtractParseNode node) throws SQLException {
+ return true;
+ }
+
+ @Override
+ public boolean visitEnter(AddParseNode node) throws SQLException {
+ return true;
+ }
+
+ @Override
+ public boolean visitEnter(MultiplyParseNode node) throws SQLException {
+ return true;
+ }
+
+ @Override
+ public boolean visitEnter(DivideParseNode node) throws SQLException {
+ return true;
+ }
+
+ @Override
+ public boolean visitEnter(BetweenParseNode node) throws SQLException {
+ return true;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/c5b80246/src/main/java/org/apache/phoenix/compile/IndexStatementRewriter.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/phoenix/compile/IndexStatementRewriter.java b/src/main/java/org/apache/phoenix/compile/IndexStatementRewriter.java
new file mode 100644
index 0000000..d0fab83
--- /dev/null
+++ b/src/main/java/org/apache/phoenix/compile/IndexStatementRewriter.java
@@ -0,0 +1,67 @@
+package org.apache.phoenix.compile;
+
+import java.sql.SQLException;
+
+import org.apache.phoenix.parse.ColumnParseNode;
+import org.apache.phoenix.parse.FamilyWildcardParseNode;
+import org.apache.phoenix.parse.ParseNode;
+import org.apache.phoenix.parse.ParseNodeFactory;
+import org.apache.phoenix.parse.ParseNodeRewriter;
+import org.apache.phoenix.parse.SelectStatement;
+import org.apache.phoenix.parse.WildcardParseNode;
+import org.apache.phoenix.schema.ColumnRef;
+import org.apache.phoenix.schema.PDataType;
+import org.apache.phoenix.util.IndexUtil;
+
+public class IndexStatementRewriter extends ParseNodeRewriter {
+ private static final ParseNodeFactory FACTORY = new ParseNodeFactory();
+
+ public IndexStatementRewriter(ColumnResolver dataResolver) {
+ super(dataResolver);
+ }
+
+ /**
+ * Rewrite the select statement by translating all data table column references to
+ * references to the corresponding index column.
+ * @param statement the select statement
+ * @return new select statement or the same one if nothing was rewritten.
+ * @throws SQLException
+ */
+ public static SelectStatement translate(SelectStatement statement, ColumnResolver dataResolver) throws SQLException {
+ return rewrite(statement, new IndexStatementRewriter(dataResolver));
+ }
+
+ @Override
+ public ParseNode visit(ColumnParseNode node) throws SQLException {
+ ColumnRef dataColRef = getResolver().resolveColumn(node.getSchemaName(), node.getTableName(), node.getName());
+ String indexColName = IndexUtil.getIndexColumnName(dataColRef.getColumn());
+ // Same alias as before, but use the index column name instead of the data column name
+ ParseNode indexColNode = new ColumnParseNode(null, indexColName, node.toString());
+ PDataType indexColType = IndexUtil.getIndexColumnDataType(dataColRef.getColumn());
+ PDataType dataColType = dataColRef.getColumn().getDataType();
+
+ // Coerce index column reference back to same type as data column so that
+ // expression behave exactly the same. No need to invert, as this will be done
+ // automatically as needed. If node is used at the top level, do not convert, as
+ // otherwise the wrapper gets in the way in the group by clause. For example,
+ // an INTEGER column in a GROUP BY gets doubly wrapped like this:
+ // CAST CAST int_col AS INTEGER AS DECIMAL
+ // This is unnecessary and problematic in the case of a null value.
+ // TODO: test case for this
+ if (!isTopLevel() && indexColType != dataColType) {
+ indexColNode = FACTORY.cast(indexColNode, dataColType);
+ }
+ return indexColNode;
+ }
+
+ @Override
+ public ParseNode visit(WildcardParseNode node) throws SQLException {
+ return WildcardParseNode.REWRITE_INSTANCE;
+ }
+
+ @Override
+ public ParseNode visit(FamilyWildcardParseNode node) throws SQLException {
+ return new FamilyWildcardParseNode(node, true);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/c5b80246/src/main/java/org/apache/phoenix/compile/KeyPart.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/phoenix/compile/KeyPart.java b/src/main/java/org/apache/phoenix/compile/KeyPart.java
new file mode 100644
index 0000000..1c8b0eb
--- /dev/null
+++ b/src/main/java/org/apache/phoenix/compile/KeyPart.java
@@ -0,0 +1,79 @@
+/*
+ * Copyright 2010 The Apache Software Foundation
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.phoenix.compile;
+
+import java.util.List;
+
+import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
+
+import org.apache.phoenix.expression.Expression;
+import org.apache.phoenix.query.KeyRange;
+import org.apache.phoenix.schema.PColumn;
+
+/**
+ *
+ * Interface that determines how a key part contributes to
+ * the forming of the key (start/stop of scan and SkipScanFilter)
+ * for each part of a multi-part primary key. It acts as the glue
+ * between a built-in function and the setting of the scan key
+ * during query compilation.
+ *
+ * @author jtaylor
+ * @since 0.12
+ */
+public interface KeyPart {
+ /**
+ * Calculate the key range given an operator and the key on
+ * the RHS of an expression. For example, given the expression
+ * SUBSTR(foo,1,3) = 'bar', the key range would be ['bar','bas'),
+ * and if foo was fixed length, the upper and lower key range
+ * bytes would be filled out to the fixed length.
+ * @param op comparison operator (=, <=, <, >=, >, !=)
+ * @param rhs the constant on the RHS of an expression.
+ * @return the key range that encompasses the range for the
+ * expression for which this keyPart is associated
+ *
+ * @see org.apache.phoenix.expression.function.ScalarFunction#newKeyPart(KeyPart)
+ */
+ public KeyRange getKeyRange(CompareOp op, Expression rhs);
+
+ /**
+ * Determines whether an expression gets extracted from the
+ * WHERE clause if it contributes toward the building of the
+ * scan key. For example, the SUBSTR built-in function may
+ * be extracted, since it may be completely represented
+ * through a key range. However, the REGEXP_SUBSTR must be
+ * left in the WHERE clause, since only the constant prefix
+ * part of the evaluation can be represented through a key
+ * range (i.e. rows may pass through that will fail when
+ * the REGEXP_SUBSTR is evaluated).
+ *
+ * @return an empty list if the expression should remain in
+ * the WHEERE clause for post filtering or a singleton list
+ * containing the expression if it should be removed.
+ */
+ public List<Expression> getExtractNodes();
+
+ /**
+ * Gets the primary key column associated with the start of this key part.
+ * @return the primary key column for this key part
+ */
+ public PColumn getColumn();
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/c5b80246/src/main/java/org/apache/phoenix/compile/LimitCompiler.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/phoenix/compile/LimitCompiler.java b/src/main/java/org/apache/phoenix/compile/LimitCompiler.java
new file mode 100644
index 0000000..ef32b0e
--- /dev/null
+++ b/src/main/java/org/apache/phoenix/compile/LimitCompiler.java
@@ -0,0 +1,109 @@
+/*
+ * Copyright 2010 The Apache Software Foundation
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.phoenix.compile;
+
+import java.sql.SQLException;
+
+import org.apache.phoenix.parse.*;
+import org.apache.phoenix.schema.*;
+
+
+public class LimitCompiler {
+ private static final ParseNodeFactory NODE_FACTORY = new ParseNodeFactory();
+
+ public static final PDatum LIMIT_DATUM = new PDatum() {
+ @Override
+ public boolean isNullable() {
+ return false;
+ }
+ @Override
+ public PDataType getDataType() {
+ return PDataType.INTEGER;
+ }
+ @Override
+ public Integer getByteSize() {
+ return getDataType().getByteSize();
+ }
+ @Override
+ public Integer getMaxLength() {
+ return null;
+ }
+ @Override
+ public Integer getScale() {
+ return null;
+ }
+ @Override
+ public ColumnModifier getColumnModifier() {
+ return null;
+ }
+ };
+
+ private LimitCompiler() {
+ }
+
+ public static Integer compile(StatementContext context, FilterableStatement statement) throws SQLException {
+ LimitNode limitNode = statement.getLimit();
+ if (limitNode == null) {
+ return null;
+ }
+ LimitParseNodeVisitor visitor = new LimitParseNodeVisitor(context);
+ limitNode.getLimitParseNode().accept(visitor);
+ return visitor.getLimit();
+ }
+
+ private static class LimitParseNodeVisitor extends TraverseNoParseNodeVisitor<Void> {
+ private final StatementContext context;
+ private Integer limit;
+
+ private LimitParseNodeVisitor(StatementContext context) {
+ this.context = context;
+ }
+
+ public Integer getLimit() {
+ return limit;
+ }
+
+ @Override
+ public Void visit(LiteralParseNode node) throws SQLException {
+ Object limitValue = node.getValue();
+ // If limit is null, leave this.limit set to zero
+ // This means that we've bound limit to null for the purpose of
+ // collecting parameter metadata.
+ if (limitValue != null) {
+ Integer limit = (Integer)LIMIT_DATUM.getDataType().toObject(limitValue, node.getType());
+ if (limit.intValue() >= 0) { // TODO: handle LIMIT 0
+ this.limit = limit;
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public Void visit(BindParseNode node) throws SQLException {
+ Object value = context.getBindManager().getBindValue(node);
+ context.getBindManager().addParamMetaData(node, LIMIT_DATUM);
+ // Resolve the bind value, create a LiteralParseNode, and call the visit method for it.
+ // In this way, we can deal with just having a literal on one side of the expression.
+ visit(NODE_FACTORY.literal(value, LIMIT_DATUM.getDataType()));
+ return null;
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/c5b80246/src/main/java/org/apache/phoenix/compile/MutatingParallelIteratorFactory.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/phoenix/compile/MutatingParallelIteratorFactory.java b/src/main/java/org/apache/phoenix/compile/MutatingParallelIteratorFactory.java
new file mode 100644
index 0000000..b19af43
--- /dev/null
+++ b/src/main/java/org/apache/phoenix/compile/MutatingParallelIteratorFactory.java
@@ -0,0 +1,115 @@
+/*
+ * Copyright 2010 The Apache Software Foundation
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.phoenix.compile;
+
+import static org.apache.phoenix.query.QueryConstants.AGG_TIMESTAMP;
+import static org.apache.phoenix.query.QueryConstants.SINGLE_COLUMN;
+import static org.apache.phoenix.query.QueryConstants.SINGLE_COLUMN_FAMILY;
+import static org.apache.phoenix.query.QueryConstants.UNGROUPED_AGG_ROW_KEY;
+
+import java.sql.SQLException;
+import java.util.List;
+
+import org.apache.hadoop.hbase.KeyValue;
+
+import org.apache.phoenix.execute.MutationState;
+import org.apache.phoenix.iterate.ParallelIterators.ParallelIteratorFactory;
+import org.apache.phoenix.iterate.PeekingResultIterator;
+import org.apache.phoenix.iterate.ResultIterator;
+import org.apache.phoenix.jdbc.PhoenixConnection;
+import org.apache.phoenix.query.ConnectionQueryServices;
+import org.apache.phoenix.query.QueryServices;
+import org.apache.phoenix.query.QueryServicesOptions;
+import org.apache.phoenix.schema.PDataType;
+import org.apache.phoenix.schema.TableRef;
+import org.apache.phoenix.schema.tuple.SingleKeyValueTuple;
+import org.apache.phoenix.schema.tuple.Tuple;
+import org.apache.phoenix.util.KeyValueUtil;
+
+/**
+ * Factory class used to instantiate an iterator to handle mutations made during a parallel scan.
+ */
+public abstract class MutatingParallelIteratorFactory implements ParallelIteratorFactory {
+ protected final PhoenixConnection connection;
+ protected final TableRef tableRef;
+
+ protected MutatingParallelIteratorFactory(PhoenixConnection connection, TableRef tableRef) {
+ this.connection = connection;
+ this.tableRef = tableRef;
+ }
+
+ /**
+ * Method that does the actual mutation work
+ */
+ abstract protected MutationState mutate(PhoenixConnection connection, ResultIterator iterator) throws SQLException;
+
+ @Override
+ public PeekingResultIterator newIterator(ResultIterator iterator) throws SQLException {
+ // Clone the connection as it's not thread safe and will be operated on in parallel
+ final PhoenixConnection connection = new PhoenixConnection(this.connection);
+ MutationState state = mutate(connection, iterator);
+ long totalRowCount = state.getUpdateCount();
+ if (connection.getAutoCommit()) {
+ connection.getMutationState().join(state);
+ connection.commit();
+ ConnectionQueryServices services = connection.getQueryServices();
+ int maxSize = services.getProps().getInt(QueryServices.MAX_MUTATION_SIZE_ATTRIB,QueryServicesOptions.DEFAULT_MAX_MUTATION_SIZE);
+ state = new MutationState(maxSize, connection, totalRowCount);
+ }
+ final MutationState finalState = state;
+ byte[] value = PDataType.LONG.toBytes(totalRowCount);
+ KeyValue keyValue = KeyValueUtil.newKeyValue(UNGROUPED_AGG_ROW_KEY, SINGLE_COLUMN_FAMILY, SINGLE_COLUMN, AGG_TIMESTAMP, value, 0, value.length);
+ final Tuple tuple = new SingleKeyValueTuple(keyValue);
+ return new PeekingResultIterator() {
+ private boolean done = false;
+
+ @Override
+ public Tuple next() throws SQLException {
+ if (done) {
+ return null;
+ }
+ done = true;
+ return tuple;
+ }
+
+ @Override
+ public void explain(List<String> planSteps) {
+ }
+
+ @Override
+ public void close() throws SQLException {
+ try {
+ // Join the child mutation states in close, since this is called in a single threaded manner
+ // after the parallel results have been processed.
+ if (!connection.getAutoCommit()) {
+ MutatingParallelIteratorFactory.this.connection.getMutationState().join(finalState);
+ }
+ } finally {
+ connection.close();
+ }
+ }
+
+ @Override
+ public Tuple peek() throws SQLException {
+ return done ? null : tuple;
+ }
+ };
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/c5b80246/src/main/java/org/apache/phoenix/compile/MutationPlan.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/phoenix/compile/MutationPlan.java b/src/main/java/org/apache/phoenix/compile/MutationPlan.java
new file mode 100644
index 0000000..11fd2cd
--- /dev/null
+++ b/src/main/java/org/apache/phoenix/compile/MutationPlan.java
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2010 The Apache Software Foundation
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.phoenix.compile;
+
+import java.sql.SQLException;
+
+import org.apache.phoenix.execute.MutationState;
+import org.apache.phoenix.jdbc.PhoenixConnection;
+
+
+public interface MutationPlan extends StatementPlan {
+ public PhoenixConnection getConnection();
+ public MutationState execute() throws SQLException;
+}
http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/c5b80246/src/main/java/org/apache/phoenix/compile/OrderByCompiler.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/phoenix/compile/OrderByCompiler.java b/src/main/java/org/apache/phoenix/compile/OrderByCompiler.java
new file mode 100644
index 0000000..cf19d77
--- /dev/null
+++ b/src/main/java/org/apache/phoenix/compile/OrderByCompiler.java
@@ -0,0 +1,126 @@
+/*
+ * Copyright 2010 The Apache Software Foundation
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.phoenix.compile;
+
+
+import java.sql.SQLException;
+import java.util.Collections;
+import java.util.LinkedHashSet;
+import java.util.List;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
+import org.apache.phoenix.compile.GroupByCompiler.GroupBy;
+import org.apache.phoenix.compile.TrackOrderPreservingExpressionCompiler.Ordering;
+import org.apache.phoenix.exception.SQLExceptionCode;
+import org.apache.phoenix.exception.SQLExceptionInfo;
+import org.apache.phoenix.expression.Expression;
+import org.apache.phoenix.expression.OrderByExpression;
+import org.apache.phoenix.parse.FilterableStatement;
+import org.apache.phoenix.parse.OrderByNode;
+import org.apache.phoenix.schema.ColumnModifier;
+
+/**
+ * Validates ORDER BY clause and builds up a list of referenced columns.
+ *
+ * @author syyang
+ * @since 0.1
+ */
+public class OrderByCompiler {
+ public static class OrderBy {
+ public static final OrderBy EMPTY_ORDER_BY = new OrderBy(Collections.<OrderByExpression>emptyList());
+ /**
+ * Used to indicate that there was an ORDER BY, but it was optimized out because
+ * rows are already returned in this order.
+ */
+ public static final OrderBy ROW_KEY_ORDER_BY = new OrderBy(Collections.<OrderByExpression>emptyList());
+
+ private final List<OrderByExpression> orderByExpressions;
+
+ private OrderBy(List<OrderByExpression> orderByExpressions) {
+ this.orderByExpressions = ImmutableList.copyOf(orderByExpressions);
+ }
+
+ public List<OrderByExpression> getOrderByExpressions() {
+ return orderByExpressions;
+ }
+ }
+ /**
+ * Gets a list of columns in the ORDER BY clause
+ * @param context the query context for tracking various states
+ * associated with the given select statement
+ * @param statement TODO
+ * @param groupBy the list of columns in the GROUP BY clause
+ * @param limit the row limit or null if no limit
+ * @return the compiled ORDER BY clause
+ * @throws SQLException
+ */
+ public static OrderBy compile(StatementContext context,
+ FilterableStatement statement,
+ GroupBy groupBy, Integer limit) throws SQLException {
+ List<OrderByNode> orderByNodes = statement.getOrderBy();
+ if (orderByNodes.isEmpty()) {
+ return OrderBy.EMPTY_ORDER_BY;
+ }
+ // accumulate columns in ORDER BY
+ TrackOrderPreservingExpressionCompiler visitor =
+ new TrackOrderPreservingExpressionCompiler(context, groupBy,
+ orderByNodes.size(), Ordering.ORDERED);
+ LinkedHashSet<OrderByExpression> orderByExpressions = Sets.newLinkedHashSetWithExpectedSize(orderByNodes.size());
+ for (OrderByNode node : orderByNodes) {
+ boolean isAscending = node.isAscending();
+ Expression expression = node.getNode().accept(visitor);
+ if (visitor.addEntry(expression, isAscending ? null : ColumnModifier.SORT_DESC)) {
+ // Detect mix of aggregate and non aggregates (i.e. ORDER BY txns, SUM(txns)
+ if (!visitor.isAggregate()) {
+ if (statement.isAggregate() || statement.isDistinct()) {
+ // Detect ORDER BY not in SELECT DISTINCT: SELECT DISTINCT count(*) FROM t ORDER BY x
+ if (statement.isDistinct()) {
+ throw new SQLExceptionInfo.Builder(SQLExceptionCode.ORDER_BY_NOT_IN_SELECT_DISTINCT)
+ .setMessage(expression.toString()).build().buildException();
+ }
+ ExpressionCompiler.throwNonAggExpressionInAggException(expression.toString());
+ }
+ }
+ if (expression.getColumnModifier() == ColumnModifier.SORT_DESC) {
+ isAscending = !isAscending;
+ }
+ OrderByExpression orderByExpression = new OrderByExpression(expression, node.isNullsLast(), isAscending);
+ orderByExpressions.add(orderByExpression);
+ }
+ visitor.reset();
+ }
+
+ if (orderByExpressions.isEmpty()) {
+ return OrderBy.EMPTY_ORDER_BY;
+ }
+ // If we're ordering by the order returned by the scan, we don't need an order by
+ if (visitor.isOrderPreserving()) {
+ return OrderBy.ROW_KEY_ORDER_BY;
+ }
+
+ return new OrderBy(Lists.newArrayList(orderByExpressions.iterator()));
+ }
+
+
+ private OrderByCompiler() {
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/c5b80246/src/main/java/org/apache/phoenix/compile/PostDDLCompiler.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/phoenix/compile/PostDDLCompiler.java b/src/main/java/org/apache/phoenix/compile/PostDDLCompiler.java
new file mode 100644
index 0000000..a209bb2
--- /dev/null
+++ b/src/main/java/org/apache/phoenix/compile/PostDDLCompiler.java
@@ -0,0 +1,220 @@
+/*
+ * Copyright 2010 The Apache Software Foundation
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.phoenix.compile;
+
+import java.sql.ParameterMetaData;
+import java.sql.SQLException;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.hadoop.hbase.client.Scan;
+import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
+
+import com.google.common.collect.Lists;
+import org.apache.phoenix.cache.ServerCacheClient.ServerCache;
+import org.apache.phoenix.compile.GroupByCompiler.GroupBy;
+import org.apache.phoenix.compile.OrderByCompiler.OrderBy;
+import org.apache.phoenix.coprocessor.UngroupedAggregateRegionObserver;
+import org.apache.phoenix.execute.AggregatePlan;
+import org.apache.phoenix.execute.MutationState;
+import org.apache.phoenix.iterate.ResultIterator;
+import org.apache.phoenix.jdbc.PhoenixConnection;
+import org.apache.phoenix.jdbc.PhoenixParameterMetaData;
+import org.apache.phoenix.parse.SelectStatement;
+import org.apache.phoenix.query.QueryConstants;
+import org.apache.phoenix.query.Scanner;
+import org.apache.phoenix.schema.ColumnRef;
+import org.apache.phoenix.schema.PColumn;
+import org.apache.phoenix.schema.PColumnFamily;
+import org.apache.phoenix.schema.PDataType;
+import org.apache.phoenix.schema.TableRef;
+import org.apache.phoenix.schema.tuple.Tuple;
+import org.apache.phoenix.util.ScanUtil;
+
+
+/**
+ *
+ * Class that compiles plan to update data values after a DDL command
+ * executes.
+ *
+ * TODO: get rid of this ugly code and just go through the standard APIs.
+ * The only time we may still need this is to manage updating the empty
+ * key value, as we sometimes need to "go back through time" to adjust
+ * this.
+ * @author jtaylor
+ * @since 0.1
+ */
+public class PostDDLCompiler {
+ private final PhoenixConnection connection;
+
+ public PostDDLCompiler(PhoenixConnection connection) {
+ this.connection = connection;
+ }
+
+ public MutationPlan compile(final List<TableRef> tableRefs, final byte[] emptyCF, final byte[] projectCF, final List<PColumn> deleteList,
+ final long timestamp) throws SQLException {
+
+ return new MutationPlan() {
+
+ @Override
+ public PhoenixConnection getConnection() {
+ return connection;
+ }
+
+ @Override
+ public ParameterMetaData getParameterMetaData() {
+ return PhoenixParameterMetaData.EMPTY_PARAMETER_META_DATA;
+ }
+
+ @Override
+ public ExplainPlan getExplainPlan() throws SQLException {
+ return ExplainPlan.EMPTY_PLAN;
+ }
+
+ @Override
+ public MutationState execute() throws SQLException {
+ if (tableRefs.isEmpty()) {
+ return null;
+ }
+ boolean wasAutoCommit = connection.getAutoCommit();
+ try {
+ connection.setAutoCommit(true);
+ SQLException sqlE = null;
+ if (deleteList == null && emptyCF == null) {
+ return new MutationState(0, connection);
+ }
+ /*
+ * Handles:
+ * 1) deletion of all rows for a DROP TABLE and subsequently deletion of all rows for a DROP INDEX;
+ * 2) deletion of all column values for a ALTER TABLE DROP COLUMN
+ * 3) updating the necessary rows to have an empty KV
+ */
+ long totalMutationCount = 0;
+ for (final TableRef tableRef : tableRefs) {
+ Scan scan = new Scan();
+ scan.setAttribute(UngroupedAggregateRegionObserver.UNGROUPED_AGG, QueryConstants.TRUE);
+ ColumnResolver resolver = new ColumnResolver() {
+ @Override
+ public List<TableRef> getTables() {
+ return Collections.singletonList(tableRef);
+ }
+ @Override
+ public ColumnRef resolveColumn(String schemaName, String tableName, String colName) throws SQLException {
+ throw new UnsupportedOperationException();
+ }
+ };
+ StatementContext context = new StatementContext(SelectStatement.COUNT_ONE, connection, resolver, Collections.<Object>emptyList(), scan);
+ ScanUtil.setTimeRange(scan, timestamp);
+ if (emptyCF != null) {
+ scan.setAttribute(UngroupedAggregateRegionObserver.EMPTY_CF, emptyCF);
+ }
+ ServerCache cache = null;
+ try {
+ if (deleteList != null) {
+ if (deleteList.isEmpty()) {
+ scan.setAttribute(UngroupedAggregateRegionObserver.DELETE_AGG, QueryConstants.TRUE);
+ // In the case of a row deletion, add index metadata so mutable secondary indexing works
+ /* TODO
+ ImmutableBytesWritable ptr = context.getTempPtr();
+ tableRef.getTable().getIndexMaintainers(ptr);
+ if (ptr.getLength() > 0) {
+ IndexMetaDataCacheClient client = new IndexMetaDataCacheClient(connection, tableRef);
+ cache = client.addIndexMetadataCache(context.getScanRanges(), ptr);
+ byte[] uuidValue = cache.getId();
+ scan.setAttribute(PhoenixIndexCodec.INDEX_UUID, uuidValue);
+ }
+ */
+ } else {
+ // In the case of the empty key value column family changing, do not send the index
+ // metadata, as we're currently managing this from the client. It's possible for the
+ // data empty column family to stay the same, while the index empty column family
+ // changes.
+ PColumn column = deleteList.get(0);
+ if (emptyCF == null) {
+ scan.addColumn(column.getFamilyName().getBytes(), column.getName().getBytes());
+ }
+ scan.setAttribute(UngroupedAggregateRegionObserver.DELETE_CF, column.getFamilyName().getBytes());
+ scan.setAttribute(UngroupedAggregateRegionObserver.DELETE_CQ, column.getName().getBytes());
+ }
+ }
+ List<byte[]> columnFamilies = Lists.newArrayListWithExpectedSize(tableRef.getTable().getColumnFamilies().size());
+ if (projectCF == null) {
+ for (PColumnFamily family : tableRef.getTable().getColumnFamilies()) {
+ columnFamilies.add(family.getName().getBytes());
+ }
+ } else {
+ columnFamilies.add(projectCF);
+ }
+ // Need to project all column families into the scan, since we haven't yet created our empty key value
+ RowProjector projector = ProjectionCompiler.compile(context, SelectStatement.COUNT_ONE, GroupBy.EMPTY_GROUP_BY);
+ // Explicitly project these column families and don't project the empty key value,
+ // since at this point we haven't added the empty key value everywhere.
+ if (columnFamilies != null) {
+ scan.getFamilyMap().clear();
+ for (byte[] family : columnFamilies) {
+ scan.addFamily(family);
+ }
+ projector = new RowProjector(projector,false);
+ }
+ QueryPlan plan = new AggregatePlan(context, SelectStatement.COUNT_ONE, tableRef, projector, null, OrderBy.EMPTY_ORDER_BY, null, GroupBy.EMPTY_GROUP_BY, null);
+ Scanner scanner = plan.getScanner();
+ ResultIterator iterator = scanner.iterator();
+ try {
+ Tuple row = iterator.next();
+ ImmutableBytesWritable ptr = context.getTempPtr();
+ totalMutationCount += (Long)projector.getColumnProjector(0).getValue(row, PDataType.LONG, ptr);
+ } catch (SQLException e) {
+ sqlE = e;
+ } finally {
+ try {
+ iterator.close();
+ } catch (SQLException e) {
+ if (sqlE == null) {
+ sqlE = e;
+ } else {
+ sqlE.setNextException(e);
+ }
+ } finally {
+ if (sqlE != null) {
+ throw sqlE;
+ }
+ }
+ }
+ } finally {
+ if (cache != null) { // Remove server cache if there is one
+ cache.close();
+ }
+ }
+
+ }
+ final long count = totalMutationCount;
+ return new MutationState(1, connection) {
+ @Override
+ public long getUpdateCount() {
+ return count;
+ }
+ };
+ } finally {
+ if (!wasAutoCommit) connection.setAutoCommit(wasAutoCommit);
+ }
+ }
+ };
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/c5b80246/src/main/java/org/apache/phoenix/compile/PostIndexDDLCompiler.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/phoenix/compile/PostIndexDDLCompiler.java b/src/main/java/org/apache/phoenix/compile/PostIndexDDLCompiler.java
new file mode 100644
index 0000000..69108d3
--- /dev/null
+++ b/src/main/java/org/apache/phoenix/compile/PostIndexDDLCompiler.java
@@ -0,0 +1,119 @@
+/*
+ * Copyright 2010 The Apache Software Foundation
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.phoenix.compile;
+
+import java.sql.ParameterMetaData;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+
+import org.apache.phoenix.execute.MutationState;
+import org.apache.phoenix.jdbc.PhoenixConnection;
+import org.apache.phoenix.jdbc.PhoenixParameterMetaData;
+import org.apache.phoenix.schema.ColumnNotFoundException;
+import org.apache.phoenix.schema.PColumn;
+import org.apache.phoenix.schema.PTable;
+import org.apache.phoenix.schema.TableRef;
+import org.apache.phoenix.util.IndexUtil;
+
+
+/**
+ * Class that compiles plan to generate initial data values after a DDL command for
+ * index table.
+ */
+public class PostIndexDDLCompiler {
+ private final PhoenixConnection connection;
+ private final TableRef dataTableRef;
+
+ public PostIndexDDLCompiler(PhoenixConnection connection, TableRef dataTableRef) {
+ this.connection = connection;
+ this.dataTableRef = dataTableRef;
+ }
+
+ public MutationPlan compile(final PTable indexTable) throws SQLException {
+ return new MutationPlan() {
+
+ @Override
+ public PhoenixConnection getConnection() {
+ return connection;
+ }
+
+ @Override
+ public ParameterMetaData getParameterMetaData() {
+ return PhoenixParameterMetaData.EMPTY_PARAMETER_META_DATA;
+ }
+
+ @Override
+ public ExplainPlan getExplainPlan() throws SQLException {
+ return ExplainPlan.EMPTY_PLAN;
+ }
+
+ @Override
+ public MutationState execute() throws SQLException {
+ boolean wasAutoCommit = connection.getAutoCommit();
+ try {
+ connection.setAutoCommit(true);
+ /*
+ * Handles:
+ * 1) Populate a newly created table with contents.
+ * 2) Activate the index by setting the INDEX_STATE to
+ */
+ // NOTE: For first version, we would use a upsert/select to populate the new index table and
+ // returns synchronously. Creating an index on an existing table with large amount of data
+ // will as a result take a very very long time.
+ // In the long term, we should change this to an asynchronous process to populate the index
+ // that would allow the user to easily monitor the process of index creation.
+ StringBuilder indexColumns = new StringBuilder();
+ StringBuilder dataColumns = new StringBuilder();
+ for (PColumn col: dataTableRef.getTable().getColumns()) {
+ String indexColName = IndexUtil.getIndexColumnName(col);
+ try {
+ indexTable.getColumn(indexColName);
+ if (col.getFamilyName() != null) {
+ dataColumns.append('"').append(col.getFamilyName()).append("\".");
+ }
+ dataColumns.append('"').append(col.getName()).append("\",");
+ indexColumns.append('"').append(indexColName).append("\",");
+ } catch (ColumnNotFoundException e) {
+ // Catch and ignore - means that this data column is not in the index
+ }
+ }
+ dataColumns.setLength(dataColumns.length()-1);
+ indexColumns.setLength(indexColumns.length()-1);
+ String schemaName = dataTableRef.getTable().getSchemaName().getString();
+ String tableName = indexTable.getTableName().getString();
+
+ StringBuilder updateStmtStr = new StringBuilder();
+ updateStmtStr.append("UPSERT /*+ NO_INDEX */ INTO ").append(schemaName.length() == 0 ? "" : '"' + schemaName + "\".").append('"').append(tableName).append("\"(")
+ .append(indexColumns).append(") SELECT ").append(dataColumns).append(" FROM ")
+ .append(schemaName.length() == 0 ? "" : '"' + schemaName + "\".").append('"').append(dataTableRef.getTable().getTableName().getString()).append('"');
+ PreparedStatement updateStmt = connection.prepareStatement(updateStmtStr.toString());
+ int rowsUpdated = 0;
+ updateStmt.execute();
+ rowsUpdated = updateStmt.getUpdateCount();
+ // Return number of rows built for index
+ return new MutationState(rowsUpdated, connection);
+ } finally {
+ if (!wasAutoCommit) connection.setAutoCommit(false);
+ }
+ }
+ };
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/c5b80246/src/main/java/org/apache/phoenix/compile/ProjectionCompiler.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/phoenix/compile/ProjectionCompiler.java b/src/main/java/org/apache/phoenix/compile/ProjectionCompiler.java
new file mode 100644
index 0000000..76ea5d2
--- /dev/null
+++ b/src/main/java/org/apache/phoenix/compile/ProjectionCompiler.java
@@ -0,0 +1,354 @@
+/*
+ * Copyright 2010 The Apache Software Foundation
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.phoenix.compile;
+
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.NavigableSet;
+import java.util.Set;
+
+import org.apache.hadoop.hbase.client.Scan;
+import org.apache.hadoop.hbase.util.Bytes;
+
+import com.google.common.collect.Iterators;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
+import org.apache.phoenix.compile.GroupByCompiler.GroupBy;
+import org.apache.phoenix.coprocessor.GroupedAggregateRegionObserver;
+import org.apache.phoenix.expression.CoerceExpression;
+import org.apache.phoenix.expression.Expression;
+import org.apache.phoenix.expression.aggregator.ClientAggregators;
+import org.apache.phoenix.expression.aggregator.ServerAggregators;
+import org.apache.phoenix.expression.function.SingleAggregateFunction;
+import org.apache.phoenix.expression.visitor.SingleAggregateFunctionVisitor;
+import org.apache.phoenix.parse.AliasedNode;
+import org.apache.phoenix.parse.BindParseNode;
+import org.apache.phoenix.parse.ColumnParseNode;
+import org.apache.phoenix.parse.FamilyWildcardParseNode;
+import org.apache.phoenix.parse.ParseNode;
+import org.apache.phoenix.parse.SelectStatement;
+import org.apache.phoenix.parse.WildcardParseNode;
+import org.apache.phoenix.schema.ArgumentTypeMismatchException;
+import org.apache.phoenix.schema.ColumnNotFoundException;
+import org.apache.phoenix.schema.ColumnRef;
+import org.apache.phoenix.schema.PColumn;
+import org.apache.phoenix.schema.PColumnFamily;
+import org.apache.phoenix.schema.PDataType;
+import org.apache.phoenix.schema.PDatum;
+import org.apache.phoenix.schema.PTable;
+import org.apache.phoenix.schema.PTableType;
+import org.apache.phoenix.schema.RowKeySchema;
+import org.apache.phoenix.schema.TableRef;
+import org.apache.phoenix.util.IndexUtil;
+import org.apache.phoenix.util.SizedUtil;
+
+
+/**
+ *
+ * Class that iterates through expressions in SELECT clause and adds projected
+ * columns to scan.
+ *
+ * @author jtaylor
+ * @since 0.1
+ */
+public class ProjectionCompiler {
+
+ private ProjectionCompiler() {
+ }
+
+ private static void projectAllColumnFamilies(PTable table, Scan scan) {
+ // Will project all known/declared column families
+ scan.getFamilyMap().clear();
+ for (PColumnFamily family : table.getColumnFamilies()) {
+ scan.addFamily(family.getName().getBytes());
+ }
+ }
+
+ private static void projectColumnFamily(PTable table, Scan scan, byte[] family) {
+ // Will project all colmuns for given CF
+ scan.addFamily(family);
+ }
+
+ public static RowProjector compile(StatementContext context, SelectStatement statement, GroupBy groupBy) throws SQLException {
+ return compile(context, statement, groupBy, Collections.<PColumn>emptyList());
+ }
+
+ private static void projectAllTableColumns(StatementContext context, TableRef tableRef, List<Expression> projectedExpressions, List<ExpressionProjector> projectedColumns) throws SQLException {
+ PTable table = tableRef.getTable();
+ for (int i = table.getBucketNum() == null ? 0 : 1; i < table.getColumns().size(); i++) {
+ ColumnRef ref = new ColumnRef(tableRef,i);
+ Expression expression = ref.newColumnExpression();
+ projectedExpressions.add(expression);
+ projectedColumns.add(new ExpressionProjector(ref.getColumn().getName().getString(), table.getName().getString(), expression, false));
+ }
+ }
+
+ private static void projectAllIndexColumns(StatementContext context, TableRef tableRef, List<Expression> projectedExpressions, List<ExpressionProjector> projectedColumns) throws SQLException {
+ PTable index = tableRef.getTable();
+ PTable table = context.getConnection().getPMetaData().getTable(index.getParentName().getString());
+ int tableOffset = table.getBucketNum() == null ? 0 : 1;
+ int indexOffset = index.getBucketNum() == null ? 0 : 1;
+ if (index.getColumns().size()-indexOffset != table.getColumns().size()-tableOffset) {
+ // We'll end up not using this by the optimizer, so just throw
+ throw new ColumnNotFoundException(WildcardParseNode.INSTANCE.toString());
+ }
+ for (int i = tableOffset; i < table.getColumns().size(); i++) {
+ PColumn tableColumn = table.getColumns().get(i);
+ PColumn indexColumn = index.getColumn(IndexUtil.getIndexColumnName(tableColumn));
+ ColumnRef ref = new ColumnRef(tableRef,indexColumn.getPosition());
+ Expression expression = ref.newColumnExpression();
+ projectedExpressions.add(expression);
+ ExpressionProjector projector = new ExpressionProjector(tableColumn.getName().getString(), table.getName().getString(), expression, false);
+ projectedColumns.add(projector);
+ }
+ }
+
+ private static void projectTableColumnFamily(StatementContext context, String cfName, TableRef tableRef, List<Expression> projectedExpressions, List<ExpressionProjector> projectedColumns) throws SQLException {
+ PTable table = tableRef.getTable();
+ PColumnFamily pfamily = table.getColumnFamily(cfName);
+ for (PColumn column : pfamily.getColumns()) {
+ ColumnRef ref = new ColumnRef(tableRef, column.getPosition());
+ Expression expression = ref.newColumnExpression();
+ projectedExpressions.add(expression);
+ projectedColumns.add(new ExpressionProjector(column.getName().toString(), table.getName()
+ .getString(), expression, false));
+ }
+ }
+
+ private static void projectIndexColumnFamily(StatementContext context, String cfName, TableRef tableRef, List<Expression> projectedExpressions, List<ExpressionProjector> projectedColumns) throws SQLException {
+ PTable index = tableRef.getTable();
+ PTable table = context.getConnection().getPMetaData().getTable(index.getParentName().getString());
+ PColumnFamily pfamily = table.getColumnFamily(cfName);
+ for (PColumn column : pfamily.getColumns()) {
+ PColumn indexColumn = index.getColumn(IndexUtil.getIndexColumnName(column));
+ ColumnRef ref = new ColumnRef(tableRef, indexColumn.getPosition());
+ Expression expression = ref.newColumnExpression();
+ projectedExpressions.add(expression);
+ projectedColumns.add(new ExpressionProjector(column.getName().toString(),
+ table.getName().getString(), expression, false));
+ }
+ }
+
+ /**
+ * Builds the projection for the scan
+ * @param context query context kept between compilation of different query clauses
+ * @param statement TODO
+ * @param groupBy compiled GROUP BY clause
+ * @param targetColumns list of columns, parallel to aliasedNodes, that are being set for an
+ * UPSERT SELECT statement. Used to coerce expression types to the expected target type.
+ * @return projector used to access row values during scan
+ * @throws SQLException
+ */
+ public static RowProjector compile(StatementContext context, SelectStatement statement, GroupBy groupBy, List<? extends PDatum> targetColumns) throws SQLException {
+ List<AliasedNode> aliasedNodes = statement.getSelect();
+ // Setup projected columns in Scan
+ SelectClauseVisitor selectVisitor = new SelectClauseVisitor(context, groupBy);
+ List<ExpressionProjector> projectedColumns = new ArrayList<ExpressionProjector>();
+ TableRef tableRef = context.getResolver().getTables().get(0);
+ PTable table = tableRef.getTable();
+ boolean isWildcard = false;
+ Scan scan = context.getScan();
+ int index = 0;
+ List<Expression> projectedExpressions = Lists.newArrayListWithExpectedSize(aliasedNodes.size());
+ List<byte[]> projectedFamilies = Lists.newArrayListWithExpectedSize(aliasedNodes.size());
+ for (AliasedNode aliasedNode : aliasedNodes) {
+ ParseNode node = aliasedNode.getNode();
+ // TODO: visitor?
+ if (node instanceof WildcardParseNode) {
+ if (statement.isAggregate()) {
+ ExpressionCompiler.throwNonAggExpressionInAggException(node.toString());
+ }
+ isWildcard = true;
+ if (tableRef.getTable().getType() == PTableType.INDEX && ((WildcardParseNode)node).isRewrite()) {
+ projectAllIndexColumns(context, tableRef, projectedExpressions, projectedColumns);
+ } else {
+ projectAllTableColumns(context, tableRef, projectedExpressions, projectedColumns);
+ }
+ } else if (node instanceof FamilyWildcardParseNode){
+ // Project everything for SELECT cf.*
+ String cfName = ((FamilyWildcardParseNode) node).getName();
+ // Delay projecting to scan, as when any other column in the column family gets
+ // added to the scan, it overwrites that we want to project the entire column
+ // family. Instead, we do the projection at the end.
+ // TODO: consider having a ScanUtil.addColumn and ScanUtil.addFamily to work
+ // around this, as this code depends on this function being the last place where
+ // columns are projected (which is currently true, but could change).
+ projectedFamilies.add(Bytes.toBytes(cfName));
+ if (tableRef.getTable().getType() == PTableType.INDEX && ((FamilyWildcardParseNode)node).isRewrite()) {
+ projectIndexColumnFamily(context, cfName, tableRef, projectedExpressions, projectedColumns);
+ } else {
+ projectTableColumnFamily(context, cfName, tableRef, projectedExpressions, projectedColumns);
+ }
+ } else {
+ Expression expression = node.accept(selectVisitor);
+ projectedExpressions.add(expression);
+ if (index < targetColumns.size()) {
+ PDatum targetColumn = targetColumns.get(index);
+ if (targetColumn.getDataType() != expression.getDataType()) {
+ PDataType targetType = targetColumn.getDataType();
+ // Check if coerce allowed using more relaxed isCastableTo check, since we promote INTEGER to LONG
+ // during expression evaluation and then convert back to INTEGER on UPSERT SELECT (and we don't have
+ // (an actual value we can specifically check against).
+ if (expression.getDataType() != null && !expression.getDataType().isCastableTo(targetType)) {
+ throw new ArgumentTypeMismatchException(targetType, expression.getDataType(), "column: " + targetColumn);
+ }
+ expression = CoerceExpression.create(expression, targetType);
+ }
+ }
+ if (node instanceof BindParseNode) {
+ context.getBindManager().addParamMetaData((BindParseNode)node, expression);
+ }
+ if (!node.isConstant()) {
+ if (!selectVisitor.isAggregate() && statement.isAggregate()) {
+ ExpressionCompiler.throwNonAggExpressionInAggException(expression.toString());
+ }
+ }
+ String columnAlias = aliasedNode.getAlias();
+ boolean isCaseSensitive = aliasedNode.isCaseSensitve() || selectVisitor.isCaseSensitive;
+ String name = columnAlias == null ? expression.toString() : columnAlias;
+ projectedColumns.add(new ExpressionProjector(name, table.getName().getString(), expression, isCaseSensitive));
+ }
+ selectVisitor.reset();
+ index++;
+ }
+
+ int estimatedKeySize = table.getRowKeySchema().getEstimatedValueLength();
+ int estimatedByteSize = 0;
+ for (Map.Entry<byte[],NavigableSet<byte[]>> entry : scan.getFamilyMap().entrySet()) {
+ PColumnFamily family = table.getColumnFamily(entry.getKey());
+ if (entry.getValue() == null) {
+ for (PColumn column : family.getColumns()) {
+ Integer byteSize = column.getByteSize();
+ estimatedByteSize += SizedUtil.KEY_VALUE_SIZE + estimatedKeySize + (byteSize == null ? RowKeySchema.ESTIMATED_VARIABLE_LENGTH_SIZE : byteSize);
+ }
+ } else {
+ for (byte[] cq : entry.getValue()) {
+ PColumn column = family.getColumn(cq);
+ Integer byteSize = column.getByteSize();
+ estimatedByteSize += SizedUtil.KEY_VALUE_SIZE + estimatedKeySize + (byteSize == null ? RowKeySchema.ESTIMATED_VARIABLE_LENGTH_SIZE : byteSize);
+ }
+ }
+ }
+
+ selectVisitor.compile();
+ // Since we don't have the empty key value in read-only tables,
+ // we must project everything.
+ boolean isProjectEmptyKeyValue = table.getType() != PTableType.VIEW && !isWildcard;
+ if (isProjectEmptyKeyValue) {
+ for (byte[] family : projectedFamilies) {
+ projectColumnFamily(table, scan, family);
+ }
+ } else {
+ /*
+ * TODO: this could be optimized by detecting:
+ * - if a column is projected that's not in the where clause
+ * - if a column is grouped by that's not in the where clause
+ * - if we're not using IS NULL or CASE WHEN expressions
+ */
+ projectAllColumnFamilies(table,scan);
+ }
+ return new RowProjector(projectedColumns, estimatedByteSize, isProjectEmptyKeyValue);
+ }
+
+ private static class SelectClauseVisitor extends ExpressionCompiler {
+ private static int getMinNullableIndex(List<SingleAggregateFunction> aggFuncs, boolean isUngroupedAggregation) {
+ int minNullableIndex = aggFuncs.size();
+ for (int i = 0; i < aggFuncs.size(); i++) {
+ SingleAggregateFunction aggFunc = aggFuncs.get(i);
+ if (isUngroupedAggregation ? aggFunc.getAggregator().isNullable() : aggFunc.getAggregatorExpression().isNullable()) {
+ minNullableIndex = i;
+ break;
+ }
+ }
+ return minNullableIndex;
+ }
+
+ /**
+ * Track whether or not the projection expression is case sensitive. We use this
+ * information to determine whether or not we normalize the column name passed
+ */
+ private boolean isCaseSensitive;
+ private int elementCount;
+
+ private SelectClauseVisitor(StatementContext context, GroupBy groupBy) {
+ super(context, groupBy);
+ reset();
+ }
+
+
+ /**
+ * Compiles projection by:
+ * 1) Adding RowCount aggregate function if not present when limiting rows. We need this
+ * to track how many rows have been scanned.
+ * 2) Reordering aggregation functions (by putting fixed length aggregates first) to
+ * optimize the positional access of the aggregated value.
+ */
+ private void compile() throws SQLException {
+ final Set<SingleAggregateFunction> aggFuncSet = Sets.newHashSetWithExpectedSize(context.getExpressionManager().getExpressionCount());
+
+ Iterator<Expression> expressions = context.getExpressionManager().getExpressions();
+ while (expressions.hasNext()) {
+ Expression expression = expressions.next();
+ expression.accept(new SingleAggregateFunctionVisitor() {
+ @Override
+ public Iterator<Expression> visitEnter(SingleAggregateFunction function) {
+ aggFuncSet.add(function);
+ return Iterators.emptyIterator();
+ }
+ });
+ }
+ if (aggFuncSet.isEmpty() && groupBy.isEmpty()) {
+ return;
+ }
+ List<SingleAggregateFunction> aggFuncs = new ArrayList<SingleAggregateFunction>(aggFuncSet);
+ Collections.sort(aggFuncs, SingleAggregateFunction.SCHEMA_COMPARATOR);
+
+ int minNullableIndex = getMinNullableIndex(aggFuncs,groupBy.isEmpty());
+ context.getScan().setAttribute(GroupedAggregateRegionObserver.AGGREGATORS, ServerAggregators.serialize(aggFuncs, minNullableIndex));
+ ClientAggregators clientAggregators = new ClientAggregators(aggFuncs, minNullableIndex);
+ context.getAggregationManager().setAggregators(clientAggregators);
+ }
+
+ @Override
+ public void reset() {
+ super.reset();
+ elementCount = 0;
+ isCaseSensitive = true;
+ }
+
+ @Override
+ protected ColumnRef resolveColumn(ColumnParseNode node) throws SQLException {
+ ColumnRef ref = super.resolveColumn(node);
+ isCaseSensitive = isCaseSensitive && node.isCaseSensitive();
+ return ref;
+ }
+
+ @Override
+ public void addElement(List<Expression> l, Expression element) {
+ elementCount++;
+ isCaseSensitive &= elementCount == 1;
+ super.addElement(l, element);
+ }
+ }
+}