@JsonCreator public Signature( @JsonProperty("name") String name, @JsonProperty("kind") FunctionKind kind, @JsonProperty("typeParameterRequirements") List<TypeParameterRequirement> typeParameterRequirements, @JsonProperty("returnType") TypeSignature returnType, @JsonProperty("argumentTypes") List<TypeSignature> argumentTypes, @JsonProperty("variableArity") boolean variableArity) { requireNonNull(name, "name is null"); requireNonNull(typeParameterRequirements, "typeParameters is null"); this.name = name; this.kind = requireNonNull(kind, "type is null"); this.typeParameterRequirements = ImmutableList.copyOf(typeParameterRequirements); this.returnType = requireNonNull(returnType, "returnType is null"); this.argumentTypes = ImmutableList.copyOf(requireNonNull(argumentTypes, "argumentTypes is null")); this.variableArity = variableArity; }
@Nullable private static Signature bindSignature(Signature signature, List<? extends Type> types, boolean allowCoercion, TypeManager typeManager) { List<TypeSignature> argumentTypes = signature.getArgumentTypes(); Map<String, Type> boundParameters = signature.bindTypeParameters(types, allowCoercion, typeManager); if (boundParameters == null) { return null; } ImmutableList.Builder<TypeSignature> boundArguments = ImmutableList.builder(); for (int i = 0; i < argumentTypes.size() - 1; i++) { boundArguments.add(argumentTypes.get(i).bindParameters(boundParameters)); } if (!argumentTypes.isEmpty()) { TypeSignature lastArgument = argumentTypes.get(argumentTypes.size() - 1).bindParameters(boundParameters); if (signature.isVariableArity()) { for (int i = 0; i < types.size() - (argumentTypes.size() - 1); i++) { boundArguments.add(lastArgument); } } else { boundArguments.add(lastArgument); } } return new Signature(signature.getName(), signature.getKind(), signature.getReturnType().bindParameters(boundParameters), boundArguments.build()); }
@Nullable private SqlFunction getRowFieldReference(String field, TypeSignature rowTypeSignature) { Type rowType = typeManager.getType(rowTypeSignature); checkState(rowType.getTypeSignature().getBase().equals(StandardTypes.ROW), "rowType is not a ROW type"); SqlFunction match = null; for (SqlFunction function : RowParametricType.ROW.createFunctions(rowType)) { if (!function.getSignature().getName().equals(field)) { continue; } checkArgument(match == null, "Ambiguous field %s in type %s", field, rowType.getDisplayName()); match = function; } return match; }
public SimpleSqlScalarFunction( Signature signature, String description, boolean hidden, MethodHandle methodHandle, Optional<MethodHandle> instanceFactory, boolean deterministic, boolean nullable, List<Boolean> nullableArguments) { super(signature.getName(), ImmutableList.of(), signature.getReturnType().toString(), signature.getArgumentTypes().stream() .map(TypeSignature::toString) .collect(ImmutableCollectors.toImmutableList())); checkArgument(signature.getTypeParameterRequirements().isEmpty(), "%s is parametric", signature); this.description = description; this.hidden = hidden; this.methodHandle = requireNonNull(methodHandle, "methodHandle is null"); this.instanceFactory = requireNonNull(instanceFactory, "instanceFactory is null"); this.deterministic = deterministic; this.nullable = nullable; this.nullableArguments = requireNonNull(nullableArguments, "nullableArguments is null"); }
public SimpleSqlAggregationFunction( String name, String description, InternalAggregationFunction function) { super(name, ImmutableList.<TypeParameterRequirement>of(), function.getFinalType().getTypeSignature().toString(), function.getParameterTypes().stream() .map(Type::getTypeSignature) .map(TypeSignature::toString) .collect(ImmutableCollectors.toImmutableList()), function.isApproximate() ? APPROXIMATE_AGGREGATE : AGGREGATE); this.description = description; this.function = requireNonNull(function, "function is null"); }
public SimpleSqlOperator( OperatorType operatorType, List<TypeSignature> argumentTypes, TypeSignature returnType, MethodHandle methodHandle, Optional<MethodHandle> instanceFactory, boolean nullable, List<Boolean> nullableArguments) { super(operatorType, ImmutableList.of(), returnType.toString(), argumentTypes.stream() .map(TypeSignature::toString) .collect(ImmutableCollectors.toImmutableList())); this.methodHandle = requireNonNull(methodHandle, "methodHandle is null"); this.instanceFactory = requireNonNull(instanceFactory, "instanceFactory is null"); this.nullable = nullable; this.nullableArguments = ImmutableList.copyOf(requireNonNull(nullableArguments, "nullableArguments is null")); }
private static List<Column> createColumnsList(QueryInfo queryInfo) { requireNonNull(queryInfo, "queryInfo is null"); StageInfo outputStage = queryInfo.getOutputStage(); requireNonNull(outputStage, "outputStage is null"); List<String> names = queryInfo.getFieldNames(); List<Type> types = outputStage.getTypes(); checkArgument(names.size() == types.size(), "names and types size mismatch"); ImmutableList.Builder<Column> list = ImmutableList.builder(); for (int i = 0; i < names.size(); i++) { String name = names.get(i); TypeSignature typeSignature = types.get(i).getTypeSignature(); String type = typeSignature.toString(); list.add(new Column(name, type, new ClientTypeSignature(typeSignature))); } return list.build(); }
@Override protected RowExpression visitFunctionCall(FunctionCall node, Void context) { List<RowExpression> arguments = node.getArguments().stream() .map(value -> process(value, context)) .collect(toImmutableList()); List<TypeSignature> argumentTypes = arguments.stream() .map(RowExpression::getType) .map(Type::getTypeSignature) .collect(toImmutableList()); Signature signature = new Signature(node.getName().getSuffix(), functionKind, types.get(node).getTypeSignature(), argumentTypes); return call(signature, types.get(node), arguments); }
public RowType(List<Type> fieldTypes, Optional<List<String>> fieldNames) { super(new TypeSignature( ROW, Lists.transform(fieldTypes, Type::getTypeSignature), fieldNames.orElse(ImmutableList.of()).stream() .collect(toImmutableList())), Block.class); ImmutableList.Builder<RowField> builder = ImmutableList.builder(); for (int i = 0; i < fieldTypes.size(); i++) { int index = i; builder.add(new RowField(fieldTypes.get(i), fieldNames.map((names) -> names.get(index)))); } fields = builder.build(); }
private Type instantiateParametricType(TypeSignature signature) { List<TypeParameter> parameters = new ArrayList<>(); for (TypeSignatureParameter parameter : signature.getParameters()) { TypeParameter typeParameter = TypeParameter.of(parameter, this); if (typeParameter == null) { return null; } parameters.add(typeParameter); } ParametricType parametricType = parametricTypes.get(signature.getBase().toLowerCase(Locale.ENGLISH)); if (parametricType == null) { return null; } Type instantiatedType = parametricType.createType(parameters); checkState(instantiatedType.getTypeSignature().equals(signature), "Instantiated parametric type name (%s) does not match expected name (%s)", instantiatedType, signature); return instantiatedType; }
@Deprecated private String rowToString() { String types = arguments.stream() .map(ClientTypeSignatureParameter::getNamedTypeSignature) .map(NamedTypeSignature::getTypeSignature) .map(TypeSignature::toString) .collect(Collectors.joining(",")); String fieldNames = arguments.stream() .map(ClientTypeSignatureParameter::getNamedTypeSignature) .map(NamedTypeSignature::getName) .map(name -> format("'%s'", name)) .collect(Collectors.joining(",")); return format("row<%s>(%s)", types, fieldNames); }
@Test public void testJsonRoundTrip() { TypeSignature bigint = BIGINT.getTypeSignature(); assertJsonRoundTrip(new ClientTypeSignature(bigint)); assertJsonRoundTrip(new ClientTypeSignature( "array", ImmutableList.of(new ClientTypeSignatureParameter(TypeSignatureParameter.of(bigint))))); assertJsonRoundTrip(new ClientTypeSignature( "foo", ImmutableList.of(new ClientTypeSignatureParameter(TypeSignatureParameter.of(42))))); assertJsonRoundTrip(new ClientTypeSignature( "row", ImmutableList.of( new ClientTypeSignatureParameter(TypeSignatureParameter.of(new NamedTypeSignature("foo", bigint))), new ClientTypeSignatureParameter(TypeSignatureParameter.of(new NamedTypeSignature("bar", bigint)))))); }
@JsonCreator public HiveColumnHandle( @JsonProperty("clientId") String clientId, @JsonProperty("name") String name, @JsonProperty("hiveType") HiveType hiveType, @JsonProperty("typeSignature") TypeSignature typeSignature, @JsonProperty("hiveColumnIndex") int hiveColumnIndex, @JsonProperty("partitionKey") boolean partitionKey) { this.clientId = requireNonNull(clientId, "clientId is null"); this.name = requireNonNull(name, "name is null"); checkArgument(hiveColumnIndex >= 0 || partitionKey, "hiveColumnIndex is negative"); this.hiveColumnIndex = hiveColumnIndex; this.hiveType = requireNonNull(hiveType, "hiveType is null"); this.typeName = requireNonNull(typeSignature, "type is null"); this.partitionKey = partitionKey; }
@Test public void test() { VarcharType varcharType = VarcharType.createVarcharType(100); System.out.println(varcharType.getDisplayName()); System.out.println(varcharType.toString()); TypeSignature varcharSig = VarcharType.getParametrizedVarcharSignature("100"); System.out.println(varcharSig.toString()); }
private Type getType(List<OrcType> types, int index) { OrcType type = types.get(index); switch (type.getOrcTypeKind()) { case BOOLEAN: return BOOLEAN; case LONG: return BIGINT; case DOUBLE: return DOUBLE; case STRING: return VARCHAR; case BINARY: return VARBINARY; case LIST: TypeSignature elementType = getType(types, type.getFieldTypeIndex(0)).getTypeSignature(); return typeManager.getParameterizedType(StandardTypes.ARRAY, ImmutableList.of(elementType), ImmutableList.of()); case MAP: TypeSignature keyType = getType(types, type.getFieldTypeIndex(0)).getTypeSignature(); TypeSignature valueType = getType(types, type.getFieldTypeIndex(1)).getTypeSignature(); return typeManager.getParameterizedType(StandardTypes.MAP, ImmutableList.of(keyType, valueType), ImmutableList.of()); case STRUCT: ImmutableList.Builder<TypeSignature> fieldTypes = ImmutableList.builder(); for (int i = 0; i < type.getFieldCount(); i++) { fieldTypes.add(getType(types, type.getFieldTypeIndex(i)).getTypeSignature()); } return typeManager.getParameterizedType(StandardTypes.ROW, fieldTypes.build(), ImmutableList.copyOf(type.getFieldNames())); } throw new PrestoException(RAPTOR_ERROR, "Unhandled ORC type: " + type); }
public static Type getOutputType(@Nullable Method outputFunction, AccumulatorStateSerializer<?> serializer, TypeManager typeManager) { if (outputFunction == null) { return serializer.getSerializedType(); } else { return typeManager.getType(TypeSignature.parseTypeSignature(outputFunction.getAnnotation(OutputFunction.class).value())); } }
@Override public ScalarFunctionImplementation specialize(Map<String, Type> types, int arity, TypeManager typeManager, FunctionRegistry functionRegistry) { Type type = types.get("T"); TypeSignature arrayType = parameterizedTypeName(StandardTypes.ARRAY, type.getTypeSignature()); Signature signature = new Signature(FUNCTION_NAME, SCALAR, VARCHAR_TYPE_SIGNATURE, arrayType, VARCHAR_TYPE_SIGNATURE, VARCHAR_TYPE_SIGNATURE); return specializeArrayJoin(types, functionRegistry, ImmutableList.of(false, false, false), signature, METHOD_HANDLE); }
@Override public ScalarFunctionImplementation specialize(Map<String, Type> types, int arity, TypeManager typeManager, FunctionRegistry functionRegistry) { Type type = types.get("T"); TypeSignature arrayType = parameterizedTypeName(StandardTypes.ARRAY, type.getTypeSignature()); Signature signature = new Signature(FUNCTION_NAME, SCALAR, VARCHAR_TYPE_SIGNATURE, arrayType, VARCHAR_TYPE_SIGNATURE); return specializeArrayJoin(types, functionRegistry, ImmutableList.of(false, false), signature, METHOD_HANDLE); }
public Signature getCoercion(TypeSignature fromType, TypeSignature toType) { Signature signature = internalOperator(OperatorType.CAST.name(), toType, ImmutableList.of(fromType)); try { getScalarFunctionImplementation(signature); } catch (PrestoException e) { if (e.getErrorCode().getCode() == FUNCTION_IMPLEMENTATION_MISSING.toErrorCode().getCode()) { throw new OperatorNotFoundException(OperatorType.CAST, ImmutableList.of(fromType), toType); } throw e; } return signature; }
public static Signature getMagicLiteralFunctionSignature(Type type) { TypeSignature argumentType = typeForMagicLiteral(type).getTypeSignature(); return new Signature(MAGIC_LITERAL_FUNCTION_PREFIX + type.getTypeSignature(), SCALAR, type.getTypeSignature(), argumentType); }
private FunctionListBuilder operator(OperatorType operatorType, Type returnType, List<Type> parameterTypes, MethodHandle function, Optional<MethodHandle> instanceFactory, boolean nullable, List<Boolean> nullableArguments) { TypeSignature returnTypeSignature = returnType.getTypeSignature(); List<TypeSignature> argumentTypes = parameterTypes.stream() .map(Type::getTypeSignature) .collect(ImmutableCollectors.toImmutableList()); operatorType.validateSignature(returnTypeSignature, argumentTypes); functions.add(SqlOperator.create(operatorType, argumentTypes, returnTypeSignature, function, instanceFactory, nullable, nullableArguments)); return this; }
private static void verifyMethodSignature(Method method, TypeSignature returnTypeName, List<TypeSignature> argumentTypeNames, TypeManager typeManager) { Type returnType = typeManager.getType(returnTypeName); requireNonNull(returnType, "returnType is null"); List<Type> argumentTypes = resolveTypes(argumentTypeNames, typeManager); checkArgument(Primitives.unwrap(method.getReturnType()) == returnType.getJavaType(), "Expected method %s return type to be %s (%s)", method, returnType.getJavaType().getName(), returnType); // skip Session argument Class<?>[] parameterTypes = method.getParameterTypes(); Annotation[][] annotations = method.getParameterAnnotations(); if (parameterTypes.length > 0 && parameterTypes[0] == ConnectorSession.class) { parameterTypes = Arrays.copyOfRange(parameterTypes, 1, parameterTypes.length); annotations = Arrays.copyOfRange(annotations, 1, annotations.length); } for (int i = 0; i < parameterTypes.length; i++) { Class<?> actualType = parameterTypes[i]; Type expectedType = argumentTypes.get(i); boolean nullable = Arrays.asList(annotations[i]).stream().anyMatch(Nullable.class::isInstance); // Only allow boxing for functions that need to see nulls if (Primitives.isWrapperType(actualType)) { checkArgument(nullable, "Method %s has parameter with type %s that is missing @Nullable", method, actualType); } if (nullable) { checkArgument(!NON_NULLABLE_ARGUMENT_TYPES.contains(actualType), "Method %s has parameter type %s, but @Nullable is not supported on this type", method, actualType); } checkArgument(Primitives.unwrap(actualType) == expectedType.getJavaType(), "Expected method %s parameter %s type to be %s (%s)", method, i, expectedType.getJavaType().getName(), expectedType); } }
public OperatorNotFoundException(OperatorType operatorType, List<? extends TypeSignature> argumentTypes) { super(OPERATOR_NOT_FOUND, formatErrorMessage(operatorType, argumentTypes, Optional.empty())); this.operatorType = requireNonNull(operatorType, "operatorType is null"); this.returnType = null; this.argumentTypes = ImmutableList.copyOf(requireNonNull(argumentTypes, "argumentTypes is null")); }
public OperatorNotFoundException(OperatorType operatorType, List<? extends TypeSignature> argumentTypes, TypeSignature returnType) { super(OPERATOR_NOT_FOUND, formatErrorMessage(operatorType, argumentTypes, Optional.of(returnType))); this.operatorType = requireNonNull(operatorType, "operatorType is null"); this.argumentTypes = ImmutableList.copyOf(requireNonNull(argumentTypes, "argumentTypes is null")); this.returnType = requireNonNull(returnType, "returnType is null"); }
private static String formatErrorMessage(OperatorType operatorType, List<? extends TypeSignature> argumentTypes, Optional<TypeSignature> returnType) { String operatorString; switch (operatorType) { case BETWEEN: return format("Cannot check if %s is BETWEEN %s and %s", argumentTypes.get(0), argumentTypes.get(1), argumentTypes.get(2)); case CAST: operatorString = format("%s%s", operatorType.getOperator(), returnType.map(value -> " to " + value).orElse("")); break; default: operatorString = format("'%s'%s", operatorType.getOperator(), returnType.map(value -> ":" + value).orElse("")); } return format("%s cannot be applied to %s", operatorString, Joiner.on(", ").join(argumentTypes)); }
public static SqlOperator create( OperatorType operatorType, List<TypeSignature> argumentTypes, TypeSignature returnType, MethodHandle methodHandle, Optional<MethodHandle> instanceFactory, boolean nullable, List<Boolean> nullableArguments) { return new SimpleSqlOperator(operatorType, argumentTypes, returnType, methodHandle, instanceFactory, nullable, nullableArguments); }
private BytecodeNode cast(BytecodeGeneratorContext generatorContext, BytecodeNode argument, TypeSignature fromType, TypeSignature toType) { Signature function = generatorContext .getRegistry() .getCoercion(fromType, toType); // TODO: do we need a full function call? (nullability checks, etc) return generatorContext.generateCall(function.getName(), generatorContext.getRegistry().getScalarFunctionImplementation(function), ImmutableList.of(argument)); }
public static TypeSignature parameterizedTypeName(String base, TypeSignature... argumentNames) { ImmutableList.Builder<TypeSignatureParameter> parameters = ImmutableList.builder(); for (TypeSignature signature : argumentNames) { parameters.add(TypeSignatureParameter.of(signature)); } return new TypeSignature(base, parameters.build()); }
private static TypeSignature[] typeParameters(List<Type> argumentTypes, Type returnType) { requireNonNull(returnType, "returnType is null"); requireNonNull(argumentTypes, "argumentTypes is null"); ImmutableList.Builder<TypeSignature> builder = ImmutableList.builder(); argumentTypes.stream() .map(Type::getTypeSignature) .forEach(builder::add); builder.add(returnType.getTypeSignature()); List<TypeSignature> signatures = builder.build(); return signatures.toArray(new TypeSignature[signatures.size()]); }
@Override public Type getType(TypeSignature signature) { Type type = types.get(signature); if (type == null) { return instantiateParametricType(signature); } return type; }
@Override @Deprecated public Type getParameterizedType(String baseTypeName, List<TypeSignature> typeParameters, List<String> literalParameters) { if (baseTypeName.equals(StandardTypes.ROW)) { return getType(new TypeSignature(baseTypeName, typeParameters, literalParameters)); } return getParameterizedType( baseTypeName, typeParameters.stream().map(TypeSignatureParameter::of).collect(toList())); }
@Override public Optional<Type> getCommonSuperType(List<? extends Type> types) { checkArgument(!types.isEmpty(), "types is empty"); Optional<TypeSignature> commonSuperTypeSignature = getCommonSuperTypeSignature( types.stream() .map(Type::getTypeSignature) .collect(toImmutableList())); return commonSuperTypeSignature.map(this::getType); }
public static Optional<TypeSignature> getCommonSuperTypeSignature(List<? extends TypeSignature> typeSignatures) { checkArgument(!typeSignatures.isEmpty(), "typeSignatures is empty"); TypeSignature superTypeSignature = UNKNOWN.getTypeSignature(); for (TypeSignature typeSignature : typeSignatures) { Optional<TypeSignature> commonSuperTypeSignature = getCommonSuperTypeSignature(superTypeSignature, typeSignature); if (!commonSuperTypeSignature.isPresent()) { return Optional.empty(); } superTypeSignature = commonSuperTypeSignature.get(); } return Optional.of(superTypeSignature); }
@Test public void testDisplayName() { TypeManager typeManager = new TypeRegistry(); Type function = typeManager.getType(TypeSignature.parseTypeSignature("function<row<double>('field'),bigint>")); assertEquals(function.getDisplayName(), "function<row(field double),bigint>"); }
public ColumnInfo( int columnType, List<Integer> columnParameterTypes, TypeSignature columnTypeSignature, Nullable nullable, boolean currency, boolean signed, int precision, int scale, int columnDisplaySize, String columnLabel, String columnName, String tableName, String schemaName, String catalogName) { this.columnType = columnType; this.columnParameterTypes = ImmutableList.copyOf(requireNonNull(columnParameterTypes, "columnParameterTypes is null")); this.columnTypeSignature = requireNonNull(columnTypeSignature, "columnTypeName is null"); this.nullable = requireNonNull(nullable, "nullable is null"); this.currency = currency; this.signed = signed; this.precision = precision; this.scale = scale; this.columnDisplaySize = columnDisplaySize; this.columnLabel = requireNonNull(columnLabel, "columnLabel is null"); this.columnName = requireNonNull(columnName, "columnName is null"); this.tableName = requireNonNull(tableName, "tableName is null"); this.schemaName = requireNonNull(schemaName, "schemaName is null"); this.catalogName = requireNonNull(catalogName, "catalogName is null"); }
private static int getType(TypeSignature type) { if (type.getBase().equals("array")) { return Types.ARRAY; } switch (type.toString()) { case "boolean": return Types.BOOLEAN; case "bigint": return Types.BIGINT; case "double": return Types.DOUBLE; case "varchar": return Types.LONGNVARCHAR; case "varbinary": return Types.LONGVARBINARY; case "time": return Types.TIME; case "time with time zone": return Types.TIME; case "timestamp": return Types.TIMESTAMP; case "timestamp with time zone": return Types.TIMESTAMP; case "date": return Types.DATE; default: return Types.JAVA_OBJECT; } }
private static TypeSignature toTypeSignature(ClientTypeSignature signature) { List<TypeSignatureParameter> parameters = signature.getArguments().stream() .map(ClientTypeSignature::legacyClientTypeSignatureParameterToTypeSignatureParameter) .collect(toList()); return new TypeSignature(signature.getRawType(), parameters); }
@Nonnull private static TypeSignature getTypeSignature(TypeInfo typeInfo) { switch (typeInfo.getCategory()) { case PRIMITIVE: PrimitiveObjectInspector.PrimitiveCategory primitiveCategory = ((PrimitiveTypeInfo) typeInfo).getPrimitiveCategory(); Type primitiveType = getPrimitiveType(primitiveCategory); if (primitiveType == null) { break; } return primitiveType.getTypeSignature(); case MAP: MapTypeInfo mapTypeInfo = checkType(typeInfo, MapTypeInfo.class, "fieldInspector"); TypeSignature keyType = getTypeSignature(mapTypeInfo.getMapKeyTypeInfo()); TypeSignature valueType = getTypeSignature(mapTypeInfo.getMapValueTypeInfo()); return new TypeSignature( StandardTypes.MAP, ImmutableList.of(TypeSignatureParameter.of(keyType), TypeSignatureParameter.of(valueType))); case LIST: ListTypeInfo listTypeInfo = checkType(typeInfo, ListTypeInfo.class, "fieldInspector"); TypeSignature elementType = getTypeSignature(listTypeInfo.getListElementTypeInfo()); return new TypeSignature( StandardTypes.ARRAY, ImmutableList.of(TypeSignatureParameter.of(elementType))); case STRUCT: StructTypeInfo structTypeInfo = checkType(typeInfo, StructTypeInfo.class, "fieldInspector"); List<TypeSignature> fieldTypes = structTypeInfo.getAllStructFieldTypeInfos() .stream() .map(HiveType::getTypeSignature) .collect(toList()); return new TypeSignature(StandardTypes.ROW, fieldTypes, structTypeInfo.getAllStructFieldNames()); } throw new PrestoException(NOT_SUPPORTED, format("Unsupported Hive type: %s", typeInfo)); }
private static boolean columnTypeSupported(List<HiveColumnHandle> columns) { List<String> fields = columns.stream() .map(HiveColumnHandle::getTypeSignature) .map(TypeSignature::getBase) .filter(base -> StandardTypes.ARRAY.equals(base) || StandardTypes.MAP.equals(base) || StandardTypes.ROW.equals(base)) .collect(toList()); return fields.isEmpty(); }
public FieldType fromPrestoType(String name) { TypeSignature typeSignature = TypeSignature.parseTypeSignature(name); return PrestoQueryExecution.fromPrestoType(typeSignature.getBase(), typeSignature.getParameters().stream() .filter(param -> param.getKind() == TYPE) .map(param -> param.getTypeSignature().getBase()).iterator()); }