in exec/java-exec/src/main/java/org/apache/drill/exec/planner/common/DrillValuesRelBase.java [119:265]
private static void writeLiteral(RexLiteral literal, JsonOutput out) throws IOException {
switch (literal.getType().getSqlTypeName()) {
case BIGINT:
if (isLiteralNull(literal)) {
out.writeBigIntNull();
} else {
out.writeBigInt((((BigDecimal) literal.getValue()).setScale(0, RoundingMode.HALF_UP)).longValue());
}
return;
case BOOLEAN:
if (isLiteralNull(literal)) {
out.writeBooleanNull();
} else {
out.writeBoolean((Boolean) literal.getValue());
}
return;
case CHAR:
if (isLiteralNull(literal)) {
out.writeVarcharNull();
} else {
// Since Calcite treats string literals as fixed char and adds trailing spaces to the strings to make them the
// same length, here we do an rtrim() to get the string without the trailing spaces. If we don't rtrim, the comparison
// with Drill's varchar column values would not return a match.
// TODO: However, note that if the user had explicitly added spaces in the string literals then even those would get
// trimmed, so this exposes another issue that needs to be resolved.
out.writeVarChar(((NlsString) literal.getValue()).rtrim().getValue());
}
return;
case DOUBLE:
if (isLiteralNull(literal)) {
out.writeDoubleNull();
} else {
out.writeDouble(((BigDecimal) literal.getValue()).doubleValue());
}
return;
case FLOAT:
if (isLiteralNull(literal)) {
out.writeFloatNull();
} else {
out.writeFloat(((BigDecimal) literal.getValue()).floatValue());
}
return;
case INTEGER:
if (isLiteralNull(literal)) {
out.writeIntNull();
} else {
out.writeInt((((BigDecimal) literal.getValue()).setScale(0, RoundingMode.HALF_UP)).intValue());
}
return;
case DECIMAL:
// Converting exact decimal into double since values in the list may have different scales
// so the resulting scale wouldn't be calculated correctly
if (isLiteralNull(literal)) {
out.writeDoubleNull();
} else {
out.writeDouble(((BigDecimal) literal.getValue()).doubleValue());
}
return;
case VARCHAR:
if (isLiteralNull(literal)) {
out.writeVarcharNull();
} else {
out.writeVarChar(((NlsString) literal.getValue()).getValue());
}
return;
case SYMBOL:
if (isLiteralNull(literal)) {
out.writeVarcharNull();
} else {
out.writeVarChar(literal.getValue().toString());
}
return;
case DATE:
if (isLiteralNull(literal)) {
out.writeDateNull();
} else {
out.writeDate(LocalDateTime.ofInstant(Instant.ofEpochMilli(new DateTime(literal.getValue()).getMillis()), ZoneOffset.UTC).toLocalDate());
}
return;
case TIME:
if (isLiteralNull(literal)) {
out.writeTimeNull();
} else {
out.writeTime(LocalDateTime.ofInstant(Instant.ofEpochMilli(new DateTime(literal.getValue()).getMillis()), ZoneOffset.UTC).toLocalTime());
}
return;
case TIMESTAMP:
if (isLiteralNull(literal)) {
out.writeTimestampNull();
} else {
out.writeTimestamp(LocalDateTime.ofInstant(Instant.ofEpochMilli(new DateTime(literal.getValue()).getMillis()), ZoneOffset.UTC));
}
return;
case INTERVAL_YEAR:
case INTERVAL_YEAR_MONTH:
case INTERVAL_MONTH:
if (isLiteralNull(literal)) {
out.writeIntervalNull();
} else {
int months = ((BigDecimal) (literal.getValue())).intValue();
out.writeInterval(new Period().plusMonths(months));
}
return;
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:
if (isLiteralNull(literal)) {
out.writeIntervalNull();
} else {
long millis = ((BigDecimal) (literal.getValue())).longValue();
long days = millis / DateTimeConstants.MILLIS_PER_DAY;
millis = millis - (days * DateTimeConstants.MILLIS_PER_DAY);
out.writeInterval(new Period().plusDays((int) days).plusMillis((int) millis));
}
return;
case NULL:
out.writeUntypedNull();
return;
case ANY:
default:
throw new UnsupportedOperationException(
String.format("Unable to convert the value of %s and type %s to a Drill constant expression.",
literal, literal.getType().getSqlTypeName()));
}
}