R Table Operator Example | SQL External Routine Programming | Teradata Vantage - R Table Operator Example - Analytics Database - Teradata Vantage

SQL External Routine Programming

Deployment
VantageCloud
VantageCore
Edition
Enterprise
IntelliFlex
VMware
Product
Analytics Database
Teradata Vantage
Release Number
17.20
Published
June 2022
Language
English (United States)
Last Update
2023-07-11
dita:mapPath
iiv1628111441820.ditamap
dita:ditavalPath
qkf1628213546010.ditaval
dita:id
B035-1147
lifecycle
latest
Product Category
Teradata Vantageā„¢
CREATE TABLE tab1(col1 INTEGER,col2 INTEGER)PRIMARY INDEX(COL1);
INSERT INTO tab1 VALUES(1, 20);
INSERT INTO tab1 VALUES(2, 30);
INSERT INTO tab1 VALUES(3, 40);
INSERT INTO tab1 VALUES(4, 50);

SELECT *
FROM TD_SYSGPL.ExecR (
   on (select * from tab1)
   hash by col1
   local order by col2
   using contract('library(tdr);
         stream<-0;
         direction<-"R";

         incols<-tdr.GetColDef(stream, direction);
         tdr.SetOutputColDef(stream,incols)')
      operator('library(tdr);
         stream<-0;
         direction<-"R";
         direction1<-"W";
         options<-0;
         inHandle<-tdr.Open(direction, stream, options);

         print(inHandle);
         outHandle<-tdr.Open(direction1, stream, options);

         print(outHandle);
         colcount <- tdr.GetColCount(stream , direction);

         colcount <- colcount -1 ;
         while(tdr.Read(inHandle)== 0)
         {
            for( index  in 0:colcount )
               {
                 att <- tdr.GetAttributeByNdx( inHandle , index , NULL);
                 tdr.SetAttributeByNdx(outHandle , index , att, NULL);
               };
               tdr.Write(outHandle);
            };
            tdr.Close(inHandle);
            tdr.Close(outHandle);')
) as d1;

Results:

   col1      col2
-------------------
    1         20
    4         50
    2         30
    3         40