diff --git a/velox/common/encode/tests/Base64Test.cpp b/velox/common/encode/tests/Base64Test.cpp index 15556583c7519..db63d8006a138 100644 --- a/velox/common/encode/tests/Base64Test.cpp +++ b/velox/common/encode/tests/Base64Test.cpp @@ -46,44 +46,28 @@ TEST_F(Base64Test, fromBase64) { } TEST_F(Base64Test, calculateDecodedSizeProperSize) { - size_t encoded_size{0}; + struct TestCase { + std::string inputBase64; + size_t initialEncodedSize; + size_t expectedDecodedSize; + size_t expectedEncodedSizeAfter; + }; - encoded_size = 20; - EXPECT_EQ( - 13, Base64::calculateDecodedSize("SGVsbG8sIFdvcmxkIQ==", encoded_size)); - EXPECT_EQ(18, encoded_size); - - encoded_size = 18; - EXPECT_EQ( - 13, Base64::calculateDecodedSize("SGVsbG8sIFdvcmxkIQ", encoded_size)); - EXPECT_EQ(18, encoded_size); - - encoded_size = 21; - EXPECT_THROW( - Base64::calculateDecodedSize("SGVsbG8sIFdvcmxkIQ==", encoded_size), - facebook::velox::encoding::Base64Exception); - - encoded_size = 32; - EXPECT_EQ( - 23, - Base64::calculateDecodedSize( - "QmFzZTY0IGVuY29kaW5nIGlzIGZ1bi4=", encoded_size)); - EXPECT_EQ(31, encoded_size); - - encoded_size = 31; - EXPECT_EQ( - 23, - Base64::calculateDecodedSize( - "QmFzZTY0IGVuY29kaW5nIGlzIGZ1bi4", encoded_size)); - EXPECT_EQ(31, encoded_size); - - encoded_size = 16; - EXPECT_EQ(10, Base64::calculateDecodedSize("MTIzNDU2Nzg5MA==", encoded_size)); - EXPECT_EQ(14, encoded_size); + std::vector testCases{ + {"SGVsbG8sIFdvcmxkIQ==", 20, 13, 18}, + {"SGVsbG8sIFdvcmxkIQ", 18, 13, 18}, + {"QmFzZTY0IGVuY29kaW5nIGlzIGZ1bi4=", 32, 23, 31}, + {"QmFzZTY0IGVuY29kaW5nIGlzIGZ1bi4", 31, 23, 31}, + {"MTIzNDU2Nzg5MA==", 16, 10, 14}, + {"MTIzNDU2Nzg5MA", 14, 10, 14}}; - encoded_size = 14; - EXPECT_EQ(10, Base64::calculateDecodedSize("MTIzNDU2Nzg5MA", encoded_size)); - EXPECT_EQ(14, encoded_size); + for (const auto& testCase : testCases) { + size_t encodedSize = testCase.initialEncodedSize; + size_t decodedSize = + Base64::calculateDecodedSize(testCase.inputBase64.c_str(), encodedSize); + EXPECT_EQ(testCase.expectedDecodedSize, decodedSize); + EXPECT_EQ(testCase.expectedEncodedSizeAfter, encodedSize); + } } } // namespace facebook::velox::encoding diff --git a/velox/docs/functions/presto/binary.rst b/velox/docs/functions/presto/binary.rst index 512ce19dcfbef..cea076cd73b68 100644 --- a/velox/docs/functions/presto/binary.rst +++ b/velox/docs/functions/presto/binary.rst @@ -137,6 +137,25 @@ Binary Functions Encodes ``bigint`` in a 64-bit 2’s complement big endian format. +.. function:: to_base32(varbinary) -> string + + Encodes a binary ``varbinary`` value into its Base32 string representation. + This function generates padded Base32 strings by default. + + Examples + -------- + Query to encode a binary value to a padded Base32 string: + :: + SELECT to_base32(ARRAY[72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]); -- 'JBSWY3DPEBLW64TMMQ======' + + Query to encode a binary value with fewer bytes: + :: + SELECT to_base32(ARRAY[104, 101, 108, 108, 111]); -- 'NBSWY3DP' + + In the above examples, the binary array `[72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]` is encoded to the padded Base32 string 'JBSWY3DPEBLW64TMMQ======'. + The binary array `[104, 101, 108, 108, 111]` is encoded to 'NBSWY3DP'. + + .. function:: to_hex(binary) -> varchar Encodes ``binary`` into a hex string representation.