Search in sources :

Example 1 with JsonGeneratorWriter

use of io.trino.util.JsonUtil.JsonGeneratorWriter in project trino by trinodb.

the class MapToJsonCast method specialize.

@Override
public ScalarFunctionImplementation specialize(BoundSignature boundSignature) {
    MapType mapType = (MapType) boundSignature.getArgumentType(0);
    Type keyType = mapType.getKeyType();
    Type valueType = mapType.getValueType();
    checkCondition(canCastToJson(mapType), INVALID_CAST_ARGUMENT, "Cannot cast %s to JSON", mapType);
    ObjectKeyProvider provider = ObjectKeyProvider.createObjectKeyProvider(keyType);
    JsonGeneratorWriter writer = JsonGeneratorWriter.createJsonGeneratorWriter(valueType, legacyRowToJson);
    MethodHandle methodHandle = METHOD_HANDLE.bindTo(provider).bindTo(writer);
    return new ChoicesScalarFunctionImplementation(boundSignature, FAIL_ON_NULL, ImmutableList.of(NEVER_NULL), methodHandle);
}
Also used : JsonGeneratorWriter(io.trino.util.JsonUtil.JsonGeneratorWriter) Type(io.trino.spi.type.Type) MapType(io.trino.spi.type.MapType) OperatorType(io.trino.spi.function.OperatorType) TypeSignature.mapType(io.trino.spi.type.TypeSignature.mapType) ObjectKeyProvider(io.trino.util.JsonUtil.ObjectKeyProvider) MapType(io.trino.spi.type.MapType) MethodHandle(java.lang.invoke.MethodHandle)

Example 2 with JsonGeneratorWriter

use of io.trino.util.JsonUtil.JsonGeneratorWriter in project trino by trinodb.

the class ArrayToJsonCast method specialize.

@Override
protected ScalarFunctionImplementation specialize(BoundSignature boundSignature) {
    ArrayType arrayType = (ArrayType) boundSignature.getArgumentTypes().get(0);
    checkCondition(canCastToJson(arrayType), INVALID_CAST_ARGUMENT, "Cannot cast %s to JSON", arrayType);
    JsonGeneratorWriter writer = JsonGeneratorWriter.createJsonGeneratorWriter(arrayType.getElementType(), legacyRowToJson);
    MethodHandle methodHandle = METHOD_HANDLE.bindTo(writer);
    return new ChoicesScalarFunctionImplementation(boundSignature, FAIL_ON_NULL, ImmutableList.of(NEVER_NULL), methodHandle);
}
Also used : ArrayType(io.trino.spi.type.ArrayType) JsonGeneratorWriter(io.trino.util.JsonUtil.JsonGeneratorWriter) MethodHandle(java.lang.invoke.MethodHandle)

Example 3 with JsonGeneratorWriter

use of io.trino.util.JsonUtil.JsonGeneratorWriter in project trino by trinodb.

the class RowToJsonCast method specialize.

@Override
protected ScalarFunctionImplementation specialize(BoundSignature boundSignature) {
    Type type = boundSignature.getArgumentType(0);
    checkCondition(canCastToJson(type), INVALID_CAST_ARGUMENT, "Cannot cast %s to JSON", type);
    List<Type> fieldTypes = type.getTypeParameters();
    List<JsonGeneratorWriter> fieldWriters = new ArrayList<>(fieldTypes.size());
    MethodHandle methodHandle;
    if (legacyRowToJson) {
        for (Type fieldType : fieldTypes) {
            fieldWriters.add(createJsonGeneratorWriter(fieldType, true));
        }
        methodHandle = LEGACY_METHOD_HANDLE.bindTo(fieldWriters);
    } else {
        List<TypeSignatureParameter> typeSignatureParameters = type.getTypeSignature().getParameters();
        List<String> fieldNames = new ArrayList<>(fieldTypes.size());
        for (int i = 0; i < fieldTypes.size(); i++) {
            fieldNames.add(typeSignatureParameters.get(i).getNamedTypeSignature().getName().orElse(""));
            fieldWriters.add(createJsonGeneratorWriter(fieldTypes.get(i), false));
        }
        methodHandle = METHOD_HANDLE.bindTo(fieldNames).bindTo(fieldWriters);
    }
    return new ChoicesScalarFunctionImplementation(boundSignature, FAIL_ON_NULL, ImmutableList.of(NEVER_NULL), methodHandle);
}
Also used : JsonGeneratorWriter(io.trino.util.JsonUtil.JsonGeneratorWriter) JsonGeneratorWriter.createJsonGeneratorWriter(io.trino.util.JsonUtil.JsonGeneratorWriter.createJsonGeneratorWriter) Type(io.trino.spi.type.Type) OperatorType(io.trino.spi.function.OperatorType) TypeSignatureParameter(io.trino.spi.type.TypeSignatureParameter) ArrayList(java.util.ArrayList) TypeVariableConstraint(io.trino.metadata.TypeVariableConstraint) MethodHandle(java.lang.invoke.MethodHandle)

Aggregations

JsonGeneratorWriter (io.trino.util.JsonUtil.JsonGeneratorWriter)3 MethodHandle (java.lang.invoke.MethodHandle)3 OperatorType (io.trino.spi.function.OperatorType)2 Type (io.trino.spi.type.Type)2 TypeVariableConstraint (io.trino.metadata.TypeVariableConstraint)1 ArrayType (io.trino.spi.type.ArrayType)1 MapType (io.trino.spi.type.MapType)1 TypeSignature.mapType (io.trino.spi.type.TypeSignature.mapType)1 TypeSignatureParameter (io.trino.spi.type.TypeSignatureParameter)1 JsonGeneratorWriter.createJsonGeneratorWriter (io.trino.util.JsonUtil.JsonGeneratorWriter.createJsonGeneratorWriter)1 ObjectKeyProvider (io.trino.util.JsonUtil.ObjectKeyProvider)1 ArrayList (java.util.ArrayList)1