Github user JamesRTaylor commented on a diff in the pull request: https://github.com/apache/phoenix/pull/34#discussion_r23275041 --- Diff: phoenix-core/src/main/java/org/apache/phoenix/compile/IndexColumnExpressionCompiler.java --- @@ -0,0 +1,311 @@ +/* + * 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.List; +import java.util.Map; + +import org.apache.hadoop.hbase.client.Scan; +import org.apache.phoenix.compile.GroupByCompiler.GroupBy; +import org.apache.phoenix.expression.ColumnExpression; +import org.apache.phoenix.expression.Expression; +import org.apache.phoenix.parse.AddParseNode; +import org.apache.phoenix.parse.AndParseNode; +import org.apache.phoenix.parse.ArrayAllComparisonNode; +import org.apache.phoenix.parse.ArrayAnyComparisonNode; +import org.apache.phoenix.parse.ArrayElemRefNode; +import org.apache.phoenix.parse.CaseParseNode; +import org.apache.phoenix.parse.CastParseNode; +import org.apache.phoenix.parse.ColumnDef; +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.ModulusParseNode; +import org.apache.phoenix.parse.MultiplyParseNode; +import org.apache.phoenix.parse.NamedTableNode; +import org.apache.phoenix.parse.NotParseNode; +import org.apache.phoenix.parse.OrParseNode; +import org.apache.phoenix.parse.ParseNode; +import org.apache.phoenix.parse.ParseNodeFactory; +import org.apache.phoenix.parse.RowValueConstructorParseNode; +import org.apache.phoenix.parse.SQLParser; +import org.apache.phoenix.parse.StringConcatParseNode; +import org.apache.phoenix.parse.SubtractParseNode; +import org.apache.phoenix.parse.TableName; +import org.apache.phoenix.schema.PColumn; +import org.apache.phoenix.schema.PTable; +import org.apache.phoenix.schema.PTableType; +import org.apache.phoenix.schema.types.PDataType; +import org.apache.phoenix.util.IndexUtil; + +import com.google.common.collect.Maps; + +/** + * Visitor that checks if nodes other than ColumnParseNode are present as an expression in an index. If the expression + * is present, then the node is not visited but processed as a ColumnParseNode. + */ +public class IndexColumnExpressionCompiler extends ExpressionCompiler { + + private static final ParseNodeFactory FACTORY = new ParseNodeFactory(); + private final Map<String, Expression> expressionMap; + + IndexColumnExpressionCompiler(StatementContext context) { + this(context, GroupBy.EMPTY_GROUP_BY, false); + } + + IndexColumnExpressionCompiler(StatementContext context, boolean resolveViewConstants) { + this(context, GroupBy.EMPTY_GROUP_BY, resolveViewConstants); + } + + IndexColumnExpressionCompiler(StatementContext context, GroupBy groupBy) { + this(context, groupBy, false); + } + + IndexColumnExpressionCompiler(StatementContext context, GroupBy groupBy, boolean resolveViewConstants) { + super(context, groupBy, resolveViewConstants); + expressionMap = Maps.newHashMapWithExpectedSize(context.getCurrentTable().getTable().getColumns().size()); + for (PColumn column : context.getCurrentTable().getTable().getColumns()) { + if (column.getExpressionStr() != null) { + Expression expression = null; + try { + ParseNode parseNode = SQLParser.parseCondition(column.getExpressionStr()); + expression = getExpression(parseNode); + } catch (SQLException e) { + throw new RuntimeException(e); + } + expressionMap.put(column.getExpressionStr(), expression); + } + } + } + + /** + * Returns the compiled expression + * + * @param node + * data table parse node + */ + private Expression getExpression(ParseNode node) throws SQLException { + PTable indexTable = this.context.getCurrentTable().getTable(); + NamedTableNode dataTableNode = NamedTableNode.create(null, TableName.create(indexTable.getParentSchemaName() + .getString(), indexTable.getParentTableName().getString()), Collections.<ColumnDef> emptyList()); + ColumnResolver resolver = FromCompiler.getResolver(dataTableNode, this.context.getConnection()); + StatementContext context = new StatementContext(this.context.getStatement(), resolver, new Scan(), + new SequenceManager(this.context.getStatement())); + ExpressionCompiler expressionCompiler = new ExpressionCompiler(context); + return node.accept(expressionCompiler); + } + + /** + * @return true if current table is an index and there is an expression that matches the given node + */ + private boolean matchesIndexedExpression(ParseNode node) throws SQLException { + if (context.getCurrentTable().getTable().getType() != PTableType.INDEX) { + return false; + } + DataTableNodeRewriter statementRewriter = new DataTableNodeRewriter(); + ParseNode dataTableParseNode = node.accept(statementRewriter); + Expression dataTableExpression = getExpression(dataTableParseNode); + return expressionMap.containsKey(dataTableExpression.toString()); + } + + private Expression convertAndVisitParseNode(ParseNode node) throws SQLException { + DataTableNodeRewriter statementRewriter = new DataTableNodeRewriter(); + ParseNode dataTableParseNode = node.accept(statementRewriter); + Expression expression = getExpression(dataTableParseNode); + ColumnParseNode columnParseNode = new ColumnParseNode(null, IndexUtil.getIndexColumnName(null, + expression.toString()), null); + PDataType expressionType = expression.getDataType(); + PDataType indexColType = IndexUtil.getIndexColumnDataType(expression.isNullable(), expression.getDataType()); + // if data type of expression is different from the index column data type, cast the to expression type + if (indexColType != expressionType) { --- End diff -- That's just one of the conversions we do. I'd rather not duplicate the logic here. I think if we just use the IndexStatementRewriter to rewrite the functional expression, we can compare apples-to-apples and just check for equality on the way out of the visitLeave methods. Can't seem to find my earlier comment, but also, don't duplicate all the visitEnter/visitLeave methods. Instead, make the ExpressionCompiler.wrapGroupByExpression() method public and override it here (rename it to something more general like transformExpression too).
--- 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 infrastruct...@apache.org or file a JIRA ticket with INFRA. ---