Search in sources :

Example 1 with SqlAggFunction

use of org.apache.calcite.sql.SqlAggFunction in project storm by apache.

the class RelNodeCompiler method aggregate.

private void aggregate(AggregateCall call) {
    SqlAggFunction aggFunction = call.getAggregation();
    String aggregationName = call.getAggregation().getName();
    Type ty = typeFactory.getJavaClass(call.getType());
    if (call.getArgList().size() != 1) {
        if (aggregationName.equals("COUNT")) {
            if (call.getArgList().size() != 0) {
                throw new UnsupportedOperationException("Count with nullable fields");
            }
        }
    }
    if (aggFunction instanceof SqlUserDefinedAggFunction) {
        AggregateFunction aggregateFunction = ((SqlUserDefinedAggFunction) aggFunction).function;
        doAggregate((AggregateFunctionImpl) aggregateFunction, reserveAggVarName(call), ty, call.getArgList());
    } else {
        List<BuiltinAggregateFunctions.TypeClass> typeClasses = BuiltinAggregateFunctions.TABLE.get(aggregationName);
        if (typeClasses == null) {
            throw new UnsupportedOperationException(aggregationName + " Not implemented");
        }
        doAggregate(AggregateFunctionImpl.create(findMatchingClass(aggregationName, typeClasses, ty)), reserveAggVarName(call), ty, call.getArgList());
    }
}
Also used : RelDataType(org.apache.calcite.rel.type.RelDataType) Type(java.lang.reflect.Type) AggregateFunction(org.apache.calcite.schema.AggregateFunction) SqlUserDefinedAggFunction(org.apache.calcite.sql.validate.SqlUserDefinedAggFunction) SqlAggFunction(org.apache.calcite.sql.SqlAggFunction)

Example 2 with SqlAggFunction

use of org.apache.calcite.sql.SqlAggFunction in project lucene-solr by apache.

the class SolrAggregate method toSolrMetric.

private Pair<String, String> toSolrMetric(Implementor implementor, AggregateCall aggCall, List<String> inNames) {
    SqlAggFunction aggregation = aggCall.getAggregation();
    List<Integer> args = aggCall.getArgList();
    switch(args.size()) {
        case 0:
            if (aggregation.equals(SqlStdOperatorTable.COUNT)) {
                return new Pair<>(aggregation.getName(), "*");
            }
        case 1:
            String inName = inNames.get(args.get(0));
            String name = implementor.fieldMappings.getOrDefault(inName, inName);
            if (SUPPORTED_AGGREGATIONS.contains(aggregation)) {
                return new Pair<>(aggregation.getName(), name);
            }
        default:
            throw new AssertionError("Invalid aggregation " + aggregation + " with args " + args + " with names" + inNames);
    }
}
Also used : SqlAggFunction(org.apache.calcite.sql.SqlAggFunction) Pair(org.apache.calcite.util.Pair)

Example 3 with SqlAggFunction

use of org.apache.calcite.sql.SqlAggFunction in project calcite by apache.

the class RelJsonReader method toAggCall.

private AggregateCall toAggCall(Map<String, Object> jsonAggCall) {
    final String aggName = (String) jsonAggCall.get("agg");
    final SqlAggFunction aggregation = relJson.toAggregation(aggName, jsonAggCall);
    final Boolean distinct = (Boolean) jsonAggCall.get("distinct");
    @SuppressWarnings("unchecked") final List<Integer> operands = (List<Integer>) jsonAggCall.get("operands");
    final Integer filterOperand = (Integer) jsonAggCall.get("filter");
    final RelDataType type = relJson.toType(cluster.getTypeFactory(), jsonAggCall.get("type"));
    return AggregateCall.create(aggregation, distinct, false, operands, filterOperand == null ? -1 : filterOperand, type, null);
}
Also used : AbstractList(java.util.AbstractList) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlAggFunction(org.apache.calcite.sql.SqlAggFunction)

Example 4 with SqlAggFunction

use of org.apache.calcite.sql.SqlAggFunction in project calcite by apache.

the class Aggregate method typeMatchesInferred.

/**
 * Returns whether the inferred type of an {@link AggregateCall} matches the
 * type it was given when it was created.
 *
 * @param aggCall Aggregate call
 * @param litmus What to do if an error is detected (types do not match)
 * @return Whether the inferred and declared types match
 */
private boolean typeMatchesInferred(final AggregateCall aggCall, final Litmus litmus) {
    SqlAggFunction aggFunction = aggCall.getAggregation();
    AggCallBinding callBinding = aggCall.createBinding(this);
    RelDataType type = aggFunction.inferReturnType(callBinding);
    RelDataType expectedType = aggCall.type;
    return RelOptUtil.eq("aggCall type", expectedType, "inferred type", type, litmus);
}
Also used : RelDataType(org.apache.calcite.rel.type.RelDataType) SqlAggFunction(org.apache.calcite.sql.SqlAggFunction)

Example 5 with SqlAggFunction

use of org.apache.calcite.sql.SqlAggFunction in project calcite by apache.

the class SqlOperatorBaseTest method testArgumentBounds.

/**
 * Test that calls all operators with all possible argument types, and for
 * each type, with a set of tricky values.
 */
@Test
public void testArgumentBounds() {
    if (!CalciteAssert.ENABLE_SLOW) {
        return;
    }
    final SqlValidatorImpl validator = (SqlValidatorImpl) tester.getValidator();
    final SqlValidatorScope scope = validator.getEmptyScope();
    final RelDataTypeFactory typeFactory = validator.getTypeFactory();
    final Builder builder = new Builder(typeFactory);
    builder.add0(SqlTypeName.BOOLEAN, true, false);
    builder.add0(SqlTypeName.TINYINT, 0, 1, -3, Byte.MAX_VALUE, Byte.MIN_VALUE);
    builder.add0(SqlTypeName.SMALLINT, 0, 1, -4, Short.MAX_VALUE, Short.MIN_VALUE);
    builder.add0(SqlTypeName.INTEGER, 0, 1, -2, Integer.MIN_VALUE, Integer.MAX_VALUE);
    builder.add0(SqlTypeName.BIGINT, 0, 1, -5, Integer.MAX_VALUE, Long.MAX_VALUE, Long.MIN_VALUE);
    builder.add1(SqlTypeName.VARCHAR, 11, "", " ", "hello world");
    builder.add1(SqlTypeName.CHAR, 5, "", "e", "hello");
    builder.add0(SqlTypeName.TIMESTAMP, 0L, DateTimeUtils.MILLIS_PER_DAY);
    for (SqlOperator op : SqlStdOperatorTable.instance().getOperatorList()) {
        switch(op.getKind()) {
            // can't handle the flag argument
            case TRIM:
            case EXISTS:
                continue;
        }
        switch(op.getSyntax()) {
            case SPECIAL:
                continue;
        }
        final SqlOperandTypeChecker typeChecker = op.getOperandTypeChecker();
        if (typeChecker == null) {
            continue;
        }
        final SqlOperandCountRange range = typeChecker.getOperandCountRange();
        for (int n = range.getMin(), max = range.getMax(); n <= max; n++) {
            final List<List<ValueType>> argValues = Collections.nCopies(n, builder.values);
            for (final List<ValueType> args : Linq4j.product(argValues)) {
                SqlNodeList nodeList = new SqlNodeList(SqlParserPos.ZERO);
                int nullCount = 0;
                for (ValueType arg : args) {
                    if (arg.value == null) {
                        ++nullCount;
                    }
                    nodeList.add(arg.node);
                }
                final SqlCall call = op.createCall(nodeList);
                final SqlCallBinding binding = new SqlCallBinding(validator, scope, call);
                if (!typeChecker.checkOperandTypes(binding, false)) {
                    continue;
                }
                final SqlPrettyWriter writer = new SqlPrettyWriter(CalciteSqlDialect.DEFAULT);
                op.unparse(writer, call, 0, 0);
                final String s = writer.toSqlString().toString();
                if (s.startsWith("OVERLAY(") || s.contains(" / 0") || s.matches("MOD\\(.*, 0\\)")) {
                    continue;
                }
                final Strong.Policy policy = Strong.policy(op.kind);
                try {
                    if (nullCount > 0 && policy == Strong.Policy.ANY) {
                        tester.checkNull(s);
                    } else {
                        final String query;
                        if (op instanceof SqlAggFunction) {
                            if (op.requiresOrder()) {
                                query = "SELECT " + s + " OVER () FROM (VALUES (1))";
                            } else {
                                query = "SELECT " + s + " FROM (VALUES (1))";
                            }
                        } else {
                            query = SqlTesterImpl.buildQuery(s);
                        }
                        tester.check(query, SqlTests.ANY_TYPE_CHECKER, SqlTests.ANY_PARAMETER_CHECKER, SqlTests.ANY_RESULT_CHECKER);
                    }
                } catch (Error e) {
                    System.out.println(s + ": " + e.getMessage());
                    throw e;
                } catch (Exception e) {
                    System.out.println("Failed: " + s + ": " + e.getMessage());
                }
            }
        }
    }
}
Also used : SqlValidatorScope(org.apache.calcite.sql.validate.SqlValidatorScope) SqlOperator(org.apache.calcite.sql.SqlOperator) SqlCall(org.apache.calcite.sql.SqlCall) TimestampString(org.apache.calcite.util.TimestampString) SqlString(org.apache.calcite.sql.util.SqlString) SqlAggFunction(org.apache.calcite.sql.SqlAggFunction) Strong(org.apache.calcite.plan.Strong) SQLException(java.sql.SQLException) SqlOperandCountRange(org.apache.calcite.sql.SqlOperandCountRange) SqlValidatorImpl(org.apache.calcite.sql.validate.SqlValidatorImpl) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) SqlCallBinding(org.apache.calcite.sql.SqlCallBinding) SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlOperandTypeChecker(org.apache.calcite.sql.type.SqlOperandTypeChecker) List(java.util.List) ArrayList(java.util.ArrayList) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlLimitsTest(org.apache.calcite.test.SqlLimitsTest) Test(org.junit.Test)

Aggregations

SqlAggFunction (org.apache.calcite.sql.SqlAggFunction)47 RelDataType (org.apache.calcite.rel.type.RelDataType)30 ArrayList (java.util.ArrayList)25 RexNode (org.apache.calcite.rex.RexNode)24 AggregateCall (org.apache.calcite.rel.core.AggregateCall)20 RexBuilder (org.apache.calcite.rex.RexBuilder)15 RelNode (org.apache.calcite.rel.RelNode)13 List (java.util.List)11 Aggregate (org.apache.calcite.rel.core.Aggregate)10 RelBuilder (org.apache.calcite.tools.RelBuilder)10 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)9 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)9 HashMap (java.util.HashMap)8 ImmutableList (com.google.common.collect.ImmutableList)7 SqlOperator (org.apache.calcite.sql.SqlOperator)6 Type (java.lang.reflect.Type)5 RelMetadataQuery (org.apache.calcite.rel.metadata.RelMetadataQuery)5 RexInputRef (org.apache.calcite.rex.RexInputRef)5 SqlKind (org.apache.calcite.sql.SqlKind)5 Mappings (org.apache.calcite.util.mapping.Mappings)5