Class FunctionKeyExpression

  • All Implemented Interfaces:
    AtomKeyExpression, KeyExpression, KeyExpressionWithChild, KeyExpressionWithChildren, PlanHashable, QueryHashable
    Direct Known Subclasses:
    CollateFunctionKeyExpression

    @API(EXPERIMENTAL)
    public abstract class FunctionKeyExpression
    extends BaseKeyExpression
    implements AtomKeyExpression, KeyExpressionWithChild
    A FunctionKeyExpression is a KeyExpression that is dynamically loaded and defined by a String name and a Key.Expression that produces sets of arguments to which the function is to be evaluated. FunctionKeyExpressions provide a mechanism by which indexes can be defined on arbitrarily complex logic applied to a record being inserted. For example, assuming a function called "substr" that functions much like the java String.substring() method, you could create, say, a unique index with key of
         function("subsstr", concat(field("firstname"), value(0), value(2)))
     
    Which would prevent duplicate records in which the first two characters of the firstname are identical.

    Similarly, the function can be made to apply in a fan-out fashion, simply by providing a argument an expression that itself fans out into a set of arguments to substr. For example, given message definitions such as:

     message SubString {
       required string content = 1;
       required int32 start = 2;
       required int32 end = 3;
     }
    
     message SubStrings {
       repeated SubString substrings = 1;
     }
     
    In which we want to have the arguments to substr driven by data stored in the records themselves, you could define the index key expression as:
         function("substr", field("substrings", FanType.FanOut).nest(concatenateFields("content", "start", "end")))
     
    This would produce the result of performing substr(content, start, end) for each SubStringvalue in substrings.

    Actual implementations of FunctionKeyExpressions are discovered by polling all available FunctionKeyExpression.Registry implementations. A Registry returns a list of FunctionKeyExpression.Builders which, given a set of arguments, are capable of creating an implementation of a function.