use of com.facebook.presto.common.type.TypeSignature in project presto by prestodb.
the class TestArraySqlFunctions method testArrayFrequencyBigint.
@Test
public void testArrayFrequencyBigint() {
FunctionAndTypeManager functionAndTypeManager = createTestFunctionAndTypeManager();
MapType type = new MapType(BIGINT, INTEGER, methodHandle(TestRowType.class, "throwUnsupportedOperation"), methodHandle(TestRowType.class, "throwUnsupportedOperation"));
TypeSignature typeSignature = TypeSignature.parseTypeSignature(type.getDisplayName());
assertFunction("array_frequency(cast(null as array(bigint)))", functionAndTypeManager.getType(typeSignature), null);
assertFunction("array_frequency(cast(array[] as array(bigint)))", functionAndTypeManager.getType(typeSignature), ImmutableMap.of());
assertFunction("array_frequency(array[cast(null as bigint), cast(null as bigint), cast(null as bigint)])", functionAndTypeManager.getType(typeSignature), ImmutableMap.of());
assertFunction("array_frequency(array[cast(null as bigint), bigint '1'])", functionAndTypeManager.getType(typeSignature), ImmutableMap.of(1L, 1));
assertFunction("array_frequency(array[cast(null as bigint), bigint '1', bigint '3', cast(null as bigint), bigint '1', bigint '3', cast(null as bigint)])", functionAndTypeManager.getType(typeSignature), ImmutableMap.of(1L, 2, 3L, 2));
assertFunction("array_frequency(array[bigint '1', bigint '1', bigint '2', bigint '2', bigint '3', bigint '1', bigint '3', bigint '2'])", functionAndTypeManager.getType(typeSignature), ImmutableMap.of(1L, 3, 2L, 3, 3L, 2));
assertFunction("array_frequency(array[bigint '45'])", functionAndTypeManager.getType(typeSignature), ImmutableMap.of(45L, 1));
assertFunction("array_frequency(array[bigint '-45'])", functionAndTypeManager.getType(typeSignature), ImmutableMap.of(-45L, 1));
assertFunction("array_frequency(array[bigint '1', bigint '3', bigint '1', bigint '3'])", functionAndTypeManager.getType(typeSignature), ImmutableMap.of(1L, 2, 3L, 2));
assertFunction("array_frequency(array[bigint '3', bigint '1', bigint '3',bigint '1'])", functionAndTypeManager.getType(typeSignature), ImmutableMap.of(1L, 2, 3L, 2));
assertFunction("array_frequency(array[bigint '4',bigint '3',bigint '3',bigint '2',bigint '2',bigint '2',bigint '1',bigint '1',bigint '1',bigint '1'])", functionAndTypeManager.getType(typeSignature), ImmutableMap.of(1L, 4, 2L, 3, 3L, 2, 4L, 1));
assertFunction("array_frequency(array[bigint '3', bigint '3', bigint '2', bigint '2', bigint '5', bigint '5', bigint '1', bigint '1'])", functionAndTypeManager.getType(typeSignature), ImmutableMap.of(1L, 2, 2L, 2, 3L, 2, 5L, 2));
}
use of com.facebook.presto.common.type.TypeSignature in project presto by prestodb.
the class TypeConverter method toPrestoType.
public static Type toPrestoType(org.apache.iceberg.types.Type type, TypeManager typeManager) {
switch(type.typeId()) {
case BOOLEAN:
return BooleanType.BOOLEAN;
case BINARY:
case FIXED:
return VarbinaryType.VARBINARY;
case DATE:
return DateType.DATE;
case DECIMAL:
Types.DecimalType decimalType = (Types.DecimalType) type;
return DecimalType.createDecimalType(decimalType.precision(), decimalType.scale());
case DOUBLE:
return DoubleType.DOUBLE;
case LONG:
return BigintType.BIGINT;
case FLOAT:
return RealType.REAL;
case INTEGER:
return IntegerType.INTEGER;
case TIME:
return TimeType.TIME;
case TIMESTAMP:
return TimestampType.TIMESTAMP;
case STRING:
return VarcharType.createUnboundedVarcharType();
case LIST:
Types.ListType listType = (Types.ListType) type;
return new ArrayType(toPrestoType(listType.elementType(), typeManager));
case MAP:
Types.MapType mapType = (Types.MapType) type;
TypeSignature keyType = toPrestoType(mapType.keyType(), typeManager).getTypeSignature();
TypeSignature valueType = toPrestoType(mapType.valueType(), typeManager).getTypeSignature();
return typeManager.getParameterizedType(StandardTypes.MAP, ImmutableList.of(TypeSignatureParameter.of(keyType), TypeSignatureParameter.of(valueType)));
case STRUCT:
List<Types.NestedField> fields = ((Types.StructType) type).fields();
return RowType.from(fields.stream().map(field -> new RowType.Field(Optional.of(field.name()), toPrestoType(field.type(), typeManager))).collect(toImmutableList()));
default:
throw new UnsupportedOperationException(format("Cannot convert from Iceberg type '%s' (%s) to Presto type", type, type.typeId()));
}
}
use of com.facebook.presto.common.type.TypeSignature in project presto by prestodb.
the class SignatureBinder method applyBoundVariables.
public static Signature applyBoundVariables(Signature signature, BoundVariables boundVariables, int arity) {
List<TypeSignature> argumentSignatures;
if (signature.isVariableArity()) {
argumentSignatures = expandVarargFormalTypeSignature(signature.getArgumentTypes(), arity);
} else {
checkArgument(signature.getArgumentTypes().size() == arity);
argumentSignatures = signature.getArgumentTypes();
}
List<TypeSignature> boundArgumentSignatures = applyBoundVariables(argumentSignatures, boundVariables);
TypeSignature boundReturnTypeSignature = applyBoundVariables(signature.getReturnType(), boundVariables);
return new Signature(signature.getName(), signature.getKind(), ImmutableList.of(), ImmutableList.of(), boundReturnTypeSignature, boundArgumentSignatures, false);
}
use of com.facebook.presto.common.type.TypeSignature in project presto by prestodb.
the class SignatureBinder method expandVarargFormalTypeSignature.
private static List<TypeSignature> expandVarargFormalTypeSignature(List<TypeSignature> formalTypeSignatures, int actualArity) {
int variableArityArgumentsCount = actualArity - formalTypeSignatures.size() + 1;
if (variableArityArgumentsCount == 0) {
return formalTypeSignatures.subList(0, formalTypeSignatures.size() - 1);
}
if (variableArityArgumentsCount == 1) {
return formalTypeSignatures;
}
checkArgument(variableArityArgumentsCount > 1 && !formalTypeSignatures.isEmpty());
ImmutableList.Builder<TypeSignature> builder = ImmutableList.builder();
builder.addAll(formalTypeSignatures);
TypeSignature lastTypeSignature = formalTypeSignatures.get(formalTypeSignatures.size() - 1);
for (int i = 1; i < variableArityArgumentsCount; i++) {
builder.add(lastTypeSignature);
}
return builder.build();
}
use of com.facebook.presto.common.type.TypeSignature in project presto by prestodb.
the class ConcatFunction method generateConcat.
private static Class<?> generateConcat(TypeSignature type, int arity) {
checkCondition(arity <= 254, NOT_SUPPORTED, "Too many arguments for string concatenation");
ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName(type.getBase() + "_concat" + arity + "ScalarFunction"), type(Object.class));
// Generate constructor
definition.declareDefaultConstructor(a(PRIVATE));
// Generate concat()
List<Parameter> parameters = IntStream.range(0, arity).mapToObj(i -> arg("arg" + i, Slice.class)).collect(toImmutableList());
MethodDefinition method = definition.declareMethod(a(PUBLIC, STATIC), "concat", type(Slice.class), parameters);
Scope scope = method.getScope();
BytecodeBlock body = method.getBody();
Variable result = scope.declareVariable(Slice.class, "result");
body.append(invokeStatic(ConcatFunction.class, "concat", Slice.class, BytecodeExpressions.newArray(ParameterizedType.type(Slice[].class), parameters))).retObject();
return defineClass(definition, Object.class, ImmutableMap.of(), new DynamicClassLoader(ConcatFunction.class.getClassLoader()));
}
Aggregations