Java Code Examples for org.apache.calcite.sql.SqlAggFunction#getKind()

The following examples show how to use org.apache.calcite.sql.SqlAggFunction#getKind() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DrillCalciteSqlAggFunctionWrapper.java    From Bats with Apache License 2.0 5 votes vote down vote up
private DrillCalciteSqlAggFunctionWrapper(
    SqlAggFunction sqlAggFunction,
    SqlReturnTypeInference sqlReturnTypeInference) {
  super(sqlAggFunction.getName(),
      sqlAggFunction.getSqlIdentifier(),
      sqlAggFunction.getKind(),
      sqlReturnTypeInference,
      sqlAggFunction.getOperandTypeInference(),
      Checker.ANY_CHECKER,
      sqlAggFunction.getFunctionType(),
      sqlAggFunction.requiresOrder(),
      sqlAggFunction.requiresOver());
  this.operator = sqlAggFunction;
}
 
Example 2
Source File: HiveSqlOperatorTable.java    From marble with Apache License 2.0 4 votes vote down vote up
private void registerUDAF(Reflections reflections) {
  Set<Class> udafClasses = Sets.union(
      reflections.getSubTypesOf(GenericUDAFResolver2.class),
      reflections.getSubTypesOf(UDAF.class));
  for (Class clazz : udafClasses) {
    boolean isWindowFunc = false;
    Description desc = (Description) clazz.getAnnotation(Description.class);
    WindowFunctionDescription windowFunctionDescription = null;
    if (desc == null) {
      windowFunctionDescription =
          (WindowFunctionDescription) clazz
              .getAnnotation(WindowFunctionDescription.class);
      if (windowFunctionDescription != null
          && windowFunctionDescription.description() != null) {
        desc = windowFunctionDescription.description();
        isWindowFunc = true;
      }
    }
    //ignore the operators that need to be excluded
    if (desc == null || isWindowFunc || EXCLUDED_HIVE_UDAF_LIST.contains(
        clazz)) {
      continue;
    }
    String[] names = desc.name().split(",");
    for (int i = 0; i < names.length; i++) {
      String upName = names[i].toUpperCase();
      methodsUDAF.put(upName, clazz);
      SqlAggFunction aggOperatorInSqlStdOperatorTable =
          (SqlAggFunction) getOperatorInSqlStdOperatorTable(
              upName, SqlSyntax.FUNCTION, true);
      HiveSqlAggFunction sqlAggFunction;
      if (aggOperatorInSqlStdOperatorTable == null) {
        sqlAggFunction = new HiveSqlAggFunction(upName, false,
            false, HiveSqlUDAFReturnTypeInference.INSTANCE);

      } else {
        sqlAggFunction = new HiveSqlAggFunction(upName,
            aggOperatorInSqlStdOperatorTable.getNameAsId(),
            aggOperatorInSqlStdOperatorTable.getKind(),
            aggOperatorInSqlStdOperatorTable.getFunctionType(),
            false, false, HiveSqlUDAFReturnTypeInference.INSTANCE);
      }
      register(sqlAggFunction);
    }

  }
}
 
Example 3
Source File: ElasticsearchAggregate.java    From dk-fitting with Apache License 2.0 4 votes vote down vote up
public void implement(Implementor implementor) {
        implementor.visitChild(0, getInput());
        ElasticsearchTable esTable = implementor.getElasticsearchTable();
        List<AggregateCall> aggCallList = getAggCallList();
        List<RelDataTypeField> fieldList = esTable.getRowType().getFieldList();
        final List<String> inNames = ElasticsearchRules.elasticsearchFieldNames(getInput().getRowType());
        final List<String> outNames = ElasticsearchRules.elasticsearchFieldNames(getRowType());
        esTable.setOutNames(outNames);

        //        final List<String> inNames = ElasticsearchRules.elasticsearchFieldNames(getInput().getRowType());
        TermsAggregationBuilder groupAggregationBuilder = null;
        AggregationBuilder groupAggregationBuilder2 = null;     //防止出现group但没有sort出错的情况
        for(int i = 0 ;i<groupSet.cardinality();i++) {
            final String inName = inNames.get(groupSet.nth(i));

            groupAggregationBuilder = AggregationBuilders.terms(inName.toLowerCase()).field(transFieldName(ElasticsearchRules.maybeQuote(inName).toLowerCase(),fieldList));
            groupAggregationBuilder2 = AggregationBuilders.terms(inName).field(transFieldName(ElasticsearchRules.maybeQuote(inName),fieldList));
            esTable.setIsGroup(true);
        }
        List<String> out = new ArrayList<String>();   //聚合函数的名称
        for(AggregateCall call : aggCallList)
        {
            SqlAggFunction function = call.getAggregation();
            List<Integer> argList = call.getArgList();
            String functionName = function.getName();
            out.add(functionName);
            switch (function.getKind())
            {
                case MIN:
                    RelDataTypeField typeField = fieldList.get(argList.get(0));
                    if (groupAggregationBuilder == null) {
                        //min值 与 原字段值 的类型是一样的
                        esTable.addAggregationBuilder(AggregationBuilders.min(functionName).field(typeField.getName().toLowerCase()),
                                ((RelDataTypeFactoryImpl.JavaType) typeField.getType()).getJavaClass());
                    }else {
                        MinAggregationBuilder minAgg = AggregationBuilders.min(functionName).field(typeField.getName().toLowerCase());
//                        esTable.addAggregationBuilder(groupAggregationBuilder.subAggregation(minAgg), call.getType().getClass());
                        groupAggregationBuilder.subAggregation(minAgg);
                        groupAggregationBuilder2.subAggregation(minAgg);
                    }
                    break;
                case MAX:
                    //max值 与 原字段值 的类型是一样的
                    RelDataTypeField typeField1 = fieldList.get(argList.get(0));
                    if(groupAggregationBuilder == null) {
                        esTable.addAggregationBuilder(AggregationBuilders.max(functionName).field(typeField1.getName().toLowerCase()),
                                ((RelDataTypeFactoryImpl.JavaType) typeField1.getType()).getJavaClass());
                    }else {
                        groupAggregationBuilder.subAggregation(AggregationBuilders.max(functionName).field(typeField1.getName().toLowerCase()));
                    }
                    break;
                case COUNT:
                    if(groupAggregationBuilder == null) {
                        if (argList == null || argList.size() == 0)//count(*)
                            esTable.addAggregationBuilder(AggregationBuilders.count(functionName), Long.class);
                        else
                            esTable.addAggregationBuilder(AggregationBuilders.count(functionName).field(transFieldName(fieldList.get(argList.get(0)).getName().toLowerCase(), fieldList)), Long.class);
                    }else {
                        groupAggregationBuilder.subAggregation(AggregationBuilders.count(functionName).field(transFieldName(fieldList.get(argList.get(0)).getName().toLowerCase(), fieldList)));
                        groupAggregationBuilder2.subAggregation(AggregationBuilders.count(functionName).field(transFieldName(fieldList.get(argList.get(0)).getName().toLowerCase(), fieldList)));
                    }
                    break;
                case SUM:
                    if(groupAggregationBuilder == null) {
                        esTable.addAggregationBuilder(AggregationBuilders.sum(functionName).field(fieldList.get(argList.get(0)).getName().toLowerCase()), Double.class);
                    }else {
                        String s = fieldList.get(argList.get(0)).getName().toLowerCase();
                        SumAggregationBuilder sumAgg = AggregationBuilders.sum(functionName).field(fieldList.get(argList.get(0)).getName().toLowerCase());
                        groupAggregationBuilder.subAggregation(sumAgg);
                        groupAggregationBuilder2.subAggregation(sumAgg);
                    }
                    break;
                case AVG:
                    if(groupAggregationBuilder == null) {
                        esTable.addAggregationBuilder(AggregationBuilders.avg(functionName).field(fieldList.get(argList.get(0)).getName().toLowerCase()), Double.class);
                    }else {
                        AvgAggregationBuilder avgAgg = AggregationBuilders.avg(functionName).field(fieldList.get(argList.get(0)).getName().toLowerCase());
                        groupAggregationBuilder.subAggregation(avgAgg);
                        groupAggregationBuilder2.subAggregation(avgAgg);
                    }
                    break;
                default:break;
            }
        }
        if (groupAggregationBuilder != null) {
            esTable.addAggregationBuilder((AbstractAggregationBuilder) groupAggregationBuilder,String.class);
            esTable.addAggregationBuilderList2(groupAggregationBuilder);
        }
        esTable.setOut(out);
    }
 
Example 4
Source File: ElasticsearchAggregate.java    From dk-fitting with Apache License 2.0 4 votes vote down vote up
public void implement(Implementor implementor) {
        implementor.visitChild(0, getInput());
        ElasticsearchTable esTable = implementor.getElasticsearchTable();
        List<AggregateCall> aggCallList = getAggCallList();
        List<RelDataTypeField> fieldList = esTable.getRowType().getFieldList();
        final List<String> inNames = ElasticsearchRules.elasticsearchFieldNames(getInput().getRowType());
        final List<String> outNames = ElasticsearchRules.elasticsearchFieldNames(getRowType());
        esTable.setOutNames(outNames);

        //        final List<String> inNames = ElasticsearchRules.elasticsearchFieldNames(getInput().getRowType());
        TermsAggregationBuilder groupAggregationBuilder = null;
        AggregationBuilder groupAggregationBuilder2 = null;     //防止出现group但没有sort出错的情况
        for(int i = 0 ;i<groupSet.cardinality();i++) {
            final String inName = inNames.get(groupSet.nth(i));

            groupAggregationBuilder = AggregationBuilders.terms(inName.toLowerCase()).field(transFieldName(ElasticsearchRules.maybeQuote(inName).toLowerCase(),fieldList));
            groupAggregationBuilder2 = AggregationBuilders.terms(inName).field(transFieldName(ElasticsearchRules.maybeQuote(inName),fieldList));
            esTable.setIsGroup(true);
        }
        List<String> out = new ArrayList<String>();   //聚合函数的名称
        for(AggregateCall call : aggCallList)
        {
            SqlAggFunction function = call.getAggregation();
            List<Integer> argList = call.getArgList();
            String functionName = function.getName();
            out.add(functionName);
            switch (function.getKind())
            {
                case MIN:
                    RelDataTypeField typeField = fieldList.get(argList.get(0));
                    if (groupAggregationBuilder == null) {
                        //min值 与 原字段值 的类型是一样的
                        esTable.addAggregationBuilder(AggregationBuilders.min(functionName).field(typeField.getName().toLowerCase()),
                                ((RelDataTypeFactoryImpl.JavaType) typeField.getType()).getJavaClass());
                    }else {
                        MinAggregationBuilder minAgg = AggregationBuilders.min(functionName).field(typeField.getName().toLowerCase());
//                        esTable.addAggregationBuilder(groupAggregationBuilder.subAggregation(minAgg), call.getType().getClass());
                        groupAggregationBuilder.subAggregation(minAgg);
                        groupAggregationBuilder2.subAggregation(minAgg);
                    }
                    break;
                case MAX:
                    //max值 与 原字段值 的类型是一样的
                    RelDataTypeField typeField1 = fieldList.get(argList.get(0));
                    if(groupAggregationBuilder == null) {
                        esTable.addAggregationBuilder(AggregationBuilders.max(functionName).field(typeField1.getName().toLowerCase()),
                                ((RelDataTypeFactoryImpl.JavaType) typeField1.getType()).getJavaClass());
                    }else {
                        groupAggregationBuilder.subAggregation(AggregationBuilders.max(functionName).field(typeField1.getName().toLowerCase()));
                    }
                    break;
                case COUNT:
                    if(groupAggregationBuilder == null) {
                        if (argList == null || argList.size() == 0)//count(*)
                            esTable.addAggregationBuilder(AggregationBuilders.count(functionName), Long.class);
                        else
                            esTable.addAggregationBuilder(AggregationBuilders.count(functionName).field(transFieldName(fieldList.get(argList.get(0)).getName().toLowerCase(), fieldList)), Long.class);
                    }else {
                        groupAggregationBuilder.subAggregation(AggregationBuilders.count(functionName).field(transFieldName(fieldList.get(argList.get(0)).getName().toLowerCase(), fieldList)));
                        groupAggregationBuilder2.subAggregation(AggregationBuilders.count(functionName).field(transFieldName(fieldList.get(argList.get(0)).getName().toLowerCase(), fieldList)));
                    }
                    break;
                case SUM:
                    if(groupAggregationBuilder == null) {
                        esTable.addAggregationBuilder(AggregationBuilders.sum(functionName).field(fieldList.get(argList.get(0)).getName().toLowerCase()), Double.class);
                    }else {
                        String s = fieldList.get(argList.get(0)).getName().toLowerCase();
                        SumAggregationBuilder sumAgg = AggregationBuilders.sum(functionName).field(fieldList.get(argList.get(0)).getName().toLowerCase());
                        groupAggregationBuilder.subAggregation(sumAgg);
                        groupAggregationBuilder2.subAggregation(sumAgg);
                    }
                    break;
                case AVG:
                    if(groupAggregationBuilder == null) {
                        esTable.addAggregationBuilder(AggregationBuilders.avg(functionName).field(fieldList.get(argList.get(0)).getName().toLowerCase()), Double.class);
                    }else {
                        AvgAggregationBuilder avgAgg = AggregationBuilders.avg(functionName).field(fieldList.get(argList.get(0)).getName().toLowerCase());
                        groupAggregationBuilder.subAggregation(avgAgg);
                        groupAggregationBuilder2.subAggregation(avgAgg);
                    }
                    break;
                default:break;
            }
        }
        if (groupAggregationBuilder != null) {
            esTable.addAggregationBuilder((AbstractAggregationBuilder) groupAggregationBuilder,String.class);
            esTable.addAggregationBuilderList2(groupAggregationBuilder);
        }
        esTable.setOut(out);
    }
 
Example 5
Source File: AggregateRemoveRule.java    From calcite with Apache License 2.0 4 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final Aggregate aggregate = call.rel(0);
  final RelNode input = aggregate.getInput();
  final RelMetadataQuery mq = call.getMetadataQuery();
  if (!SqlFunctions.isTrue(mq.areColumnsUnique(input, aggregate.getGroupSet()))) {
    return;
  }

  final RelBuilder relBuilder = call.builder();
  final RexBuilder rexBuilder = relBuilder.getRexBuilder();
  final List<RexNode> projects = new ArrayList<>();
  for (AggregateCall aggCall : aggregate.getAggCallList()) {
    final SqlAggFunction aggregation = aggCall.getAggregation();
    if (aggregation.getKind() == SqlKind.SUM0) {
      // Bail out for SUM0 to avoid potential infinite rule matching,
      // because it may be generated by transforming SUM aggregate
      // function to SUM0 and COUNT.
      return;
    }
    final SqlSplittableAggFunction splitter =
        Objects.requireNonNull(
            aggregation.unwrap(SqlSplittableAggFunction.class));
    final RexNode singleton = splitter.singleton(
        rexBuilder, input.getRowType(), aggCall);
    projects.add(singleton);
  }

  final RelNode newInput = convert(input, aggregate.getTraitSet().simplify());
  relBuilder.push(newInput);
  if (!projects.isEmpty()) {
    projects.addAll(0, relBuilder.fields(aggregate.getGroupSet()));
    relBuilder.project(projects);
  } else if (newInput.getRowType().getFieldCount()
      > aggregate.getRowType().getFieldCount()) {
    // If aggregate was projecting a subset of columns, and there were no
    // aggregate functions, add a project for the same effect.
    relBuilder.project(relBuilder.fields(aggregate.getGroupSet()));
  }
  call.getPlanner().prune(aggregate);
  call.transformTo(relBuilder.build());
}