Created
November 18, 2024 21:07
-
-
Save DenisCarriere/5f51d88eb2ef38697c99e70719e50df1 to your computer and use it in GitHub Desktop.
Snowflake to UInt256
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Modified BYTES_TO_BIG_ENDIAN_HEX function | |
CREATE OR REPLACE FUNCTION BYTES_TO_BIG_ENDIAN_HEX(hex_str STRING) | |
RETURNS STRING | |
LANGUAGE PYTHON | |
RUNTIME_VERSION = '3.8' | |
HANDLER = 'bytes_to_big_endian_hex' | |
AS | |
$$ | |
def bytes_to_big_endian_hex(hex_str): | |
# Validate input length (must be even) | |
if len(hex_str) % 2 != 0: | |
raise ValueError("Hex string length must be even.") | |
# Split the hex string into bytes (2 hex digits per byte) | |
byte_list = [hex_str[i:i+2] for i in range(0, len(hex_str), 2)] | |
# Reverse the byte order to convert from little-endian to big-endian | |
byte_list_rev = byte_list[::-1] | |
# Join the bytes back into a hex string | |
big_endian_hex = ''.join(byte_list_rev) | |
# Ensure the output is 64 characters (32 bytes) by padding with leading zeros | |
big_endian_hex = big_endian_hex.rjust(64, '0') | |
return big_endian_hex | |
$$; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- HEX_TO_DECIMAL function remains the same | |
CREATE OR REPLACE FUNCTION HEX_TO_DECIMAL(hex_str STRING) | |
RETURNS STRING | |
LANGUAGE PYTHON | |
RUNTIME_VERSION = '3.8' | |
HANDLER = 'hex_to_decimal' | |
AS | |
$$ | |
def hex_to_decimal(hex_str): | |
# Remove leading zeros to prevent issues with large numbers | |
hex_str = hex_str.lstrip('0') or '0' | |
# Convert the hex string to an integer | |
return int(hex_str, 16) | |
$$; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment