org.eigenbase.sql
Class SqlIntervalQualifier

java.lang.Object
  extended by org.eigenbase.sql.SqlNode
      extended by org.eigenbase.sql.SqlIntervalQualifier
All Implemented Interfaces:
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:

An instance of this class is immutable.

Since:
Oct 31, 2004
Version:
$Id: //open/dev/farrago/src/org/eigenbase/sql/SqlIntervalQualifier.java#21 $
Author:
Wael Chatila

Nested Class Summary
static class SqlIntervalQualifier.TimeUnit
          Enumeration of time units used to construct an interval.
 
Field Summary
private  SqlIntervalQualifier.TimeUnit endUnit
           
private  int fractionalSecondPrecision
           
private  int startPrecision
           
private  SqlIntervalQualifier.TimeUnit startUnit
           
private static int USE_DEFAULT_PRECISION
           
private  boolean useDefaultFractionalSecondPrecision
           
private  boolean useDefaultStartPrecision
           
 
Fields inherited from class org.eigenbase.sql.SqlNode
emptyArray
 
Constructor Summary
SqlIntervalQualifier(SqlIntervalQualifier.TimeUnit startUnit, int startPrecision, SqlIntervalQualifier.TimeUnit endUnit, int fractionalSecondPrecision, SqlParserPos pos)
           
SqlIntervalQualifier(SqlIntervalQualifier.TimeUnit startUnit, SqlIntervalQualifier.TimeUnit endUnit, SqlParserPos pos)
           
 
Method Summary
<R> R
accept(SqlVisitor<R> visitor)
          Accepts a generic visitor.
 SqlNode clone(SqlParserPos pos)
          Clones a SqlNode with a different position.
static int combineFractionalSecondPrecisionPreservingDefault(SqlIntervalQualifier qual1, SqlIntervalQualifier qual2)
           
static int combineStartPrecisionPreservingDefault(SqlIntervalQualifier qual1, SqlIntervalQualifier qual2)
           
 boolean equalsDeep(SqlNode node, boolean fail)
          Returns whether this node is structurally equivalent to another node.
 int[] evaluateIntervalLiteral(String value)
          Validates an INTERVAL literal according to the rules specified by the interval qualifier.
private  int[] evaluateIntervalLiteralAsDay(int sign, String value)
          Validates an INTERVAL literal against a DAY interval qualifier.
private  int[] evaluateIntervalLiteralAsDayToHour(int sign, String value)
          Validates an INTERVAL literal against a DAY TO HOUR interval qualifier.
private  int[] evaluateIntervalLiteralAsDayToMinute(int sign, String value)
          Validates an INTERVAL literal against a DAY TO MINUTE interval qualifier.
private  int[] evaluateIntervalLiteralAsDayToSecond(int sign, String value)
          Validates an INTERVAL literal against a DAY TO SECOND interval qualifier.
private  int[] evaluateIntervalLiteralAsHour(int sign, String value)
          Validates an INTERVAL literal against an HOUR interval qualifier.
private  int[] evaluateIntervalLiteralAsHourToMinute(int sign, String value)
          Validates an INTERVAL literal against an HOUR TO MINUTE interval qualifier.
private  int[] evaluateIntervalLiteralAsHourToSecond(int sign, String value)
          Validates an INTERVAL literal against an HOUR TO SECOND interval qualifier.
private  int[] evaluateIntervalLiteralAsMinute(int sign, String value)
          Validates an INTERVAL literal against an MINUTE interval qualifier.
private  int[] evaluateIntervalLiteralAsMinuteToSecond(int sign, String value)
          Validates an INTERVAL literal against an MINUTE TO SECOND interval qualifier.
private  int[] evaluateIntervalLiteralAsMonth(int sign, String value)
          Validates an INTERVAL literal against a MONTH interval qualifier.
private  int[] evaluateIntervalLiteralAsSecond(int sign, String value)
          Validates an INTERVAL literal against an SECOND interval qualifier.
private  int[] evaluateIntervalLiteralAsYear(int sign, String value)
          Validates an INTERVAL literal against a YEAR interval qualifier.
private  int[] evaluateIntervalLiteralAsYearToMonth(int sign, String value)
          Validates an INTERVAL literal against a YEAR TO MONTH interval qualifier.
private  int[] fillIntervalValueArray(int sign, int year, int month)
           
private  int[] fillIntervalValueArray(int sign, int day, int hour, int minute, int second, int secondFrac)
           
static long getConversion(SqlIntervalQualifier.TimeUnit unit)
           
static int getDefaultPrecisionId()
           
 SqlIntervalQualifier.TimeUnit getEndUnit()
           
 int getFractionalSecondPrecision()
           
 int getFractionalSecondPrecisionPreservingDefault()
           
private  int getIntervalSign(String value)
           
 int getStartPrecision()
           
 int getStartPrecisionPreservingDefault()
           
 SqlIntervalQualifier.TimeUnit getStartUnit()
           
private  boolean isFractionalSecondFieldInRange(int field)
           
private  boolean isLeadFieldInRange(int field, SqlIntervalQualifier.TimeUnit unit)
           
private  boolean isSecondaryFieldInRange(int field, SqlIntervalQualifier.TimeUnit unit)
           
 boolean isYearMonth()
           
private  int normalizeSecondFraction(String secondFracStr)
           
private  String stripLeadingSign(String value)
           
 void unparse(SqlWriter writer, int leftPrec, int rightPrec)
          Writes a SQL representation of this node to a writer.
 void validate(SqlValidator validator, SqlValidatorScope scope)
          Validates this node.
 
Methods inherited from class org.eigenbase.sql.SqlNode
clone, cloneArray, equalDeep, findValidOptions, getKind, getMonotonicity, getParserPosition, isA, toSqlString, toSqlString, toString, validateExpr
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

USE_DEFAULT_PRECISION

private static final int USE_DEFAULT_PRECISION
See Also:
Constant Field Values

startPrecision

private final int startPrecision

startUnit

private final SqlIntervalQualifier.TimeUnit startUnit

endUnit

private final SqlIntervalQualifier.TimeUnit endUnit

fractionalSecondPrecision

private final int fractionalSecondPrecision

useDefaultStartPrecision

private final boolean useDefaultStartPrecision

useDefaultFractionalSecondPrecision

private final boolean useDefaultFractionalSecondPrecision
Constructor Detail

SqlIntervalQualifier

public SqlIntervalQualifier(SqlIntervalQualifier.TimeUnit startUnit,
                            int startPrecision,
                            SqlIntervalQualifier.TimeUnit endUnit,
                            int fractionalSecondPrecision,
                            SqlParserPos pos)

SqlIntervalQualifier

public SqlIntervalQualifier(SqlIntervalQualifier.TimeUnit startUnit,
                            SqlIntervalQualifier.TimeUnit endUnit,
                            SqlParserPos pos)
Method Detail

validate

public void validate(SqlValidator validator,
                     SqlValidatorScope scope)
Description copied from class: SqlNode
Validates this node.

The typical implementation of this method will make a callback to the validator appropriate to the node type and context. The validator has methods such as SqlValidator.validateLiteral(org.eigenbase.sql.SqlLiteral) for these purposes.

Specified by:
validate in class SqlNode
scope - Validator

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,
                          boolean fail)
Description copied from class: SqlNode
Returns whether this node is structurally equivalent to another node. Some examples:

Specified by:
equalsDeep in class SqlNode

getDefaultPrecisionId

public static int getDefaultPrecisionId()

getStartPrecision

public int getStartPrecision()

getStartPrecisionPreservingDefault

public int getStartPrecisionPreservingDefault()

combineStartPrecisionPreservingDefault

public static int combineStartPrecisionPreservingDefault(SqlIntervalQualifier qual1,
                                                         SqlIntervalQualifier qual2)

getFractionalSecondPrecision

public int getFractionalSecondPrecision()

getFractionalSecondPrecisionPreservingDefault

public int getFractionalSecondPrecisionPreservingDefault()

combineFractionalSecondPrecisionPreservingDefault

public static int combineFractionalSecondPrecisionPreservingDefault(SqlIntervalQualifier qual1,
                                                                    SqlIntervalQualifier qual2)

getStartUnit

public SqlIntervalQualifier.TimeUnit getStartUnit()

getEndUnit

public SqlIntervalQualifier.TimeUnit getEndUnit()

clone

public SqlNode clone(SqlParserPos pos)
Description copied from class: SqlNode
Clones a SqlNode with a different position.

Overrides:
clone in class SqlNode

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.eigenbase.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

isYearMonth

public boolean isYearMonth()

getIntervalSign

private int getIntervalSign(String value)

stripLeadingSign

private String stripLeadingSign(String value)

isLeadFieldInRange

private boolean isLeadFieldInRange(int field,
                                   SqlIntervalQualifier.TimeUnit unit)

isFractionalSecondFieldInRange

private boolean isFractionalSecondFieldInRange(int field)

isSecondaryFieldInRange

private boolean isSecondaryFieldInRange(int field,
                                        SqlIntervalQualifier.TimeUnit unit)

normalizeSecondFraction

private int normalizeSecondFraction(String secondFracStr)

fillIntervalValueArray

private int[] fillIntervalValueArray(int sign,
                                     int year,
                                     int month)

fillIntervalValueArray

private int[] fillIntervalValueArray(int sign,
                                     int day,
                                     int hour,
                                     int minute,
                                     int second,
                                     int secondFrac)

evaluateIntervalLiteralAsYear

private int[] evaluateIntervalLiteralAsYear(int sign,
                                            String value)
Validates an INTERVAL literal against a YEAR interval qualifier.

Returns:
null if the interval value is illegal.

evaluateIntervalLiteralAsYearToMonth

private int[] evaluateIntervalLiteralAsYearToMonth(int sign,
                                                   String value)
Validates an INTERVAL literal against a YEAR TO MONTH interval qualifier.

Returns:
null if the interval value is illegal.

evaluateIntervalLiteralAsMonth

private int[] evaluateIntervalLiteralAsMonth(int sign,
                                             String value)
Validates an INTERVAL literal against a MONTH interval qualifier.

Returns:
null if the interval value is illegal.

evaluateIntervalLiteralAsDay

private int[] evaluateIntervalLiteralAsDay(int sign,
                                           String value)
Validates an INTERVAL literal against a DAY interval qualifier.

Returns:
null if the interval value is illegal.

evaluateIntervalLiteralAsDayToHour

private int[] evaluateIntervalLiteralAsDayToHour(int sign,
                                                 String value)
Validates an INTERVAL literal against a DAY TO HOUR interval qualifier.

Returns:
null if the interval value is illegal.

evaluateIntervalLiteralAsDayToMinute

private int[] evaluateIntervalLiteralAsDayToMinute(int sign,
                                                   String value)
Validates an INTERVAL literal against a DAY TO MINUTE interval qualifier.

Returns:
null if the interval value is illegal.

evaluateIntervalLiteralAsDayToSecond

private int[] evaluateIntervalLiteralAsDayToSecond(int sign,
                                                   String value)
Validates an INTERVAL literal against a DAY TO SECOND interval qualifier.

Returns:
null if the interval value is illegal.

evaluateIntervalLiteralAsHour

private int[] evaluateIntervalLiteralAsHour(int sign,
                                            String value)
Validates an INTERVAL literal against an HOUR interval qualifier.

Returns:
null if the interval value is illegal.

evaluateIntervalLiteralAsHourToMinute

private int[] evaluateIntervalLiteralAsHourToMinute(int sign,
                                                    String value)
Validates an INTERVAL literal against an HOUR TO MINUTE interval qualifier.

Returns:
null if the interval value is illegal.

evaluateIntervalLiteralAsHourToSecond

private int[] evaluateIntervalLiteralAsHourToSecond(int sign,
                                                    String value)
Validates an INTERVAL literal against an HOUR TO SECOND interval qualifier.

Returns:
null if the interval value is illegal.

evaluateIntervalLiteralAsMinute

private int[] evaluateIntervalLiteralAsMinute(int sign,
                                              String value)
Validates an INTERVAL literal against an MINUTE interval qualifier.

Returns:
null if the interval value is illegal.

evaluateIntervalLiteralAsMinuteToSecond

private int[] evaluateIntervalLiteralAsMinuteToSecond(int sign,
                                                      String value)
Validates an INTERVAL literal against an MINUTE TO SECOND interval qualifier.

Returns:
null if the interval value is illegal.

evaluateIntervalLiteralAsSecond

private int[] evaluateIntervalLiteralAsSecond(int sign,
                                              String value)
Validates an INTERVAL literal against an SECOND interval qualifier.

Returns:
null if the interval value is illegal.

evaluateIntervalLiteral

public int[] evaluateIntervalLiteral(String value)
Validates an INTERVAL literal according to the rules specified by the interval qualifier. The assumption is made that the interval qualfier has been validated prior to calling this method. Evaluating against an invalid qualifier could lead to strange results.

Returns:
null if the interval value is illegal.

getConversion

public static long getConversion(SqlIntervalQualifier.TimeUnit unit)