AWS Lambda Function
# hash_256.py
# A simple lambda function to concatenate scalar UDCF input
# Returns a sha256 hexdigest hash of the concatenated arguments
from hashlib import sha256
# map the column to a string type
def transform(args):
nargs = len(args)
# concatenate all the arguments together
result = ''.join(map(str, args))
# hash the result into a hexdigest string prefix
hash = sha256(result.encode())
return hash.hexdigest()[:7]
# concatenate arguments of the scalar udcf into a single string
def lambda_handler(event, context):
# the data key is part of the default JSON format in the event dictionary
assert "data" in event
# scalar udcfs only pass a single row to the lambda function
rows = event['data']
assert len(rows) == 1
# the row contains arguments passed to the scalar udcf in the SQL query
row = rows[0]
assert len(row) > 1
# the first element of the row is a virtual index value
index = row[0]
# the return value of the lambda function that follows the default JSON format
output = {"data": []}
# the index is needed as the first element in the output row
# to follow the default JSON format
output_row = [index]
# process the arguments
result = transform(row[1:])
# add the final result to the output row
output_row.append(result)
output["data"].append(output_row)
return output
Output
See the output for testing the lambda_handler:
event = {
"data": [ [0, ‘test’] ]
}
{'data': [[0, '9f86d08']]}
UDCF Execution
BTEQ -- Enter your SQL request or BTEQ command: INSERT INTO demo_table VALUES ( 1, 'test' ); *** Insert completed. One row added. *** Total elapsed time was 1 second. INSERT INTO demo_table VALUES ( 2, 'hello' ); *** Insert completed. One row added. *** Total elapsed time was 1 second. INSERT INTO demo_table VALUES ( 3, 'world' ); *** Insert completed. One row added. *** Total elapsed time was 1 second. BTEQ -- Enter your SQL request or BTEQ command: select sha_256(col2) from demo_table; *** Query completed. 3 rows found. One column returned. *** Total elapsed time was 1 second. sha_256(col2) ---------- 9f86d08 2cf24db 486ea46