Class FunctionUtil
java.lang.Object
com.activeviam.activepivot.mdx.impl.internal.func.base.impl.FunctionUtil
Utility functions for Mdx functions.
- Author:
- ActiveViam
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
Signature for a function without arguments.static final Collection
<?> Unknown result during plan.static final int
Constant used byconversionCost(Type, Type)
. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> void
appendAndFork
(ArrayList<ArrayList<T>> container, Collection<T> toAppend) Helper method to perform union with ambiguities.static IMdxFixedMember
asFixedMember
(IExpression expression) If the expression is aIMemberExpression
, then the result will be the associated member.static Boolean
asLiteralBoolean
(IExpression expression) If the expression is a literal boolean, then the result will be its value.static Number
asLiteralNumber
(IExpression expression) If the expression is a literal number, then the result will be its value.static Object
asLiteralScalar
(IExpression expression) If the expression is a literal scalar, then the result will be its value.static String
asLiteralString
(IExpression expression) If the expression is a literal string, then the result will be its value.static ITuple
asLiteralTuple
(IExpression expression) If the expression is a literal tuple, then the result will be its value.static Number
Returns o if o is a number.static void
checkNotNull
(Object o, IFunctionCall call, int index) Throw an exception ifo
is null.static <R extends IExpressionDependency>
RcomputeArgsUnionDependency
(IFunctionCall call, IExpressionDependencyVisitor<R> visitor, int nbArg) Helper method to compute the union of the dependencies of the function call's arguments.static <R extends IExpressionDependency>
RcomputeUnionAsValueDependency
(IFunctionCall call, IExpressionDependencyVisitor<R> visitor, int nbArg) Helper method to compute the union of the dependencies of the function call's arguments when they are evaluated as a value.static int
conversionCost
(Type from, Type to) Get the conversion cost betweenfrom
andto
.static IExpression
convertAsUnresolved
(IExpression exp, Type to) Convert the expression into the specified type.static IFunctionKeyword
ensureKeyword
(IExpression expression) Ensures that an expression is a function keyword.static IFunctionCall
errorFunctionCall
(Type returnType, String errorMsg) Creates an OLAP element that is effectively a function call to a special error function.static IFunction
findFunction
(String name, boolean caseSensitive) Retrieve a function.static IFunctionKeyword
findFunctionKeyword
(String name) Retrieve aIFunctionKeyword
.static Comparator
<IPair<ITuple, Number>> Gets a comparator sorting pairs by their values in ascending order.static int
In Mdxnull
== 0.static <T> Collection
<T> Gets a singleton collection withNULL_SINGLETON_COLLEC
.static String
static Comparator
<IPair<ITuple, Number>> Gets a comparator sorting pairs by their values in descending order.static boolean
Check if the specified number is an int:static boolean
isKeyword
(IExpression expression, IFunctionKeyword expected) Check if the expression is the given keyword.static boolean
isLiteralScalar
(IExpression expression) Tests if an expression represents a literal scalar.static boolean
isNullScalar
(IExpression expression) Tests if the expression is theNULL
literal.static boolean
isPresentationExpression
(IExpression expression) Checks if an expression is a presentation expression.static void
matchKeyword
(IExpression expression, IContextBase context, IFunctionKeyword expected, String functionName) Throw an exception is the keyword represented byexpression
is not equal to theexpectedValue
static void
planEvaluateCurrentForEach
(IExpressionPlanner planner, ISet set) Plan the evaluation of each tuple of the set.static void
planEvaluateValueForEach
(IExpressionPlanner planner, ISet set, IExpression expression) Plan the evaluation of the expression on each tuple of the set.static Number
static int
whichKeyword
(IExpression expression, IFunctionKeyword[] expectedValues, String functionName) Throws an exception is the keyword represented byexpression
is not in theexpectedValues
static <T> T
whichKeyword
(IExpression expression, Map<IFunctionKeyword, T> expectedToResults, String functionName) Throws an exception is the keyword represented byexpression
is not in the keys ofexpectedToResults
, else returns the mapped value.
-
Field Details
-
NULL_SINGLETON_COLLEC
Unknown result during plan. -
NONE_PARAM_SIGNATURE
Signature for a function without arguments.- See Also:
-
TO_SPECIFIC
public static final int TO_SPECIFICConstant used byconversionCost(Type, Type)
.- See Also:
-
-
Method Details
-
getSignatureName
- Parameters:
t
- The type to retrieve signature name- Returns:
- The signature name of the given type
-
getNullSingletonCollection
Gets a singleton collection withNULL_SINGLETON_COLLEC
.- Type Parameters:
T
- Expected type of the collection- Returns:
- Unknown result for plan
-
ensureKeyword
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 byexpression
is not in theexpectedValues
- Parameters:
expression
- expression to convert into a keywordexpectedValues
- expected keywords for an expressionfunctionName
- 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 MdxExceptionThrows an exception is the keyword represented byexpression
is not in the keys ofexpectedToResults
, else returns the mapped value.- Parameters:
expression
- Expression to convert into a keywordexpectedToResults
- Keys are the possible values ofexpression
, values are the values to return if a key matches theexpression
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 testexpected
- 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
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 byexpression
is not equal to theexpectedValue
- Parameters:
expression
- expression to testcontext
- matching contextexpected
- expected keyword for the expressionfunctionName
- Used in the exception message- Throws:
MdxException
- if the expression does match the expected keyword
-
findFunction
Retrieve a function.- Parameters:
name
- the name of the functioncaseSensitive
-true
if the lookup must be case sensitive- Returns:
- a function
-
findFunctionKeyword
Retrieve aIFunctionKeyword
.- 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 resultsset
- set of values to considerexpression
- 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 resultsset
- set of values to consider- Throws:
MdxException
- if an error occurs
-
asNumber
Returns o if o is a number. Returns null otherwise.- Parameters:
o
- an Object- Returns:
- o if o is a Number; null otherwise.
-
toNumber
- Parameters:
o
- an Object- Returns:
- o
- Throws:
MdxException
- if o is not a Number
-
isLiteralScalar
Tests if an expression represents a literal scalar.- Parameters:
expression
- expression to test- Returns:
true
if the expression is a scalarILiteral
- See Also:
-
asLiteralScalar
If the expression is a literal scalar, then the result will be its value. Otherwise the result will benull
.- Parameters:
expression
- expression to convert- Returns:
- the value of the literal or
null
- See Also:
-
isNullScalar
Tests if the expression is theNULL
literal.- Parameters:
expression
- expression to test
-
asLiteralNumber
If the expression is a literal number, then the result will be its value. Otherwise the result will benull
.- Parameters:
expression
- expression to convert- Returns:
- the value of the literal or
null
- See Also:
-
asLiteralString
If the expression is a literal string, then the result will be its value. Otherwise the result will benull
.- Parameters:
expression
- expression to convert- Returns:
- the value of the literal or
null
- See Also:
-
asLiteralBoolean
If the expression is a literal boolean, then the result will be its value. Otherwise the result will benull
.- Parameters:
expression
- expression to convert- Returns:
- the value of the literal or
null
- See Also:
-
asFixedMember
If the expression is aIMemberExpression
, then the result will be the associated member. Otherwise the result will benull
.- Parameters:
expression
- expression to convert- Returns:
- a member or
null
-
asLiteralTuple
If the expression is a literal tuple, then the result will be its value. Otherwise the result will benull
.- Parameters:
expression
- expression to convert- Returns:
- the value of the literal or
null
- See Also:
-
conversionCost
Get the conversion cost betweenfrom
andto
.- Parameters:
from
- the originating typeto
- the target type- Returns:
- the conversion cost
-
convertAsUnresolved
Convert the expression into the specified type. The result will be an unresolved expression to let theIExpressionResolver
choose the function definition.- Parameters:
exp
- the expression to convertto
- 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 callvisitor
- the visitor use to compute and manipulate the depenciesnbArg
- 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 callvisitor
- the visitor use to compute and manipulate the depenciesnbArg
- 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
Helper method to perform union with ambiguities.- Type Parameters:
T
- type of the elements to fork- Parameters:
container
- container receiving elements to unittoAppend
- elements to distribute in the container
-
getBottomComparator
Gets a comparator sorting pairs by their values in ascending order.- Returns:
- a comparator
-
getTopComparator
Gets a comparator sorting pairs by their values in descending order.- Returns:
- a comparator
-
isInt
Check if the specified number is an int:n != null && n.intValue() == n.doubleValue()
- Parameters:
n
- Thenumber
to check- Returns:
- true if the given number is an int
-
getMdxIntValue
In Mdxnull
== 0.Sugar for:
null == n ? 0 : n.intValue()
- Parameters:
n
- a number- Returns:
- the int value
-
checkNotNull
Throw an exception ifo
is null.- Parameters:
o
- the object to checkcall
- function call in progress, provided for debug purposeindex
- index of the call argument tested- Throws:
MdxException
- ifo
is null- See Also:
-
errorFunctionCall
Creates an OLAP element that is effectively a function call to a special error function. This function throws andMdxRuntimeException
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 functionerrorMsg
- The error message to print when throwing theexception
- Returns:
- The
FunctionCall
object
-