@Override public Slice getSlice() { return wrappedBuffer(value); }
@Override public Block deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { byte[] decoded = Base64.getDecoder().decode(jsonParser.readValueAs(String.class)); return readBlock(blockEncodingSerde, Slices.wrappedBuffer(decoded)); } }
@Override protected Object getNonNullValue() { return Slices.wrappedBuffer(new byte[16]); }
private static Slice[] createTestValue(int positionCount) { Slice[] expectedValues = new Slice[positionCount]; for (int position = 0; position < positionCount; position++) { expectedValues[position] = Slices.wrappedBuffer((byte) position); } return expectedValues; }
@Override public void encodeValueInto(int depth, Block block, int position, SliceOutput output) { Slice slice = type.getSlice(block, position); byte[] data = slice.getBytes(); slice = Slices.wrappedBuffer(base64Encoder.encode(data)); output.writeBytes(slice); }
@Description("encode binary data as base64") @ScalarFunction @SqlType(StandardTypes.VARCHAR) public static Slice toBase64(@SqlType(StandardTypes.VARBINARY) Slice slice) { return Slices.wrappedBuffer(Base64.getEncoder().encode(slice.getBytes())); }
@Override protected Object getGreaterValue(Object value) { byte[] address = ((Slice) value).getBytes(); checkState(++address[address.length - 1] != 0, "Last byte of address is 0xff"); return Slices.wrappedBuffer(address); }
@Override protected Object getNonNullValue() { return Slices.wrappedBuffer(InetAddresses.forString("::").getAddress()); }
@Description("encode binary data as base64 using the URL safe alphabet") @ScalarFunction("to_base64url") @SqlType(StandardTypes.VARCHAR) public static Slice toBase64Url(@SqlType(StandardTypes.VARBINARY) Slice slice) { return Slices.wrappedBuffer(Base64.getUrlEncoder().encode(slice.getBytes())); }
private static void assertByteCountWithoutTrailingSpace(byte[] actual, int offset, int length, byte[] expected) { Slice slice = wrappedBuffer(actual); int trimmedLength = byteCountWithoutTrailingSpace(slice, offset, length); byte[] bytes = slice.getBytes(offset, trimmedLength); assertEquals(bytes, expected); }
@ScalarOperator(CAST) @SqlType(StandardTypes.HYPER_LOG_LOG) public static Slice castToHyperLogLog(@SqlType(SetDigestType.NAME) Slice slice) { checkArgument(slice.getByte(0) == 1, "Legacy version of SetDigest cannot cast to HyperLogLog"); int hllLength = slice.getInt(SIZE_OF_BYTE); return Slices.wrappedBuffer(slice.getBytes(SIZE_OF_BYTE + SIZE_OF_INT, hllLength)); } }
@Description("Compute HMAC with SHA512") @ScalarFunction @SqlType(StandardTypes.VARBINARY) public static Slice hmacSha512(@SqlType(StandardTypes.VARBINARY) Slice slice, @SqlType(StandardTypes.VARBINARY) Slice key) { return wrappedBuffer(Hashing.hmacSha512(key.getBytes()).hashBytes(slice.getBytes()).asBytes()); } }
@Description("decode URL safe base64 encoded binary data") @ScalarFunction("from_base64url") @SqlType(StandardTypes.VARBINARY) public static Slice fromBase64UrlVarbinary(@SqlType(StandardTypes.VARBINARY) Slice slice) { try { return Slices.wrappedBuffer(Base64.getUrlDecoder().decode(slice.getBytes())); } catch (IllegalArgumentException e) { throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e); } }
@ScalarFunction(deterministic = false) // if not non-deterministic, constant folding code accidentally fix invalid characters @SqlType(StandardTypes.VARCHAR) public static Slice invalidUtf8() { return Slices.wrappedBuffer(new byte[] { // AAA\uD800AAAA\uDFFFAAA, D800 and DFFF are valid unicode code points, but not valid UTF8 (byte) 0x41, 0x41, (byte) 0xed, (byte) 0xa0, (byte) 0x80, 0x41, 0x41, 0x41, 0x41, (byte) 0xed, (byte) 0xbf, (byte) 0xbf, 0x41, 0x41, 0x41, }); }
@Description("Returns the Well-Known Binary (WKB) representation of the geometry") @ScalarFunction("ST_AsBinary") @SqlType(VARBINARY) public static Slice stAsBinary(@SqlType(GEOMETRY_TYPE_NAME) Slice input) { return wrappedBuffer(deserialize(input).asBinary()); }
private static void verifySlice() { Slice slice = Slices.wrappedBuffer(new byte[5]); slice.setByte(4, 0xDE); slice.setByte(3, 0xAD); slice.setByte(2, 0xBE); slice.setByte(1, 0xEF); if (slice.getInt(1) != 0xDEADBEEF) { failRequirement("Slice library produced an unexpected result"); } }
private static void assertLike(byte[] value, String pattern, boolean expected) { Expression predicate = new LikePredicate( rawStringLiteral(Slices.wrappedBuffer(value)), new StringLiteral(pattern), Optional.empty()); assertEquals(evaluate(predicate), expected); }
public static FeatureUnitNormalizer deserialize(byte[] modelData) { SliceInput input = Slices.wrappedBuffer(modelData).getInput(); FeatureUnitNormalizer model = new FeatureUnitNormalizer(); while (input.isReadable()) { int key = input.readInt(); model.mins.put(key, input.readDouble()); model.maxs.put(key, input.readDouble()); } return model; }
@Test public void testHashCode() { Slice data = Slices.wrappedBuffer(ALL_BYTES); Block block = VARBINARY.createBlockBuilder(null, 1, ALL_BYTES.length) .writeBytes(data, 0, data.length()) .closeEntry() .build(); assertEquals(VarbinaryOperators.hashCode(data), VARBINARY.hash(block, 0)); }
@Test public void testExpressionConstantFolding() { Expression originalExpression = comparison(GREATER_THAN, C_VARBINARY.toSymbolReference(), function("from_hex", stringLiteral("123456"))); ExtractionResult result = fromPredicate(originalExpression); assertEquals(result.getRemainingExpression(), TRUE_LITERAL); Slice value = Slices.wrappedBuffer(BaseEncoding.base16().decode("123456")); assertEquals(result.getTupleDomain(), withColumnDomains(ImmutableMap.of(C_VARBINARY, Domain.create(ValueSet.ofRanges(Range.greaterThan(VARBINARY, value)), false)))); Expression expression = toPredicate(result.getTupleDomain()); assertEquals(expression, comparison(GREATER_THAN, C_VARBINARY.toSymbolReference(), varbinaryLiteral(value))); }