use of io.trino.spi.type.Type in project TiBigData by tidb-incubator.
the class TiDBPageSink method appendColumn.
private void appendColumn(Page page, int position, int channel) throws SQLException {
Block block = page.getBlock(channel);
int parameter = channel + 1;
if (block.isNull(position)) {
statement.setObject(parameter, null);
return;
}
Type type = columnTypes.get(channel);
switch(type.getDisplayName()) {
case "boolean":
statement.setBoolean(parameter, type.getBoolean(block, position));
break;
case "tinyint":
statement.setByte(parameter, SignedBytes.checkedCast(type.getLong(block, position)));
break;
case "smallint":
statement.setShort(parameter, Shorts.checkedCast(type.getLong(block, position)));
break;
case "integer":
statement.setInt(parameter, toIntExact(type.getLong(block, position)));
break;
case "bigint":
statement.setLong(parameter, type.getLong(block, position));
break;
case "real":
statement.setFloat(parameter, intBitsToFloat(toIntExact(type.getLong(block, position))));
break;
case "double":
statement.setDouble(parameter, type.getDouble(block, position));
break;
case "date":
// convert to midnight in default time zone
long utcMillis = DAYS.toMillis(type.getLong(block, position));
long localMillis = getInstanceUTC().getZone().getMillisKeepLocal(DateTimeZone.getDefault(), utcMillis);
statement.setDate(parameter, new Date(localMillis));
break;
case "varbinary":
statement.setBytes(parameter, type.getSlice(block, position).getBytes());
break;
default:
if (type instanceof DecimalType) {
statement.setBigDecimal(parameter, readBigDecimal((DecimalType) type, block, position));
} else if (type instanceof VarcharType || type instanceof CharType) {
statement.setString(parameter, type.getSlice(block, position).toStringUtf8());
} else if (type instanceof TimestampType) {
statement.setTimestamp(parameter, new Timestamp(type.getLong(block, position) / 1000 - TimeZone.getDefault().getRawOffset()));
} else if (type instanceof TimeType) {
statement.setTime(parameter, new Time(type.getLong(block, position)));
} else {
throw new TrinoException(NOT_SUPPORTED, "Unsupported column type: " + type.getDisplayName());
}
}
}
use of io.trino.spi.type.Type in project trino by trinodb.
the class MetadataManager method resolve.
@VisibleForTesting
public ResolvedFunction resolve(Session session, FunctionBinding functionBinding, FunctionMetadata functionMetadata, FunctionDependencyDeclaration declaration) {
Map<TypeSignature, Type> dependentTypes = declaration.getTypeDependencies().stream().map(typeSignature -> applyBoundVariables(typeSignature, functionBinding)).collect(toImmutableMap(Function.identity(), typeManager::getType, (left, right) -> left));
ImmutableSet.Builder<ResolvedFunction> functions = ImmutableSet.builder();
declaration.getFunctionDependencies().stream().map(functionDependency -> {
try {
List<TypeSignature> argumentTypes = applyBoundVariables(functionDependency.getArgumentTypes(), functionBinding);
return resolvedFunctionInternal(session, functionDependency.getName(), fromTypeSignatures(argumentTypes));
} catch (TrinoException e) {
if (functionDependency.isOptional()) {
return null;
}
throw e;
}
}).filter(Objects::nonNull).forEach(functions::add);
declaration.getOperatorDependencies().stream().map(operatorDependency -> {
try {
List<TypeSignature> argumentTypes = applyBoundVariables(operatorDependency.getArgumentTypes(), functionBinding);
return resolvedFunctionInternal(session, QualifiedName.of(mangleOperatorName(operatorDependency.getOperatorType())), fromTypeSignatures(argumentTypes));
} catch (TrinoException e) {
if (operatorDependency.isOptional()) {
return null;
}
throw e;
}
}).filter(Objects::nonNull).forEach(functions::add);
declaration.getCastDependencies().stream().map(castDependency -> {
try {
Type fromType = typeManager.getType(applyBoundVariables(castDependency.getFromType(), functionBinding));
Type toType = typeManager.getType(applyBoundVariables(castDependency.getToType(), functionBinding));
return getCoercion(session, fromType, toType);
} catch (TrinoException e) {
if (castDependency.isOptional()) {
return null;
}
throw e;
}
}).filter(Objects::nonNull).forEach(functions::add);
return new ResolvedFunction(functionBinding.getBoundSignature(), functionBinding.getFunctionId(), functionMetadata.getKind(), functionMetadata.isDeterministic(), functionMetadata.getFunctionNullability(), dependentTypes, functions.build());
}
use of io.trino.spi.type.Type in project trino by trinodb.
the class FunctionManager method verifyMethodHandleSignature.
private static void verifyMethodHandleSignature(BoundSignature boundSignature, FunctionInvoker functionInvoker, InvocationConvention convention) {
MethodHandle methodHandle = functionInvoker.getMethodHandle();
MethodType methodType = methodHandle.type();
checkArgument(convention.getArgumentConventions().size() == boundSignature.getArgumentTypes().size(), "Expected %s arguments, but got %s", boundSignature.getArgumentTypes().size(), convention.getArgumentConventions().size());
int expectedParameterCount = convention.getArgumentConventions().stream().mapToInt(InvocationArgumentConvention::getParameterCount).sum();
expectedParameterCount += methodType.parameterList().stream().filter(ConnectorSession.class::equals).count();
if (functionInvoker.getInstanceFactory().isPresent()) {
expectedParameterCount++;
}
checkArgument(expectedParameterCount == methodType.parameterCount(), "Expected %s method parameters, but got %s", expectedParameterCount, methodType.parameterCount());
int parameterIndex = 0;
if (functionInvoker.getInstanceFactory().isPresent()) {
verifyFunctionSignature(convention.supportsInstanceFactor(), "Method requires instance factory, but calling convention does not support an instance factory");
MethodHandle factoryMethod = functionInvoker.getInstanceFactory().orElseThrow();
verifyFunctionSignature(methodType.parameterType(parameterIndex).equals(factoryMethod.type().returnType()), "Invalid return type");
parameterIndex++;
}
int lambdaArgumentIndex = 0;
for (int argumentIndex = 0; argumentIndex < boundSignature.getArgumentTypes().size(); argumentIndex++) {
// skip session parameters
while (methodType.parameterType(parameterIndex).equals(ConnectorSession.class)) {
verifyFunctionSignature(convention.supportsSession(), "Method requires session, but calling convention does not support session");
parameterIndex++;
}
Class<?> parameterType = methodType.parameterType(parameterIndex);
Type argumentType = boundSignature.getArgumentTypes().get(argumentIndex);
InvocationArgumentConvention argumentConvention = convention.getArgumentConvention(argumentIndex);
switch(argumentConvention) {
case NEVER_NULL:
verifyFunctionSignature(parameterType.isAssignableFrom(argumentType.getJavaType()), "Expected argument type to be %s, but is %s", argumentType, parameterType);
break;
case NULL_FLAG:
verifyFunctionSignature(parameterType.isAssignableFrom(argumentType.getJavaType()), "Expected argument type to be %s, but is %s", argumentType.getJavaType(), parameterType);
verifyFunctionSignature(methodType.parameterType(parameterIndex + 1).equals(boolean.class), "Expected null flag parameter to be followed by a boolean parameter");
break;
case BOXED_NULLABLE:
verifyFunctionSignature(parameterType.isAssignableFrom(wrap(argumentType.getJavaType())), "Expected argument type to be %s, but is %s", wrap(argumentType.getJavaType()), parameterType);
break;
case BLOCK_POSITION:
verifyFunctionSignature(parameterType.equals(Block.class) && methodType.parameterType(parameterIndex + 1).equals(int.class), "Expected BLOCK_POSITION argument have parameters Block and int");
break;
case FUNCTION:
Class<?> lambdaInterface = functionInvoker.getLambdaInterfaces().get(lambdaArgumentIndex);
verifyFunctionSignature(parameterType.equals(lambdaInterface), "Expected function interface to be %s, but is %s", lambdaInterface, parameterType);
lambdaArgumentIndex++;
break;
default:
throw new UnsupportedOperationException("Unknown argument convention: " + argumentConvention);
}
parameterIndex += argumentConvention.getParameterCount();
}
Type returnType = boundSignature.getReturnType();
switch(convention.getReturnConvention()) {
case FAIL_ON_NULL:
verifyFunctionSignature(methodType.returnType().isAssignableFrom(returnType.getJavaType()), "Expected return type to be %s, but is %s", returnType.getJavaType(), methodType.returnType());
break;
case NULLABLE_RETURN:
verifyFunctionSignature(methodType.returnType().isAssignableFrom(wrap(returnType.getJavaType())), "Expected return type to be %s, but is %s", returnType.getJavaType(), wrap(methodType.returnType()));
break;
default:
throw new UnsupportedOperationException("Unknown return convention: " + convention.getReturnConvention());
}
}
use of io.trino.spi.type.Type in project trino by trinodb.
the class GlobalFunctionCatalog method checkNotSpecializedTypeOperator.
/**
* Type operators are handled automatically by the engine, so custom operator implementations
* cannot be registered for these.
*/
private static void checkNotSpecializedTypeOperator(Signature signature) {
String name = signature.getName();
if (!isOperatorName(name)) {
return;
}
OperatorType operatorType = unmangleOperator(name);
// The trick here is the Generic*Operator implementations implement these exact signatures,
// so we only these exact signatures to be registered. Since, only a single function with
// a specific signature can be registered, it prevents others from being registered.
Type expectedReturnType;
TypeVariableConstraint typeParameter;
switch(operatorType) {
case EQUAL:
case IS_DISTINCT_FROM:
case INDETERMINATE:
expectedReturnType = BOOLEAN;
typeParameter = comparableTypeParameter("T");
break;
case HASH_CODE:
case XX_HASH_64:
expectedReturnType = BIGINT;
typeParameter = comparableTypeParameter("T");
break;
case COMPARISON_UNORDERED_FIRST:
case COMPARISON_UNORDERED_LAST:
expectedReturnType = INTEGER;
typeParameter = orderableTypeParameter("T");
break;
case LESS_THAN:
case LESS_THAN_OR_EQUAL:
expectedReturnType = BOOLEAN;
typeParameter = orderableTypeParameter("T");
break;
default:
return;
}
Signature expectedSignature = new Signature(signature.getName(), ImmutableList.of(typeParameter), ImmutableList.of(), expectedReturnType.getTypeSignature(), Collections.nCopies(operatorType.getArgumentCount(), new TypeSignature("T")), false);
checkArgument(signature.equals(expectedSignature), "Can not register %s functionMetadata: %s", operatorType, signature);
}
use of io.trino.spi.type.Type in project trino by trinodb.
the class InternalBlockEncodingSerde method readType.
@Override
public Type readType(SliceInput sliceInput) {
requireNonNull(sliceInput, "sliceInput is null");
String id = readLengthPrefixedString(sliceInput);
Type type = types.apply(TypeId.of(id));
if (type == null) {
throw new IllegalArgumentException("Unknown type " + id);
}
return type;
}
Aggregations