Combining Multidimensional Arrays with APPLY Table Operator | Open Analytics Framework - Combining Multidimensional Arrays with APPLY Table Operator - Teradata Vantage

Teradata® VantageCloud Lake

Deployment
VantageCloud
Edition
Lake
Product
Teradata Vantage
Published
January 2023
ft:locale
en-US
ft:lastEdition
2024-12-11
dita:mapPath
phg1621910019905.ditamap
dita:ditavalPath
pny1626732985837.ditaval
dita:id
phg1621910019905

Use the Apply class to create a teradataml Apply class object with the characteristics you want to consider for the call to the APPLY Table Operator.

In this example, specify the following:
  • The apply_command argument to call the R interpreter in your user environment and execute your script.
  • The returns argument with the list of output variables and types returned by your script.
  • The env_name argument to specify your user environment handler.
  1. Call to the Apply class.
    apply_obj = Apply(
        apply_command='Rscript abind.R',
        returns={"OUTPUT": VARCHAR(200)},
        env_name=demo_env,
        style='csv',
        delimiter='#'
    )

    You can request to view SQL queries submitted by teradataml in the background with the following statement:

    display.print_sqlmr_query = True
  2. Run the R script inside the user environment with the execute_script method of the Apply class object.
    apply_obj.execute_script().head(n=5)

    Observe that after running the R statement, the system prints for you the corresponding SQL query as requested before producing the results.

    SELECT DISTINCT * FROM apply (
    RETURNS (OUTPUT VARCHAR(200))
    using
    environment('abind_r_env')
    apply_command('Rscript abind.R')
    style('csv')
    delimiter('#')
    ) as dt;

    Out:

    x1 x2 x3 x4 y1 y2 y3 y4
    [2,]   2   5   8  11
     1  2  3  4  5  6  7  8
         x y
    ?
    [1,]    1    4    7   10
    [3,]    3    7   11   15   19
         [,1] [,2] [,3] [,4] [,5]
    [2,]    5    9   13   17
    [1] 3 4 2
    [2,]    2    5    8   11
    [2,] 2 6 10 14 18
    [1,]   1   4   7  10
    [3,]    3    7   11   15
    [4,]    7   11   15   19
    [3,]  103  106  109  112
    [1] 3 2 4
    [1,] 1 5
    [4,]   20   24   28   32
    [2,]   18   22   26   30
    [4,]    4    8   12   16
         x.a x.b y.a y.b
    [1] 3 8
    x    1    2    3    4
    [4,] 4 8
    , , x
         [,1] [,2] [,3] [,4]
    [2,]    2    6   10   14   18
    [2,]    2    6   10   14
    [4,]    4    8   12   16   20
    [1,] 1 5  9 13 17
    [1,]  101  104  107  110
    [1,]    1    2    3    4
    , , y
    [3,] 3 7
    [1,]   17   21   25   29
    [1,]    1    5    9   13
         5    9   13   17
      [,1] [,2] [,3] [,4]
    [5,]    8   12   16   20
         x
    [3,] 3 7 11 15 19
         7   11   15   19
    [3,]    6   10   14   18
    [3,]   3   6   9  12
    [2,] 2 6
    [2,]  102  105  108  111
    [4,] 4 8 12 16 20
         8   12   16   20
    [3,]    3    6    9   12
    [1] 2 3 4
    [3,]   19   23   27   31
    [1,]    1    5    9   13   17
         6   10   14   18
    [1] 6 4