use of org.apache.calcite.sql.SqlCollation 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.SqlCollation in project flink by apache.
the class RexLiteral method fromJdbcString.
/**
* Converts a Jdbc string into a RexLiteral. This method accepts a string, as returned by the
* Jdbc method ResultSet.getString(), and restores the string into an equivalent RexLiteral. It
* allows one to use Jdbc strings as a common format for data.
*
* <p>If a null literal is provided, then a null pointer will be returned.
*
* @param type data type of literal to be read
* @param typeName type family of literal
* @param literal the (non-SQL encoded) string representation, as returned by the Jdbc call to
* return a column as a string
* @return a typed RexLiteral, or null
*/
public static RexLiteral fromJdbcString(RelDataType type, SqlTypeName typeName, String literal) {
if (literal == null) {
return null;
}
switch(typeName) {
case CHAR:
Charset charset = type.getCharset();
SqlCollation collation = type.getCollation();
NlsString str = new NlsString(literal, charset.name(), collation);
return new RexLiteral(str, type, typeName);
case BOOLEAN:
boolean b = ConversionUtil.toBoolean(literal);
return new RexLiteral(b, type, typeName);
case DECIMAL:
case DOUBLE:
BigDecimal d = new BigDecimal(literal);
return new RexLiteral(d, type, typeName);
case BINARY:
byte[] bytes = ConversionUtil.toByteArrayFromString(literal, 16);
return new RexLiteral(new ByteString(bytes), type, typeName);
case NULL:
return new RexLiteral(null, type, typeName);
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:
long millis = SqlParserUtil.intervalToMillis(literal, type.getIntervalQualifier());
return new RexLiteral(BigDecimal.valueOf(millis), type, typeName);
case INTERVAL_YEAR:
case INTERVAL_YEAR_MONTH:
case INTERVAL_MONTH:
long months = SqlParserUtil.intervalToMonths(literal, type.getIntervalQualifier());
return new RexLiteral(BigDecimal.valueOf(months), type, typeName);
case DATE:
case TIME:
case TIMESTAMP:
String format = getCalendarFormat(typeName);
TimeZone tz = DateTimeUtils.UTC_ZONE;
final Comparable v;
switch(typeName) {
case DATE:
final Calendar cal = DateTimeUtils.parseDateFormat(literal, new SimpleDateFormat(format, Locale.ROOT), tz);
if (cal == null) {
throw new AssertionError("fromJdbcString: invalid date/time value '" + literal + "'");
}
v = DateString.fromCalendarFields(cal);
break;
default:
// Allow fractional seconds for times and timestamps
assert format != null;
final DateTimeUtils.PrecisionTime ts = DateTimeUtils.parsePrecisionDateTimeLiteral(literal, new SimpleDateFormat(format, Locale.ROOT), tz, -1);
if (ts == null) {
throw new AssertionError("fromJdbcString: invalid date/time value '" + literal + "'");
}
switch(typeName) {
case TIMESTAMP:
v = TimestampString.fromCalendarFields(ts.getCalendar()).withFraction(ts.getFraction());
break;
case TIME:
v = TimeString.fromCalendarFields(ts.getCalendar()).withFraction(ts.getFraction());
break;
default:
throw new AssertionError();
}
}
return new RexLiteral(v, type, typeName);
case SYMBOL:
// Symbols are for internal use
default:
throw new AssertionError("fromJdbcString: unsupported type");
}
}
Aggregations