Example: Ordering Input Parameters to a Table Function - Advanced SQL Engine - Teradata Database

SQL Data Definition Language Syntax and Examples

Product
Advanced SQL Engine
Teradata Database
Release Number
17.05
Published
January 2021
Language
English (United States)
Last Update
2021-01-22
dita:mapPath
ncd1596241368722.ditamap
dita:ditavalPath
hoy1596145193032.ditaval
dita:id
B035-1144
lifecycle
previous
Product Category
Teradata Vantage™

The following simple example illustrates the use of the HASH BY and LOCAL ORDER BY clauses for a table UDF. Table function add2int takes two integer values as input and returns both of them and their sum.

Query Q1 selects all columns from add2int, which requests its input, dt, to be hashed by dt.y1 and value-ordered on each AMP by dt.x1. Note that the specified hashing and local ordering might not be relevant to the add2int function and is only used for illustration.

The expected outcome of Q1 is that dt is first spooled and then hashed by y1 among the AMPs. On each AMP, the rows are sorted by the value of x1. The final hashed and sorted spool is used as the input to add2int.

     CREATE TABLE t1 (
       a1 INTEGER,
       b1 INTEGER);
     CREATE TABLE t2 (
       a2 INTEGER,
       b2 INTEGER);
     REPLACE FUNCTION add2int (
       a INTEGER, 
       b INTEGER)
     RETURNS TABLE (
       addend1 INTEGER, 
       addend2 INTEGER, 
       mysum INTEGER)
     SPECIFIC add2int  
     LANGUAGE C  
     NO SQL  
     PARAMETER STYLE SQL
     NOT DETERMINISTIC
     CALLED ON NULL INPUT
     EXTERNAL NAME 'CS!add3int!add2int.c';
/* Query Q1 */
     WITH dt(x1,y1) AS (SELECT a1,b1
                        FROM t1)
     SELECT *  
     FROM TABLE (add2int(dt.x1,dt.y1) 
     HASH BY y1 
     LOCAL ORDER BY x1) AS tf;