Search in sources :

Example 16 with TypeSignature

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)));
}
Also used : TypeSignature.parseTypeSignature(io.prestosql.spi.type.TypeSignature.parseTypeSignature) TypeSignature(io.prestosql.spi.type.TypeSignature) TypeSignature.parseTypeSignature(io.prestosql.spi.type.TypeSignature.parseTypeSignature) Signature(io.prestosql.spi.function.Signature) TypeSignature(io.prestosql.spi.type.TypeSignature) Test(org.testng.annotations.Test)

Example 17 with TypeSignature

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)));
}
Also used : TypeSignature.parseTypeSignature(io.prestosql.spi.type.TypeSignature.parseTypeSignature) TypeSignature(io.prestosql.spi.type.TypeSignature) TypeSignature.parseTypeSignature(io.prestosql.spi.type.TypeSignature.parseTypeSignature) Signature(io.prestosql.spi.function.Signature) TypeSignature(io.prestosql.spi.type.TypeSignature) Test(org.testng.annotations.Test)

Example 18 with TypeSignature

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();
}
Also used : TypeSignature.parseTypeSignature(io.prestosql.spi.type.TypeSignature.parseTypeSignature) TypeSignature(io.prestosql.spi.type.TypeSignature) TypeSignature.parseTypeSignature(io.prestosql.spi.type.TypeSignature.parseTypeSignature) Signature(io.prestosql.spi.function.Signature) TypeSignature(io.prestosql.spi.type.TypeSignature) Test(org.testng.annotations.Test)

Example 19 with TypeSignature

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()));
    }
}
Also used : ArrayType(io.prestosql.spi.type.ArrayType) TypeSignature(io.prestosql.spi.type.TypeSignature) Schema(org.apache.avro.Schema) GenericJsonSchema(org.apache.pulsar.client.impl.schema.generic.GenericJsonSchema) GenericAvroSchema(org.apache.pulsar.client.impl.schema.generic.GenericAvroSchema) LogicalType(org.apache.avro.LogicalType) LogicalTypes(org.apache.avro.LogicalTypes) RowType(io.prestosql.spi.type.RowType)

Example 20 with TypeSignature

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()));
    }
}
Also used : ArrayType(io.prestosql.spi.type.ArrayType) TypeSignature(io.prestosql.spi.type.TypeSignature) Schema(org.apache.avro.Schema) GenericJsonSchema(org.apache.pulsar.client.impl.schema.generic.GenericJsonSchema) LogicalType(org.apache.avro.LogicalType) LogicalTypes(org.apache.avro.LogicalTypes) RowType(io.prestosql.spi.type.RowType)

Aggregations

TypeSignature (io.prestosql.spi.type.TypeSignature)79 Signature (io.prestosql.spi.function.Signature)36 Type (io.prestosql.spi.type.Type)27 Test (org.testng.annotations.Test)24 TypeSignature.parseTypeSignature (io.prestosql.spi.type.TypeSignature.parseTypeSignature)22 ImmutableList (com.google.common.collect.ImmutableList)18 BuiltInFunctionHandle (io.prestosql.spi.function.BuiltInFunctionHandle)18 CallExpression (io.prestosql.spi.relation.CallExpression)18 PrestoException (io.prestosql.spi.PrestoException)17 List (java.util.List)14 RowExpression (io.prestosql.spi.relation.RowExpression)12 QualifiedObjectName (io.prestosql.spi.connector.QualifiedObjectName)11 RowType (io.prestosql.spi.type.RowType)11 ArrayList (java.util.ArrayList)11 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)10 VariableReferenceExpression (io.prestosql.spi.relation.VariableReferenceExpression)10 ArrayType (io.prestosql.spi.type.ArrayType)10 VarcharType.createUnboundedVarcharType (io.prestosql.spi.type.VarcharType.createUnboundedVarcharType)10 HashSet (java.util.HashSet)10 Domain (io.prestosql.spi.predicate.Domain)9