ApplyDriver Version 1

Code in this section is saved in a file called ApplyDriverV1.java.

Code

Get a local AE connection handle of type "function." It can be called from a table or scalar function.

// ApplyDriverV1.java
package org.netezza.education;

import org.netezza.ae.*;

public class ApplyDriverV1 {
    public static final void main(String [] args) {
        NzaeFactory factory = NzaeFactory.Source.getFactory();

        if (factory.isLocal()) {
            NzaeInitialization init = new NzaeInitialization();
            Nzae ae = factory.getLocalFunctionApi(init);
            ApplyOperation.runAe(ae);
            ae.close();
        } else {
            throw new RuntimeException("Expecting Local AE only");
        }
    }
}

Compilation

Two Java sources are compiled and deployed as class files.

# mod1comp1.bsh
$NZ_EXPORT_DIR/ae/utilities/bin/compile_ae --language java --template compile
--version 3 \
--db dev "ApplyOperation.java ApplyDriverV1.java"

Registration as a Scalar Function

The --template udf parameter indicates a scalar function.

$NZ_EXPORT_DIR/ae/utilities/bin/register_ae --language java --template udf
--version 3 --db dev \
--sig "applyOperationV1Sf(varargs)" --return double \
--define java_class=org.netezza.education.ApplyDriverV1 --level 1

Running as a Scalar Function

SELECT applyOperationV1Sf('+', 1, 2, 3);
 APPLYOPERATIONV1SF
--------------------
                  6

SELECT f1, f2, f3, f4, '' AS equals, applyOperationV1Sf('+', f1, f2, f3, f4)
 FROM edutestdata WHERE color = 'red';
  F1  |  F2  |  F3 |   F4  | EQUALS | APPLYOPERATIONV1SF
------+------+-----+-------+--------+--------------------
    1 |    2 |   3 | 4.000 |        |                 10
 -100 |  300 | 0.5 | 0.500 |        |                201
  100 | -300 | 0.5 | 0.500 |        |               -199
  100 |  300 | 0.5 | 0.500 |        |                401

Registration as a Table Function

The --template udtf parameter indicates a table function.

$NZ_EXPORT_DIR/ae/utilities/bin/register_ae --language java --template udtf
--version 3 --db dev \
     --level 1 --sig "applyOperationV1Tf(varargs)" \
     --return "table(result double)" \
     --define java_class=org.netezza.education.ApplyDriverV1

Running as a Table Function

SELECT * FROM TABLE WITH FINAL(applyOperationV1Tf('+', 1, 2, 3));
 RESULT
--------
      6

SELECT f1, f2, f3, f4, '' AS equals, result FROM edutestdata,
 TABLE WITH FINAL(applyOperationV1Tf('+', f1, f2, f3, f4))
 WHERE color = 'red';
  F1  |  F2  | F3  |  F4   | EQUALS | RESULT
------+------+-----+-------+--------+--------
 -100 |  300 | 0.5 | 0.500 |        |    201
  100 | -300 | 0.5 | 0.500 |        |   -199
    1 |    2 |   3 | 4.000 |        |     10
  100 |  300 | 0.5 | 0.500 |        |    401