Beispiel #1
0
  /** Build smap AVG -> SUM/COUNT; assumes that select list and having clause have been analyzed. */
  private Expr.SubstitutionMap createAvgSMap(
      ArrayList<FunctionCallExpr> aggExprs, Analyzer analyzer)
      throws AnalysisException, AuthorizationException {
    Expr.SubstitutionMap result = new Expr.SubstitutionMap();
    for (FunctionCallExpr aggExpr : aggExprs) {
      if (!aggExpr.getFnName().getFunction().equals("avg")) continue;
      // Transform avg(TIMESTAMP) to cast(avg(cast(TIMESTAMP as DOUBLE)) as TIMESTAMP)
      CastExpr inCastExpr = null;
      if (aggExpr.getChild(0).type_.getPrimitiveType() == PrimitiveType.TIMESTAMP) {
        inCastExpr = new CastExpr(ColumnType.DOUBLE, aggExpr.getChild(0).clone(null), false);
      }

      List<Expr> sumInputExprs =
          Lists.newArrayList(
              aggExpr.getChild(0).type_.getPrimitiveType() == PrimitiveType.TIMESTAMP
                  ? inCastExpr
                  : aggExpr.getChild(0).clone(null));
      List<Expr> countInputExpr = Lists.newArrayList(aggExpr.getChild(0).clone(null));

      FunctionCallExpr sumExpr =
          new FunctionCallExpr("sum", new FunctionParams(aggExpr.isDistinct(), sumInputExprs));
      FunctionCallExpr countExpr =
          new FunctionCallExpr("count", new FunctionParams(aggExpr.isDistinct(), countInputExpr));
      ArithmeticExpr divExpr =
          new ArithmeticExpr(ArithmeticExpr.Operator.DIVIDE, sumExpr, countExpr);

      if (aggExpr.getChild(0).type_.getPrimitiveType() == PrimitiveType.TIMESTAMP) {
        CastExpr outCastExpr = new CastExpr(ColumnType.TIMESTAMP, divExpr, false);
        outCastExpr.analyze(analyzer);
        result.addMapping(aggExpr, outCastExpr);
      } else {
        divExpr.analyze(analyzer);
        result.addMapping(aggExpr, divExpr);
      }
    }
    LOG.debug("avg smap: " + result.debugString());
    return result;
  }
Beispiel #2
0
  /*
   * Create a map from COUNT([ALL]) -> zeroifnull(COUNT([ALL])) if
   * i) There is no GROUP-BY, and
   * ii) There are other distinct aggregates to be evaluated.
   * This transformation is necessary for COUNT to correctly return 0 for empty
   * input relations.
   */
  private Expr.SubstitutionMap createCountAllMap(List<FunctionCallExpr> aggExprs, Analyzer analyzer)
      throws AuthorizationException, AnalysisException {
    Expr.SubstitutionMap scalarCountAllMap = new Expr.SubstitutionMap();

    if (groupingExprs_ != null && !groupingExprs_.isEmpty()) {
      // There are grouping expressions, so no substitution needs to be done.
      return scalarCountAllMap;
    }

    com.google.common.base.Predicate<FunctionCallExpr> isNotDistinctPred =
        new com.google.common.base.Predicate<FunctionCallExpr>() {
          public boolean apply(FunctionCallExpr expr) {
            return !expr.isDistinct();
          }
        };
    if (Iterables.all(aggExprs, isNotDistinctPred)) {
      // Only [ALL] aggs, so no substitution needs to be done.
      return scalarCountAllMap;
    }

    com.google.common.base.Predicate<FunctionCallExpr> isCountPred =
        new com.google.common.base.Predicate<FunctionCallExpr>() {
          public boolean apply(FunctionCallExpr expr) {
            return expr.getFnName().getFunction().equals("count");
          }
        };

    Iterable<FunctionCallExpr> countAllAggs =
        Iterables.filter(aggExprs, Predicates.and(isCountPred, isNotDistinctPred));
    for (FunctionCallExpr countAllAgg : countAllAggs) {
      // Replace COUNT(ALL) with zeroifnull(COUNT(ALL))
      ArrayList<Expr> zeroIfNullParam = Lists.newArrayList(countAllAgg.clone(null));
      FunctionCallExpr zeroIfNull = new FunctionCallExpr("zeroifnull", zeroIfNullParam);
      zeroIfNull.analyze(analyzer);
      scalarCountAllMap.addMapping(countAllAgg, zeroIfNull);
    }

    return scalarCountAllMap;
  }