Class RexLiteral

    • Constructor Detail

      • RexLiteral

        RexLiteral​(java.lang.Comparable value,
                   RelDataType type,
                   SqlTypeName typeName)
        Creates a RexLiteral.
    • Method Detail

      • computeDigest

        public final java.lang.String computeDigest​(RexDigestIncludeType includeType)
        Returns a string which concisely describes the definition of this rex literal. Two literals are equivalent if and only if their digests are the same.

        The digest does not contain the expression's identity, but does include the identity of children.

        Technically speaking 1:INT differs from 1:FLOAT, so we need data type in the literal's digest, however we want to avoid extra verbosity of the RelOptNode.getDigest() for readability purposes, so we omit type info in certain cases. For instance, 1:INT becomes 1 (INT is implied by default), however 1:BIGINT always holds the type

        Here's a non-exhaustive list of the "well known cases":

        • Hide "NOT NULL" for not null literals
        • Hide INTEGER, BOOLEAN, SYMBOL, TIME(0), TIMESTAMP(0), DATE(0) types
        • Hide collation when it matches IMPLICIT/COERCIBLE
        • Hide charset when it matches default
        • Hide CHAR(xx) when literal length is equal to the precision of the type. In other words, use 'Bob' instead of 'Bob':CHAR(3)
        • Hide BOOL for AND/OR arguments. In other words, AND(true, null) means null is BOOL.
        • Hide types for literals in simple binary operations (e.g. +, -, *, /, comparison) when type of the other argument is clear. See RexCall.computeDigest(boolean) For instance: =(true. null) means null is BOOL. =($0, null) means the type of null matches the type of $0.
        Parameters:
        includeType - whether the digest should include type or not
        Returns:
        digest
      • valueMatchesType

        public static boolean valueMatchesType​(java.lang.Comparable value,
                                               SqlTypeName typeName,
                                               boolean strict)
        Returns:
        whether value is appropriate for its type (we have rules about these things)
      • shouldIncludeType

        private static RexDigestIncludeType shouldIncludeType​(java.lang.Comparable value,
                                                              RelDataType type)
        Computes if data type can be omitted from the digset.

        For instance, 1:BIGINT has to keep data type while 1:INT should be represented as just 1.

        Implementation assumption: this method should be fast. In fact might call NlsString.getValue() which could decode the string, however we rely on the cache there.

        Parameters:
        value - value of the literal
        type - type of the literal
        Returns:
        NO_TYPE when type can be omitted, ALWAYS otherwise
        See Also:
        computeDigest(RexDigestIncludeType)
      • getTimeUnits

        private static java.util.List<org.apache.calcite.avatica.util.TimeUnit> getTimeUnits​(SqlTypeName typeName)
        Returns a list of the time units covered by an interval type such as HOUR TO SECOND. Adds MILLISECOND if the end is SECOND, to deal with fractional seconds.
      • intervalString

        private java.lang.String intervalString​(java.math.BigDecimal v)
      • pad

        private static void pad​(java.lang.StringBuilder b,
                                java.lang.String s,
                                int width)
      • width

        private static int width​(org.apache.calcite.avatica.util.TimeUnit timeUnit)
      • printAsJava

        public void printAsJava​(java.io.PrintWriter pw)
        Prints the value this literal as a Java string constant.
      • appendAsJava

        private static void appendAsJava​(java.lang.Comparable value,
                                         java.lang.Appendable destination,
                                         SqlTypeName typeName,
                                         boolean java,
                                         RexDigestIncludeType includeType)
        Appends the specified value in the provided destination as a Java string. The value must be consistent with the type, as per valueMatchesType(java.lang.Comparable, org.apache.calcite.sql.type.SqlTypeName, boolean).

        Typical return values:

        • true
        • null
        • "Hello, world!"
        • 1.25
        • 1234ABCD

        The destination where the value is appended must not incur I/O operations. This method is not meant to be used for writing the values to permanent storage.

        Parameters:
        value - a value to be appended to the provided destination as a Java string
        destination - a destination where to append the specified value
        typeName - a type name to be used for the transformation of the value to a Java string
        includeType - an indicator whether to include the data type in the Java representation
        Throws:
        java.lang.IllegalStateException - if the appending to the destination Appendable fails due to I/O
      • fromJdbcString

        public static RexLiteral fromJdbcString​(RelDataType type,
                                                SqlTypeName typeName,
                                                java.lang.String literal)
        Converts a Jdbc string into a RexLiteral. This method accepts a string, as returned by the Jdbc method ResultSet.getString(), and restores the string into an equivalent RexLiteral. It allows one to use Jdbc strings as a common format for data.

        If a null literal is provided, then a null pointer will be returned.

        Parameters:
        type - data type of literal to be read
        typeName - type family of literal
        literal - the (non-SQL encoded) string representation, as returned by the Jdbc call to return a column as a string
        Returns:
        a typed RexLiteral, or null
      • getCalendarFormat

        private static java.lang.String getCalendarFormat​(SqlTypeName typeName)
      • getKind

        public SqlKind getKind()
        Description copied from class: RexNode
        Returns the kind of node this is.
        Overrides:
        getKind in class RexNode
        Returns:
        Node kind, never null
      • isNull

        public boolean isNull()
        Returns whether this literal's value is null.
      • getValue

        public java.lang.Comparable getValue()
        Returns the value of this literal.

        For backwards compatibility, returns DATE. TIME and TIMESTAMP as a Calendar value in UTC time zone.

      • getValue2

        public java.lang.Object getValue2()
        Returns the value of this literal, in the form that the calculator program builder wants it.
      • getValue3

        public java.lang.Object getValue3()
        Returns the value of this literal, in the form that the rex-to-lix translator wants it.
      • getValue4

        public java.lang.Comparable getValue4()
        Returns the value of this literal, in the form that RexInterpreter wants it.
      • getValueAs

        public <T> T getValueAs​(java.lang.Class<T> clazz)
        Returns the value of this literal as an instance of the specified class.

        The following SQL types allow more than one form:

        • CHAR as NlsString or String
        • TIME as TimeString, Integer (milliseconds since midnight), Calendar (in UTC)
        • DATE as DateString, Integer (days since 1970-01-01), Calendar
        • TIMESTAMP as TimestampString, Long (milliseconds since 1970-01-01 00:00:00), Calendar
        • DECIMAL as BigDecimal or Long

        Called with clazz = Comparable, returns the value in its native form.

        Type Parameters:
        T - Return type
        Parameters:
        clazz - Desired return type
        Returns:
        Value of this literal in the desired type
      • booleanValue

        public static boolean booleanValue​(RexNode node)
      • isAlwaysTrue

        public boolean isAlwaysTrue()
        Description copied from class: RexNode
        Returns whether this expression always returns true. (Such as if this expression is equal to the literal TRUE.)
        Overrides:
        isAlwaysTrue in class RexNode
      • isAlwaysFalse

        public boolean isAlwaysFalse()
        Description copied from class: RexNode
        Returns whether this expression always returns false. (Such as if this expression is equal to the literal FALSE.)
        Overrides:
        isAlwaysFalse in class RexNode
      • value

        public static java.lang.Comparable value​(RexNode node)
      • intValue

        public static int intValue​(RexNode node)
      • stringValue

        public static java.lang.String stringValue​(RexNode node)
      • findValue

        private static java.lang.Comparable findValue​(RexNode node)
      • isNullLiteral

        public static boolean isNullLiteral​(RexNode node)
      • equals

        private static boolean equals​(java.lang.Object o1,
                                      java.lang.Object o2)