@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; }
@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; }
final SqlNumericLiteral one = SqlLiteral.createExactNumeric("1", pos); denominator = new SqlCase(SqlParserPos.ZERO, countCasted, SqlNodeList.of(SqlStdOperatorTable.EQUALS.createCall(pos, countCasted, one)), SqlNodeList.of(getCastedSqlNode(nullLiteral, varType, pos, null)), SqlStdOperatorTable.MINUS.createCall(pos, countCasted, one));
final SqlNumericLiteral one = SqlLiteral.createExactNumeric("1", pos); denominator = new SqlCase(SqlParserPos.ZERO, countCasted, SqlNodeList.of(SqlStdOperatorTable.EQUALS.createCall(pos, countCasted, one)), SqlNodeList.of(getCastedSqlNode(nullLiteral, varType, pos, null)), SqlStdOperatorTable.MINUS.createCall(pos, countCasted, one));