use of io.trino.spi.type.TimestampType in project TiBigData by tidb-incubator.
the class TypeHelpers method toSqlString.
public static String toSqlString(Type type) {
if (type instanceof TimeWithTimeZoneType || type instanceof TimestampWithTimeZoneType) {
throw new TrinoException(NOT_SUPPORTED, "Unsupported column type: " + type.getDisplayName());
}
if (type instanceof TimestampType) {
return format("timestamp(%s)", ((TimestampType) type).getPrecision());
}
if (type instanceof VarcharType) {
VarcharType varcharType = (VarcharType) type;
if (varcharType.isUnbounded()) {
return "longtext";
}
Integer length = varcharType.getLength().orElseThrow(IllegalStateException::new);
if (length <= 255) {
return "tinytext";
}
if (length <= 65535) {
return "text";
}
if (length <= 16777215) {
return "mediumtext";
}
return "longtext";
}
if (type instanceof CharType) {
int length = ((CharType) type).getLength();
if (length <= 255) {
return "char(" + length + ")";
}
return "text";
}
if (type instanceof DecimalType) {
return format("decimal(%s, %s)", ((DecimalType) type).getPrecision(), ((DecimalType) type).getScale());
}
if (type instanceof TimeType) {
return format("time(%s)", ((TimeType) type).getPrecision());
}
String sqlType = SQL_TYPES.get(type);
if (sqlType != null) {
return sqlType;
}
return type.getDisplayName();
}
use of io.trino.spi.type.TimestampType in project trino by trinodb.
the class HiveWriteUtils method getJavaObjectInspector.
public static ObjectInspector getJavaObjectInspector(Type type) {
if (type.equals(BOOLEAN)) {
return javaBooleanObjectInspector;
}
if (type.equals(BIGINT)) {
return javaLongObjectInspector;
}
if (type.equals(INTEGER)) {
return javaIntObjectInspector;
}
if (type.equals(SMALLINT)) {
return javaShortObjectInspector;
}
if (type.equals(TINYINT)) {
return javaByteObjectInspector;
}
if (type.equals(REAL)) {
return javaFloatObjectInspector;
}
if (type.equals(DOUBLE)) {
return javaDoubleObjectInspector;
}
if (type instanceof VarcharType) {
return writableStringObjectInspector;
}
if (type instanceof CharType) {
return writableHiveCharObjectInspector;
}
if (type.equals(VARBINARY)) {
return javaByteArrayObjectInspector;
}
if (type.equals(DATE)) {
return javaDateObjectInspector;
}
if (type instanceof TimestampType) {
return javaTimestampObjectInspector;
}
if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
return getPrimitiveJavaObjectInspector(new DecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale()));
}
if (isArrayType(type)) {
return ObjectInspectorFactory.getStandardListObjectInspector(getJavaObjectInspector(type.getTypeParameters().get(0)));
}
if (isMapType(type)) {
ObjectInspector keyObjectInspector = getJavaObjectInspector(type.getTypeParameters().get(0));
ObjectInspector valueObjectInspector = getJavaObjectInspector(type.getTypeParameters().get(1));
return ObjectInspectorFactory.getStandardMapObjectInspector(keyObjectInspector, valueObjectInspector);
}
if (isRowType(type)) {
return ObjectInspectorFactory.getStandardStructObjectInspector(type.getTypeSignature().getParameters().stream().map(parameter -> parameter.getNamedTypeSignature().getName().get()).collect(toImmutableList()), type.getTypeParameters().stream().map(HiveWriteUtils::getJavaObjectInspector).collect(toImmutableList()));
}
throw new IllegalArgumentException("unsupported type: " + type);
}
use of io.trino.spi.type.TimestampType in project trino by trinodb.
the class HiveWriteUtils method getRowColumnInspector.
public static ObjectInspector getRowColumnInspector(Type type) {
if (type.equals(BOOLEAN)) {
return writableBooleanObjectInspector;
}
if (type.equals(BIGINT)) {
return writableLongObjectInspector;
}
if (type.equals(INTEGER)) {
return writableIntObjectInspector;
}
if (type.equals(SMALLINT)) {
return writableShortObjectInspector;
}
if (type.equals(TINYINT)) {
return writableByteObjectInspector;
}
if (type.equals(REAL)) {
return writableFloatObjectInspector;
}
if (type.equals(DOUBLE)) {
return writableDoubleObjectInspector;
}
if (type instanceof VarcharType) {
VarcharType varcharType = (VarcharType) type;
if (varcharType.isUnbounded()) {
// Values for such columns must be stored as STRING in Hive
return writableStringObjectInspector;
}
if (varcharType.getBoundedLength() <= HiveVarchar.MAX_VARCHAR_LENGTH) {
// VARCHAR columns with the length less than or equal to 65535 are supported natively by Hive
return getPrimitiveWritableObjectInspector(getVarcharTypeInfo(varcharType.getBoundedLength()));
}
}
if (type instanceof CharType) {
CharType charType = (CharType) type;
int charLength = charType.getLength();
return getPrimitiveWritableObjectInspector(getCharTypeInfo(charLength));
}
if (type.equals(VARBINARY)) {
return writableBinaryObjectInspector;
}
if (type.equals(DATE)) {
return writableDateObjectInspector;
}
if (type instanceof TimestampType) {
return writableTimestampObjectInspector;
}
if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
return getPrimitiveWritableObjectInspector(new DecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale()));
}
if (isArrayType(type) || isMapType(type) || isRowType(type)) {
return getJavaObjectInspector(type);
}
throw new IllegalArgumentException("unsupported type: " + type);
}
use of io.trino.spi.type.TimestampType in project trino by trinodb.
the class TestHiveBucketing method testHashingCompare.
@Test
public void testHashingCompare() {
assertBucketEquals("string", "Trino rocks", 1132136730, -399107423);
assertEquals(HiveBucketing.getBucketNumber(1132136730, 4), 2);
assertEquals(HiveBucketing.getBucketNumber(-399107423, 4), 1);
assertBucketEquals("boolean", null, 0, 0);
assertBucketEquals("boolean", true, 1, 1);
assertBucketEquals("boolean", false, 0, 0);
assertBucketEquals("tinyint", null, 0, 0);
assertBucketEquals("tinyint", (byte) 5, 5, 5);
assertBucketEquals("tinyint", Byte.MIN_VALUE, -128, -128);
assertBucketEquals("tinyint", Byte.MAX_VALUE, 127, 127);
assertBucketEquals("smallint", null, 0, 0);
assertBucketEquals("smallint", (short) 300, 300, 2107031704);
assertBucketEquals("smallint", Short.MIN_VALUE, -32768, 1342976838);
assertBucketEquals("smallint", Short.MAX_VALUE, 32767, -684075052);
assertBucketEquals("int", null, 0, 0);
assertBucketEquals("int", 300_000, 300000, -678663480);
assertBucketEquals("int", Integer.MIN_VALUE, -2147483648, 1194881028);
assertBucketEquals("int", Integer.MAX_VALUE, 2147483647, 1133859967);
assertBucketEquals("bigint", null, 0, 0);
assertBucketEquals("bigint", 300_000_000_000L, -647710651, -888935297);
assertBucketEquals("bigint", Long.MIN_VALUE, -2147483648, 1728983947);
assertBucketEquals("bigint", Long.MAX_VALUE, -2147483648, -536577852);
assertBucketEquals("float", null, 0, 0);
assertBucketEquals("float", 12.34F, 1095069860, -381747602);
assertBucketEquals("float", -Float.MAX_VALUE, -8388609, 470252243);
assertBucketEquals("float", Float.MIN_VALUE, 1, 1206721797);
assertBucketEquals("float", Float.POSITIVE_INFINITY, 2139095040, -292175804);
assertBucketEquals("float", Float.NEGATIVE_INFINITY, -8388608, -1433270801);
assertBucketEquals("float", Float.NaN, 2143289344, -480354314);
// also a NaN
assertBucketEquals("float", intBitsToFloat(0xffc00000), 2143289344, -480354314);
// also a NaN
assertBucketEquals("float", intBitsToFloat(0x7fc00000), 2143289344, -480354314);
// also a NaN
assertBucketEquals("float", intBitsToFloat(0x7fc01234), 2143289344, -480354314);
// also a NaN
assertBucketEquals("float", intBitsToFloat(0xffc01234), 2143289344, -480354314);
assertBucketEquals("double", null, 0, 0);
assertBucketEquals("double", 12.34, 986311098, -2070733568);
assertBucketEquals("double", -Double.MAX_VALUE, 1048576, 14392725);
assertBucketEquals("double", Double.MIN_VALUE, 1, -8838199);
assertBucketEquals("double", Double.POSITIVE_INFINITY, 2146435072, 1614292060);
assertBucketEquals("double", Double.NEGATIVE_INFINITY, -1048576, 141388605);
assertBucketEquals("double", Double.NaN, 2146959360, 1138026565);
// also a NaN
assertBucketEquals("double", longBitsToDouble(0xfff8000000000000L), 2146959360, 1138026565);
// also a NaN
assertBucketEquals("double", longBitsToDouble(0x7ff8123412341234L), 2146959360, 1138026565);
// also a NaN
assertBucketEquals("double", longBitsToDouble(0xfff8123412341234L), 2146959360, 1138026565);
assertBucketEquals("varchar(15)", null, 0, 0);
assertBucketEquals("varchar(15)", "", 1, -965378730);
assertBucketEquals("varchar(15)", "test string", -189841218, -138301454);
// 3-byte UTF-8 sequences (in Basic Plane, i.e. Plane 0)
assertBucketEquals("varchar(15)", "\u5f3a\u5927\u7684Trino\u5f15\u64ce", 1899852420, 1784416344);
// 4 code points: 20FFC - 20FFF. 4-byte UTF-8 sequences in Supplementary Plane 2
assertBucketEquals("varchar(15)", "\uD843\uDFFC\uD843\uDFFD\uD843\uDFFE\uD843\uDFFF", -457487557, -697348811);
assertBucketEquals("string", null, 0, 0);
assertBucketEquals("string", "", 0, -965378730);
assertBucketEquals("string", "test string", -318923937, -138301454);
// 3-byte UTF-8 sequences (in Basic Plane, i.e. Plane 0)
assertBucketEquals("string", "\u5f3a\u5927\u7684Trino\u5f15\u64ce", 1688501507, 1784416344);
// 4 code points: 20FFC - 20FFF. 4-byte UTF-8 sequences in Supplementary Plane 2
assertBucketEquals("string", "\uD843\uDFFC\uD843\uDFFD\uD843\uDFFE\uD843\uDFFF", -1810797254, -697348811);
assertBucketEquals("date", null, 0, 0);
assertBucketEquals("date", Date.valueOf("1970-01-01"), 0, 1362653161);
assertBucketEquals("date", Date.valueOf("2015-11-19"), 16758, 8542395);
assertBucketEquals("date", Date.valueOf("1950-11-19"), -6983, -431619185);
for (BucketingVersion version : BucketingVersion.values()) {
List<TypeInfo> typeInfos = ImmutableList.of(timestampTypeInfo);
assertThatThrownBy(() -> version.getBucketHashCode(typeInfos, new Object[] { 0 })).hasMessage("Computation of Hive bucket hashCode is not supported for Hive primitive category: TIMESTAMP");
TimestampType timestampType = createTimestampType(3);
BlockBuilder builder = timestampType.createBlockBuilder(null, 1);
timestampType.writeLong(builder, 0);
Page page = new Page(builder.build());
assertThatThrownBy(() -> version.getBucketHashCode(typeInfos, page, 0)).hasMessage("Computation of Hive bucket hashCode is not supported for Hive primitive category: TIMESTAMP");
}
assertBucketEquals("array<double>", null, 0, 0);
assertBucketEquals("array<boolean>", ImmutableList.of(), 0, 0);
assertBucketEquals("array<smallint>", ImmutableList.of((short) 5, (short) 8, (short) 13), 5066, -905011156);
assertBucketEquals("array<string>", ImmutableList.of("test1", "test2", "test3", "test4"), 957612994, 1305539282);
assertBucketEquals("array<array<bigint>>", ImmutableList.of(ImmutableList.of(10L, 20L), ImmutableList.of(-10L, -20L), asList((Object) null)), 326368, 611324477);
assertBucketEquals("map<float,date>", null, 0, 0);
assertBucketEquals("map<double,timestamp>", ImmutableMap.of(), 0, 0);
assertBucketEquals("map<string,bigint>", ImmutableMap.of("key", 123L, "key2", 123456789L, "key3", -123456L), 127880789, -1910999650);
assertBucketEquals("map<array<double>,map<int,string>>", ImmutableMap.of(ImmutableList.of(12.3, 45.7), ImmutableMap.of(123, "test99")), -34001111, -1565874874);
// multiple bucketing columns
assertBucketEquals(ImmutableList.of("float", "array<smallint>", "map<string,bigint>"), ImmutableList.of(12.34F, ImmutableList.of((short) 5, (short) 8, (short) 13), ImmutableMap.of("key", 123L)), 95411006, 932898434);
assertBucketEquals(ImmutableList.of("double", "array<smallint>", "boolean", "map<string,bigint>", "tinyint"), asList(null, ImmutableList.of((short) 5, (short) 8, (short) 13), null, ImmutableMap.of("key", 123L), null), 154207826, -1120812524);
}
use of io.trino.spi.type.TimestampType in project trino by trinodb.
the class TypeCoercion method compatibility.
private TypeCompatibility compatibility(Type fromType, Type toType) {
if (fromType.equals(toType)) {
return TypeCompatibility.compatible(toType, true);
}
if (fromType.equals(UnknownType.UNKNOWN)) {
return TypeCompatibility.compatible(toType, true);
}
if (toType.equals(UnknownType.UNKNOWN)) {
return TypeCompatibility.compatible(fromType, false);
}
String fromTypeBaseName = fromType.getBaseName();
String toTypeBaseName = toType.getBaseName();
if (fromTypeBaseName.equals(toTypeBaseName)) {
if (fromTypeBaseName.equals(StandardTypes.DECIMAL)) {
Type commonSuperType = getCommonSuperTypeForDecimal((DecimalType) fromType, (DecimalType) toType);
return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
}
if (fromTypeBaseName.equals(StandardTypes.VARCHAR)) {
Type commonSuperType = getCommonSuperTypeForVarchar((VarcharType) fromType, (VarcharType) toType);
return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
}
if (fromTypeBaseName.equals(StandardTypes.CHAR)) {
Type commonSuperType = getCommonSuperTypeForChar((CharType) fromType, (CharType) toType);
return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
}
if (fromTypeBaseName.equals(StandardTypes.ROW)) {
return typeCompatibilityForRow((RowType) fromType, (RowType) toType);
}
if (fromTypeBaseName.equals(StandardTypes.TIMESTAMP)) {
Type commonSuperType = createTimestampType(Math.max(((TimestampType) fromType).getPrecision(), ((TimestampType) toType).getPrecision()));
return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
}
if (fromTypeBaseName.equals(StandardTypes.TIMESTAMP_WITH_TIME_ZONE)) {
Type commonSuperType = createTimestampWithTimeZoneType(Math.max(((TimestampWithTimeZoneType) fromType).getPrecision(), ((TimestampWithTimeZoneType) toType).getPrecision()));
return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
}
if (fromTypeBaseName.equals(StandardTypes.TIME)) {
Type commonSuperType = createTimeType(Math.max(((TimeType) fromType).getPrecision(), ((TimeType) toType).getPrecision()));
return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
}
if (fromTypeBaseName.equals(StandardTypes.TIME_WITH_TIME_ZONE)) {
Type commonSuperType = createTimeWithTimeZoneType(Math.max(((TimeWithTimeZoneType) fromType).getPrecision(), ((TimeWithTimeZoneType) toType).getPrecision()));
return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
}
if (isCovariantParametrizedType(fromType)) {
return typeCompatibilityForCovariantParametrizedType(fromType, toType);
}
return TypeCompatibility.incompatible();
}
Optional<Type> coercedType = coerceTypeBase(fromType, toType.getBaseName());
if (coercedType.isPresent()) {
return compatibility(coercedType.get(), toType);
}
coercedType = coerceTypeBase(toType, fromType.getBaseName());
if (coercedType.isPresent()) {
TypeCompatibility typeCompatibility = compatibility(fromType, coercedType.get());
if (!typeCompatibility.isCompatible()) {
return TypeCompatibility.incompatible();
}
return TypeCompatibility.compatible(typeCompatibility.getCommonSuperType(), false);
}
return TypeCompatibility.incompatible();
}
Aggregations