private SqlNode expandAvg( final SqlNode arg, final RelDataType avgType, final SqlRexContext cx) { final SqlParserPos pos = SqlParserPos.ZERO; final SqlNode sum = SqlStdOperatorTable.SUM.createCall(pos, arg); final RexNode sumRex = cx.convertExpression(sum); final SqlNode sumCast; sumCast = getCastedSqlNode(sum, avgType, pos, sumRex); final SqlNode count = SqlStdOperatorTable.COUNT.createCall(pos, arg); return SqlStdOperatorTable.DIVIDE.createCall( pos, sumCast, count); }
private SqlNode expandAvg( final SqlNode arg, final RelDataType avgType, final SqlRexContext cx) { final SqlParserPos pos = SqlParserPos.ZERO; final SqlNode sum = SqlStdOperatorTable.SUM.createCall(pos, arg); final RexNode sumRex = cx.convertExpression(sum); final SqlNode sumCast; sumCast = getCastedSqlNode(sum, avgType, pos, sumRex); final SqlNode count = SqlStdOperatorTable.COUNT.createCall(pos, arg); return SqlStdOperatorTable.DIVIDE.createCall( pos, sumCast, count); }
private SqlNode expandAvg( final SqlNode arg) { final SqlParserPos pos = SqlParserPos.ZERO; final SqlNode sum = DrillCalciteSqlAggFunctionWrapper.SUM.createCall(pos, arg); final SqlNode count = SqlStdOperatorTable.COUNT.createCall(pos, arg); final SqlNode sumAsDouble = CastHighOp.createCall(pos, sum); return SqlStdOperatorTable.DIVIDE.createCall( pos, sumAsDouble, count); }
private SqlNode expandRegrSzz( final SqlNode arg1, final SqlNode arg2, final RelDataType avgType, final SqlRexContext cx, boolean variance) { final SqlParserPos pos = SqlParserPos.ZERO; final SqlNode count = SqlStdOperatorTable.REGR_COUNT.createCall(pos, arg1, arg2); final SqlNode varPop = expandCovariance(arg1, variance ? arg1 : arg2, arg2, avgType, cx, true); final RexNode varPopRex = cx.convertExpression(varPop); final SqlNode varPopCast; varPopCast = getCastedSqlNode(varPop, avgType, pos, varPopRex); return SqlStdOperatorTable.MULTIPLY.createCall(pos, varPopCast, count); }
private SqlNode expandRegrSzz( final SqlNode arg1, final SqlNode arg2, final RelDataType avgType, final SqlRexContext cx, boolean variance) { final SqlParserPos pos = SqlParserPos.ZERO; final SqlNode count = SqlStdOperatorTable.REGR_COUNT.createCall(pos, arg1, arg2); final SqlNode varPop = expandCovariance(arg1, variance ? arg1 : arg2, arg2, avgType, cx, true); final RexNode varPopRex = cx.convertExpression(varPop); final SqlNode varPopCast; varPopCast = getCastedSqlNode(varPop, avgType, pos, varPopRex); return SqlStdOperatorTable.MULTIPLY.createCall(pos, varPopCast, count); }
private SqlCall createOverCall(SqlAggFunction op, List<SqlNode> operands, SqlWindow window) { if (op instanceof SqlSumEmptyIsZeroAggFunction) { // Rewrite "SUM0(x) OVER w" to "COALESCE(SUM(x) OVER w, 0)" final SqlCall node = createOverCall(SqlStdOperatorTable.SUM, operands, window); return SqlStdOperatorTable.COALESCE.createCall(POS, node, SqlLiteral.createExactNumeric("0", POS)); } final SqlCall aggFunctionCall = op.createCall(POS, operands); return SqlStdOperatorTable.OVER.createCall(POS, aggFunctionCall, window); }
final SqlNode count; if (dependent == null) { sumArgSquared = SqlStdOperatorTable.SUM.createCall(pos, argSquared); sum0 = SqlStdOperatorTable.SUM.createCall(pos, arg0, arg1); sum1 = SqlStdOperatorTable.SUM.createCall(pos, arg1, arg0); count = SqlStdOperatorTable.REGR_COUNT.createCall(pos, arg0, arg1); } else { sumArgSquared = SqlStdOperatorTable.SUM.createCall(pos, argSquared, dependent); sum0 = SqlStdOperatorTable.SUM.createCall( pos, arg0, Objects.equals(dependent, arg0Input) ? arg1 : dependent); sum1 = SqlStdOperatorTable.SUM.createCall( pos, arg1, Objects.equals(dependent, arg1Input) ? arg0 : dependent); count = SqlStdOperatorTable.REGR_COUNT.createCall( pos, arg0, Objects.equals(dependent, arg0Input) ? arg1 : dependent);
final SqlNode count; if (dependent == null) { sumArgSquared = SqlStdOperatorTable.SUM.createCall(pos, argSquared); sum0 = SqlStdOperatorTable.SUM.createCall(pos, arg0, arg1); sum1 = SqlStdOperatorTable.SUM.createCall(pos, arg1, arg0); count = SqlStdOperatorTable.REGR_COUNT.createCall(pos, arg0, arg1); } else { sumArgSquared = SqlStdOperatorTable.SUM.createCall(pos, argSquared, dependent); sum0 = SqlStdOperatorTable.SUM.createCall( pos, arg0, Objects.equals(dependent, arg0Input) ? arg1 : dependent); sum1 = SqlStdOperatorTable.SUM.createCall( pos, arg1, Objects.equals(dependent, arg1Input) ? arg0 : dependent); count = SqlStdOperatorTable.REGR_COUNT.createCall( pos, arg0, Objects.equals(dependent, arg0Input) ? arg1 : dependent);
@Override public SqlNode rewriteSingleValueExpr(SqlNode aggCall) { final SqlNode operand = ((SqlBasicCall) aggCall).operand(0); final SqlLiteral nullLiteral = SqlLiteral.createNull(SqlParserPos.ZERO); final SqlNode unionOperand = SqlStdOperatorTable.VALUES.createCall(SqlParserPos.ZERO, SqlLiteral.createApproxNumeric("0", SqlParserPos.ZERO)); // For hsqldb, generate // CASE COUNT(*) // WHEN 0 THEN NULL // WHEN 1 THEN MIN(<result>) // ELSE (VALUES 1 UNION ALL VALUES 1) // END final SqlNode caseExpr = new SqlCase(SqlParserPos.ZERO, SqlStdOperatorTable.COUNT.createCall(SqlParserPos.ZERO, operand), SqlNodeList.of( SqlLiteral.createExactNumeric("0", SqlParserPos.ZERO), SqlLiteral.createExactNumeric("1", SqlParserPos.ZERO) ), SqlNodeList.of( nullLiteral, SqlStdOperatorTable.MIN.createCall(SqlParserPos.ZERO, operand) ), SqlStdOperatorTable.SCALAR_QUERY.createCall(SqlParserPos.ZERO, SqlStdOperatorTable.UNION_ALL .createCall(SqlParserPos.ZERO, unionOperand, unionOperand))); LOGGER.debug("SINGLE_VALUE rewritten into [{}]", caseExpr); return caseExpr; }
@Override public SqlNode rewriteSingleValueExpr(SqlNode aggCall) { final SqlNode operand = ((SqlBasicCall) aggCall).operand(0); final SqlLiteral nullLiteral = SqlLiteral.createNull(SqlParserPos.ZERO); final SqlNode unionOperand = SqlStdOperatorTable.VALUES.createCall(SqlParserPos.ZERO, SqlLiteral.createApproxNumeric("0", SqlParserPos.ZERO)); // For hsqldb, generate // CASE COUNT(*) // WHEN 0 THEN NULL // WHEN 1 THEN MIN(<result>) // ELSE (VALUES 1 UNION ALL VALUES 1) // END final SqlNode caseExpr = new SqlCase(SqlParserPos.ZERO, SqlStdOperatorTable.COUNT.createCall(SqlParserPos.ZERO, operand), SqlNodeList.of( SqlLiteral.createExactNumeric("0", SqlParserPos.ZERO), SqlLiteral.createExactNumeric("1", SqlParserPos.ZERO)), SqlNodeList.of( nullLiteral, SqlStdOperatorTable.MIN.createCall(SqlParserPos.ZERO, operand)), SqlStdOperatorTable.SCALAR_QUERY.createCall(SqlParserPos.ZERO, SqlStdOperatorTable.UNION_ALL .createCall(SqlParserPos.ZERO, unionOperand, unionOperand))); LOGGER.debug("SINGLE_VALUE rewritten into [{}]", caseExpr); return caseExpr; }
rexOver.getAggOperator().createCall(POS, nodeList);
@Override public SqlNode rewriteSingleValueExpr(SqlNode aggCall) { final SqlNode operand = ((SqlBasicCall) aggCall).operand(0); final SqlLiteral nullLiteral = SqlLiteral.createNull(SqlParserPos.ZERO); final SqlNode unionOperand = new SqlSelect(SqlParserPos.ZERO, SqlNodeList.EMPTY, SqlNodeList.of(nullLiteral), null, null, null, null, SqlNodeList.EMPTY, null, null, null); // For MySQL, generate // CASE COUNT(*) // WHEN 0 THEN NULL // WHEN 1 THEN <result> // ELSE (SELECT NULL UNION ALL SELECT NULL) // END final SqlNode caseExpr = new SqlCase(SqlParserPos.ZERO, SqlStdOperatorTable.COUNT.createCall(SqlParserPos.ZERO, operand), SqlNodeList.of( SqlLiteral.createExactNumeric("0", SqlParserPos.ZERO), SqlLiteral.createExactNumeric("1", SqlParserPos.ZERO)), SqlNodeList.of( nullLiteral, operand), SqlStdOperatorTable.SCALAR_QUERY.createCall(SqlParserPos.ZERO, SqlStdOperatorTable.UNION_ALL .createCall(SqlParserPos.ZERO, unionOperand, unionOperand))); LOGGER.debug("SINGLE_VALUE rewritten into [{}]", caseExpr); return caseExpr; }
@Override public SqlNode rewriteSingleValueExpr(SqlNode aggCall) { final SqlNode operand = ((SqlBasicCall) aggCall).operand(0); final SqlLiteral nullLiteral = SqlLiteral.createNull(SqlParserPos.ZERO); final SqlNode unionOperand = new SqlSelect(SqlParserPos.ZERO, SqlNodeList.EMPTY, SqlNodeList.of(nullLiteral), null, null, null, null, SqlNodeList.EMPTY, null, null, null); // For MySQL, generate // CASE COUNT(*) // WHEN 0 THEN NULL // WHEN 1 THEN <result> // ELSE (SELECT NULL UNION ALL SELECT NULL) // END final SqlNode caseExpr = new SqlCase(SqlParserPos.ZERO, SqlStdOperatorTable.COUNT.createCall(SqlParserPos.ZERO, operand), SqlNodeList.of( SqlLiteral.createExactNumeric("0", SqlParserPos.ZERO), SqlLiteral.createExactNumeric("1", SqlParserPos.ZERO) ), SqlNodeList.of( nullLiteral, operand ), SqlStdOperatorTable.SCALAR_QUERY.createCall(SqlParserPos.ZERO, SqlStdOperatorTable.UNION_ALL .createCall(SqlParserPos.ZERO, unionOperand, unionOperand))); LOGGER.debug("SINGLE_VALUE rewritten into [{}]", caseExpr); return caseExpr; }
/** Converts a call to an aggregate function to an expression. */ public SqlNode toSql(AggregateCall aggCall) { final SqlOperator op = aggCall.getAggregation(); final List<SqlNode> operandList = Expressions.list(); for (int arg : aggCall.getArgList()) { operandList.add(field(arg)); } final SqlLiteral qualifier = aggCall.isDistinct() ? SqlSelectKeyword.DISTINCT.symbol(POS) : null; final SqlNode[] operands = operandList.toArray(new SqlNode[0]); List<SqlNode> orderByList = Expressions.list(); for (RelFieldCollation field : aggCall.collation.getFieldCollations()) { addOrderItem(orderByList, field); } SqlNodeList orderList = new SqlNodeList(orderByList, POS); if (op instanceof SqlSumEmptyIsZeroAggFunction) { final SqlNode node = withOrder( SqlStdOperatorTable.SUM.createCall(qualifier, POS, operands), orderList); return SqlStdOperatorTable.COALESCE.createCall(POS, node, SqlLiteral.createExactNumeric("0", POS)); } else { return withOrder(op.createCall(qualifier, POS, operands), orderList); } }
final SqlNode elseExpr; final SqlNode countCall = SqlStdOperatorTable.COUNT.createCall(POS, operand); break; default: wrappedOperand = SqlStdOperatorTable.MIN.createCall(POS, operand); unionOperand = SqlStdOperatorTable.VALUES.createCall(POS, SqlLiteral.createApproxNumeric("0", POS));
final SqlNode argSquaredCasted = getCastedSqlNode(argSquared, varType, pos, cx.convertExpression(argSquared)); final SqlNode sumArgSquared = SqlStdOperatorTable.SUM.createCall(pos, argSquaredCasted); final SqlNode sumArgSquaredCasted = getCastedSqlNode(sumArgSquared, varType, pos, cx.convertExpression(sumArgSquared)); final SqlNode sum = SqlStdOperatorTable.SUM.createCall(pos, arg); final SqlNode sumCasted = getCastedSqlNode(sum, varType, pos, cx.convertExpression(sum)); final SqlNode sumSquared = final SqlNode sumSquaredCasted = getCastedSqlNode(sumSquared, varType, pos, cx.convertExpression(sumSquared)); final SqlNode count = SqlStdOperatorTable.COUNT.createCall(pos, arg); final SqlNode countCasted = getCastedSqlNode(count, varType, pos, cx.convertExpression(count));
final SqlNode argSquaredCasted = getCastedSqlNode(argSquared, varType, pos, cx.convertExpression(argSquared)); final SqlNode sumArgSquared = SqlStdOperatorTable.SUM.createCall(pos, argSquaredCasted); final SqlNode sumArgSquaredCasted = getCastedSqlNode(sumArgSquared, varType, pos, cx.convertExpression(sumArgSquared)); final SqlNode sum = SqlStdOperatorTable.SUM.createCall(pos, arg); final SqlNode sumCasted = getCastedSqlNode(sum, varType, pos, cx.convertExpression(sum)); final SqlNode sumSquared = final SqlNode sumSquaredCasted = getCastedSqlNode(sumSquared, varType, pos, cx.convertExpression(sumSquared)); final SqlNode count = SqlStdOperatorTable.COUNT.createCall(pos, arg); final SqlNode countCasted = getCastedSqlNode(count, varType, pos, cx.convertExpression(count));
SqlStdOperatorTable.MULTIPLY.createCall(pos, sum, sum); final SqlNode count = SqlStdOperatorTable.COUNT.createCall(pos, castHighArg); final SqlNode avgSumSquared = SqlStdOperatorTable.DIVIDE.createCall(