Search in sources :

Example 1 with RowValueConstructorExpression

use of org.apache.phoenix.expression.RowValueConstructorExpression in project phoenix by apache.

the class ExpressionCompiler method visitLeave.

@Override
public Expression visitLeave(ComparisonParseNode node, List<Expression> children) throws SQLException {
    ParseNode lhsNode = node.getChildren().get(0);
    ParseNode rhsNode = node.getChildren().get(1);
    Expression lhsExpr = children.get(0);
    Expression rhsExpr = children.get(1);
    CompareOp op = node.getFilterOp();
    if (lhsNode instanceof RowValueConstructorParseNode && rhsNode instanceof RowValueConstructorParseNode) {
        int i = 0;
        for (; i < Math.min(lhsExpr.getChildren().size(), rhsExpr.getChildren().size()); i++) {
            addBindParamMetaData(lhsNode.getChildren().get(i), rhsNode.getChildren().get(i), lhsExpr.getChildren().get(i), rhsExpr.getChildren().get(i));
        }
        for (; i < lhsExpr.getChildren().size(); i++) {
            addBindParamMetaData(lhsNode.getChildren().get(i), null, lhsExpr.getChildren().get(i), null);
        }
        for (; i < rhsExpr.getChildren().size(); i++) {
            addBindParamMetaData(null, rhsNode.getChildren().get(i), null, rhsExpr.getChildren().get(i));
        }
    } else if (lhsExpr instanceof RowValueConstructorExpression) {
        addBindParamMetaData(lhsNode.getChildren().get(0), rhsNode, lhsExpr.getChildren().get(0), rhsExpr);
        for (int i = 1; i < lhsExpr.getChildren().size(); i++) {
            addBindParamMetaData(lhsNode.getChildren().get(i), null, lhsExpr.getChildren().get(i), null);
        }
    } else if (rhsExpr instanceof RowValueConstructorExpression) {
        addBindParamMetaData(lhsNode, rhsNode.getChildren().get(0), lhsExpr, rhsExpr.getChildren().get(0));
        for (int i = 1; i < rhsExpr.getChildren().size(); i++) {
            addBindParamMetaData(null, rhsNode.getChildren().get(i), null, rhsExpr.getChildren().get(i));
        }
    } else {
        addBindParamMetaData(lhsNode, rhsNode, lhsExpr, rhsExpr);
    }
    return wrapGroupByExpression(ComparisonExpression.create(op, children, context.getTempPtr(), context.getCurrentTable().getTable().rowKeyOrderOptimizable()));
}
Also used : DecimalAddExpression(org.apache.phoenix.expression.DecimalAddExpression) TimestampSubtractExpression(org.apache.phoenix.expression.TimestampSubtractExpression) ArrayConstructorExpression(org.apache.phoenix.expression.ArrayConstructorExpression) Expression(org.apache.phoenix.expression.Expression) LikeExpression(org.apache.phoenix.expression.LikeExpression) ByteBasedLikeExpression(org.apache.phoenix.expression.ByteBasedLikeExpression) DoubleSubtractExpression(org.apache.phoenix.expression.DoubleSubtractExpression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) InListExpression(org.apache.phoenix.expression.InListExpression) DateSubtractExpression(org.apache.phoenix.expression.DateSubtractExpression) ArrayElemRefExpression(org.apache.phoenix.expression.function.ArrayElemRefExpression) CaseExpression(org.apache.phoenix.expression.CaseExpression) DoubleDivideExpression(org.apache.phoenix.expression.DoubleDivideExpression) NotExpression(org.apache.phoenix.expression.NotExpression) DoubleAddExpression(org.apache.phoenix.expression.DoubleAddExpression) DecimalDivideExpression(org.apache.phoenix.expression.DecimalDivideExpression) RowKeyColumnExpression(org.apache.phoenix.expression.RowKeyColumnExpression) RowValueConstructorExpression(org.apache.phoenix.expression.RowValueConstructorExpression) RoundTimestampExpression(org.apache.phoenix.expression.function.RoundTimestampExpression) StringConcatExpression(org.apache.phoenix.expression.StringConcatExpression) ComparisonExpression(org.apache.phoenix.expression.ComparisonExpression) TimestampAddExpression(org.apache.phoenix.expression.TimestampAddExpression) CoerceExpression(org.apache.phoenix.expression.CoerceExpression) StringBasedLikeExpression(org.apache.phoenix.expression.StringBasedLikeExpression) ArrayAnyComparisonExpression(org.apache.phoenix.expression.function.ArrayAnyComparisonExpression) DecimalSubtractExpression(org.apache.phoenix.expression.DecimalSubtractExpression) ModulusExpression(org.apache.phoenix.expression.ModulusExpression) DoubleMultiplyExpression(org.apache.phoenix.expression.DoubleMultiplyExpression) DecimalMultiplyExpression(org.apache.phoenix.expression.DecimalMultiplyExpression) DateAddExpression(org.apache.phoenix.expression.DateAddExpression) RoundDecimalExpression(org.apache.phoenix.expression.function.RoundDecimalExpression) LongAddExpression(org.apache.phoenix.expression.LongAddExpression) LongSubtractExpression(org.apache.phoenix.expression.LongSubtractExpression) IsNullExpression(org.apache.phoenix.expression.IsNullExpression) AndExpression(org.apache.phoenix.expression.AndExpression) LongMultiplyExpression(org.apache.phoenix.expression.LongMultiplyExpression) ArrayAllComparisonExpression(org.apache.phoenix.expression.function.ArrayAllComparisonExpression) OrExpression(org.apache.phoenix.expression.OrExpression) LongDivideExpression(org.apache.phoenix.expression.LongDivideExpression) BindParseNode(org.apache.phoenix.parse.BindParseNode) ModulusParseNode(org.apache.phoenix.parse.ModulusParseNode) LikeParseNode(org.apache.phoenix.parse.LikeParseNode) UDFParseNode(org.apache.phoenix.parse.UDFParseNode) ComparisonParseNode(org.apache.phoenix.parse.ComparisonParseNode) SequenceValueParseNode(org.apache.phoenix.parse.SequenceValueParseNode) InListParseNode(org.apache.phoenix.parse.InListParseNode) AndParseNode(org.apache.phoenix.parse.AndParseNode) ExistsParseNode(org.apache.phoenix.parse.ExistsParseNode) SubtractParseNode(org.apache.phoenix.parse.SubtractParseNode) NotParseNode(org.apache.phoenix.parse.NotParseNode) DivideParseNode(org.apache.phoenix.parse.DivideParseNode) StringConcatParseNode(org.apache.phoenix.parse.StringConcatParseNode) OrParseNode(org.apache.phoenix.parse.OrParseNode) ParseNode(org.apache.phoenix.parse.ParseNode) LiteralParseNode(org.apache.phoenix.parse.LiteralParseNode) FunctionParseNode(org.apache.phoenix.parse.FunctionParseNode) RowValueConstructorParseNode(org.apache.phoenix.parse.RowValueConstructorParseNode) MultiplyParseNode(org.apache.phoenix.parse.MultiplyParseNode) ColumnParseNode(org.apache.phoenix.parse.ColumnParseNode) CaseParseNode(org.apache.phoenix.parse.CaseParseNode) CastParseNode(org.apache.phoenix.parse.CastParseNode) AddParseNode(org.apache.phoenix.parse.AddParseNode) SubqueryParseNode(org.apache.phoenix.parse.SubqueryParseNode) ArithmeticParseNode(org.apache.phoenix.parse.ArithmeticParseNode) IsNullParseNode(org.apache.phoenix.parse.IsNullParseNode) CompareOp(org.apache.hadoop.hbase.filter.CompareFilter.CompareOp) RowValueConstructorParseNode(org.apache.phoenix.parse.RowValueConstructorParseNode) RowValueConstructorExpression(org.apache.phoenix.expression.RowValueConstructorExpression)

Example 2 with RowValueConstructorExpression

use of org.apache.phoenix.expression.RowValueConstructorExpression in project phoenix by apache.

the class QueryCompiler method getKeyExpressionCombinations.

private boolean getKeyExpressionCombinations(Pair<Expression, Expression> combination, StatementContext context, SelectStatement select, TableRef table, JoinType type, final List<Expression> joinExpressions, final List<Expression> hashExpressions) throws SQLException {
    if ((type != JoinType.Inner && type != JoinType.Semi) || this.noChildParentJoinOptimization)
        return false;
    Scan scanCopy = ScanUtil.newScan(context.getScan());
    StatementContext contextCopy = new StatementContext(statement, context.getResolver(), scanCopy, new SequenceManager(statement));
    contextCopy.setCurrentTable(table);
    List<Expression> lhsCombination = Lists.<Expression>newArrayList();
    boolean complete = WhereOptimizer.getKeyExpressionCombination(lhsCombination, contextCopy, select, joinExpressions);
    if (lhsCombination.isEmpty())
        return false;
    List<Expression> rhsCombination = Lists.newArrayListWithExpectedSize(lhsCombination.size());
    for (int i = 0; i < lhsCombination.size(); i++) {
        Expression lhs = lhsCombination.get(i);
        for (int j = 0; j < joinExpressions.size(); j++) {
            if (lhs == joinExpressions.get(j)) {
                rhsCombination.add(hashExpressions.get(j));
                break;
            }
        }
    }
    if (lhsCombination.size() == 1) {
        combination.setFirst(lhsCombination.get(0));
        combination.setSecond(rhsCombination.get(0));
    } else {
        combination.setFirst(new RowValueConstructorExpression(lhsCombination, false));
        combination.setSecond(new RowValueConstructorExpression(rhsCombination, false));
    }
    return type == JoinType.Semi && complete;
}
Also used : Expression(org.apache.phoenix.expression.Expression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) RowValueConstructorExpression(org.apache.phoenix.expression.RowValueConstructorExpression) Scan(org.apache.hadoop.hbase.client.Scan) Hint(org.apache.phoenix.parse.HintNode.Hint) RowValueConstructorExpression(org.apache.phoenix.expression.RowValueConstructorExpression)

Example 3 with RowValueConstructorExpression

use of org.apache.phoenix.expression.RowValueConstructorExpression in project phoenix by apache.

the class HashCacheClient method evaluateKeyExpression.

/**
     * Evaluate the RHS key expression and wrap the result as a new Expression.
     * Unlike other types of Expression which will be evaluated and wrapped as a 
     * single LiteralExpression, RowValueConstructorExpression should be handled 
     * differently. We should evaluate each child of RVC and wrap them into a new
     * RVC Expression, in order to make sure that the later coercion between the 
     * LHS key expression and this RHS key expression will be successful.
     * 
     * @param keyExpression the RHS key expression
     * @param tuple the input tuple
     * @param ptr the temporary pointer
     * @return the Expression containing the evaluated result
     * @throws SQLException 
     */
public static Expression evaluateKeyExpression(Expression keyExpression, Tuple tuple, ImmutableBytesWritable ptr) throws SQLException {
    if (!(keyExpression instanceof RowValueConstructorExpression)) {
        PDataType type = keyExpression.getDataType();
        keyExpression.reset();
        if (keyExpression.evaluate(tuple, ptr)) {
            return LiteralExpression.newConstant(type.toObject(ptr, keyExpression.getSortOrder()), type);
        }
        return LiteralExpression.newConstant(null, type);
    }
    List<Expression> children = keyExpression.getChildren();
    List<Expression> values = Lists.newArrayListWithExpectedSize(children.size());
    for (Expression child : children) {
        PDataType type = child.getDataType();
        child.reset();
        if (child.evaluate(tuple, ptr)) {
            values.add(LiteralExpression.newConstant(type.toObject(ptr, child.getSortOrder()), type));
        } else {
            values.add(LiteralExpression.newConstant(null, type));
        }
    }
    // might be coerced later.
    return new RowValueConstructorExpression(values, false);
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType) Expression(org.apache.phoenix.expression.Expression) RowValueConstructorExpression(org.apache.phoenix.expression.RowValueConstructorExpression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) RowValueConstructorExpression(org.apache.phoenix.expression.RowValueConstructorExpression)

Example 4 with RowValueConstructorExpression

use of org.apache.phoenix.expression.RowValueConstructorExpression in project phoenix by apache.

the class WhereOptimizer method getKeyExpressionCombination.

/**
     * Get an optimal combination of key expressions for hash join key range optimization.
     * @return returns true if the entire combined expression is covered by key range optimization
     * @param result the optimal combination of key expressions
     * @param context the temporary context to get scan ranges set by pushKeyExpressionsToScan()
     * @param statement the statement being compiled
     * @param expressions the join key expressions
     * @return the optimal list of key expressions
     */
public static boolean getKeyExpressionCombination(List<Expression> result, StatementContext context, FilterableStatement statement, List<Expression> expressions) throws SQLException {
    List<Integer> candidateIndexes = Lists.newArrayList();
    final List<Integer> pkPositions = Lists.newArrayList();
    PTable table = context.getCurrentTable().getTable();
    for (int i = 0; i < expressions.size(); i++) {
        Expression expression = expressions.get(i);
        // TODO this is a temporary fix for PHOENIX-3029.
        if (expression instanceof CoerceExpression && expression.getSortOrder() != expression.getChildren().get(0).getSortOrder()) {
            continue;
        }
        KeyExpressionVisitor visitor = new KeyExpressionVisitor(context, table);
        KeyExpressionVisitor.KeySlots keySlots = expression.accept(visitor);
        int minPkPos = Integer.MAX_VALUE;
        if (keySlots != null) {
            Iterator<KeyExpressionVisitor.KeySlot> iterator = keySlots.iterator();
            while (iterator.hasNext()) {
                KeyExpressionVisitor.KeySlot slot = iterator.next();
                if (slot.getPKPosition() < minPkPos) {
                    minPkPos = slot.getPKPosition();
                }
            }
            if (minPkPos != Integer.MAX_VALUE) {
                candidateIndexes.add(i);
                pkPositions.add(minPkPos);
            }
        }
    }
    if (candidateIndexes.isEmpty())
        return false;
    Collections.sort(candidateIndexes, new Comparator<Integer>() {

        @Override
        public int compare(Integer left, Integer right) {
            return pkPositions.get(left) - pkPositions.get(right);
        }
    });
    List<Expression> candidates = Lists.newArrayList();
    List<List<Expression>> sampleValues = Lists.newArrayList();
    for (Integer index : candidateIndexes) {
        candidates.add(expressions.get(index));
    }
    for (int i = 0; i < 2; i++) {
        List<Expression> group = Lists.newArrayList();
        for (Expression expression : candidates) {
            PDataType type = expression.getDataType();
            group.add(LiteralExpression.newConstant(type.getSampleValue(), type));
        }
        sampleValues.add(group);
    }
    int count = 0;
    int offset = table.getBucketNum() == null ? 0 : SaltingUtil.NUM_SALTING_BYTES;
    int maxPkSpan = 0;
    Expression remaining = null;
    while (count < candidates.size()) {
        Expression lhs = count == 0 ? candidates.get(0) : new RowValueConstructorExpression(candidates.subList(0, count + 1), false);
        Expression firstRhs = count == 0 ? sampleValues.get(0).get(0) : new RowValueConstructorExpression(sampleValues.get(0).subList(0, count + 1), true);
        Expression secondRhs = count == 0 ? sampleValues.get(1).get(0) : new RowValueConstructorExpression(sampleValues.get(1).subList(0, count + 1), true);
        Expression testExpression = InListExpression.create(Lists.newArrayList(lhs, firstRhs, secondRhs), false, context.getTempPtr(), context.getCurrentTable().getTable().rowKeyOrderOptimizable());
        remaining = pushKeyExpressionsToScan(context, statement, testExpression);
        if (context.getScanRanges().isPointLookup()) {
            count++;
            // found the best match
            break;
        }
        int pkSpan = context.getScanRanges().getBoundPkColumnCount() - offset;
        if (pkSpan <= maxPkSpan) {
            break;
        }
        maxPkSpan = pkSpan;
        count++;
    }
    result.addAll(candidates.subList(0, count));
    return count == candidates.size() && (context.getScanRanges().isPointLookup() || context.getScanRanges().useSkipScanFilter()) && (remaining == null || remaining.equals(LiteralExpression.newConstant(true, Determinism.ALWAYS)));
}
Also used : PTable(org.apache.phoenix.schema.PTable) Hint(org.apache.phoenix.parse.HintNode.Hint) PDataType(org.apache.phoenix.schema.types.PDataType) BaseExpression(org.apache.phoenix.expression.BaseExpression) BaseTerminalExpression(org.apache.phoenix.expression.BaseTerminalExpression) Expression(org.apache.phoenix.expression.Expression) LikeExpression(org.apache.phoenix.expression.LikeExpression) CoerceExpression(org.apache.phoenix.expression.CoerceExpression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) InListExpression(org.apache.phoenix.expression.InListExpression) RowKeyColumnExpression(org.apache.phoenix.expression.RowKeyColumnExpression) RowValueConstructorExpression(org.apache.phoenix.expression.RowValueConstructorExpression) IsNullExpression(org.apache.phoenix.expression.IsNullExpression) AndExpression(org.apache.phoenix.expression.AndExpression) ComparisonExpression(org.apache.phoenix.expression.ComparisonExpression) OrExpression(org.apache.phoenix.expression.OrExpression) CoerceExpression(org.apache.phoenix.expression.CoerceExpression) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) ArrayList(java.util.ArrayList) RowValueConstructorExpression(org.apache.phoenix.expression.RowValueConstructorExpression)

Aggregations

Expression (org.apache.phoenix.expression.Expression)4 LiteralExpression (org.apache.phoenix.expression.LiteralExpression)4 RowValueConstructorExpression (org.apache.phoenix.expression.RowValueConstructorExpression)4 AndExpression (org.apache.phoenix.expression.AndExpression)2 CoerceExpression (org.apache.phoenix.expression.CoerceExpression)2 ComparisonExpression (org.apache.phoenix.expression.ComparisonExpression)2 InListExpression (org.apache.phoenix.expression.InListExpression)2 IsNullExpression (org.apache.phoenix.expression.IsNullExpression)2 LikeExpression (org.apache.phoenix.expression.LikeExpression)2 OrExpression (org.apache.phoenix.expression.OrExpression)2 RowKeyColumnExpression (org.apache.phoenix.expression.RowKeyColumnExpression)2 PDataType (org.apache.phoenix.schema.types.PDataType)2 ArrayList (java.util.ArrayList)1 Collections.singletonList (java.util.Collections.singletonList)1 List (java.util.List)1 Scan (org.apache.hadoop.hbase.client.Scan)1 CompareOp (org.apache.hadoop.hbase.filter.CompareFilter.CompareOp)1 ArrayConstructorExpression (org.apache.phoenix.expression.ArrayConstructorExpression)1 BaseExpression (org.apache.phoenix.expression.BaseExpression)1 BaseTerminalExpression (org.apache.phoenix.expression.BaseTerminalExpression)1