Class SqlIntervalQualifier

  • All Implemented Interfaces:
    java.lang.Cloneable

    public class SqlIntervalQualifier
    extends SqlNode
    Represents an INTERVAL qualifier.

    INTERVAL qualifier is defined as follows:

    <interval qualifier> ::=
       <start field> TO <end field>
      | <single datetime field>
    <start field> ::=
       <non-second primary datetime field>
       [ <left paren> <interval leading field precision> <right paren> ]
    <end field> ::=
       <non-second primary datetime field>
      | SECOND [ <left paren> <interval fractional seconds precision> <right paren> ]
    <single datetime field> ::=
      <non-second primary datetime field>
      [ <left paren> <interval leading field precision> <right paren> ]
      | SECOND [ <left paren> <interval leading field precision>
      [ <comma> <interval fractional seconds precision> ] <right paren> ]
    <primary datetime field> ::=
      <non-second primary datetime field>
      | SECOND
    <non-second primary datetime field> ::= YEAR | MONTH | DAY | HOUR | MINUTE
    <interval fractional seconds precision> ::= <unsigned integer>
    <interval leading field precision> ::= <unsigned integer>

    Examples include:

    • INTERVAL '1:23:45.678' HOUR TO SECOND
    • INTERVAL '1 2:3:4' DAY TO SECOND
    • INTERVAL '1 2:3:4' DAY(4) TO SECOND(4)

    An instance of this class is immutable.

    • Field Detail

      • ZERO

        private static final java.math.BigDecimal ZERO
      • THOUSAND

        private static final java.math.BigDecimal THOUSAND
      • INT_MAX_VALUE_PLUS_ONE

        private static final java.math.BigDecimal INT_MAX_VALUE_PLUS_ONE
      • startPrecision

        private final int startPrecision
      • timeUnitRange

        public final org.apache.calcite.avatica.util.TimeUnitRange timeUnitRange
      • fractionalSecondPrecision

        private final int fractionalSecondPrecision
      • POWERS10

        private static final java.math.BigDecimal[] POWERS10
    • Constructor Detail

      • SqlIntervalQualifier

        public SqlIntervalQualifier​(org.apache.calcite.avatica.util.TimeUnit startUnit,
                                    int startPrecision,
                                    org.apache.calcite.avatica.util.TimeUnit endUnit,
                                    int fractionalSecondPrecision,
                                    SqlParserPos pos)
      • SqlIntervalQualifier

        public SqlIntervalQualifier​(org.apache.calcite.avatica.util.TimeUnit startUnit,
                                    org.apache.calcite.avatica.util.TimeUnit endUnit,
                                    SqlParserPos pos)
    • Method Detail

      • accept

        public <R> R accept​(SqlVisitor<R> visitor)
        Description copied from class: SqlNode
        Accepts a generic visitor.

        Implementations of this method in subtypes simply call the appropriate visit method on the visitor object.

        The type parameter R must be consistent with the type parameter of the visitor.

        Specified by:
        accept in class SqlNode
      • equalsDeep

        public boolean equalsDeep​(SqlNode node,
                                  Litmus litmus)
        Description copied from class: SqlNode
        Returns whether this node is structurally equivalent to another node. Some examples:
        • 1 + 2 is structurally equivalent to 1 + 2
        • 1 + 2 + 3 is structurally equivalent to (1 + 2) + 3, but not to 1 + (2 + 3), because the '+' operator is left-associative
        Specified by:
        equalsDeep in class SqlNode
      • getStartPrecisionPreservingDefault

        public int getStartPrecisionPreservingDefault()
      • useDefaultStartPrecision

        public boolean useDefaultStartPrecision()
        Returns true if start precision is not specified.
      • getFractionalSecondPrecision

        public int getFractionalSecondPrecision​(RelDataTypeSystem typeSystem)
      • getFractionalSecondPrecisionPreservingDefault

        public int getFractionalSecondPrecisionPreservingDefault()
      • useDefaultFractionalSecondPrecision

        public boolean useDefaultFractionalSecondPrecision()
        Returns true if fractional second precision is not specified.
      • getStartUnit

        public org.apache.calcite.avatica.util.TimeUnit getStartUnit()
      • getEndUnit

        public org.apache.calcite.avatica.util.TimeUnit getEndUnit()
      • getUnit

        public org.apache.calcite.avatica.util.TimeUnit getUnit()
        Returns SECOND for both HOUR TO SECOND and SECOND.
      • unparse

        public void unparse​(SqlWriter writer,
                            int leftPrec,
                            int rightPrec)
        Description copied from class: SqlNode
        Writes a SQL representation of this node to a writer.

        The leftPrec and rightPrec parameters give us enough context to decide whether we need to enclose the expression in parentheses. For example, we need parentheses around "2 + 3" if preceded by "5 *". This is because the precedence of the "*" operator is greater than the precedence of the "+" operator.

        The algorithm handles left- and right-associative operators by giving them slightly different left- and right-precedence.

        If SqlWriter.isAlwaysUseParentheses() is true, we use parentheses even when they are not required by the precedence rules.

        For the details of this algorithm, see SqlCall.unparse(org.apache.calcite.sql.SqlWriter, int, int).

        Specified by:
        unparse in class SqlNode
        Parameters:
        writer - Target writer
        leftPrec - The precedence of the SqlNode immediately preceding this node in a depth-first scan of the parse tree
        rightPrec - The precedence of the SqlNode immediately
      • isSingleDatetimeField

        public boolean isSingleDatetimeField()
        Returns whether this interval has a single datetime field.

        Returns true if it is of the form unit, false if it is of the form unit TO unit.

      • isYearMonth

        public final boolean isYearMonth()
      • getIntervalSign

        public int getIntervalSign​(java.lang.String value)
        Returns:
        1 or -1
      • stripLeadingSign

        private java.lang.String stripLeadingSign​(java.lang.String value)
      • isLeadFieldInRange

        private boolean isLeadFieldInRange​(RelDataTypeSystem typeSystem,
                                           java.math.BigDecimal value,
                                           org.apache.calcite.avatica.util.TimeUnit unit)
      • checkLeadFieldInRange

        private void checkLeadFieldInRange​(RelDataTypeSystem typeSystem,
                                           int sign,
                                           java.math.BigDecimal value,
                                           org.apache.calcite.avatica.util.TimeUnit unit,
                                           SqlParserPos pos)
      • isFractionalSecondFieldInRange

        private boolean isFractionalSecondFieldInRange​(java.math.BigDecimal field)
      • isSecondaryFieldInRange

        private boolean isSecondaryFieldInRange​(java.math.BigDecimal field,
                                                org.apache.calcite.avatica.util.TimeUnit unit)
      • normalizeSecondFraction

        private java.math.BigDecimal normalizeSecondFraction​(java.lang.String secondFracStr)
      • fillIntervalValueArray

        private int[] fillIntervalValueArray​(int sign,
                                             java.math.BigDecimal year,
                                             java.math.BigDecimal month)
      • fillIntervalValueArray

        private int[] fillIntervalValueArray​(int sign,
                                             java.math.BigDecimal day,
                                             java.math.BigDecimal hour,
                                             java.math.BigDecimal minute,
                                             java.math.BigDecimal second,
                                             java.math.BigDecimal secondFrac)
      • evaluateIntervalLiteralAsYear

        private int[] evaluateIntervalLiteralAsYear​(RelDataTypeSystem typeSystem,
                                                    int sign,
                                                    java.lang.String value,
                                                    java.lang.String originalValue,
                                                    SqlParserPos pos)
        Validates an INTERVAL literal against a YEAR interval qualifier.
        Throws:
        CalciteContextException - if the interval value is illegal
      • evaluateIntervalLiteralAsYearToMonth

        private int[] evaluateIntervalLiteralAsYearToMonth​(RelDataTypeSystem typeSystem,
                                                           int sign,
                                                           java.lang.String value,
                                                           java.lang.String originalValue,
                                                           SqlParserPos pos)
        Validates an INTERVAL literal against a YEAR TO MONTH interval qualifier.
        Throws:
        CalciteContextException - if the interval value is illegal
      • evaluateIntervalLiteralAsMonth

        private int[] evaluateIntervalLiteralAsMonth​(RelDataTypeSystem typeSystem,
                                                     int sign,
                                                     java.lang.String value,
                                                     java.lang.String originalValue,
                                                     SqlParserPos pos)
        Validates an INTERVAL literal against a MONTH interval qualifier.
        Throws:
        CalciteContextException - if the interval value is illegal
      • evaluateIntervalLiteralAsDay

        private int[] evaluateIntervalLiteralAsDay​(RelDataTypeSystem typeSystem,
                                                   int sign,
                                                   java.lang.String value,
                                                   java.lang.String originalValue,
                                                   SqlParserPos pos)
        Validates an INTERVAL literal against a DAY interval qualifier.
        Throws:
        CalciteContextException - if the interval value is illegal
      • evaluateIntervalLiteralAsDayToHour

        private int[] evaluateIntervalLiteralAsDayToHour​(RelDataTypeSystem typeSystem,
                                                         int sign,
                                                         java.lang.String value,
                                                         java.lang.String originalValue,
                                                         SqlParserPos pos)
        Validates an INTERVAL literal against a DAY TO HOUR interval qualifier.
        Throws:
        CalciteContextException - if the interval value is illegal
      • evaluateIntervalLiteralAsDayToMinute

        private int[] evaluateIntervalLiteralAsDayToMinute​(RelDataTypeSystem typeSystem,
                                                           int sign,
                                                           java.lang.String value,
                                                           java.lang.String originalValue,
                                                           SqlParserPos pos)
        Validates an INTERVAL literal against a DAY TO MINUTE interval qualifier.
        Throws:
        CalciteContextException - if the interval value is illegal
      • evaluateIntervalLiteralAsDayToSecond

        private int[] evaluateIntervalLiteralAsDayToSecond​(RelDataTypeSystem typeSystem,
                                                           int sign,
                                                           java.lang.String value,
                                                           java.lang.String originalValue,
                                                           SqlParserPos pos)
        Validates an INTERVAL literal against a DAY TO SECOND interval qualifier.
        Throws:
        CalciteContextException - if the interval value is illegal
      • evaluateIntervalLiteralAsHour

        private int[] evaluateIntervalLiteralAsHour​(RelDataTypeSystem typeSystem,
                                                    int sign,
                                                    java.lang.String value,
                                                    java.lang.String originalValue,
                                                    SqlParserPos pos)
        Validates an INTERVAL literal against an HOUR interval qualifier.
        Throws:
        CalciteContextException - if the interval value is illegal
      • evaluateIntervalLiteralAsHourToMinute

        private int[] evaluateIntervalLiteralAsHourToMinute​(RelDataTypeSystem typeSystem,
                                                            int sign,
                                                            java.lang.String value,
                                                            java.lang.String originalValue,
                                                            SqlParserPos pos)
        Validates an INTERVAL literal against an HOUR TO MINUTE interval qualifier.
        Throws:
        CalciteContextException - if the interval value is illegal
      • evaluateIntervalLiteralAsHourToSecond

        private int[] evaluateIntervalLiteralAsHourToSecond​(RelDataTypeSystem typeSystem,
                                                            int sign,
                                                            java.lang.String value,
                                                            java.lang.String originalValue,
                                                            SqlParserPos pos)
        Validates an INTERVAL literal against an HOUR TO SECOND interval qualifier.
        Throws:
        CalciteContextException - if the interval value is illegal
      • evaluateIntervalLiteralAsMinute

        private int[] evaluateIntervalLiteralAsMinute​(RelDataTypeSystem typeSystem,
                                                      int sign,
                                                      java.lang.String value,
                                                      java.lang.String originalValue,
                                                      SqlParserPos pos)
        Validates an INTERVAL literal against an MINUTE interval qualifier.
        Throws:
        CalciteContextException - if the interval value is illegal
      • evaluateIntervalLiteralAsMinuteToSecond

        private int[] evaluateIntervalLiteralAsMinuteToSecond​(RelDataTypeSystem typeSystem,
                                                              int sign,
                                                              java.lang.String value,
                                                              java.lang.String originalValue,
                                                              SqlParserPos pos)
        Validates an INTERVAL literal against an MINUTE TO SECOND interval qualifier.
        Throws:
        CalciteContextException - if the interval value is illegal
      • evaluateIntervalLiteralAsSecond

        private int[] evaluateIntervalLiteralAsSecond​(RelDataTypeSystem typeSystem,
                                                      int sign,
                                                      java.lang.String value,
                                                      java.lang.String originalValue,
                                                      SqlParserPos pos)
        Validates an INTERVAL literal against an SECOND interval qualifier.
        Throws:
        CalciteContextException - if the interval value is illegal
      • evaluateIntervalLiteral

        public int[] evaluateIntervalLiteral​(java.lang.String value,
                                             SqlParserPos pos,
                                             RelDataTypeSystem typeSystem)
        Validates an INTERVAL literal according to the rules specified by the interval qualifier. The assumption is made that the interval qualifier has been validated prior to calling this method. Evaluating against an invalid qualifier could lead to strange results.
        Returns:
        field values, never null
        Throws:
        CalciteContextException - if the interval value is illegal
      • parseField

        private java.math.BigDecimal parseField​(java.util.regex.Matcher m,
                                                int i)
      • fieldExceedsPrecisionException

        private CalciteContextException fieldExceedsPrecisionException​(SqlParserPos pos,
                                                                       int sign,
                                                                       java.math.BigDecimal value,
                                                                       org.apache.calcite.avatica.util.TimeUnit type,
                                                                       int precision)