Search in sources :

Example 11 with SqlKind

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlKind in project drill by axbaretto.

the class RewriteCombineBinaryOperators method visitCall.

@Override
public RexNode visitCall(RexCall call) {
    SqlOperator op = call.getOperator();
    SqlKind kind = op.getKind();
    RelDataType type = call.getType();
    if (kind == SqlKind.AND) {
        List<RexNode> conjuncts = Lists.newArrayList();
        for (RexNode child : call.getOperands()) {
            conjuncts.addAll(RelOptUtil.conjunctions(child.accept(this)));
        }
        return RexUtil.composeConjunction(builder, conjuncts, true);
    }
    if (kind == SqlKind.OR) {
        List<RexNode> disjuncts = Lists.newArrayList();
        for (RexNode child : call.getOperands()) {
            disjuncts.addAll(RelOptUtil.disjunctions(child.accept(this)));
        }
        return RexUtil.composeDisjunction(builder, disjuncts, true);
    }
    return builder.makeCall(type, op, visitChildren(call));
}
Also used : SqlOperator(org.apache.calcite.sql.SqlOperator) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlKind(org.apache.calcite.sql.SqlKind) RexNode(org.apache.calcite.rex.RexNode)

Example 12 with SqlKind

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlKind in project drill by apache.

the class MetadataAggregateHelper method addColumnAggregateCalls.

/**
 * Adds aggregate calls to calculate column metadata either from column data itself
 * or from previously calculated metadata.
 *
 * @param fieldRef  field reference
 * @param fieldName field name
 */
private void addColumnAggregateCalls(FieldReference fieldRef, String fieldName) {
    List<SchemaPath> interestingColumns = context.interestingColumns();
    if (createNewAggregations()) {
        if (interestingColumns == null || interestingColumns.contains(fieldRef)) {
            // collect statistics for all or only interesting columns if they are specified
            AnalyzeColumnUtils.COLUMN_STATISTICS_FUNCTIONS.forEach((statisticsKind, sqlKind) -> {
                // constructs "case when projectMetadataColumn is null then column1 else null end" call
                // to avoid using default values for required columns when data for empty result is obtained
                LogicalExpression caseExpr = IfExpression.newBuilder().setIfCondition(new IfExpression.IfCondition(new FunctionCall("isnull", Collections.singletonList(FieldReference.getWithQuotedRef(columnNamesOptions.projectMetadataColumn())), ExpressionPosition.UNKNOWN), fieldRef)).setElse(NullExpression.INSTANCE).build();
                LogicalExpression call = new FunctionCall(sqlKind.name(), Collections.singletonList(caseExpr), ExpressionPosition.UNKNOWN);
                valueExpressions.add(new NamedExpression(call, FieldReference.getWithQuotedRef(AnalyzeColumnUtils.getColumnStatisticsFieldName(fieldName, statisticsKind))));
            });
        }
    } else if (AnalyzeColumnUtils.isColumnStatisticsField(fieldName) || AnalyzeColumnUtils.isMetadataStatisticsField(fieldName)) {
        SqlKind function = AnalyzeColumnUtils.COLUMN_STATISTICS_FUNCTIONS.get(AnalyzeColumnUtils.getStatisticsKind(fieldName));
        if (function == SqlKind.COUNT) {
            // for the case when aggregation was done, call SUM function for the results of COUNT aggregate call
            function = SqlKind.SUM;
        }
        LogicalExpression functionCall = new FunctionCall(function.name(), Collections.singletonList(fieldRef), ExpressionPosition.UNKNOWN);
        valueExpressions.add(new NamedExpression(functionCall, fieldRef));
    }
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression) IfExpression(org.apache.drill.common.expression.IfExpression) SchemaPath(org.apache.drill.common.expression.SchemaPath) NamedExpression(org.apache.drill.common.logical.data.NamedExpression) FunctionCall(org.apache.drill.common.expression.FunctionCall) SqlKind(org.apache.calcite.sql.SqlKind)

Example 13 with SqlKind

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlKind in project drill by apache.

the class DrillReduceAggregatesRule method reduceAgg.

private RexNode reduceAgg(Aggregate oldAggRel, AggregateCall oldCall, List<AggregateCall> newCalls, Map<AggregateCall, RexNode> aggCallMapping, List<RexNode> inputExprs) {
    final SqlAggFunction sqlAggFunction = DrillCalciteWrapperUtility.extractSqlOperatorFromWrapper(oldCall.getAggregation());
    if (sqlAggFunction instanceof SqlSumAggFunction) {
        // case COUNT(x) when 0 then null else SUM0(x) end
        return reduceSum(oldAggRel, oldCall, newCalls, aggCallMapping);
    }
    if (sqlAggFunction instanceof SqlAvgAggFunction) {
        // causes the loss of the scale
        if (oldCall.getType().getSqlTypeName() == SqlTypeName.DECIMAL) {
            return oldAggRel.getCluster().getRexBuilder().addAggCall(oldCall, oldAggRel.getGroupCount(), newCalls, aggCallMapping, ImmutableList.of(getFieldType(oldAggRel.getInput(), oldCall.getArgList().get(0))));
        }
        final SqlKind subtype = sqlAggFunction.getKind();
        switch(subtype) {
            case AVG:
                // replace original AVG(x) with SUM(x) / COUNT(x)
                return reduceAvg(oldAggRel, oldCall, newCalls, aggCallMapping);
            case STDDEV_POP:
                // / COUNT(x))
                return reduceStddev(oldAggRel, oldCall, true, true, newCalls, aggCallMapping, inputExprs);
            case STDDEV_SAMP:
                // / CASE COUNT(x) WHEN 1 THEN NULL ELSE COUNT(x) - 1 END)
                return reduceStddev(oldAggRel, oldCall, false, true, newCalls, aggCallMapping, inputExprs);
            case VAR_POP:
                // / COUNT(x)
                return reduceStddev(oldAggRel, oldCall, true, false, newCalls, aggCallMapping, inputExprs);
            case VAR_SAMP:
                // / CASE COUNT(x) WHEN 1 THEN NULL ELSE COUNT(x) - 1 END
                return reduceStddev(oldAggRel, oldCall, false, false, newCalls, aggCallMapping, inputExprs);
            default:
                throw Util.unexpected(subtype);
        }
    } else {
        // anything else:  preserve original call
        RexBuilder rexBuilder = oldAggRel.getCluster().getRexBuilder();
        final int nGroups = oldAggRel.getGroupCount();
        List<RelDataType> oldArgTypes = new ArrayList<>();
        List<Integer> ordinals = oldCall.getArgList();
        assert ordinals.size() <= inputExprs.size();
        for (int ordinal : ordinals) {
            oldArgTypes.add(inputExprs.get(ordinal).getType());
        }
        // different RelDataTypes
        if (aggCallMapping.containsKey(oldCall) && !aggCallMapping.get(oldCall).getType().equals(oldCall.getType())) {
            int index = newCalls.size() + nGroups;
            newCalls.add(oldCall);
            return rexBuilder.makeInputRef(oldCall.getType(), index);
        }
        return rexBuilder.addAggCall(oldCall, nGroups, newCalls, aggCallMapping, oldArgTypes);
    }
}
Also used : SqlAvgAggFunction(org.apache.calcite.sql.fun.SqlAvgAggFunction) SqlSumAggFunction(org.apache.calcite.sql.fun.SqlSumAggFunction) ArrayList(java.util.ArrayList) RexBuilder(org.apache.calcite.rex.RexBuilder) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlAggFunction(org.apache.calcite.sql.SqlAggFunction) SqlKind(org.apache.calcite.sql.SqlKind)

Example 14 with SqlKind

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlKind in project drill by apache.

the class IndexableExprMarker method operandsAreIndexable.

public boolean operandsAreIndexable(RexCall call) {
    SqlKind kind = call.getKind();
    boolean kindIsRight = (SqlKind.COMPARISON.contains(kind) || kind == SqlKind.LIKE || kind == SqlKind.SIMILAR);
    if (!kindIsRight) {
        return false;
    }
    int inputReference = 0;
    for (RexNode operand : call.operands) {
        // a.b = a.c, instead of a.b ='hello', so this cannot apply index
        if (containInputRef(operand)) {
            inputReference++;
            if (inputReference >= 2) {
                return false;
            }
        }
    }
    return true;
}
Also used : SqlKind(org.apache.calcite.sql.SqlKind) RexNode(org.apache.calcite.rex.RexNode)

Example 15 with SqlKind

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlKind in project drill by apache.

the class RewriteAsBinaryOperators method visitCall.

@Override
public RexNode visitCall(RexCall call) {
    SqlOperator op = call.getOperator();
    SqlKind kind = op.getKind();
    RelDataType type = call.getType();
    if (kind == SqlKind.OR || kind == SqlKind.AND) {
        if (call.getOperands().size() > 2) {
            List<RexNode> children = new ArrayList<>(call.getOperands());
            RexNode left = children.remove(0).accept(this);
            RexNode right = builder.makeCall(type, op, children).accept(this);
            return builder.makeCall(type, op, ImmutableList.of(left, right));
        }
    }
    return builder.makeCall(type, op, visitChildren(call));
}
Also used : SqlOperator(org.apache.calcite.sql.SqlOperator) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlKind(org.apache.calcite.sql.SqlKind) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

SqlKind (org.apache.calcite.sql.SqlKind)44 RexNode (org.apache.calcite.rex.RexNode)21 ArrayList (java.util.ArrayList)16 SqlOperator (org.apache.calcite.sql.SqlOperator)14 RelDataType (org.apache.calcite.rel.type.RelDataType)11 RexCall (org.apache.calcite.rex.RexCall)10 RexInputRef (org.apache.calcite.rex.RexInputRef)8 List (java.util.List)7 AggregateCall (org.apache.calcite.rel.core.AggregateCall)7 RexBuilder (org.apache.calcite.rex.RexBuilder)7 SqlNode (org.apache.calcite.sql.SqlNode)6 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)6 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)5 SqlAggFunction (org.apache.calcite.sql.SqlAggFunction)5 SqlCall (org.apache.calcite.sql.SqlCall)5 ImmutableList (com.google.common.collect.ImmutableList)4 SqlBasicCall (org.apache.calcite.sql.SqlBasicCall)4 Aggregate (org.apache.calcite.rel.core.Aggregate)3 LogicalAggregate (org.apache.calcite.rel.logical.LogicalAggregate)3 SqlTypeName (org.apache.calcite.sql.type.SqlTypeName)3