A ScalarFunction must implement computeValue(). This function is given a RowView, which is an abstract of table row. This row is provided by RowIterator and is run by the scalar execution engine. This function emits ValueHolder to the scalar execution engine and the database.
Below is an example of a simple function that computes the factorial for one given numerical column.
public ValueHolder computeValue(RowView row) { this.outputValue_.setNull(); if (!row.isNullAt(0)) { row.getValueAt(0, this.outputValue_); long result = this.outputValue_.toLong(); if (result < 0) { throw new ClientVisibleException( "negative factorial is not defined: " + result); } else if (result == 0) { result = 1; // 0! = 1 } else { // result > 0 for (long i=result-1; i>1; i--) { result *= i; if (result < 0) throw new ClientVisibleException( "result overflow " + result); } } this.outputValue_.setLong(result); } return this.outputValue_; }
See the scalar-sdk/ directory in the SQL-Analytics SDK for sample user defined scalar functions. For reference information on the user defined scalar functions and other SQL-Analytics API, see the Javadoc reference in the SDK bundle.