org.eigenbase.rex
Class RexLiteral

java.lang.Object
  extended by org.eigenbase.rex.RexNode
      extended by org.eigenbase.rex.RexLiteral

public class RexLiteral
extends RexNode

Constant value in a row-expression.

There are several methods for creating literals in RexBuilder: RexBuilder.makeLiteral(boolean) and so forth.

How is the value stored? In that respect, the class is somewhat of a black box. There is a getValue() method which returns the value as an object, but the type of that value is implementation detail, and it is best that your code does not depend upon that knowledge. It is better to use task-oriented methods such as getValue2() and toJavaString(java.lang.Comparable, org.eigenbase.sql.type.SqlTypeName).

The allowable types and combinations are:

TypeName Meaing Value type
SqlTypeName.NULL The null value. It has its own special type. null
SqlTypeName.BOOLEAN Boolean, namely TRUE, FALSE or UNKNOWN. Boolean, or null represents the UNKNOWN value
SqlTypeName.DECIMAL Exact number, for example 0, -.5, 12345. BigDecimal
SqlTypeName.DOUBLE Approximate number, for example 6.023E-23. BigDecimal
SqlTypeName.DATE Date, for example DATE '1969-04'29' Calendar
SqlTypeName.TIME Time, for example TIME '18:37:42.567' Calendar
SqlTypeName.TIMESTAMP Timestamp, for example TIMESTAMP '1969-04-29 18:37:42.567' Calendar
SqlTypeName.CHAR Character constant, for example 'Hello, world!', '', _N'Bonjour', _ISO-8859-1'It''s superman!' COLLATE SHIFT_JIS$ja_JP$2. These are always CHAR, never VARCHAR. NlsString
SqlTypeName.BINARY Binary constant, for example X'7F34'. (The number of hexits must be even; see above.) These constants are always BINARY, never VARBINARY. ByteBuffer
SqlTypeName.SYMBOL A symbol is a special type used to make parsing easier; it is not part of the SQL standard, and is not exposed to end-users. It is used to hold a flag, such as the LEADING flag in a call to the function TRIM([LEADING|TRAILING|BOTH] chars FROM string). A class which implements the Enum14.Value interface

Since:
Nov 24, 2003
Version:
$Id: //open/dev/farrago/src/org/eigenbase/rex/RexLiteral.java#30 $
Author:
jhyde

Field Summary
private  RelDataType type
          The real type of this literal, as reported by getType().
private  SqlTypeName typeName
          An indication of the broad type of this literal -- even if its type isn't a SQL type.
private  Comparable value
          The value of this literal.
 
Fields inherited from class org.eigenbase.rex.RexNode
digest, EMPTY_ARRAY
 
Constructor Summary
RexLiteral(Comparable value, RelDataType type, SqlTypeName typeName)
          Creates a RexLiteral.
 
Method Summary
<R> R
accept(RexVisitor<R> visitor)
          Accepts a visitor, dispatching to the right overloaded visitXxx method.
static boolean booleanValue(RexNode node)
           
 RexLiteral clone()
           
 boolean equals(Object obj)
           
private static boolean equals(Object o1, Object o2)
           
private static Comparable findValue(RexNode node)
           
static RexLiteral fromJdbcString(RelDataType type, SqlTypeName typeName, String literal)
          Converts a Jdbc string into a RexLiteral.
private static String getCalendarFormat(SqlTypeName typeName)
           
 RexKind getKind()
          Returns the kind of node this is.
 RelDataType getType()
           
 SqlTypeName getTypeName()
           
 Comparable getValue()
          Returns the value of this literal.
 Object getValue2()
          Returns the value of this literal, in the form that the calculator program builder wants it.
 int hashCode()
           
static int intValue(RexNode node)
           
 boolean isAlwaysTrue()
          Returns whether this expression always returns true.
static boolean isNullLiteral(RexNode node)
           
private static void printAsJava(Comparable value, PrintWriter pw, SqlTypeName typeName, boolean java)
          Prints a value as a Java string.
 void printAsJava(PrintWriter pw)
          Prints the value this literal as a Java string constant.
private static void printDatetime(PrintWriter pw, ZonelessDatetime datetime, Comparable value)
           
static String stringValue(RexNode node)
           
private static String toJavaString(Comparable value, SqlTypeName typeName)
           
static boolean valueMatchesType(Comparable value, SqlTypeName typeName)
           
 
Methods inherited from class org.eigenbase.rex.RexNode
isA, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

value

private final Comparable value
The value of this literal. Must be consistent with its type, as per valueMatchesType(java.lang.Comparable, org.eigenbase.sql.type.SqlTypeName). For example, you can't store an Integer value here just because you feel like it -- all numbers are represented by a BigDecimal. But since this field is private, it doesn't really matter how the values are stored.


type

private final RelDataType type
The real type of this literal, as reported by getType().


typeName

private final SqlTypeName typeName
An indication of the broad type of this literal -- even if its type isn't a SQL type. Sometimes this will be different than the SQL type; for example, all exact numbers, including integers have typeName SqlTypeName.DECIMAL. See valueMatchesType(java.lang.Comparable, org.eigenbase.sql.type.SqlTypeName) for the definitive story.

Constructor Detail

RexLiteral

RexLiteral(Comparable value,
           RelDataType type,
           SqlTypeName typeName)
Creates a RexLiteral.

"Precondition:"
type != null, valueMatchesType(value,typeName), (value == null) == type.isNullable()
Method Detail

valueMatchesType

public static boolean valueMatchesType(Comparable value,
                                       SqlTypeName typeName)
Returns:
whether value is appropriate for its type (we have rules about these things)

toJavaString

private static String toJavaString(Comparable value,
                                   SqlTypeName typeName)

printAsJava

public void printAsJava(PrintWriter pw)
Prints the value this literal as a Java string constant.


printAsJava

private static void printAsJava(Comparable value,
                                PrintWriter pw,
                                SqlTypeName typeName,
                                boolean java)
Prints a value as a Java string. The value must be consistent with the type, as per valueMatchesType(java.lang.Comparable, org.eigenbase.sql.type.SqlTypeName).

Typical return values:

Parameters:
value - Value
pw - Writer to write to
typeName - Type family

printDatetime

private static void printDatetime(PrintWriter pw,
                                  ZonelessDatetime datetime,
                                  Comparable value)

fromJdbcString

public static RexLiteral fromJdbcString(RelDataType type,
                                        SqlTypeName typeName,
                                        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 String getCalendarFormat(SqlTypeName typeName)

getTypeName

public SqlTypeName getTypeName()

getType

public RelDataType getType()
Specified by:
getType in class RexNode

getKind

public RexKind getKind()
Description copied from class: RexNode
Returns the kind of node this is.

Overrides:
getKind in class RexNode
Returns:
A RexKind value, never null

getValue

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

"Postcondition:"
valueMatchesType(return, typeName)

getValue2

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


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

equals

public boolean equals(Object obj)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

intValue

public static int intValue(RexNode node)

stringValue

public static String stringValue(RexNode node)

findValue

private static Comparable findValue(RexNode node)

isNullLiteral

public static boolean isNullLiteral(RexNode node)

clone

public RexLiteral clone()
Specified by:
clone in class RexNode

equals

private static boolean equals(Object o1,
                              Object o2)

accept

public <R> R accept(RexVisitor<R> visitor)
Description copied from class: RexNode
Accepts a visitor, dispatching to the right overloaded visitXxx method.

Also see RexProgram.apply(RexVisitor, RexNode[], RexNode), which applies a visitor to several expressions simultaneously.

Specified by:
accept in class RexNode