use of org.apache.calcite.sql.SqlWriter 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.SqlWriter 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);
}
Aggregations