use of org.apache.calcite.sql.type.SqlTypeName in project flink by splunk.
the class RexLiteral method appendAsJava.
/**
* Appends the specified value in the provided destination as a Java string. The value must be
* consistent with the type, as per {@link #valueMatchesType}.
*
* <p>Typical return values:
*
* <ul>
* <li>true
* <li>null
* <li>"Hello, world!"
* <li>1.25
* <li>1234ABCD
* </ul>
*
* @param value Value to be appended to the provided destination as a Java string
* @param sb Destination to which to append the specified value
* @param typeName Type name to be used for the transformation of the value to a Java string
* @param type Type to be used for the transformation of the value to a Java string
* @param includeType Whether to include the data type in the Java representation
*/
private static void appendAsJava(Comparable value, StringBuilder sb, SqlTypeName typeName, RelDataType type, boolean java, RexDigestIncludeType includeType) {
switch(typeName) {
case CHAR:
NlsString nlsString = (NlsString) value;
if (java) {
Util.printJavaString(sb, nlsString.getValue(), true);
} else {
boolean includeCharset = (nlsString.getCharsetName() != null) && !nlsString.getCharsetName().equals(CalciteSystemProperty.DEFAULT_CHARSET.value());
sb.append(nlsString.asSql(includeCharset, false));
}
break;
case BOOLEAN:
assert value instanceof Boolean;
sb.append(value.toString());
break;
case DECIMAL:
assert value instanceof BigDecimal;
sb.append(value.toString());
break;
case DOUBLE:
assert value instanceof BigDecimal;
sb.append(Util.toScientificNotation((BigDecimal) value));
break;
case BIGINT:
assert value instanceof BigDecimal;
long narrowLong = ((BigDecimal) value).longValue();
sb.append(String.valueOf(narrowLong));
sb.append('L');
break;
case BINARY:
assert value instanceof ByteString;
sb.append("X'");
sb.append(((ByteString) value).toString(16));
sb.append("'");
break;
case NULL:
assert value == null;
sb.append("null");
break;
case SARG:
assert value instanceof Sarg;
// noinspection unchecked,rawtypes
Util.asStringBuilder(sb, sb2 -> printSarg(sb2, (Sarg) value, type));
break;
case SYMBOL:
assert value instanceof Enum;
sb.append("FLAG(");
sb.append(value.toString());
sb.append(")");
break;
case DATE:
assert value instanceof DateString;
sb.append(value.toString());
break;
case TIME:
case TIME_WITH_LOCAL_TIME_ZONE:
assert value instanceof TimeString;
sb.append(value.toString());
break;
case TIMESTAMP:
case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
assert value instanceof TimestampString;
sb.append(value.toString());
break;
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:
assert value instanceof BigDecimal;
sb.append(value.toString());
break;
case MULTISET:
case ROW:
final List<RexLiteral> list = (List) value;
Util.asStringBuilder(sb, sb2 -> Util.printList(sb, list.size(), (sb3, i) -> sb3.append(list.get(i).computeDigest(includeType))));
break;
case GEOMETRY:
final String wkt = GeoFunctions.ST_AsWKT((Geometries.Geom) value);
sb.append(wkt);
break;
default:
assert valueMatchesType(value, typeName, true);
throw Util.needToImplement(typeName);
}
}
use of org.apache.calcite.sql.type.SqlTypeName in project traindb by traindb-project.
the class SqlImplementor method toSql.
/**
* Converts a {@link RexLiteral} to a {@link SqlLiteral}.
*/
public static SqlNode toSql(RexLiteral literal) {
SqlTypeName typeName = literal.getTypeName();
switch(typeName) {
case SYMBOL:
final Enum symbol = (Enum) literal.getValue();
return SqlLiteral.createSymbol(symbol, POS);
case ROW:
// noinspection unchecked
final List<RexLiteral> list = castNonNull(literal.getValueAs(List.class));
return SqlStdOperatorTable.ROW.createCall(POS, list.stream().map(e -> toSql(e)).collect(Util.toImmutableList()));
case SARG:
final Sarg arg = literal.getValueAs(Sarg.class);
throw new AssertionError("sargs [" + arg + "] should be handled as part of predicates, not as literals");
default:
break;
}
SqlTypeFamily family = requireNonNull(typeName.getFamily(), () -> "literal " + literal + " has null SqlTypeFamily, and is SqlTypeName is " + typeName);
switch(family) {
case CHARACTER:
return SqlLiteral.createCharString((String) castNonNull(literal.getValue2()), POS);
case NUMERIC:
case EXACT_NUMERIC:
return SqlLiteral.createExactNumeric(castNonNull(literal.getValueAs(BigDecimal.class)).toPlainString(), POS);
case APPROXIMATE_NUMERIC:
return SqlLiteral.createApproxNumeric(castNonNull(literal.getValueAs(BigDecimal.class)).toPlainString(), POS);
case BOOLEAN:
return SqlLiteral.createBoolean(castNonNull(literal.getValueAs(Boolean.class)), POS);
case INTERVAL_YEAR_MONTH:
case INTERVAL_DAY_TIME:
final boolean negative = castNonNull(literal.getValueAs(Boolean.class));
return SqlLiteral.createInterval(negative ? -1 : 1, castNonNull(literal.getValueAs(String.class)), castNonNull(literal.getType().getIntervalQualifier()), POS);
case DATE:
return SqlLiteral.createDate(castNonNull(literal.getValueAs(DateString.class)), POS);
case TIME:
return SqlLiteral.createTime(castNonNull(literal.getValueAs(TimeString.class)), literal.getType().getPrecision(), POS);
case TIMESTAMP:
return SqlLiteral.createTimestamp(castNonNull(literal.getValueAs(TimestampString.class)), literal.getType().getPrecision(), POS);
case ANY:
case NULL:
switch(typeName) {
case NULL:
return SqlLiteral.createNull(POS);
default:
break;
}
// fall through
default:
throw new AssertionError(literal + ": " + typeName);
}
}
use of org.apache.calcite.sql.type.SqlTypeName in project traindb by traindb-project.
the class TrainDBJdbcSchema method sqlType.
private static RelDataType sqlType(RelDataTypeFactory typeFactory, int dataType, int precision, int scale, @Nullable String typeString) {
// Fall back to ANY if type is unknown
final SqlTypeName sqlTypeName = Util.first(SqlTypeName.getNameForJdbcType(dataType), SqlTypeName.ANY);
switch(sqlTypeName) {
case ARRAY:
RelDataType component = null;
if (typeString != null && typeString.endsWith(" ARRAY")) {
// E.g. hsqldb gives "INTEGER ARRAY", so we deduce the component type
// "INTEGER".
final String remaining = typeString.substring(0, typeString.length() - " ARRAY".length());
component = parseTypeString(typeFactory, remaining);
}
if (component == null) {
component = typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.ANY), true);
}
return typeFactory.createArrayType(component, -1);
default:
break;
}
if (precision >= 0 && scale >= 0 && sqlTypeName.allowsPrecScale(true, true)) {
return typeFactory.createSqlType(sqlTypeName, precision, scale);
} else if (precision >= 0 && sqlTypeName.allowsPrecNoScale()) {
return typeFactory.createSqlType(sqlTypeName, precision);
} else {
assert sqlTypeName.allowsNoPrecNoScale();
return typeFactory.createSqlType(sqlTypeName);
}
}
use of org.apache.calcite.sql.type.SqlTypeName in project traindb by traindb-project.
the class TrainDBJdbcSchema method parseTypeString.
/**
* Given "INTEGER", returns BasicSqlType(INTEGER).
* Given "VARCHAR(10)", returns BasicSqlType(VARCHAR, 10).
* Given "NUMERIC(10, 2)", returns BasicSqlType(NUMERIC, 10, 2).
*/
private static RelDataType parseTypeString(RelDataTypeFactory typeFactory, String typeString) {
int precision = -1;
int scale = -1;
int open = typeString.indexOf("(");
if (open >= 0) {
int close = typeString.indexOf(")", open);
if (close >= 0) {
String rest = typeString.substring(open + 1, close);
typeString = typeString.substring(0, open);
int comma = rest.indexOf(",");
if (comma >= 0) {
precision = Integer.parseInt(rest.substring(0, comma));
scale = Integer.parseInt(rest.substring(comma));
} else {
precision = Integer.parseInt(rest);
}
}
}
try {
final SqlTypeName typeName = SqlTypeName.valueOf(typeString);
return typeName.allowsPrecScale(true, true) ? typeFactory.createSqlType(typeName, precision, scale) : typeName.allowsPrecScale(true, false) ? typeFactory.createSqlType(typeName, precision) : typeFactory.createSqlType(typeName);
} catch (IllegalArgumentException e) {
return typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.ANY), true);
}
}
use of org.apache.calcite.sql.type.SqlTypeName in project hazelcast by hazelcast.
the class HazelcastTypeFactory method leastRestrictive.
/**
* Finds the widest bit width integer type belonging to the same type name
* (family) as the given target integer type from the given list of types.
*
* @param targetType the target type to find the widest instance of.
* @param types the list of types to inspect.
* @return the widest integer type found.
*/
private static RelDataType leastRestrictive(RelDataType targetType, List<RelDataType> types) {
SqlTypeName typeName = targetType.getSqlTypeName();
assert HazelcastTypeUtils.isNumericIntegerType(typeName);
int maxBitWidth = -1;
RelDataType maxBitWidthType = null;
for (RelDataType type : types) {
if (type.getSqlTypeName() != typeName) {
continue;
}
int bitWidth = ((HazelcastIntegerType) type).getBitWidth();
if (bitWidth > maxBitWidth) {
maxBitWidth = bitWidth;
maxBitWidthType = type;
}
}
assert maxBitWidthType != null;
assert maxBitWidthType.getSqlTypeName() == typeName;
return HazelcastIntegerType.create((HazelcastIntegerType) maxBitWidthType, targetType.isNullable());
}
Aggregations