Class Variadic
The key is the @Function
annotation declaring the function variadic
to PostgreSQL. The Java method parameter is declared as an ordinary array,
not with Java's ...
syntax; in fact, that would be impossible for a
function with a composite return type (where the Java signature would have to
include a ResultSet
parameter after the variadic input parameter).
-
Method Summary
Modifier and TypeMethodDescriptionstatic double
sumOfSquares
(double[] vals) Compute a double-precision sum of squares, treating any null input as zero.static Double
sumOfSquaresBoxed
(Double[] vals) Compute a double-precision sum of squares, returning null if any input value is null.
-
Method Details
-
sumOfSquaresBoxed
@Function(schema="javatest", effects=IMMUTABLE, onNullInput=RETURNS_NULL, variadic=true, provides="sumOfSquaresBoxed") public static Double sumOfSquaresBoxed(Double[] vals) Compute a double-precision sum of squares, returning null if any input value is null.The
RETURNS_NULL
annotation does not mean the array collecting the variadic arguments cannot have null entries; it only means PostgreSQL will never call this function with null for the array itself. -
sumOfSquares
@Function(schema="javatest", effects=IMMUTABLE, onNullInput=RETURNS_NULL, variadic=true, provides="sumOfSquares") public static double sumOfSquares(@SQLType(defaultValue={}) double[] vals) Compute a double-precision sum of squares, treating any null input as zero.The
RETURNS_NULL
annotation does not mean the array collecting the variadic arguments cannot have null entries; it only means PostgreSQL will never call this function with null for the array itself. Because the Java parameter type here is primitive and cannot represent nulls, PL/Java will have silently replaced any nulls in the input with zeros.This version also demonstrates using
@SQLType
to give the variadic parameter an empty-array default, so PostgreSQL will allow the function to be called with no corresponding arguments. Without that, PostgreSQL won't recognize a call to the function unless at least one argument corresponding to the variadic parameter is supplied.
-