Skip to content

Commit

Permalink
Introduce utility class for encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe-Abraham committed May 8, 2024
1 parent 59797b0 commit cab5865
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 36 deletions.
56 changes: 20 additions & 36 deletions velox/common/encode/tests/Base64Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<TestCase> 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
19 changes: 19 additions & 0 deletions velox/docs/functions/presto/binary.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit cab5865

Please sign in to comment.