These are protective controls that allow a system operator to set limits on some potentially problematic aspects of SPARQL behaviour. The following limits are currently exposed:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intDefault maximum digits used forMAX_EXPONENT_DIGITSandMAX_FACTORIAL_DIGITSstatic final intDefault maximum length value used for theMAX_LENGTH_PATH_FOR_REDUCTIONcontrolstatic final longDefault maximum permitted regular expression evaluation timestatic intConfigurable maximum number of digits for an exponent calculationstatic intConfigurable maximum number of digits for a factorial calculationstatic intSpecifies the maximum length (inclusive) of a path that will be reduced by expanding it into individual invocations of the path expression linked by intermediate variables.static longMaximum permitted regular expression evaluation time, defaults toDEFAULT_MAX_REGEX_EVALUATION_TIME -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanisReducibleLength(long N) Checks whether a given length of path is considered reduciblestatic voidpreValidateExponentCalculation(double a, double b) Calculates how many digits raisingato the power ofbwould require.static voidCalculates how many digits raisingato the power ofbwould require.static voidCalculates the Stirling approximation for the factorial of a number and uses it to validate whether calculating a factorial would produce too many digits throwing anExprEvalExceptionif it would.
-
Field Details
-
DEFAULT_MAX_DIGITS
public static final int DEFAULT_MAX_DIGITSDefault maximum digits used forMAX_EXPONENT_DIGITSandMAX_FACTORIAL_DIGITS- See Also:
-
MAX_EXPONENT_DIGITS
public static int MAX_EXPONENT_DIGITSConfigurable maximum number of digits for an exponent calculationAny mathematical function that involves exponentiation can require large amounts of computation, and potentially produce an output vastly larger than its inputs. By restricting the maximum number of digits such computations may produce we avoid excessive computation.
-
MAX_FACTORIAL_DIGITS
public static int MAX_FACTORIAL_DIGITSConfigurable maximum number of digits for a factorial calculationA factorial computation may involve many calculations and depending on input produce a vastly larger output than the input. By restricting the maximum number of digits such computations may produce we avoid excessive computation.
-
DEFAULT_MAX_PATH_REDUCTION_LENGTH
public static final int DEFAULT_MAX_PATH_REDUCTION_LENGTHDefault maximum length value used for theMAX_LENGTH_PATH_FOR_REDUCTIONcontrol- See Also:
-
MAX_LENGTH_PATH_FOR_REDUCTION
public static int MAX_LENGTH_PATH_FOR_REDUCTIONSpecifies the maximum length (inclusive) of a path that will be reduced by expanding it into individual invocations of the path expression linked by intermediate variables. Defaults to 5.This control prevents a query using a path like
:x :p{N} :ywith a large value ofNbeing expanded into a massive algebra tree that yields no performance benefits.- See Also:
-
DEFAULT_MAX_REGEX_EVALUATION_TIME
public static final long DEFAULT_MAX_REGEX_EVALUATION_TIMEDefault maximum permitted regular expression evaluation time- See Also:
-
MAX_REGEX_EVALUATION_TIME
public static long MAX_REGEX_EVALUATION_TIMEMaximum permitted regular expression evaluation time, defaults toDEFAULT_MAX_REGEX_EVALUATION_TIMEFor some regular expression patterns the evaluation time can be excessive, especially if a pattern, or the pattern combined with the data leads to catastrophic backtracking. Therefore, we bound the evaluation time by using a special
CharSequence/CharacterIteratorwrapper that enforces a time limit. If the limit is exceeded then anExprEvalExceptionis thrown. This does impose a small performance penalty though this is negligible and considered an acceptable trade-off for the protection it provides.If an application makes extensive use of complex regular expressions, and you are certain that those are not subject to catastrophic backtracking you can either increase the limit appropriately. Or you can set to a zero/negative value to disable the limit entirely.
-
-
Method Details
-
preValidateExponentCalculation
Calculates how many digits raisingato the power ofbwould require. If the number of digits is very large then throw an expression evaluation error to prevent execution.- Parameters:
a- A, base number to raise by the exponentb- B, exponent to raise by- See Also:
-
preValidateExponentCalculation
public static void preValidateExponentCalculation(double a, double b) Calculates how many digits raisingato the power ofbwould require. If the number of digits is very large then throw an expression evaluation error to prevent execution.This is based upon the following mathematical formula, for
a^bthen number of digits isfloor(b * log10(a)) + 1.- Parameters:
a- A, base number to raise by exponentb- B, exponent to raise by- Throws:
ExprEvalException- Thrown if the calculation would require too many digits
-
preValidateFactorial
Calculates the Stirling approximation for the factorial of a number and uses it to validate whether calculating a factorial would produce too many digits throwing anExprEvalExceptionif it would.Based on Python example from Number of Digits in n!
def stirling(n): return floor( ((n+0.5)*log(n) - n + 0.5*log(2*pi))/log(10) ) + 1
Note this method assumes the caller has already validated that the given input is non-negative.
- Parameters:
n- Number to compute the factorial of- Throws:
ExprEvalException- Thrown if calculating the factorial would produce too many digits
-
isReducibleLength
public static boolean isReducibleLength(long N) Checks whether a given length of path is considered reducibleOnly paths that are non-zero length and less than, or equal to, the configured
MAX_LENGTH_PATH_FOR_REDUCTION(default 5) are considered reducible. Anything else is left as-is as either reduction would change semantics (for zero-length paths), or could lead to very large algebra trees which would negatively impact performance.- Parameters:
N- Path length- Returns:
- True if reducible, false otherwise
-