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);
}
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);
}
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);
}
Aggregations