use of org.apache.calcite.sql.validate.SqlValidator in project calcite by apache.
the class SqlValidatorTest method testCoalesceWithRewrite.
@Test
public void testCoalesceWithRewrite() {
SqlValidator validator = tester.getValidator();
validator.setCallRewrite(true);
if (validator.shouldExpandIdentifiers()) {
tester.checkRewrite(validator, "select coalesce(deptno, empno) from emp", "SELECT CASE WHEN `EMP`.`DEPTNO` IS NOT NULL THEN `EMP`.`DEPTNO` ELSE `EMP`.`EMPNO` END\n" + "FROM `CATALOG`.`SALES`.`EMP` AS `EMP`");
} else {
tester.checkRewrite(validator, "select coalesce(deptno, empno) from emp", "SELECT CASE WHEN `DEPTNO` IS NOT NULL THEN `DEPTNO` ELSE `EMPNO` END\n" + "FROM `EMP`");
}
}
use of org.apache.calcite.sql.validate.SqlValidator in project calcite by apache.
the class SqlValidatorTest method testRewriteWithOffsetWithoutOrderBy.
@Test
public void testRewriteWithOffsetWithoutOrderBy() {
SqlValidator validator = tester.getValidator();
validator.setIdentifierExpansion(false);
final String sql = "select name from dept offset 2";
final String expected = "SELECT `NAME`\n" + "FROM `DEPT`\n" + "OFFSET 2 ROWS";
tester.checkRewrite(validator, sql, expected);
}
use of org.apache.calcite.sql.validate.SqlValidator in project calcite by apache.
the class SqlValidatorTest method testRewriteWithLimitWithDynamicParameters.
@Test
public void testRewriteWithLimitWithDynamicParameters() {
SqlValidator validator = tester.getValidator();
validator.setIdentifierExpansion(false);
final String sql = "select name from dept offset ? rows fetch next ? rows only";
final String expected = "SELECT `NAME`\n" + "FROM `DEPT`\n" + "OFFSET ? ROWS\n" + "FETCH NEXT ? ROWS ONLY";
tester.checkRewrite(validator, sql, expected);
}
use of org.apache.calcite.sql.validate.SqlValidator in project calcite by apache.
the class SqlValidatorTest method testRewriteWithUnionFetchWithoutOrderBy.
@Test
public void testRewriteWithUnionFetchWithoutOrderBy() {
SqlValidator validator = tester.getValidator();
validator.setIdentifierExpansion(false);
final String sql = "select name from dept union all select name from dept limit 2";
final String expected = "SELECT *\n" + "FROM (SELECT `NAME`\n" + "FROM `DEPT`\n" + "UNION ALL\n" + "SELECT `NAME`\n" + "FROM `DEPT`)\n" + "FETCH NEXT 2 ROWS ONLY";
tester.checkRewrite(validator, sql, expected);
}
use of org.apache.calcite.sql.validate.SqlValidator in project calcite by apache.
the class SqlNodeToRexConverterImpl method convertLiteral.
public RexNode convertLiteral(SqlRexContext cx, SqlLiteral literal) {
RexBuilder rexBuilder = cx.getRexBuilder();
RelDataTypeFactory typeFactory = cx.getTypeFactory();
SqlValidator validator = cx.getValidator();
if (literal.getValue() == null) {
// Since there is no eq. RexLiteral of SqlLiteral.Unknown we
// treat it as a cast(null as boolean)
RelDataType type;
if (literal.getTypeName() == SqlTypeName.BOOLEAN) {
type = typeFactory.createSqlType(SqlTypeName.BOOLEAN);
type = typeFactory.createTypeWithNullability(type, true);
} else {
type = validator.getValidatedNodeType(literal);
}
return rexBuilder.makeCast(type, rexBuilder.constantNull());
}
BitString bitString;
SqlIntervalLiteral.IntervalValue intervalValue;
long l;
switch(literal.getTypeName()) {
case DECIMAL:
// exact number
BigDecimal bd = literal.getValueAs(BigDecimal.class);
return rexBuilder.makeExactLiteral(bd, literal.createSqlType(typeFactory));
case DOUBLE:
// TODO: preserve fixed-point precision and large integers
return rexBuilder.makeApproxLiteral(literal.getValueAs(BigDecimal.class));
case CHAR:
return rexBuilder.makeCharLiteral(literal.getValueAs(NlsString.class));
case BOOLEAN:
return rexBuilder.makeLiteral(literal.getValueAs(Boolean.class));
case BINARY:
bitString = literal.getValueAs(BitString.class);
Preconditions.checkArgument((bitString.getBitCount() % 8) == 0, "incomplete octet");
// An even number of hexits (e.g. X'ABCD') makes whole number
// of bytes.
ByteString byteString = new ByteString(bitString.getAsByteArray());
return rexBuilder.makeBinaryLiteral(byteString);
case SYMBOL:
return rexBuilder.makeFlag(literal.getValueAs(Enum.class));
case TIMESTAMP:
return rexBuilder.makeTimestampLiteral(literal.getValueAs(TimestampString.class), ((SqlTimestampLiteral) literal).getPrec());
case TIME:
return rexBuilder.makeTimeLiteral(literal.getValueAs(TimeString.class), ((SqlTimeLiteral) literal).getPrec());
case DATE:
return rexBuilder.makeDateLiteral(literal.getValueAs(DateString.class));
case INTERVAL_YEAR:
case INTERVAL_YEAR_MONTH:
case INTERVAL_MONTH:
case INTERVAL_DAY:
case INTERVAL_DAY_HOUR:
case INTERVAL_DAY_MINUTE:
case INTERVAL_DAY_SECOND:
case INTERVAL_HOUR:
case INTERVAL_HOUR_MINUTE:
case INTERVAL_HOUR_SECOND:
case INTERVAL_MINUTE:
case INTERVAL_MINUTE_SECOND:
case INTERVAL_SECOND:
SqlIntervalQualifier sqlIntervalQualifier = literal.getValueAs(SqlIntervalLiteral.IntervalValue.class).getIntervalQualifier();
return rexBuilder.makeIntervalLiteral(literal.getValueAs(BigDecimal.class), sqlIntervalQualifier);
default:
throw Util.unexpected(literal.getTypeName());
}
}
Aggregations