add/udf_int_to_binary-and-binary-to_int (#87)

* add binary udf

* add binary to int

* name change

* return str add

* add udf tests

* udf test big int

* test name fix

* bin_to_int big int update

* add large-num

* swap int_to_binary to string sig

* int_to_binary function fix
This commit is contained in:
Matt Romano 2024-02-28 13:47:41 -08:00 committed by GitHub
parent 992947a4ea
commit 883675b402
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 99 additions and 0 deletions

View File

@ -229,4 +229,53 @@ def transform_hex_to_bech32(hex, hrp=''):
return hrp + '1' + ''.join([CHARSET[d] for d in data5bit + checksum])
{% endmacro %}
{% macro create_udf_int_to_binary() %}
def int_to_binary(num):
num = int(num)
is_negative = num < 0
if is_negative:
num = -num
binary_string = bin(num)[2:]
if is_negative:
inverted_string = "".join("1" if bit == "0" else "0" for bit in binary_string)
carry = 1
result = ""
for i in range(len(inverted_string) - 1, -1, -1):
if inverted_string[i] == "1" and carry == 1:
result = "0" + result
elif inverted_string[i] == "0" and carry == 1:
result = "1" + result
carry = 0
else:
result = inverted_string[i] + result
binary_string = result
return binary_string
{% endmacro %}
{% macro create_udf_binary_to_int() %}
def binary_to_int(binary):
for char in binary:
if char not in "01":
raise ValueError("Input string must be a valid binary string.")
integer = 0
for i, digit in enumerate(binary[::-1]):
digit_int = int(digit)
integer += digit_int * 2**i
return str(integer)
{% endmacro %}

View File

@ -272,4 +272,26 @@
sql: |
{{ create_udf_hex_to_bech32() | indent(4) }}
- name: {{ schema }}.udf_int_to_binary
signature:
- [num, STRING]
return_type: TEXT
options: |
LANGUAGE PYTHON
RUNTIME_VERSION = '3.8'
HANDLER = 'int_to_binary'
sql: |
{{ create_udf_int_to_binary() | indent(4) }}
- name: {{ schema }}.udf_binary_to_int
signature:
- [binary, STRING]
return_type: TEXT
options: |
LANGUAGE PYTHON
RUNTIME_VERSION = '3.8'
HANDLER = 'binary_to_int'
sql: |
{{ create_udf_binary_to_int() | indent(4) }}
{% endmacro %}

View File

@ -197,6 +197,34 @@ models:
[['array', [0, 1, 2]]]
assertions:
- result = 'array=%5B0%2C+1%2C+2%5D'
- name: udf_int_to_binary
tests:
- test_udf:
name: test_utils__udf_int_to_binary
args: 123456789
assertions:
- result = '111010110111100110100010101'
- name: udf_int_to_binary
tests:
- test_udf:
name: test_utils__udf_int_to_binary_large_number
args: "'123456789123456789123456789123456789123456789'"
assertions:
- result = '101100010010011011011100101001111010001001110011010111111101111101010111011001001101000001111110001010100001011011010000100000001000101111100010101'
- name: udf_binary_to_int
tests:
- test_udf:
name: test_utils__udf_binary_to_int
args: '111010110111100110100010101'
assertions:
- result = '123456789'
- name: udf_binary_to_int
tests:
- test_udf:
name: test_utils__udf_binary_to_int_large_number
args: "'110110110100110110100101110101100110100000101111100010101'"
assertions:
- result = '123456789123456789'
- name: udf_evm_decode_log
tests:
- test_udf: