16.20 - Example: SCRIPT Calls Python for Data Type Conversion - Advanced SQL Engine - Teradata Database

Teradata Vantage™ - SQL Operators and User-Defined Functions

Product
Advanced SQL Engine
Teradata Database
Release Number
16.20
Release Date
April 2020
Content Type
Administration
Programming Reference
Publication ID
B035-1210-162K
Language
English (United States)

The following example shows the script table operator returning a BYTE and other data types in the return value list:

SELECT * FROM SCRIPT(
ON script_syn_res_tab001
SCRIPT_COMMAND('cat')
DELIMITER(',')
RETURNS ('uid int', 'helen BYTE', 'word varchar(10)', 'id int')
) as srpt;

In the following Python script, the print command returns HEX numbers for a BYTE (20) return value.

The 'mapper5.py' script:

#!/usr/bin/python
import sys
print '040A1b202EC4a256406F7D7Ffffcf3 33 22.4 abc'

Save the mapper5.py script in /home/tdatuser or modify the directory location in the following SQL.

Test the script:

Select * from SCRIPT(SCRIPT_COMMAND ('/home/tdatuser/mapper5.py' ) returns ('v byte(20)', 'v2
int', 'v3 float', 'v4 varchar(10)') ) as d1;

Result:

v v2 v3 v4
040A1B202EC4A256406F7D7FFFFCF30000000000 33 2.24000000000000E 001 abc
040A1B202EC4A256406F7D7FFFFCF30000000000 33 2.24000000000000E 001 abc
040A1B202EC4A256406F7D7FFFFCF30000000000 33 2.24000000000000E 001 abc
040A1B202EC4A256406F7D7FFFFCF30000000000 33 2.24000000000000E 001 abc

In the example, the delimiter is a tab. You can specify another character as the delimiter; for example, to specify a comma as the delimiter:

#!/usr/bin/python
import sys
print '040A1b202EC4a256406F7D7Ffffcf3,33,22.4,abc'
Change the SELECT statement accordingly:
SELECT * from SCRIPT(SCRIPT_COMMAND ('/home/tdatuser/mapper5.py' ) delimiter(',')
returns ('v byte(20)', 'v2 int', 'v3 float', 'v4 varchar(10)') ) as d1;