builder.covers(varbinary, 0);
}
@Override
public TOverloadResult resultType() {
return TOverloadResult.custom(new TCustomOverloadResult() {
@Override
public TInstance resultInstance(List<TPreptimeValue> inputs, TPreptimeContext context) {
TInstance inputType = inputs.get(0).type();
int binaryLength = inputType.attribute(TBinary.Attrs.LENGTH);
int base64Length = (binaryLength * 4 + 2) / 3; // round up for ='s
return varchar.instance(base64Length, inputType.nullability());
}
});
}
@Override
protected void doEvaluate(TExecutionContext context,
LazyList<? extends ValueSource> inputs,
ValueTarget output) {
byte[] binary = inputs.get(0).getBytes();
output.putString(Strings.toBase64(binary), null);
}
};
TScalar string_to_base64 = new TScalarBase()
{
@Override
public String displayName() {
return "TO_BASE64";
}
@Override
protected void buildInputSets(TInputSetBuilder builder) {
builder.covers(varchar, 0);
}
@Override
public TOverloadResult resultType() {
return TOverloadResult.custom(new TCustomOverloadResult() {
@Override
public TInstance resultInstance(List<TPreptimeValue> inputs, TPreptimeContext context) {
TInstance inputType = inputs.get(0).type();
int stringLength = inputType.attribute(StringAttribute.MAX_LENGTH);
int encodedLength = (int)Math.ceil(stringLength * Charset.forName(StringFactory.Charset.of(inputType.attribute(StringAttribute.CHARSET))).newEncoder().maxBytesPerChar());
int base64Length = (encodedLength * 4 + 2) / 3; // round up for ='s
return varchar.instance(base64Length, inputType.nullability());
}
});
}
@Override
protected void doEvaluate(TExecutionContext context,
LazyList<? extends ValueSource> inputs,
ValueTarget output) {
String charset = StringFactory.Charset.of(context.inputTypeAt(0).attribute(StringAttribute.CHARSET));
String string = inputs.get(0).getString();
try {
byte[] binary = string.getBytes(charset);
output.putString(Strings.toBase64(binary), null);
}
catch (UnsupportedEncodingException ex)
{
context.warnClient(new InvalidParameterValueException("Unknown CHARSET: " + charset));
output.putNull();
}
}
};
TScalar from_base64 = new TScalarBase()
{
@Override
public String displayName() {
return "FROM_BASE64";
}
@Override
protected void buildInputSets(TInputSetBuilder builder) {
builder.covers(varchar, 0);
}
@Override
public TOverloadResult resultType() {
return TOverloadResult.custom(new TCustomOverloadResult() {
@Override
public TInstance resultInstance(List<TPreptimeValue> inputs, TPreptimeContext context) {
TInstance inputType = inputs.get(0).type();
int stringLength = inputType.attribute(StringAttribute.MAX_LENGTH);
int binaryLength = stringLength / 4 * 3;