Class SparqlLimits

java.lang.Object
org.apache.jena.sparql.SparqlLimits

public class SparqlLimits extends Object
Static limits that control what's permitted for some SPARQL compilation, optimisation and expression evaluation

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 Details

    • DEFAULT_MAX_DIGITS

      public static final int DEFAULT_MAX_DIGITS
      Default maximum digits used for MAX_EXPONENT_DIGITS and MAX_FACTORIAL_DIGITS
      See Also:
    • MAX_EXPONENT_DIGITS

      public static int MAX_EXPONENT_DIGITS
      Configurable maximum number of digits for an exponent calculation

      Any 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_DIGITS
      Configurable maximum number of digits for a factorial calculation

      A 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_LENGTH
      Default maximum length value used for the MAX_LENGTH_PATH_FOR_REDUCTION control
      See Also:
    • MAX_LENGTH_PATH_FOR_REDUCTION

      public static int MAX_LENGTH_PATH_FOR_REDUCTION
      Specifies 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} :y with a large value of N being 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_TIME
      Default maximum permitted regular expression evaluation time
      See Also:
    • MAX_REGEX_EVALUATION_TIME

      public static long MAX_REGEX_EVALUATION_TIME
      Maximum permitted regular expression evaluation time, defaults to DEFAULT_MAX_REGEX_EVALUATION_TIME

      For 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/CharacterIterator wrapper that enforces a time limit. If the limit is exceeded then an ExprEvalException is 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

      public static void preValidateExponentCalculation(BigInteger a, BigInteger b)
      Calculates how many digits raising a to the power of b would 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 exponent
      b - B, exponent to raise by
      See Also:
    • preValidateExponentCalculation

      public static void preValidateExponentCalculation(double a, double b)
      Calculates how many digits raising a to the power of b would 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^b then number of digits is floor(b * log10(a)) + 1.

      Parameters:
      a - A, base number to raise by exponent
      b - B, exponent to raise by
      Throws:
      ExprEvalException - Thrown if the calculation would require too many digits
    • preValidateFactorial

      public static void preValidateFactorial(BigInteger n)
      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 an ExprEvalException if 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 reducible

      Only 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