java.lang.Object
com.activeviam.activepivot.mdx.impl.internal.func.base.impl.FunctionUtil

public class FunctionUtil extends Object
Utility functions for Mdx functions.
Author:
ActiveViam
  • Field Details

  • Method Details

    • getSignatureName

      public static String getSignatureName(Type t)
      Parameters:
      t - The type to retrieve signature name
      Returns:
      The signature name of the given type
    • getNullSingletonCollection

      public static <T> Collection<T> getNullSingletonCollection()
      Gets a singleton collection with NULL_SINGLETON_COLLEC.
      Type Parameters:
      T - Expected type of the collection
      Returns:
      Unknown result for plan
    • ensureKeyword

      public static IFunctionKeyword ensureKeyword(IExpression expression) throws MdxException
      Ensures that an expression is a function keyword.

      This practically is a dynamic cast.

      Parameters:
      expression - expression to test
      Returns:
      the equivalent keyword
      Throws:
      MdxException - if the expression does not represent a keyword
    • whichKeyword

      public static int whichKeyword(IExpression expression, IFunctionKeyword[] expectedValues, String functionName) throws MdxException
      Throws an exception is the keyword represented by expression is not in the expectedValues
      Parameters:
      expression - expression to convert into a keyword
      expectedValues - expected keywords for an expression
      functionName - name of the function calling this method, for debug purpose
      Returns:
      index of matching keyword in the provided array of keywords
      Throws:
      MdxException - if the expression does not convert into one of the wanted keywords
    • whichKeyword

      public static <T> T whichKeyword(IExpression expression, Map<IFunctionKeyword,T> expectedToResults, String functionName) throws MdxException
      Throws an exception is the keyword represented by expression is not in the keys of expectedToResults, else returns the mapped value.
      Parameters:
      expression - Expression to convert into a keyword
      expectedToResults - Keys are the possible values of expression, values are the values to return if a key matches the expression
      functionName - Name of the function calling this method, for debug purpose
      Returns:
      The mapped value for the matching keyword
      Throws:
      MdxException - if the expression does not convert into one of the wanted keywords
    • isKeyword

      public static boolean isKeyword(IExpression expression, IFunctionKeyword expected) throws MdxException
      Check if the expression is the given keyword.
      Parameters:
      expression - expression to test
      expected - expected keyword value
      Returns:
      true if the expression is the given keyword
      Throws:
      MdxException - if the expression does not convert into the wanted keyword
    • isPresentationExpression

      public static boolean isPresentationExpression(IExpression expression)
      Checks if an expression is a presentation expression.
      Parameters:
      expression - The expression to check
      Returns:
      true if the given expression is a presentation expression
    • matchKeyword

      public static void matchKeyword(IExpression expression, IContextBase context, IFunctionKeyword expected, String functionName) throws MdxException
      Throw an exception is the keyword represented by expression is not equal to the expectedValue
      Parameters:
      expression - expression to test
      context - matching context
      expected - expected keyword for the expression
      functionName - Used in the exception message
      Throws:
      MdxException - if the expression does match the expected keyword
    • findFunction

      public static IFunction findFunction(String name, boolean caseSensitive)
      Retrieve a function.
      Parameters:
      name - the name of the function
      caseSensitive - true if the lookup must be case sensitive
      Returns:
      a function
    • findFunctionKeyword

      public static IFunctionKeyword findFunctionKeyword(String name)
      Retrieve a IFunctionKeyword.
      Parameters:
      name - the name of the keyword
      Returns:
      a keyword
    • planEvaluateValueForEach

      public static void planEvaluateValueForEach(IExpressionPlanner planner, ISet set, IExpression expression) throws MdxException
      Plan the evaluation of the expression on each tuple of the set.
      Parameters:
      planner - planner for the expression gathering results
      set - set of values to consider
      expression - expression to plan for each member
      Throws:
      MdxException - if an error occurs
    • planEvaluateCurrentForEach

      public static void planEvaluateCurrentForEach(IExpressionPlanner planner, ISet set) throws MdxException
      Plan the evaluation of each tuple of the set.
      Parameters:
      planner - planner for the expression gathering results
      set - set of values to consider
      Throws:
      MdxException - if an error occurs
    • asNumber

      public static Number asNumber(Object o)
      Returns o if o is a number. Returns null otherwise.
      Parameters:
      o - an Object
      Returns:
      o if o is a Number; null otherwise.
    • toNumber

      public static Number toNumber(Object o) throws MdxException
      Parameters:
      o - an Object
      Returns:
      o
      Throws:
      MdxException - if o is not a Number
    • isLiteralScalar

      public static boolean isLiteralScalar(IExpression expression)
      Tests if an expression represents a literal scalar.
      Parameters:
      expression - expression to test
      Returns:
      true if the expression is a scalar ILiteral
      See Also:
    • asLiteralScalar

      public static Object asLiteralScalar(IExpression expression)
      If the expression is a literal scalar, then the result will be its value. Otherwise the result will be null.
      Parameters:
      expression - expression to convert
      Returns:
      the value of the literal or null
      See Also:
    • isNullScalar

      public static boolean isNullScalar(IExpression expression)
      Tests if the expression is the NULL literal.
      Parameters:
      expression - expression to test
    • asLiteralNumber

      public static Number asLiteralNumber(IExpression expression)
      If the expression is a literal number, then the result will be its value. Otherwise the result will be null.
      Parameters:
      expression - expression to convert
      Returns:
      the value of the literal or null
      See Also:
    • asLiteralString

      public static String asLiteralString(IExpression expression)
      If the expression is a literal string, then the result will be its value. Otherwise the result will be null.
      Parameters:
      expression - expression to convert
      Returns:
      the value of the literal or null
      See Also:
    • asLiteralBoolean

      @Nullable public static Boolean asLiteralBoolean(IExpression expression)
      If the expression is a literal boolean, then the result will be its value. Otherwise the result will be null.
      Parameters:
      expression - expression to convert
      Returns:
      the value of the literal or null
      See Also:
    • asFixedMember

      public static IMdxFixedMember asFixedMember(IExpression expression)
      If the expression is a IMemberExpression, then the result will be the associated member. Otherwise the result will be null.
      Parameters:
      expression - expression to convert
      Returns:
      a member or null
    • asLiteralTuple

      public static ITuple asLiteralTuple(IExpression expression)
      If the expression is a literal tuple, then the result will be its value. Otherwise the result will be null.
      Parameters:
      expression - expression to convert
      Returns:
      the value of the literal or null
      See Also:
    • conversionCost

      public static int conversionCost(Type from, Type to)
      Get the conversion cost between from and to.
      Parameters:
      from - the originating type
      to - the target type
      Returns:
      the conversion cost
    • convertAsUnresolved

      public static IExpression convertAsUnresolved(IExpression exp, Type to)
      Convert the expression into the specified type. The result will be an unresolved expression to let the IExpressionResolver choose the function definition.
      Parameters:
      exp - the expression to convert
      to - the target type
      Returns:
      the converted expression or null if the conversion is not allowed
    • computeArgsUnionDependency

      public static <R extends IExpressionDependency> R computeArgsUnionDependency(IFunctionCall call, IExpressionDependencyVisitor<R> visitor, int nbArg) throws MdxException
      Helper method to compute the union of the dependencies of the function call's arguments.
      Type Parameters:
      R - type of visited expressions
      Parameters:
      call - the function call
      visitor - the visitor use to compute and manipulate the depencies
      nbArg - the number of arguments to process
      Returns:
      a dependency which is the union of the dependencies
      Throws:
      MdxException - if an error occurs
      See Also:
    • computeUnionAsValueDependency

      public static <R extends IExpressionDependency> R computeUnionAsValueDependency(IFunctionCall call, IExpressionDependencyVisitor<R> visitor, int nbArg) throws MdxException
      Helper method to compute the union of the dependencies of the function call's arguments when they are evaluated as a value.
      Type Parameters:
      R - type of visited expressions
      Parameters:
      call - the function call
      visitor - the visitor use to compute and manipulate the depencies
      nbArg - the number of arguments to process
      Returns:
      a dependency which is the union of the dependencies
      Throws:
      MdxException - if an error occurs
      See Also:
    • appendAndFork

      public static <T> void appendAndFork(ArrayList<ArrayList<T>> container, Collection<T> toAppend)
      Helper method to perform union with ambiguities.
      Type Parameters:
      T - type of the elements to fork
      Parameters:
      container - container receiving elements to unit
      toAppend - elements to distribute in the container
    • getBottomComparator

      public static Comparator<IPair<ITuple,Number>> getBottomComparator()
      Gets a comparator sorting pairs by their values in ascending order.
      Returns:
      a comparator
    • getTopComparator

      public static Comparator<IPair<ITuple,Number>> getTopComparator()
      Gets a comparator sorting pairs by their values in descending order.
      Returns:
      a comparator
    • isInt

      public static boolean isInt(Number n)
      Check if the specified number is an int:
       n != null && n.intValue() == n.doubleValue()
       
      Parameters:
      n - The number to check
      Returns:
      true if the given number is an int
    • getMdxIntValue

      public static int getMdxIntValue(Number n)
      In Mdx null == 0.

      Sugar for:

       null == n ? 0 : n.intValue()
       
      Parameters:
      n - a number
      Returns:
      the int value
    • checkNotNull

      public static void checkNotNull(Object o, IFunctionCall call, int index) throws MdxException
      Throw an exception if o is null.
      Parameters:
      o - the object to check
      call - function call in progress, provided for debug purpose
      index - index of the call argument tested
      Throws:
      MdxException - if o is null
      See Also:
    • errorFunctionCall

      public static IFunctionCall errorFunctionCall(Type returnType, String errorMsg)
      Creates an OLAP element that is effectively a function call to a special error function. This function throws and MdxRuntimeException at evaluation stage. Using such calls helps to move exception processing from resolution stage to evaluation stage. This increases MDX engine usability, because only those MDX queries will throw that are actually referencing missing / buggy OLAP elements.
      Parameters:
      returnType - The return type of the throwing function
      errorMsg - The error message to print when throwing the exception
      Returns:
      The FunctionCall object