Class SqlIntervalQualifier
- java.lang.Object
-
- org.apache.calcite.sql.SqlNode
-
- org.apache.calcite.sql.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 Summary
Fields Modifier and Type Field Description private int
fractionalSecondPrecision
private static java.math.BigDecimal
INT_MAX_VALUE_PLUS_ONE
private static java.math.BigDecimal[]
POWERS10
private int
startPrecision
private static java.math.BigDecimal
THOUSAND
org.apache.calcite.avatica.util.TimeUnitRange
timeUnitRange
private static java.math.BigDecimal
ZERO
-
Fields inherited from class org.apache.calcite.sql.SqlNode
EMPTY_ARRAY, pos
-
-
Constructor Summary
Constructors Constructor Description SqlIntervalQualifier(org.apache.calcite.avatica.util.TimeUnit startUnit, int startPrecision, org.apache.calcite.avatica.util.TimeUnit endUnit, int fractionalSecondPrecision, SqlParserPos pos)
SqlIntervalQualifier(org.apache.calcite.avatica.util.TimeUnit startUnit, org.apache.calcite.avatica.util.TimeUnit endUnit, SqlParserPos pos)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description <R> R
accept(SqlVisitor<R> visitor)
Accepts a generic visitor.private void
checkLeadFieldInRange(RelDataTypeSystem typeSystem, int sign, java.math.BigDecimal value, org.apache.calcite.avatica.util.TimeUnit unit, SqlParserPos pos)
SqlNode
clone(SqlParserPos pos)
Clones a SqlNode with a different position.static int
combineFractionalSecondPrecisionPreservingDefault(RelDataTypeSystem typeSystem, SqlIntervalQualifier qual1, SqlIntervalQualifier qual2)
static int
combineStartPrecisionPreservingDefault(RelDataTypeSystem typeSystem, SqlIntervalQualifier qual1, SqlIntervalQualifier qual2)
boolean
equalsDeep(SqlNode node, Litmus litmus)
Returns whether this node is structurally equivalent to another node.int[]
evaluateIntervalLiteral(java.lang.String value, SqlParserPos pos, RelDataTypeSystem typeSystem)
Validates an INTERVAL literal according to the rules specified by the interval qualifier.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.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.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.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.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.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.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.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.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.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.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.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.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.private CalciteContextException
fieldExceedsPrecisionException(SqlParserPos pos, int sign, java.math.BigDecimal value, org.apache.calcite.avatica.util.TimeUnit type, int precision)
private int[]
fillIntervalValueArray(int sign, java.math.BigDecimal year, java.math.BigDecimal month)
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)
org.apache.calcite.avatica.util.TimeUnit
getEndUnit()
int
getFractionalSecondPrecision(RelDataTypeSystem typeSystem)
int
getFractionalSecondPrecisionPreservingDefault()
int
getIntervalSign(java.lang.String value)
int
getStartPrecision(RelDataTypeSystem typeSystem)
int
getStartPrecisionPreservingDefault()
org.apache.calcite.avatica.util.TimeUnit
getStartUnit()
org.apache.calcite.avatica.util.TimeUnit
getUnit()
ReturnsSECOND
for bothHOUR TO SECOND
andSECOND
.private CalciteContextException
invalidValueException(SqlParserPos pos, java.lang.String value)
private boolean
isFractionalSecondFieldInRange(java.math.BigDecimal field)
private boolean
isLeadFieldInRange(RelDataTypeSystem typeSystem, java.math.BigDecimal value, org.apache.calcite.avatica.util.TimeUnit unit)
private boolean
isSecondaryFieldInRange(java.math.BigDecimal field, org.apache.calcite.avatica.util.TimeUnit unit)
boolean
isSingleDatetimeField()
Returns whether this interval has a single datetime field.boolean
isYearMonth()
private java.math.BigDecimal
normalizeSecondFraction(java.lang.String secondFracStr)
private java.math.BigDecimal
parseField(java.util.regex.Matcher m, int i)
private java.lang.String
stripLeadingSign(java.lang.String value)
SqlTypeName
typeName()
void
unparse(SqlWriter writer, int leftPrec, int rightPrec)
Writes a SQL representation of this node to a writer.boolean
useDefaultFractionalSecondPrecision()
Returnstrue
if fractional second precision is not specified.boolean
useDefaultStartPrecision()
Returnstrue
if start precision is not specified.void
validate(SqlValidator validator, SqlValidatorScope scope)
Validates this node.-
Methods inherited from class org.apache.calcite.sql.SqlNode
clone, clone, cloneArray, equalDeep, equalDeep, equalsDeep, findValidOptions, getKind, getMonotonicity, getParserPosition, isA, toSqlString, toSqlString, toString, validateExpr
-
-
-
-
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
-
typeName
public SqlTypeName typeName()
-
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.apache.calcite.sql.SqlLiteral)
for these purposes.
-
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 thevisitor object
.The type parameter
R
must be consistent with the type parameter of the visitor.
-
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 classSqlNode
-
getStartPrecision
public int getStartPrecision(RelDataTypeSystem typeSystem)
-
getStartPrecisionPreservingDefault
public int getStartPrecisionPreservingDefault()
-
useDefaultStartPrecision
public boolean useDefaultStartPrecision()
Returnstrue
if start precision is not specified.
-
combineStartPrecisionPreservingDefault
public static int combineStartPrecisionPreservingDefault(RelDataTypeSystem typeSystem, SqlIntervalQualifier qual1, SqlIntervalQualifier qual2)
-
getFractionalSecondPrecision
public int getFractionalSecondPrecision(RelDataTypeSystem typeSystem)
-
getFractionalSecondPrecisionPreservingDefault
public int getFractionalSecondPrecisionPreservingDefault()
-
useDefaultFractionalSecondPrecision
public boolean useDefaultFractionalSecondPrecision()
Returnstrue
if fractional second precision is not specified.
-
combineFractionalSecondPrecisionPreservingDefault
public static int combineFractionalSecondPrecisionPreservingDefault(RelDataTypeSystem typeSystem, SqlIntervalQualifier qual1, SqlIntervalQualifier qual2)
-
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()
ReturnsSECOND
for bothHOUR TO SECOND
andSECOND
.
-
clone
public SqlNode clone(SqlParserPos pos)
Description copied from class:SqlNode
Clones a SqlNode with a different position.
-
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
andrightPrec
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)
.
-
isSingleDatetimeField
public boolean isSingleDatetimeField()
Returns whether this interval has a single datetime field.Returns
true
if it is of the formunit
,false
if it is of the formunit 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)
-
invalidValueException
private CalciteContextException invalidValueException(SqlParserPos pos, java.lang.String value)
-
fieldExceedsPrecisionException
private CalciteContextException fieldExceedsPrecisionException(SqlParserPos pos, int sign, java.math.BigDecimal value, org.apache.calcite.avatica.util.TimeUnit type, int precision)
-
-