use of io.prestosql.spi.type.TypeSignature in project hetu-core by openlookeng.
the class TestSignatureBinder method testBindLiteralForDecimal.
@Test
public void testBindLiteralForDecimal() {
TypeSignature leftType = parseTypeSignature("decimal(p1,s1)", ImmutableSet.of("p1", "s1"));
TypeSignature rightType = parseTypeSignature("decimal(p2,s2)", ImmutableSet.of("p2", "s2"));
Signature function = functionSignature().returnType(parseTypeSignature(StandardTypes.BOOLEAN)).argumentTypes(leftType, rightType).build();
assertThat(function).boundTo("decimal(2,1)", "decimal(1,0)").produces(new BoundVariables(ImmutableMap.of(), ImmutableMap.of("p1", 2L, "s1", 1L, "p2", 1L, "s2", 0L)));
}
use of io.prestosql.spi.type.TypeSignature in project hetu-core by openlookeng.
the class TestSignatureBinder method testBindLiteralForVarchar.
@Test
public void testBindLiteralForVarchar() {
TypeSignature leftType = parseTypeSignature("varchar(x)", ImmutableSet.of("x"));
TypeSignature rightType = parseTypeSignature("varchar(y)", ImmutableSet.of("y"));
Signature function = functionSignature().returnType(parseTypeSignature(StandardTypes.BOOLEAN)).argumentTypes(leftType, rightType).build();
assertThat(function).boundTo("varchar(42)", "varchar(44)").produces(new BoundVariables(ImmutableMap.of(), ImmutableMap.of("x", 42L, "y", 44L)));
assertThat(function).boundTo("unknown", "varchar(44)").withCoercion().produces(new BoundVariables(ImmutableMap.of(), ImmutableMap.of("x", 0L, "y", 44L)));
}
use of io.prestosql.spi.type.TypeSignature in project hetu-core by openlookeng.
the class TestSignatureBinder method testBindDifferentLiteralParameters.
@Test
public void testBindDifferentLiteralParameters() {
TypeSignature argType = parseTypeSignature("decimal(p,s)", ImmutableSet.of("p", "s"));
Signature function = functionSignature().returnType(parseTypeSignature(StandardTypes.BOOLEAN)).argumentTypes(argType, argType).build();
assertThat(function).boundTo("decimal(2,1)", "decimal(3,1)").fails();
}
use of io.prestosql.spi.type.TypeSignature in project pulsar by yahoo.
the class PulsarAvroRowDecoderFactory method parseAvroPrestoType.
private Type parseAvroPrestoType(String fieldname, Schema schema) {
Schema.Type type = schema.getType();
LogicalType logicalType = schema.getLogicalType();
switch(type) {
case STRING:
case ENUM:
return createUnboundedVarcharType();
case NULL:
throw new UnsupportedOperationException(format("field '%s' NULL type code should not be reached," + "please check the schema or report the bug.", fieldname));
case FIXED:
case BYTES:
// When the precision > 36, throw Exception.
if (logicalType instanceof LogicalTypes.Decimal) {
LogicalTypes.Decimal decimal = (LogicalTypes.Decimal) logicalType;
return DecimalType.createDecimalType(decimal.getPrecision(), decimal.getScale());
}
return VarbinaryType.VARBINARY;
case INT:
if (logicalType == LogicalTypes.timeMillis()) {
return TIME;
} else if (logicalType == LogicalTypes.date()) {
return DATE;
}
return IntegerType.INTEGER;
case LONG:
if (logicalType == LogicalTypes.timestampMillis()) {
return TimestampType.TIMESTAMP;
}
// TODO: support timestamp_microseconds logicalType : https://github.com/prestosql/presto/issues/1284
return BigintType.BIGINT;
case FLOAT:
return RealType.REAL;
case DOUBLE:
return DoubleType.DOUBLE;
case BOOLEAN:
return BooleanType.BOOLEAN;
case ARRAY:
return new ArrayType(parseAvroPrestoType(fieldname, schema.getElementType()));
case MAP:
// The key for an avro map must be string
TypeSignature valueType = parseAvroPrestoType(fieldname, schema.getValueType()).getTypeSignature();
return typeManager.getParameterizedType(StandardTypes.MAP, ImmutableList.of(TypeSignatureParameter.typeParameter(VarcharType.VARCHAR.getTypeSignature()), TypeSignatureParameter.typeParameter(valueType)));
case RECORD:
if (schema.getFields().size() > 0) {
return RowType.from(schema.getFields().stream().map(field -> new RowType.Field(Optional.of(field.name()), parseAvroPrestoType(field.name(), field.schema()))).collect(toImmutableList()));
} else {
throw new UnsupportedOperationException(format("field '%s' of record type has no fields, " + "please check schema definition. ", fieldname));
}
case UNION:
for (Schema nestType : schema.getTypes()) {
if (nestType.getType() != Schema.Type.NULL) {
return parseAvroPrestoType(fieldname, nestType);
}
}
throw new UnsupportedOperationException(format("field '%s' of UNION type must contains not NULL type.", fieldname));
default:
throw new UnsupportedOperationException(format("Can't convert from schema type '%s' (%s) to presto type.", schema.getType(), schema.getFullName()));
}
}
use of io.prestosql.spi.type.TypeSignature in project pulsar by yahoo.
the class PulsarJsonRowDecoderFactory method parseJsonPrestoType.
private Type parseJsonPrestoType(String fieldname, Schema schema) {
Schema.Type type = schema.getType();
LogicalType logicalType = schema.getLogicalType();
switch(type) {
case STRING:
case ENUM:
return createUnboundedVarcharType();
case NULL:
throw new UnsupportedOperationException(format("field '%s' NULL type code should not be reached , " + "please check the schema or report the bug.", fieldname));
case FIXED:
case BYTES:
// When the precision > 36, throw Exception.
if (logicalType instanceof LogicalTypes.Decimal) {
LogicalTypes.Decimal decimal = (LogicalTypes.Decimal) logicalType;
return DecimalType.createDecimalType(decimal.getPrecision(), decimal.getScale());
}
return VarbinaryType.VARBINARY;
case INT:
if (logicalType == LogicalTypes.timeMillis()) {
return TIME;
} else if (logicalType == LogicalTypes.date()) {
return DATE;
}
return IntegerType.INTEGER;
case LONG:
if (logicalType == LogicalTypes.timestampMillis()) {
return TimestampType.TIMESTAMP;
}
return BigintType.BIGINT;
case FLOAT:
return RealType.REAL;
case DOUBLE:
return DoubleType.DOUBLE;
case BOOLEAN:
return BooleanType.BOOLEAN;
case ARRAY:
return new ArrayType(parseJsonPrestoType(fieldname, schema.getElementType()));
case MAP:
// The key for an avro map must be string.
TypeSignature valueType = parseJsonPrestoType(fieldname, schema.getValueType()).getTypeSignature();
return typeManager.getParameterizedType(StandardTypes.MAP, ImmutableList.of(TypeSignatureParameter.typeParameter(VarcharType.VARCHAR.getTypeSignature()), TypeSignatureParameter.typeParameter(valueType)));
case RECORD:
if (schema.getFields().size() > 0) {
return RowType.from(schema.getFields().stream().map(field -> new RowType.Field(Optional.of(field.name()), parseJsonPrestoType(field.name(), field.schema()))).collect(toImmutableList()));
} else {
throw new UnsupportedOperationException(format("field '%s' of record type has no fields, " + "please check schema definition. ", fieldname));
}
case UNION:
for (Schema nestType : schema.getTypes()) {
if (nestType.getType() != Schema.Type.NULL) {
return parseJsonPrestoType(fieldname, nestType);
}
}
throw new UnsupportedOperationException(format("field '%s' of UNION type must contains not NULL type.", fieldname));
default:
throw new UnsupportedOperationException(format("Can't convert from schema type '%s' (%s) to presto type.", schema.getType(), schema.getFullName()));
}
}
Aggregations