SnowConvert AI - Teradata - BTEQ¶
Translation references to convert Teradata BTEQ files to Python
Basic Teradata Query (BTEQ) is a general-purpose, command-based program that enables users on a workstation to communicate with one or more Teradata Database systems, and to format reports for both print and screen output.
To simulate the BTEQ functionality for Teradata in Snowflake, BTEQ files and commands are transformed to Python code, similar to the transformations performed for MultiLoad and FastLoad scripts. The generated code uses the Snowflake Python project called snowconvert.helpers which contains the required functions to simulate the BTEQ statements in Snowflake.
BTEQ Commands Translation¶
The following table presents the conversion for the BTEQ commands to Snowflake.
Teradata |
Snowflake |
Notes |
|---|---|---|
ERRORCODE != 0 |
snowconvert.helpers.error_code != 0 |
|
.EXPORT DATA FILE=fileName |
Export.report(“fileName”, “,”) |
The function has no functionality |
.EXPORT INDICDATA FILE=fileName |
Export.report(“fileName”, “,”) |
The function has no functionality |
.EXPORT REPORT FILE=fileName |
Export.report(“fileName”, “,”) |
The function has no functionality |
.EXPORT DIF FILE=fileName |
Export.report(“fileName”, “,”) |
The function has no functionality |
.EXPORT RESET |
Export.reset() |
The function has no functionality |
.IF ERRORCODE != 0 THEN .QUIT ERRORCODE |
If snowconvert.helpers.error_code != 0: snowconvert.helpers.quit_application (snowconvert.helpers.error_code) |
|
.IMPORT RESET |
snowconvert.helpers.import_reset() |
The function has no functionality |
.LABEL newLabel |
def NEWLABEL(): |
|
.LOGOFF |
The statement is commented |
|
.LOGON |
The statement is commented |
|
.LOGMECH |
The statement is commented |
|
.OS /fs/fs01/bin/filename.sh ‘load’ |
snowconvert.helpers.os(“”/fs/fs01/bin/filename.sh ‘load’ “”) |
|
.RUN FILE=newFile |
for statement in snowconvert.helpers.readrun(“newFile”): eval(statement) |
|
.SET DEFAULTS |
Export.defaults() |
The function has no functionality |
.SET ERRORLEVEL 3807 SEVERITY 0 |
snowconvert.helpers.set_error_level(3807, 0) |
|
.SET RECORMODE OFF |
Export.record_mode(False) |
|
.SET RECORMODE ON |
Export.record_mode(True) |
|
.SET SEPARATOR ‘|’ |
Export.separator_string(‘|’) |
The function has no functionality |
.SET WIDTH 120 |
Export.width(120) |
The function has no functionality |
.Remark “”Hello world!””” |
snowconvert.helpers.remark(r””””””Hello world!””””””) |
|
.QUIT ERRORCODE |
snowconvert.helpers.quit_application( snowconvert.helpers.error_code ) |
|
.QUIT |
snowconvert.helpers.quit_application() |
|
SQL statements |
exec(statement) |
|
$(<$INPUT_SQL_FILE) |
exec_file(“$INPUT_SQL_FILE”) |
|
= (Repeat previous command) |
snowconvert.helpers.repeat_previous_sql_statement(con) |
For more complicated statements presented in the previous table, subsections with detailed examples are provided below.
.GOTO Conversion¶
Since we are converting BTEQ scripts to Python, certain structures that are valid in BTEQ are not inherently supported in Python. This is the case for the .GOTO command using the .Label commands.
For this reason, an alternative has been developed so that the functionality of these commands can be emulated, turning the .Label commands into functions with subsequent call statements.
Check the following code:
In the example above, there were five commands. Two of them were.Labelcommands. The commandFIRSTLABELwas transformed into a function with the statement(s) that follow it below until another.LABELcommand is found. When another label is called (in this case, SECONDLABEL), that call ends the first function and starts a new one.
If we were to migrate the previous example, the result would be:
Notice there is a call to the functionFIRSTLABEL, this function has only one statement, which would be the only non-label command that followsFIRSTLABELin the original code. Before theFIRSTLABELfunction ends, it calls SECONDLABEL, with the statements that followed it.
Notes:
Creating a connector variable
con = None, and populating it in themain()function:con = snowconvert.helpers.log_on().Setting up a log:
snowconvert.helpers.configure_log().
Execute Query Statements¶
Every SQL statement found in a BTEQ file will be executed through theexecfunction provided by the snowconvert.helpers. Take for example the following code:
This is converted to:
Execute Script Files¶
Files that contain a user’s BTEQ commands and Teradata SQL statements are called scripts, run files, macros, or stored procedures. For example, create a file called SAMPFILE, and enter the following BTEQ script:
To execute the run file, enter either form of the BTEQ RUN command:
If you convert the second code, the result is the following:
The snowconvert.helpers.readrun("sampfile") will return each line from the SAMPFILE and in theFORstatement, each one of the lines will be passed to the eval function, a method that parses the expression passed to it and runs python expression (the SAMPFILE should be converted to work) within the program.
Execute SQL Files¶
In some instances during the execution of a BTEQ file a SQL file can be found, take for example the SQL file called NEWSQL:
This can be executed during a script with the following line:
And after the conversion of the script the result is:
The exec_file helper function will read each line from the NEWSQL file and then use the exec function as explained in the section Execute query statement.
Known Issues ¶
No issues were found.
REPEAT¶
Translation specification for the REPEAT statement.
Note
Some parts in the output code are omitted for clarity reasons.
As per Teradata’s documentation, the REPEAT statement enables users to specify the maximum number of times the next SQL request is to be submitted. Note that a SQL request can be a single or multi-statement request. This is defined by the position of the semicolons for each statement following the REPEAT statement.
Syntax¶
Sample Source Patterns¶
With this input data:
inputData.dat¶
inputData2.dat¶
Teradata:¶
Query¶
TESTTABU Result¶
| C1 | C2 |
|---|---|
| A | X |
| D | X |
| G | X |
| B | Y |
| E | Y |
| H | Y |
| C | Z |
| F | Z |
| I | Z |
| ? | _ |
| ? | _ |
| ? | _ |
| * | null |
| [ | null |
TESTTABU2 Result¶
| MY_NAME | MY_AGE |
|---|---|
| John Doe | 23 |
| John Doe | 23 |
| John Doe | 23 |
| John Doe | 23 |
| John Doe | 23 |
| John Doe | 23 |
| John Doe | 23 |
| John Doe | 23 |
| John Doe | 23 |
| John Doe | 23 |
| Bob Alice | 21 |
| Bob Alice | 21 |
| Bob Alice | 21 |
| Bob Alice | 21 |
| Bob Alice | 21 |
Snowflake:¶
Query¶
TESTTABU Result¶
| C1 | C2 |
|---|---|
| A | X |
| D | X |
| G | X |
| B | Y |
| E | Y |
| H | Y |
| C | Z |
| F | Z |
| I | Z |
| ? | _ |
| ? | _ |
| ? | _ |
| * | null |
| [ | null |
TESTTABU2 Result¶
| MY_NAME | MY_AGE |
|---|---|
| John Doe | 23 |
| John Doe | 23 |
| John Doe | 23 |
| John Doe | 23 |
| John Doe | 23 |
| John Doe | 23 |
| John Doe | 23 |
| John Doe | 23 |
| John Doe | 23 |
| John Doe | 23 |
| Bob Alice | 21 |
| Bob Alice | 21 |
| Bob Alice | 21 |
| Bob Alice | 21 |
| Bob Alice | 21 |
Known Issues ¶
No issues were found.
Related EWIs ¶
No related EWIs.
USING REQUEST MODIFIER¶
Translation specification for the USING REQUEST MODIFIER query.
Note
Some parts in the output code are omitted for clarity reasons.
As per Teradata’s documentation, the USING REQUEST MODIFIER defines one or more variable parameter names to be used in the subsequent SELECT, INSERT, UPDATE, or DELETE statements to import or export data.
The syntax for this statement is as follows:
As stated in Teradata’s documentation, the USING REQUEST MODIFIER needs to be preceded by an .IMPORT statement for it to load the data into the defined parameters.
Thus, the transformation for this statement follows these steps:
Call the
import_file()function from the SnowConvert AI Helpers. This loads the data into a temporary file.Call the
using()function from the SnowConvert AI Helpers to create a dictionary with the loaded data.For each query, run the
exec()function from the SnowConvert AI Helpers and pass the previously defined dictionary. This will use Snowflake Python Connector data binding capabilities.
With this input data:
Teradata (MultiLoad)
Query¶
Result¶
| ROW | C1 | C2 |
|---|---|---|
| 1 | A | X |
| 2 | B | Y |
| 3 | C | Z |
Snowflake (Python)
Query¶
Result¶
| ROW | C1 | C2 |
|---|---|---|
| 1 | A | X |
| 2 | B | Y |
| 3 | C | Z |
Known Issues¶
1. .REPEAT command is not yet supported
The .REPEAT command is not yet supported. This means that the USING REQUEST MODIFIER will only use the data loaded from the first row of the input file. Thus, the queries will only run once.
This issue should be fixed when the .REPEAT command receives proper transformation support.
If you have any additional questions regarding this documentation, you can email us at snowconvert-support@snowflake.com.
Related EWIs ¶
No related EWIs.