10.6.2.6 rqRowEval2 Function

The function rqRowEval2 when used in Oracle Autonomous Database,chunks data into sets of rows and then runs a user-defined R function on each chunk.

The function rqRowEval2 passes the data specified by the INP_NAM parameter to the user-defined R function. You can pass arguments to the R function with the PAR_LST parameter. The ROW_NUM parameter specifies the number of rows that should be passed to each invocation of the R function. The last chunk may have fewer rows than the number specified.

The rqRowEval2 function supports data-parallel execution, in which one or more R engines perform the same R function, or task, on disjoint chunks of data. Oracle Database handles the management and control of the potentially multiple R engines that run on the database server machine, automatically chunking and passing data to the R engines executing in parallel. Oracle Database ensures that R function executions for all chunks of rows complete, or the rqRowEval2 function returns an error.

You define the form of the returned value with the OUT_FMT parameter.

Syntax

rqRowEval2(
    INP_NAM VARCHAR2,
    PAR_LST VARCHAR2,
    OUT_FMT VARCHAR2,
    ROW_NUM NUMBER,
    SCR_NAME VARCHAR2,
    SCR_OWNER VARCHAR2 DEFAULT NULL,
    ENV_NAME  VARCHAR2 DEFAULT NULL
    )

Parameters

Table 10-20 Parameters of the rqRowEval2 Function

Parameter Description

INP_NAM

The name of a table or view that specifies the data to pass to the R function specified by the SCR_NAME parameter. If using a table or view owned by another user, use the format

<owner name>.<table/view name>.

You must have read access to the specified table or view.

PAR_LST

A JSON string that contains additional parameters to pass to the user-defined R function specified by the SCR_NAME parameter. Special control arguments, which start with ore, are not passed to the function specified by SCR_NAME, but instead control what happens before or after the invocation of the function.

For example, to capture images rendered in the R function, use:

'{"ore_graphics_flag":true}'

See also: Special Control Arguments.

OUT_FMT

The format of the output returned by the function. It can be one of the following:

  • A JSON string that specifies the column names and data types of the table returned by the function. Any image data is discarded.
  • The string 'JSON', which specifies that the table returned contains a CLOB that is a JSON string.
  • The string 'XML', which specifies that the table returned contains a CLOB that is an XML string. The XML can contain both structured data and images, with structured or semi-structured R objects first, followed by the image or images generated by the R function.
  • The string 'PNG', which specifies that the table returned contains a BLOB that has the image or images generated by the R function. Images are returned as a base 64 encoding of the PNG representation.

See also: Output Formats.

ROW_NUM

The number of rows in a chunk. The R script is executed in each chunk.

SCR_NAME

The name of a user-defined R function in the OML4R script repository.

SCR_OWNER

The owner of the registered R script. The default value is NULL. If NULL, will search for the R script in the user’s script repository.

ENV_NAME

The name of the conda environment that should be used when running the named user-defined R function.

Return Value

Function rqRowEval2 returns a table that has the structure specified by the OUT_FMT parameter value.

Examples

Example 10-42 Using an rqRowEval2 Function

This example creates a user-defined function and saves the function in the OML4R script repository.

The PL/SQL block, creates the script scoreLM and add it to the script repository.

%script

BEGIN
    sys.rqScriptCreate('scoreLM',
        'function(dat, dsname){
            ore.load(dsname)
            dat$Petal.Length_pred <- predict(mod, newdata=dat)
            dat[,c("Petal.Length_pred","Petal.Length","Species")]}',
        v_global => FALSE,
        v_overwrite => TRUE);
END;
/

The results is:

PL/SQL procedure successfully completed.


---------------------------

Example 10-43 JSON Output

The PAR_LST argument specifies using MEDIUM service level with the special control argument ore_service_level and. In the OUT_FMT argument, the string 'JSON', specifies that the table returned contains a CLOB that is a JSON string. The SCR_NAME parameter specifies the scoreLM function in the script repository as the R function to call. The JSON output is a CLOB. You can call set long [length] to get more output.

%script

set long 1000

SELECT * FROM table(rqRowEval2(
 inp_nam => 'IRIS',
 par_lst => '{"dsname":"ds-1", "ore_parallel_flag":true, "ore_service_level":"MEDIUM"}',
 out_fmt => 'JSON',
 row_num => 5,
 scr_name => 'scoreLM'));

The result is:

---------------------------
NAME   VALUE                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
       [{"Petal.Length_pred":1.5295,"Species":"setosa","Petal.Length":1},{"Petal.Length_pred":1.3066,"Species":"setosa","Petal.Length":1.1},{"Petal.Length_pred":1.5295,"Species":"setosa","Petal.Length":1.2},{"Petal.Length_pred":1.5295,"Species":"setosa","Petal.Length":1.2},{"Petal.Length_pred":1.5295,"Species":"setosa","Petal.Length":1.3},{"Petal.Length_pred":1.5295,"Species":"setosa","Petal.Length":1.3},{"Petal.Length_pred":1.5295,"Species":"setosa","Petal.Length":1.3},{"Petal.Length_pred":1.5295,"Species":"setosa","Petal.Length":1.3},{"Petal.Length_pred":1.7525,"Species":"setosa","Petal.Length":1.3},{"Petal.Length_pred":1.7525,"Species":"setosa","Petal.Length":1.3},{"Petal.Length_pred":1.9755,"Species":"setosa","Petal.Length":1.3},{"Petal.Length_pred":1.3066,"Species":"setosa","Petal.Length":1.4},{"Petal.Length_pred":1.3066,"Species":"setosa","Petal.Length":1.4},{"Petal.Length_pred":1.5295,"Species":"setosa","Petal.Length":1.4},{"Petal.Length_pred":1.5295,"Species":"setosa","Petal.Length":1 

Example 10-44 Relational

Run the Select statement to get an Relational output.

%script

SELECT * FROM table(rqRowEval2(
 inp_nam => 'IRIS',
 par_lst => '{"dsname":"ds-1", "ore_parallel_flag":true, "ore_service_level":"MEDIUM"}',
 out_fmt => '{"Petal.Length_pred":"NUMBER", "Petal.Length":"NUMBER", "Species":"VARCHAR2(10)"}',
 row_num => 5,
 scr_name => 'scoreLM'));

The result is:

Petal.Length_pred   Petal.Length   Species   
             1.5295              1 setosa    
             1.3066            1.1 setosa    
             1.5295            1.2 setosa    
             1.5295            1.2 setosa    
             1.5295            1.3 setosa    
             1.5295            1.3 setosa    
             1.5295            1.3 setosa    
             1.5295            1.3 setosa    
             1.7525            1.3 setosa    
             1.7525            1.3 setosa    
             1.9755            1.3 setosa    
             1.3066            1.4 setosa    
             1.3066            1.4 setosa    
             1.5295            1.4 setosa    

Petal.Length_pred   Petal.Length   Species   
             1.5295            1.4 setosa    
             1.5295            1.4 setosa    
             1.5295            1.4 setosa    
             1.5295            1.4 setosa    
             1.5295            1.4 setosa    
             1.5295            1.4 setosa    
             1.5295            1.4 setosa    
             1.7525            1.4 setosa    
             1.7525            1.4 setosa    
             1.7525            1.4 setosa    
             1.3066            1.5 setosa    
             1.3066            1.5 setosa    
             1.5295            1.5 setosa    
             1.5295            1.5 setosa    

Petal.Length_pred   Petal.Length   Species   
             1.5295            1.5 setosa    
             1.5295            1.5 setosa    
             1.5295            1.5 setosa    
             1.5295            1.5 setosa    
             1.5295            1.5 setosa    
             1.7525            1.5 setosa    
             1.9755            1.5 setosa    
             1.9755            1.5 setosa    
             1.9755            1.5 setosa    
             1.5295            1.6 setosa    
             1.5295            1.6 setosa    
             1.5295            1.6 setosa    
             1.5295            1.6 setosa    
             1.5295            1.6 setosa    

Petal.Length_pred   Petal.Length   Species      
             1.9755            1.6 setosa       
             2.4215            1.6 setosa       
             1.5295            1.7 setosa       
             1.7525            1.7 setosa       
             1.9755            1.7 setosa       
             2.1985            1.7 setosa       
             1.5295            1.9 setosa       
             1.9755            1.9 setosa       
             3.5365              3 versicolor   
             3.3135            3.3 versicolor   
             3.3135            3.3 versicolor   
             3.3135            3.5 versicolor   
             3.3135            3.5 versicolor   
             3.9825            3.6 versicolor   

Petal.Length_pred   Petal.Length   Species      
             3.3135            3.7 versicolor   
             3.5365            3.8 versicolor   
             3.5365            3.9 versicolor   
             3.7595            3.9 versicolor   
             4.2055            3.9 versicolor   
             3.3135              4 versicolor   
             3.7595              4 versicolor   
             3.9825              4 versicolor   
             3.9825              4 versicolor   
             3.9825              4 versicolor   
             3.3135            4.1 versicolor   
             3.9825            4.1 versicolor   
             3.9825            4.1 versicolor   
             3.7595            4.2 versicolor   

Petal.Length_pred   Petal.Length   Species      
             3.9825            4.2 versicolor   
             3.9825            4.2 versicolor   
             4.4285            4.2 versicolor   
             3.9825            4.3 versicolor   
             3.9825            4.3 versicolor   
             3.7595            4.4 versicolor   
             3.9825            4.4 versicolor   
             4.2055            4.4 versicolor   
             4.2055            4.4 versicolor   
             4.8745            4.5 virginica    
             3.9825            4.5 versicolor   
             4.4285            4.5 versicolor   
             4.4285            4.5 versicolor   
             4.4285            4.5 versicolor   

Petal.Length_pred   Petal.Length   Species      
             4.4285            4.5 versicolor   
             4.4285            4.5 versicolor   
             4.6515            4.5 versicolor   
             3.9825            4.6 versicolor   
             4.2055            4.6 versicolor   
             4.4285            4.6 versicolor   
             3.7595            4.7 versicolor   
             4.2055            4.7 versicolor   
             4.2055            4.7 versicolor   
             4.4285            4.7 versicolor   
             4.6515            4.7 versicolor   
             5.0975            4.8 virginica    
             5.0975            4.8 virginica    
             4.2055            4.8 versicolor   

Petal.Length_pred   Petal.Length   Species      
             5.0975            4.8 versicolor   
             5.0975            4.9 virginica    
             5.0975            4.9 virginica    
             5.5434            4.9 virginica    
             4.4285            4.9 versicolor   
             4.4285            4.9 versicolor   
             4.4285              5 virginica    
             5.3204              5 virginica    
             5.5434              5 virginica    
             4.8745              5 versicolor   
             4.4285            5.1 virginica    
             5.0975            5.1 virginica    
             5.3204            5.1 virginica    
             5.3204            5.1 virginica    

Petal.Length_pred   Petal.Length   Species      
             5.5434            5.1 virginica    
             6.2124            5.1 virginica    
             6.4354            5.1 virginica    
             4.6515            5.1 versicolor   
             5.5434            5.2 virginica    
             6.2124            5.2 virginica    
             5.3204            5.3 virginica    
             6.2124            5.3 virginica    
             5.7664            5.4 virginica    
             6.2124            5.4 virginica    
             5.0975            5.5 virginica    
             5.0975            5.5 virginica    
             5.7664            5.5 virginica    
             4.2055            5.6 virginica    

Petal.Length_pred   Petal.Length   Species     
             5.0975            5.6 virginica   
             5.7664            5.6 virginica   
             5.9894            5.6 virginica   
             6.4354            5.6 virginica   
             6.4354            5.6 virginica   
             5.7664            5.7 virginica   
             6.2124            5.7 virginica   
             6.6584            5.7 virginica   
             4.6515            5.8 virginica   
             5.0975            5.8 virginica   
             5.9894            5.8 virginica   
             5.7664            5.9 virginica   
             6.2124            5.9 virginica   
             5.0975              6 virginica   

Petal.Length_pred   Petal.Length   Species     
             6.6584              6 virginica   
             5.3204            6.1 virginica   
             6.2124            6.1 virginica   
             6.6584            6.1 virginica   
             5.0975            6.3 virginica   
             5.5434            6.4 virginica   
             5.7664            6.6 virginica   
             5.5434            6.7 virginica   
             5.9894            6.7 virginica   
             6.2124            6.9 virginica   


150 rows selected. 


---------------------------

The following code runs a SQL query to retrieve all columns from the result of an rqRowEval2 function call, which generates a PNG output based on a specified script and environment.

select *
from table(rqRowEval2(
inp_nam => 'IRIS',
par_lst => '{"ore_graphics_flag":true}',
out_fmt => 'PNG',
row_num => 50,
scr_name => 'test_ggplot2_inp',
scr_owner => NULL,
env_name => 'myrenv'));

The output appears as follows:

NAME ID VALUE IMAGE
---------- ---------- -------------------- ------------------------------
CHUNK_1 1 "hello world" 89504E470D0A1A0A0000000D494844
                        52000001E0000001E008060000007D
                        D4BE950000200049444154789CECDD
                        777C53D5FFC7F1579AA46D3A81963D
                        CAA60C0505BE381805510115411410
                        5CA8881B45
CHUNK_2 1 "hello world" 89504E470D0A1A0A0000000D494844
                        52000001E0000001E008060000007D
                        D4BE950000200049444154789CEDDD
                        777814F5DAC6F17B5B7AA5465A420F
NAME       ID          VALUE               IMAGE
---------- ---------- -------------------- ------------------------------
                                           4D22458ED251041BC502088258100B
                                           0A2AA2A2E7
CHUNK_3    1           "hello world"       89504E470D0A1A0A0000000D494844
                                           52000001E0000001E008060000007D
                                           D4BE950000200049444154789CEDDD
                                           777814F5C2C5F1B3BB61D30381D04B
                                           420F4D90F2220AA1280AA84893AA08
                                           2A6247011B