use of com.facebook.presto.common.type.TypeSignature in project presto by prestodb.
the class TestAnnotationEngineForAggregates method testSimpleImplicitSpecializedAggregationParse.
// @Test - this is not yet supported
public void testSimpleImplicitSpecializedAggregationParse() {
Signature expectedSignature = new Signature(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, "implicit_specialized_aggregate"), FunctionKind.AGGREGATE, ImmutableList.of(typeVariable("T")), ImmutableList.of(), parseTypeSignature("T"), ImmutableList.of(new TypeSignature(ARRAY, TypeSignatureParameter.of(parseTypeSignature("T"))), parseTypeSignature("T")), false);
ParametricAggregation aggregation = parseFunctionDefinition(ImplicitSpecializedAggregationFunction.class);
assertEquals(aggregation.getDescription(), "Simple implicit specialized aggregate");
assertTrue(aggregation.isDeterministic());
assertEquals(aggregation.getSignature(), expectedSignature);
ParametricImplementationsGroup<AggregationImplementation> implementations = aggregation.getImplementations();
assertImplementationCount(implementations, 0, 0, 2);
AggregationImplementation implementation1 = implementations.getSpecializedImplementations().get(0);
assertTrue(implementation1.hasSpecializedTypeParameters());
assertFalse(implementation1.hasSpecializedTypeParameters());
List<AggregationMetadata.ParameterMetadata.ParameterType> expectedMetadataTypes = ImmutableList.of(AggregationMetadata.ParameterMetadata.ParameterType.STATE, AggregationMetadata.ParameterMetadata.ParameterType.INPUT_CHANNEL, AggregationMetadata.ParameterMetadata.ParameterType.INPUT_CHANNEL);
assertTrue(implementation1.getInputParameterMetadataTypes().equals(expectedMetadataTypes));
AggregationImplementation implementation2 = implementations.getSpecializedImplementations().get(1);
assertTrue(implementation2.hasSpecializedTypeParameters());
assertFalse(implementation2.hasSpecializedTypeParameters());
assertTrue(implementation2.getInputParameterMetadataTypes().equals(expectedMetadataTypes));
InternalAggregationFunction specialized = aggregation.specialize(BoundVariables.builder().setTypeVariable("T", DoubleType.DOUBLE).build(), 1, FUNCTION_AND_TYPE_MANAGER);
assertEquals(specialized.getFinalType(), DoubleType.DOUBLE);
assertTrue(specialized.isDecomposable());
assertEquals(specialized.name(), "implicit_specialized_aggregate");
}
use of com.facebook.presto.common.type.TypeSignature in project presto by prestodb.
the class TestRowParametricType method testTypeSignatureRoundTrip.
@Test
public void testTypeSignatureRoundTrip() {
FunctionAndTypeManager functionAndTypeManager = createTestFunctionAndTypeManager();
TypeSignature typeSignature = new TypeSignature(ROW, TypeSignatureParameter.of(new NamedTypeSignature(Optional.of(new RowFieldName("col1", false)), new TypeSignature(BIGINT))), TypeSignatureParameter.of(new NamedTypeSignature(Optional.of(new RowFieldName("col2", true)), new TypeSignature(DOUBLE))));
List<TypeParameter> parameters = typeSignature.getParameters().stream().map(parameter -> TypeParameter.of(parameter, functionAndTypeManager)).collect(Collectors.toList());
Type rowType = RowParametricType.ROW.createType(parameters);
assertEquals(rowType.getTypeSignature(), typeSignature);
}
use of com.facebook.presto.common.type.TypeSignature in project presto by prestodb.
the class DeltaTypeUtils method convertDeltaDataTypePrestoDataType.
/**
* Convert given Delta data type to Presto data type signature.
*
* @param tableName Used in error messages when an unsupported data type is encountered.
* @param columnName Used in error messages when an unsupported data type is encountered.
* @param deltaType Data type to convert
* @return
*/
public static TypeSignature convertDeltaDataTypePrestoDataType(SchemaTableName tableName, String columnName, DataType deltaType) {
checkArgument(deltaType != null);
if (deltaType instanceof StructType) {
StructType deltaStructType = (StructType) deltaType;
ImmutableList.Builder<TypeSignatureParameter> typeSignatureBuilder = ImmutableList.builder();
Arrays.stream(deltaStructType.getFields()).forEach(field -> {
String rowFieldName = field.getName().toLowerCase(Locale.US);
TypeSignature rowFieldType = convertDeltaDataTypePrestoDataType(tableName, columnName + "." + field.getName(), field.getDataType());
typeSignatureBuilder.add(TypeSignatureParameter.of(new NamedTypeSignature(Optional.of(new RowFieldName(rowFieldName, false)), rowFieldType)));
});
return new TypeSignature(StandardTypes.ROW, typeSignatureBuilder.build());
} else if (deltaType instanceof ArrayType) {
ArrayType deltaArrayType = (ArrayType) deltaType;
TypeSignature elementType = convertDeltaDataTypePrestoDataType(tableName, columnName, deltaArrayType.getElementType());
return new TypeSignature(ARRAY, ImmutableList.of(TypeSignatureParameter.of(elementType)));
} else if (deltaType instanceof MapType) {
MapType deltaMapType = (MapType) deltaType;
TypeSignature keyType = convertDeltaDataTypePrestoDataType(tableName, columnName, deltaMapType.getKeyType());
TypeSignature valueType = convertDeltaDataTypePrestoDataType(tableName, columnName, deltaMapType.getValueType());
return new TypeSignature(MAP, ImmutableList.of(TypeSignatureParameter.of(keyType), TypeSignatureParameter.of(valueType)));
}
return convertDeltaPrimitiveTypeToPrestoPrimitiveType(tableName, columnName, deltaType).getTypeSignature();
}
use of com.facebook.presto.common.type.TypeSignature in project presto by prestodb.
the class HiveUtil method translateHiveUnsupportedTypeSignatureForTemporaryTable.
private static TypeSignature translateHiveUnsupportedTypeSignatureForTemporaryTable(TypeSignature typeSignature) {
List<TypeSignatureParameter> parameters = typeSignature.getParameters();
if (typeSignature.getBase().equals("unknown")) {
return new TypeSignature(StandardTypes.BOOLEAN);
}
if (typeSignature.getBase().equals(StandardTypes.ROW)) {
ImmutableList.Builder<TypeSignatureParameter> updatedParameters = ImmutableList.builder();
for (int i = 0; i < parameters.size(); i++) {
TypeSignatureParameter typeSignatureParameter = parameters.get(i);
checkArgument(typeSignatureParameter.isNamedTypeSignature(), "unexpected row type signature parameter: %s", typeSignatureParameter);
NamedTypeSignature namedTypeSignature = typeSignatureParameter.getNamedTypeSignature();
updatedParameters.add(TypeSignatureParameter.of(new NamedTypeSignature(Optional.of(namedTypeSignature.getFieldName().orElse(new RowFieldName("_field_" + i, false))), translateHiveUnsupportedTypeSignatureForTemporaryTable(namedTypeSignature.getTypeSignature()))));
}
return new TypeSignature(StandardTypes.ROW, updatedParameters.build());
}
if (!parameters.isEmpty()) {
ImmutableList.Builder<TypeSignatureParameter> updatedParameters = ImmutableList.builder();
for (TypeSignatureParameter parameter : parameters) {
switch(parameter.getKind()) {
case LONG:
case VARIABLE:
updatedParameters.add(parameter);
continue;
case TYPE:
updatedParameters.add(TypeSignatureParameter.of(translateHiveUnsupportedTypeSignatureForTemporaryTable(parameter.getTypeSignature())));
break;
case NAMED_TYPE:
NamedTypeSignature namedTypeSignature = parameter.getNamedTypeSignature();
updatedParameters.add(TypeSignatureParameter.of(new NamedTypeSignature(namedTypeSignature.getFieldName(), translateHiveUnsupportedTypeSignatureForTemporaryTable(namedTypeSignature.getTypeSignature()))));
break;
default:
throw new IllegalArgumentException("Unexpected parameter type: " + parameter.getKind());
}
}
return new TypeSignature(typeSignature.getBase(), updatedParameters.build());
}
return typeSignature;
}
use of com.facebook.presto.common.type.TypeSignature in project presto by prestodb.
the class DecimalOperators method decimalAddOperator.
private static SqlScalarFunction decimalAddOperator() {
TypeSignature decimalLeftSignature = parseTypeSignature("decimal(a_precision, a_scale)", ImmutableSet.of("a_precision", "a_scale"));
TypeSignature decimalRightSignature = parseTypeSignature("decimal(b_precision, b_scale)", ImmutableSet.of("b_precision", "b_scale"));
TypeSignature decimalResultSignature = parseTypeSignature("decimal(r_precision, r_scale)", ImmutableSet.of("r_precision", "r_scale"));
Signature signature = SignatureBuilder.builder().kind(SCALAR).operatorType(ADD).longVariableConstraints(longVariableExpression("r_precision", "min(38, max(a_precision - a_scale, b_precision - b_scale) + max(a_scale, b_scale) + 1)"), longVariableExpression("r_scale", "max(a_scale, b_scale)")).argumentTypes(decimalLeftSignature, decimalRightSignature).returnType(decimalResultSignature).build();
return SqlScalarFunction.builder(DecimalOperators.class, ADD).signature(signature).deterministic(true).choice(choice -> choice.implementation(methodsGroup -> methodsGroup.methods("addShortShortShort").withExtraParameters(DecimalOperators::calculateShortRescaleParameters)).implementation(methodsGroup -> methodsGroup.methods("addShortShortLong", "addLongLongLong", "addShortLongLong", "addLongShortLong").withExtraParameters(DecimalOperators::calculateLongRescaleParameters))).build();
}
Aggregations