use of org.apache.calcite.sql.SqlLiteral in project calcite by apache.
the class SqlLiteralChainOperator method unparse.
public void unparse(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) {
final SqlWriter.Frame frame = writer.startList("", "");
SqlCollation collation = null;
for (Ord<SqlNode> operand : Ord.zip(call.getOperandList())) {
SqlLiteral rand = (SqlLiteral) operand.e;
if (operand.i > 0) {
// SQL:2003 says there must be a newline between string
// fragments.
writer.newlineAndIndent();
}
if (rand instanceof SqlCharStringLiteral) {
NlsString nls = ((SqlCharStringLiteral) rand).getNlsString();
if (operand.i == 0) {
collation = nls.getCollation();
// print with prefix
writer.literal(nls.asSql(true, false));
} else {
// print without prefix
writer.literal(nls.asSql(false, false));
}
} else if (operand.i == 0) {
// print with prefix
rand.unparse(writer, leftPrec, rightPrec);
} else {
// print without prefix
if (rand.getTypeName() == SqlTypeName.BINARY) {
BitString bs = (BitString) rand.getValue();
writer.literal("'" + bs.toHexString() + "'");
} else {
writer.literal("'" + rand.toValue() + "'");
}
}
}
if (collation != null) {
collation.unparse(writer, 0, 0);
}
writer.endList(frame);
}
use of org.apache.calcite.sql.SqlLiteral in project calcite by apache.
the class MssqlSqlDialect method unparseFloor.
/**
* Unparses datetime floor for Microsoft SQL Server.
* There is no TRUNC function, so simulate this using calls to CONVERT.
*
* @param writer Writer
* @param call Call
*/
private void unparseFloor(SqlWriter writer, SqlCall call) {
SqlLiteral node = call.operand(1);
TimeUnitRange unit = (TimeUnitRange) node.getValue();
switch(unit) {
case YEAR:
unparseFloorWithUnit(writer, call, 4, "-01-01");
break;
case MONTH:
unparseFloorWithUnit(writer, call, 7, "-01");
break;
case WEEK:
writer.print("CONVERT(DATETIME, CONVERT(VARCHAR(10), " + "DATEADD(day, - (6 + DATEPART(weekday, ");
call.operand(0).unparse(writer, 0, 0);
writer.print(")) % 7, ");
call.operand(0).unparse(writer, 0, 0);
writer.print("), 126))");
break;
case DAY:
unparseFloorWithUnit(writer, call, 10, "");
break;
case HOUR:
unparseFloorWithUnit(writer, call, 13, ":00:00");
break;
case MINUTE:
unparseFloorWithUnit(writer, call, 16, ":00");
break;
case SECOND:
unparseFloorWithUnit(writer, call, 19, ":00");
break;
default:
throw new IllegalArgumentException("MSSQL does not support FLOOR for time unit: " + unit);
}
}
use of org.apache.calcite.sql.SqlLiteral in project calcite by apache.
the class MysqlSqlDialect method unparseFloor.
/**
* Unparses datetime floor for MySQL. There is no TRUNC function, so simulate
* this using calls to DATE_FORMAT.
*
* @param writer Writer
* @param call Call
*/
private void unparseFloor(SqlWriter writer, SqlCall call) {
SqlLiteral node = call.operand(1);
TimeUnitRange unit = (TimeUnitRange) node.getValue();
if (unit == TimeUnitRange.WEEK) {
writer.print("STR_TO_DATE");
SqlWriter.Frame frame = writer.startList("(", ")");
writer.print("DATE_FORMAT(");
call.operand(0).unparse(writer, 0, 0);
writer.print(", '%x%v-1'), '%x%v-%w'");
writer.endList(frame);
return;
}
String format;
switch(unit) {
case YEAR:
format = "%Y-01-01";
break;
case MONTH:
format = "%Y-%m-01";
break;
case DAY:
format = "%Y-%m-%d";
break;
case HOUR:
format = "%Y-%m-%d %k:00:00";
break;
case MINUTE:
format = "%Y-%m-%d %k:%i:00";
break;
case SECOND:
format = "%Y-%m-%d %k:%i:%s";
break;
default:
throw new AssertionError("MYSQL does not support FLOOR for time unit: " + unit);
}
writer.print("DATE_FORMAT");
SqlWriter.Frame frame = writer.startList("(", ")");
call.operand(0).unparse(writer, 0, 0);
writer.sep(",", true);
writer.print("'" + format + "'");
writer.endList(frame);
}
use of org.apache.calcite.sql.SqlLiteral in project calcite by apache.
the class MysqlSqlDialect method rewriteSingleValueExpr.
@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;
}
use of org.apache.calcite.sql.SqlLiteral in project calcite by apache.
the class OracleSqlDialect method unparseCall.
@Override
public void unparseCall(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) {
if (call.getOperator() == SqlStdOperatorTable.SUBSTRING) {
SqlUtil.unparseFunctionSyntax(OracleSqlOperatorTable.SUBSTR, writer, call);
} else {
switch(call.getKind()) {
case FLOOR:
if (call.operandCount() != 2) {
super.unparseCall(writer, call, leftPrec, rightPrec);
return;
}
final SqlLiteral timeUnitNode = call.operand(1);
final TimeUnitRange timeUnit = timeUnitNode.getValueAs(TimeUnitRange.class);
SqlCall call2 = SqlFloorFunction.replaceTimeUnitOperand(call, timeUnit.name(), timeUnitNode.getParserPosition());
SqlFloorFunction.unparseDatetimeFunction(writer, call2, "TRUNC", true);
break;
default:
super.unparseCall(writer, call, leftPrec, rightPrec);
}
}
}
Aggregations