Java Code Examples for org.apache.calcite.sql.SqlLiteral#createNull()

The following examples show how to use org.apache.calcite.sql.SqlLiteral#createNull() . 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: SqlCase.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a call to the switched form of the case operator, viz:
 *
 * <blockquote><code>CASE value<br>
 * WHEN whenList[0] THEN thenList[0]<br>
 * WHEN whenList[1] THEN thenList[1]<br>
 * ...<br>
 * ELSE elseClause<br>
 * END</code></blockquote>
 */
public static SqlCase createSwitched(SqlParserPos pos, SqlNode value,
    SqlNodeList whenList, SqlNodeList thenList, SqlNode elseClause) {
  if (null != value) {
    List<SqlNode> list = whenList.getList();
    for (int i = 0; i < list.size(); i++) {
      SqlNode e = list.get(i);
      final SqlCall call;
      if (e instanceof SqlNodeList) {
        call = SqlStdOperatorTable.IN.createCall(pos, value, e);
      } else {
        call = SqlStdOperatorTable.EQUALS.createCall(pos, value, e);
      }
      list.set(i, call);
    }
  }

  if (null == elseClause) {
    elseClause = SqlLiteral.createNull(pos);
  }

  return new SqlCase(pos, null, whenList, thenList, elseClause);
}
 
Example 2
Source File: SqlCase.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a call to the switched form of the case operator, viz:
 *
 * <blockquote><code>CASE value<br>
 * WHEN whenList[0] THEN thenList[0]<br>
 * WHEN whenList[1] THEN thenList[1]<br>
 * ...<br>
 * ELSE elseClause<br>
 * END</code></blockquote>
 */
public static SqlCase createSwitched(SqlParserPos pos, SqlNode value,
    SqlNodeList whenList, SqlNodeList thenList, SqlNode elseClause) {
  if (null != value) {
    List<SqlNode> list = whenList.getList();
    for (int i = 0; i < list.size(); i++) {
      SqlNode e = list.get(i);
      final SqlCall call;
      if (e instanceof SqlNodeList) {
        call = SqlStdOperatorTable.IN.createCall(pos, value, e);
      } else {
        call = SqlStdOperatorTable.EQUALS.createCall(pos, value, e);
      }
      list.set(i, call);
    }
  }

  if (null == elseClause) {
    elseClause = SqlLiteral.createNull(pos);
  }

  return new SqlCase(pos, null, whenList, thenList, elseClause);
}
 
Example 3
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 4
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 5
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 6
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;
}
 
Example 7
Source File: RexToSqlNodeConverterImpl.java    From Bats with Apache License 2.0 4 votes vote down vote up
public SqlNode convertLiteral(RexLiteral literal) {
  // Numeric
  if (SqlTypeFamily.EXACT_NUMERIC.getTypeNames().contains(
      literal.getTypeName())) {
    return SqlLiteral.createExactNumeric(
        literal.getValue().toString(),
        SqlParserPos.ZERO);
  }

  if (SqlTypeFamily.APPROXIMATE_NUMERIC.getTypeNames().contains(
      literal.getTypeName())) {
    return SqlLiteral.createApproxNumeric(
        literal.getValue().toString(),
        SqlParserPos.ZERO);
  }

  // Timestamp
  if (SqlTypeFamily.TIMESTAMP.getTypeNames().contains(
      literal.getTypeName())) {
    return SqlLiteral.createTimestamp(
        literal.getValueAs(TimestampString.class),
        0,
        SqlParserPos.ZERO);
  }

  // Date
  if (SqlTypeFamily.DATE.getTypeNames().contains(
      literal.getTypeName())) {
    return SqlLiteral.createDate(
        literal.getValueAs(DateString.class),
        SqlParserPos.ZERO);
  }

  // Time
  if (SqlTypeFamily.TIME.getTypeNames().contains(
      literal.getTypeName())) {
    return SqlLiteral.createTime(
        literal.getValueAs(TimeString.class),
        0,
        SqlParserPos.ZERO);
  }

  // String
  if (SqlTypeFamily.CHARACTER.getTypeNames().contains(
      literal.getTypeName())) {
    return SqlLiteral.createCharString(
        ((NlsString) (literal.getValue())).getValue(),
        SqlParserPos.ZERO);
  }

  // Boolean
  if (SqlTypeFamily.BOOLEAN.getTypeNames().contains(
      literal.getTypeName())) {
    return SqlLiteral.createBoolean(
        (Boolean) literal.getValue(),
        SqlParserPos.ZERO);
  }

  // Null
  if (SqlTypeFamily.NULL == literal.getTypeName().getFamily()) {
    return SqlLiteral.createNull(SqlParserPos.ZERO);
  }

  return null;
}
 
Example 8
Source File: RexToSqlNodeConverterImpl.java    From calcite with Apache License 2.0 4 votes vote down vote up
public SqlNode convertLiteral(RexLiteral literal) {
  // Numeric
  if (SqlTypeFamily.EXACT_NUMERIC.getTypeNames().contains(
      literal.getTypeName())) {
    return SqlLiteral.createExactNumeric(
        literal.getValue().toString(),
        SqlParserPos.ZERO);
  }

  if (SqlTypeFamily.APPROXIMATE_NUMERIC.getTypeNames().contains(
      literal.getTypeName())) {
    return SqlLiteral.createApproxNumeric(
        literal.getValue().toString(),
        SqlParserPos.ZERO);
  }

  // Timestamp
  if (SqlTypeFamily.TIMESTAMP.getTypeNames().contains(
      literal.getTypeName())) {
    return SqlLiteral.createTimestamp(
        literal.getValueAs(TimestampString.class),
        0,
        SqlParserPos.ZERO);
  }

  // Date
  if (SqlTypeFamily.DATE.getTypeNames().contains(
      literal.getTypeName())) {
    return SqlLiteral.createDate(
        literal.getValueAs(DateString.class),
        SqlParserPos.ZERO);
  }

  // Time
  if (SqlTypeFamily.TIME.getTypeNames().contains(
      literal.getTypeName())) {
    return SqlLiteral.createTime(
        literal.getValueAs(TimeString.class),
        0,
        SqlParserPos.ZERO);
  }

  // String
  if (SqlTypeFamily.CHARACTER.getTypeNames().contains(
      literal.getTypeName())) {
    return SqlLiteral.createCharString(
        ((NlsString) (literal.getValue())).getValue(),
        SqlParserPos.ZERO);
  }

  // Boolean
  if (SqlTypeFamily.BOOLEAN.getTypeNames().contains(
      literal.getTypeName())) {
    return SqlLiteral.createBoolean(
        (Boolean) literal.getValue(),
        SqlParserPos.ZERO);
  }

  // Null
  if (SqlTypeFamily.NULL == literal.getTypeName().getFamily()) {
    return SqlLiteral.createNull(SqlParserPos.ZERO);
  }

  return null;
}