Java Code Examples for org.apache.calcite.sql.SqlNodeList#of()

The following examples show how to use org.apache.calcite.sql.SqlNodeList#of() . 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: HsqldbSqlDialect.java    From Bats with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 2
Source File: MysqlSqlDialect.java    From Bats with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 3
Source File: MssqlSqlDialect.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc}
 *
 * <p>MSSQL does not support NULLS FIRST, so we emulate using CASE
 * expressions. For example,
 *
 * <blockquote>{@code ORDER BY x NULLS FIRST}</blockquote>
 *
 * <p>becomes
 *
 * <blockquote>
 *   {@code ORDER BY CASE WHEN x IS NULL THEN 0 ELSE 1 END, x}
 * </blockquote>
 */
@Override public SqlNode emulateNullDirection(SqlNode node,
    boolean nullsFirst, boolean desc) {
  // Default ordering preserved
  if (nullCollation.isDefaultOrder(nullsFirst, desc)) {
    return null;
  }

  // Grouping node should preserve grouping, no emulation needed
  if (node.getKind() == SqlKind.GROUPING) {
    return node;
  }

  // Emulate nulls first/last with case ordering
  final SqlParserPos pos = SqlParserPos.ZERO;
  final SqlNodeList whenList =
      SqlNodeList.of(SqlStdOperatorTable.IS_NULL.createCall(pos, node));

  final SqlNode oneLiteral = SqlLiteral.createExactNumeric("1", pos);
  final SqlNode zeroLiteral = SqlLiteral.createExactNumeric("0", pos);

  if (nullsFirst) {
    // IS NULL THEN 0 ELSE 1 END
    return SqlStdOperatorTable.CASE.createCall(null, pos,
        null, whenList, SqlNodeList.of(zeroLiteral), oneLiteral);
  } else {
    // IS NULL THEN 1 ELSE 0 END
    return SqlStdOperatorTable.CASE.createCall(null, pos,
        null, whenList, SqlNodeList.of(oneLiteral), zeroLiteral);
  }
}
 
Example 4
Source File: HsqldbSqlDialect.java    From calcite with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 5
Source File: MysqlSqlDialect.java    From calcite with Apache License 2.0 5 votes vote down vote up
@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, SqlNodeList.EMPTY);
  // 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;
}