Search in sources :

Example 31 with ParseNode

use of org.apache.phoenix.parse.ParseNode in project phoenix by apache.

the class ExpressionCompiler method visitLeave.

@Override
public Expression visitLeave(CastParseNode node, List<Expression> children) throws SQLException {
    ParseNode childNode = node.getChildren().get(0);
    PDataType targetDataType = node.getDataType();
    Expression childExpr = children.get(0);
    PDataType fromDataType = childExpr.getDataType();
    if (childNode instanceof BindParseNode) {
        context.getBindManager().addParamMetaData((BindParseNode) childNode, childExpr);
    }
    Expression expr = childExpr;
    if (fromDataType != null) {
        /*
             * IndexStatementRewriter creates a CAST parse node when rewriting the query to use
             * indexed columns. Without this check present we wrongly and unnecessarily
             * end up creating a RoundExpression. 
             */
        if (context.getCurrentTable().getTable().getType() != PTableType.INDEX) {
            expr = convertToRoundExpressionIfNeeded(fromDataType, targetDataType, children);
        }
    }
    boolean rowKeyOrderOptimizable = context.getCurrentTable().getTable().rowKeyOrderOptimizable();
    return wrapGroupByExpression(CoerceExpression.create(expr, targetDataType, SortOrder.getDefault(), expr.getMaxLength(), rowKeyOrderOptimizable));
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType) 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) BindParseNode(org.apache.phoenix.parse.BindParseNode)

Example 32 with ParseNode

use of org.apache.phoenix.parse.ParseNode in project phoenix by apache.

the class ExpressionCompiler method visitLeave.

@Override
public Expression visitLeave(IsNullParseNode node, List<Expression> children) throws SQLException {
    ParseNode childNode = node.getChildren().get(0);
    Expression child = children.get(0);
    if (childNode instanceof BindParseNode) {
        // TODO: valid/possibe?
        context.getBindManager().addParamMetaData((BindParseNode) childNode, child);
    }
    return wrapGroupByExpression(IsNullExpression.create(child, node.isNegate(), context.getTempPtr()));
}
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) BindParseNode(org.apache.phoenix.parse.BindParseNode)

Example 33 with ParseNode

use of org.apache.phoenix.parse.ParseNode in project phoenix by apache.

the class GroupByCompiler method compile.

/**
     * 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, boolean isOrderPreserving) 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
         */
    boolean isUngroupedAggregate = false;
    if (groupByNodes.isEmpty()) {
        if (statement.isAggregate()) {
            // TODO: PHOENIX-2989 suggests some ways to optimize the latter case
            if (statement.getHint().hasHint(Hint.RANGE_SCAN) || statement.getHaving() != null) {
                return GroupBy.UNGROUPED_GROUP_BY;
            }
            groupByNodes = Lists.newArrayListWithExpectedSize(statement.getSelect().size());
            for (AliasedNode aliasedNode : statement.getSelect()) {
                if (aliasedNode.getNode() instanceof DistinctCountParseNode) {
                    // only add children of DistinctCount nodes
                    groupByNodes.addAll(aliasedNode.getNode().getChildren());
                } else {
                    // if we found anything else, do not attempt any further optimization
                    return GroupBy.UNGROUPED_GROUP_BY;
                }
            }
            isUngroupedAggregate = true;
        } else if (statement.isDistinct()) {
            groupByNodes = Lists.newArrayListWithExpectedSize(statement.getSelect().size());
            for (AliasedNode aliasedNode : statement.getSelect()) {
                // for distinct at all select expression as group by conditions
                groupByNodes.add(aliasedNode.getNode());
            }
        } else {
            return GroupBy.EMPTY_GROUP_BY;
        }
    }
    // Accumulate expressions in GROUP BY
    ExpressionCompiler compiler = new ExpressionCompiler(context, GroupBy.EMPTY_GROUP_BY);
    List<Expression> expressions = Lists.newArrayListWithExpectedSize(groupByNodes.size());
    for (int i = 0; i < groupByNodes.size(); i++) {
        ParseNode node = groupByNodes.get(i);
        Expression expression = node.accept(compiler);
        if (!expression.isStateless()) {
            if (compiler.isAggregate()) {
                throw new SQLExceptionInfo.Builder(SQLExceptionCode.AGGREGATE_IN_GROUP_BY).setMessage(expression.toString()).build().buildException();
            }
            expressions.add(expression);
        }
        compiler.reset();
    }
    if (expressions.isEmpty()) {
        return GroupBy.EMPTY_GROUP_BY;
    }
    GroupBy groupBy = new GroupBy.GroupByBuilder().setIsOrderPreserving(isOrderPreserving).setExpressions(expressions).setKeyExpressions(expressions).setIsUngroupedAggregate(isUngroupedAggregate).build();
    return groupBy;
}
Also used : AliasedNode(org.apache.phoenix.parse.AliasedNode) Hint(org.apache.phoenix.parse.HintNode.Hint) DistinctCountParseNode(org.apache.phoenix.parse.DistinctCountParseNode) Expression(org.apache.phoenix.expression.Expression) CoerceExpression(org.apache.phoenix.expression.CoerceExpression) DistinctCountParseNode(org.apache.phoenix.parse.DistinctCountParseNode) ParseNode(org.apache.phoenix.parse.ParseNode) SQLExceptionInfo(org.apache.phoenix.exception.SQLExceptionInfo)

Example 34 with ParseNode

use of org.apache.phoenix.parse.ParseNode in project phoenix by apache.

the class HavingCompiler method compile.

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() != PBoolean.INSTANCE) {
        throw TypeMismatchException.newException(PBoolean.INSTANCE, expression.getDataType(), expression.toString());
    }
    if (LiteralExpression.isBooleanFalseOrNull(expression)) {
        context.setScanRanges(ScanRanges.NOTHING);
        return null;
    } else if (LiteralExpression.isTrue(expression)) {
        return null;
    }
    if (!expressionBuilder.isAggregate()) {
        throw new SQLExceptionInfo.Builder(SQLExceptionCode.ONLY_AGGREGATE_IN_HAVING_CLAUSE).build().buildException();
    }
    return expression;
}
Also used : LiteralExpression(org.apache.phoenix.expression.LiteralExpression) Expression(org.apache.phoenix.expression.Expression) FunctionParseNode(org.apache.phoenix.parse.FunctionParseNode) AndParseNode(org.apache.phoenix.parse.AndParseNode) ColumnParseNode(org.apache.phoenix.parse.ColumnParseNode) ParseNode(org.apache.phoenix.parse.ParseNode)

Example 35 with ParseNode

use of org.apache.phoenix.parse.ParseNode in project phoenix by apache.

the class IndexStatementRewriter method visit.

@Override
public ParseNode visit(ColumnParseNode node) throws SQLException {
    ColumnRef dataColRef = getResolver().resolveColumn(node.getSchemaName(), node.getTableName(), node.getName());
    PColumn dataCol = dataColRef.getColumn();
    TableRef dataTableRef = dataColRef.getTableRef();
    // view constants if based on an UPDATABLE view
    if (dataCol.getViewConstant() != null) {
        byte[] viewConstant = dataCol.getViewConstant();
        // Ignore last byte, as it's only there so we can have a way to differentiate null
        // from the absence of a value.
        ptr.set(viewConstant, 0, viewConstant.length - 1);
        Object literal = dataCol.getDataType().toObject(ptr);
        return new LiteralParseNode(literal, dataCol.getDataType());
    }
    TableName tName = getReplacedTableName(dataTableRef);
    if (multiTableRewriteMap != null && tName == null)
        return node;
    String indexColName = IndexUtil.getIndexColumnName(dataCol);
    ParseNode indexColNode = new ColumnParseNode(tName, '"' + indexColName + '"', node.getAlias());
    PDataType indexColType = IndexUtil.getIndexColumnDataType(dataCol);
    PDataType dataColType = dataColRef.getColumn().getDataType();
    // TODO: test case for this
    if (!isTopLevel() && indexColType != dataColType) {
        indexColNode = FACTORY.cast(indexColNode, dataColType, null, null);
    }
    return indexColNode;
}
Also used : PColumn(org.apache.phoenix.schema.PColumn) TableName(org.apache.phoenix.parse.TableName) PDataType(org.apache.phoenix.schema.types.PDataType) ColumnParseNode(org.apache.phoenix.parse.ColumnParseNode) WildcardParseNode(org.apache.phoenix.parse.WildcardParseNode) LiteralParseNode(org.apache.phoenix.parse.LiteralParseNode) ColumnParseNode(org.apache.phoenix.parse.ColumnParseNode) TableWildcardParseNode(org.apache.phoenix.parse.TableWildcardParseNode) ParseNode(org.apache.phoenix.parse.ParseNode) FamilyWildcardParseNode(org.apache.phoenix.parse.FamilyWildcardParseNode) ColumnRef(org.apache.phoenix.schema.ColumnRef) TableRef(org.apache.phoenix.schema.TableRef) LiteralParseNode(org.apache.phoenix.parse.LiteralParseNode)

Aggregations

ParseNode (org.apache.phoenix.parse.ParseNode)49 ColumnParseNode (org.apache.phoenix.parse.ColumnParseNode)34 Expression (org.apache.phoenix.expression.Expression)23 AndParseNode (org.apache.phoenix.parse.AndParseNode)23 ComparisonParseNode (org.apache.phoenix.parse.ComparisonParseNode)22 LiteralExpression (org.apache.phoenix.expression.LiteralExpression)20 LiteralParseNode (org.apache.phoenix.parse.LiteralParseNode)19 SubqueryParseNode (org.apache.phoenix.parse.SubqueryParseNode)18 AliasedNode (org.apache.phoenix.parse.AliasedNode)16 ExistsParseNode (org.apache.phoenix.parse.ExistsParseNode)15 RowValueConstructorParseNode (org.apache.phoenix.parse.RowValueConstructorParseNode)15 SelectStatement (org.apache.phoenix.parse.SelectStatement)14 UDFParseNode (org.apache.phoenix.parse.UDFParseNode)14 BindParseNode (org.apache.phoenix.parse.BindParseNode)13 RowKeyColumnExpression (org.apache.phoenix.expression.RowKeyColumnExpression)12 FunctionParseNode (org.apache.phoenix.parse.FunctionParseNode)12 SequenceValueParseNode (org.apache.phoenix.parse.SequenceValueParseNode)12 AndExpression (org.apache.phoenix.expression.AndExpression)11 CoerceExpression (org.apache.phoenix.expression.CoerceExpression)11 RowValueConstructorExpression (org.apache.phoenix.expression.RowValueConstructorExpression)11