@PublicEvolving public final class Expressions extends Object
$("myField").plus(10).abs()
This class contains static methods for referencing table columns, creating literals, and
building more complex Expression
chains. ApiExpressions
are pure
API entities that are further translated into ResolvedExpressions
under the hood.
For fluent definition of expressions and easier readability, we recommend adding a star import to the methods of this class:
import static org.apache.flink.table.api.Expressions.*;
Check the documentation for more programming language specific APIs, for example, by using Scala implicits.
Modifier and Type | Field and Description |
---|---|
static ApiExpression |
CURRENT_RANGE
Offset constant to be used in the
following clause of Over windows. |
static ApiExpression |
CURRENT_ROW
Offset constant to be used in the
following clause of Over windows. |
static ApiExpression |
UNBOUNDED_RANGE
Offset constant to be used in the
preceding clause of unbounded Over windows. |
static ApiExpression |
UNBOUNDED_ROW
Offset constant to be used in the
preceding clause of unbounded Over windows. |
Constructor and Description |
---|
Expressions() |
Modifier and Type | Method and Description |
---|---|
static ApiExpression |
$(String name)
Creates an unresolved reference to a table's column.
|
static ApiExpression |
and(Object predicate0,
Object predicate1,
Object... predicates)
Boolean AND in three-valued logic.
|
static ApiExpression |
array(Object head,
Object... tail)
Creates an array of literals.
|
static ApiExpression |
atan2(Object y,
Object x)
Calculates the arc tangent of a given coordinate.
|
static ApiExpression |
call(Class<? extends UserDefinedFunction> function,
Object... arguments)
A call to an unregistered, inline function.
|
static ApiExpression |
call(String path,
Object... arguments)
A call to a function that will be looked up in a catalog.
|
static ApiExpression |
call(UserDefinedFunction function,
Object... arguments)
A call to an unregistered, inline function.
|
static ApiExpression |
callSql(String sqlExpression)
A call to a SQL expression.
|
static ApiExpression |
coalesce(Object... args)
Returns the first argument that is not NULL.
|
static ApiExpression |
col(String name)
Creates an unresolved reference to a table's column.
|
static ApiExpression |
concat(Object string,
Object... strings)
Returns the string that results from concatenating the arguments.
|
static ApiExpression |
concatWs(Object separator,
Object string,
Object... strings)
Returns the string that results from concatenating the arguments and separator.
|
static ApiExpression |
convertTz(Object dateStr,
Object tzFrom,
Object tzTo)
Converts a datetime dateStr (with default ISO timestamp format 'yyyy-MM-dd HH:mm:ss') from
time zone tzFrom to time zone tzTo.
|
static ApiExpression |
currentDatabase()
Return the current database, the return type of this expression is
DataTypes.STRING() . |
static ApiExpression |
currentDate()
Returns the current SQL date in local time zone, the return type of this expression is
DataTypes.DATE() . |
static ApiExpression |
currentTime()
Returns the current SQL time in local time zone, the return type of this expression is
DataTypes.TIME() . |
static ApiExpression |
currentTimestamp()
Returns the current SQL timestamp in local time zone, the return type of this expression is
DataTypes.TIMESTAMP_WITH_LOCAL_TIME_ZONE() . |
static ApiExpression |
currentWatermark(Object rowtimeAttribute)
Returns the current watermark for the given rowtime attribute, or
NULL if no common
watermark of all upstream operations is available at the current operation in the pipeline. |
static ApiExpression |
dateFormat(Object timestamp,
Object format)
Formats a timestamp as a string using a specified format.
|
static ApiExpression |
e()
Returns a value that is closer than any other value to e.
|
static ApiExpression |
fromUnixtime(Object unixtime)
Converts unix timestamp (seconds since '1970-01-01 00:00:00' UTC) to datetime string in the
"yyyy-MM-dd HH:mm:ss" format.
|
static ApiExpression |
fromUnixtime(Object unixtime,
Object format)
Converts unix timestamp (seconds since '1970-01-01 00:00:00' UTC) to datetime string in the
given format.
|
static ApiExpression |
ifThenElse(Object condition,
Object ifTrue,
Object ifFalse)
Ternary conditional operator that decides which of two other expressions should be evaluated
based on a evaluated boolean condition.
|
static ApiExpression |
jsonArray(JsonOnNull onNull,
Object... values)
Builds a JSON array string from a list of values.
|
static ApiExpression |
jsonArrayAgg(JsonOnNull onNull,
Object itemExpr)
Builds a JSON object string by aggregating items into an array.
|
static ApiExpression |
jsonObject(JsonOnNull onNull,
Object... keyValues)
Builds a JSON object string from a list of key-value pairs.
|
static ApiExpression |
jsonObjectAgg(JsonOnNull onNull,
Object keyExpr,
Object valueExpr)
Builds a JSON object string by aggregating key-value expressions into a single JSON object.
|
static ApiExpression |
jsonString(Object value)
Serializes a value into JSON.
|
static ApiExpression |
lit(Object v)
Creates a SQL literal.
|
static ApiExpression |
lit(Object v,
DataType dataType)
Creates a SQL literal of a given
DataType . |
static ApiExpression |
localTime()
Returns the current SQL time in local time zone, the return type of this expression is
DataTypes.TIME() , this is a synonym for currentTime() . |
static ApiExpression |
localTimestamp()
Returns the current SQL timestamp in local time zone, the return type of this expression is
DataTypes.TIMESTAMP() . |
static ApiExpression |
log(Object value)
Calculates the logarithm of the given value.
|
static ApiExpression |
log(Object base,
Object value)
Calculates the logarithm of the given value to the given base.
|
static ApiExpression |
map(Object key,
Object value,
Object... tail)
Creates a map of expressions.
|
static ApiExpression |
mapFromArrays(Object key,
Object value)
Creates a map from an array of keys and an array of values.
|
static ApiExpression |
negative(Object v)
Returns negative numeric.
|
static ApiExpression |
not(Object expression)
Inverts a given boolean expression.
|
static ApiExpression |
nullOf(DataType dataType)
Returns a null literal value of a given data type.
|
static ApiExpression |
nullOf(TypeInformation<?> typeInfo)
Deprecated.
This method will be removed in future versions as it uses the old type system. It
is recommended to use
nullOf(DataType) instead which uses the new type system
based on DataTypes . Please make sure to use either the old or the new type system
consistently to avoid unintended behavior. See the website documentation for more
information. |
static ApiExpression |
or(Object predicate0,
Object predicate1,
Object... predicates)
Boolean OR in three-valued logic.
|
static ApiExpression |
pi()
Returns a value that is closer than any other value to pi.
|
static ApiExpression |
rand()
Returns a pseudorandom double value between 0.0 (inclusive) and 1.0 (exclusive).
|
static ApiExpression |
rand(Object seed)
Returns a pseudorandom double value between 0.0 (inclusive) and 1.0 (exclusive) with a
initial seed.
|
static ApiExpression |
randInteger(Object bound)
Returns a pseudorandom integer value between 0 (inclusive) and the specified value
(exclusive).
|
static ApiExpression |
randInteger(Object seed,
Object bound)
Returns a pseudorandom integer value between 0 (inclusive) and the specified value
(exclusive) with a initial seed.
|
static ApiExpression |
range(int start,
int end)
Indicates an index based range, which can be used in columns selection.
|
static ApiExpression |
range(String start,
String end)
Indicates a range from 'start' to 'end', which can be used in columns selection.
|
static ApiExpression |
row(Object head,
Object... tail)
Creates a row of expressions.
|
static ApiExpression |
rowInterval(Long rows)
Creates an interval of rows.
|
static ApiExpression |
sourceWatermark()
Source watermark declaration for
Schema . |
static ApiExpression |
temporalOverlaps(Object leftTimePoint,
Object leftTemporal,
Object rightTimePoint,
Object rightTemporal)
Determines whether two anchored time intervals overlap.
|
static ApiExpression |
timestampDiff(TimePointUnit timePointUnit,
Object timePoint1,
Object timePoint2)
Returns the (signed) number of
TimePointUnit between timePoint1 and timePoint2. |
static ApiExpression |
toDate(Object dateStr)
Converts the given date string with format 'yyyy-MM-dd' to
DataTypes.DATE() . |
static ApiExpression |
toDate(Object dateStr,
Object format)
Converts the date string with the specified format to
DataTypes.DATE() . |
static ApiExpression |
toTimestamp(Object timestampStr)
Converts the given date time string with format 'yyyy-MM-dd HH:mm:ss' under the 'UTC+0' time
zone to
DataTypes.TIMESTAMP() . |
static ApiExpression |
toTimestamp(Object timestampStr,
Object format)
Converts the given time string with the specified format under the 'UTC+0' time zone to
DataTypes.TIMESTAMP() . |
static ApiExpression |
toTimestampLtz(Object numericEpochTime,
Object precision)
Converts a numeric type epoch time to
DataTypes.TIMESTAMP_LTZ(int) . |
static ApiExpression |
unixTimestamp()
Gets the current unix timestamp in seconds.
|
static ApiExpression |
unixTimestamp(Object timestampStr)
Converts the given date time string with format 'yyyy-MM-dd HH:mm:ss' to unix timestamp (in
seconds), using the time zone specified in the table config.
|
static ApiExpression |
unixTimestamp(Object timestampStr,
Object format)
Converts the given date time string with the specified format to unix timestamp (in seconds),
using the specified timezone in table config.
|
static ApiExpression |
uuid()
Returns an UUID (Universally Unique Identifier) string (e.g.,
"3d3c68f7-f608-473f-b60c-b0c44ad4cc4e") according to RFC 4122 type 4 (pseudo randomly
generated) UUID.
|
static ApiExpression |
withAllColumns()
Creates an expression that selects all columns.
|
static ApiExpression |
withColumns(Object head,
Object... tail)
Creates an expression that selects a range of columns.
|
static ApiExpression |
withoutColumns(Object head,
Object... tail)
Creates an expression that selects all columns except for the given range of columns.
|
public static final ApiExpression UNBOUNDED_ROW
preceding
clause of unbounded Over
windows.
Use this constant for a time interval. Unbounded over windows start with the first row of a
partition.public static final ApiExpression UNBOUNDED_RANGE
preceding
clause of unbounded Over
windows.
Use this constant for a row-count interval. Unbounded over windows start with the first row
of a partition.public static final ApiExpression CURRENT_ROW
following
clause of Over
windows. Use this
for setting the upper bound of the window to the current row.public static final ApiExpression CURRENT_RANGE
following
clause of Over
windows. Use this
for setting the upper bound of the window to the sort key of the current row, i.e., all rows
with the same sort key as the current row are included in the window.public static ApiExpression $(String name)
Example:
tab.select($("key"), $("value"))
col(String)
,
withAllColumns()
public static ApiExpression col(String name)
Because $(String)
is not supported by every JVM language due to the dollar sign,
this method provides a synonym with the same behavior.
Example:
tab.select(col("key"), col("value"))
withAllColumns()
public static ApiExpression lit(Object v)
The data type is derived from the object's class and its value.
For example:
lit(12)
leads to INT
lit("abc")
leads to CHAR(3)
lit(new BigDecimal("123.45"))
leads to DECIMAL(5, 2)
See ValueDataTypeConverter
for a list of supported literal values.
public static ApiExpression lit(Object v, DataType dataType)
DataType
.
The method lit(Object)
is preferred as it extracts the DataType
automatically. Use this method only when necessary. The class of v
must be supported
according to the LogicalType.supportsInputConversion(Class)
.
public static ApiExpression range(String start, String end)
Example:
Table table = ...
table.select(withColumns(range(b, c)))
public static ApiExpression range(int start, int end)
Example:
Table table = ...
table.select(withColumns(range(3, 4)))
public static ApiExpression and(Object predicate0, Object predicate1, Object... predicates)
public static ApiExpression or(Object predicate0, Object predicate1, Object... predicates)
public static ApiExpression not(Object expression)
This method supports a three-valued logic by preserving NULL
. This means if the
input expression is NULL
, the result will also be NULL
.
The resulting type is nullable if and only if the input type is nullable.
Examples:
not(lit(true)) // false
not(lit(false)) // true
not(lit(null, DataTypes.BOOLEAN())) // null
public static ApiExpression currentDate()
DataTypes.DATE()
.public static ApiExpression currentTime()
DataTypes.TIME()
.public static ApiExpression currentTimestamp()
DataTypes.TIMESTAMP_WITH_LOCAL_TIME_ZONE()
.public static ApiExpression currentWatermark(Object rowtimeAttribute)
NULL
if no common
watermark of all upstream operations is available at the current operation in the pipeline.
The function returns the watermark with the same type as the rowtime attribute, but with
an adjusted precision of 3. For example, if the rowtime attribute is TIMESTAMP_LTZ(9)
, the function will return TIMESTAMP_LTZ(3)
.
If no watermark has been emitted yet, the function will return NULL
. Users must
take care of this when comparing against it, e.g. in order to filter out late data you can
use
WHERE CURRENT_WATERMARK(ts) IS NULL OR ts > CURRENT_WATERMARK(ts)
public static ApiExpression currentDatabase()
DataTypes.STRING()
.public static ApiExpression localTime()
DataTypes.TIME()
, this is a synonym for currentTime()
.public static ApiExpression localTimestamp()
DataTypes.TIMESTAMP()
.public static ApiExpression toDate(Object dateStr)
DataTypes.DATE()
.dateStr
- The date string.DataTypes.DATE()
type.public static ApiExpression toDate(Object dateStr, Object format)
DataTypes.DATE()
.dateStr
- The date string.format
- The format of the string.DataTypes.DATE()
type.public static ApiExpression toTimestamp(Object timestampStr)
DataTypes.TIMESTAMP()
.timestampStr
- The date time string.DataTypes.TIMESTAMP()
type.public static ApiExpression toTimestamp(Object timestampStr, Object format)
DataTypes.TIMESTAMP()
.timestampStr
- The date time string.format
- The format of the string.DataTypes.TIMESTAMP()
type.public static ApiExpression toTimestampLtz(Object numericEpochTime, Object precision)
DataTypes.TIMESTAMP_LTZ(int)
.
The supported precision is 0 or 3:
numericEpochTime
- The epoch time with numeric type.precision
- The precision to indicate the epoch time is in second or millisecond.DataTypes.TIMESTAMP_LTZ(int)
type.public static ApiExpression temporalOverlaps(Object leftTimePoint, Object leftTemporal, Object rightTimePoint, Object rightTemporal)
leftEnd >= rightStart && rightEnd >= leftStart
.
It evaluates: leftEnd >= rightStart && rightEnd >= leftStart
e.g.
temporalOverlaps(
lit("2:55:00").toTime(),
lit(1).hours(),
lit("3:30:00").toTime(),
lit(2).hours()
)
leads to true
public static ApiExpression dateFormat(Object timestamp, Object format)
For example dataFormat($("time"), "%Y, %d %M")
results in strings formatted as
"2017, 05 May".
timestamp
- The timestamp to format as string.format
- The format of the string.public static ApiExpression timestampDiff(TimePointUnit timePointUnit, Object timePoint1, Object timePoint2)
TimePointUnit
between timePoint1 and timePoint2.
For example, timestampDiff(TimePointUnit.DAY, lit("2016-06-15").toDate(),
lit("2016-06-18").toDate()
leads to 3.
timePointUnit
- The unit to compute diff.timePoint1
- The first point in time.timePoint2
- The second point in time.public static ApiExpression convertTz(Object dateStr, Object tzFrom, Object tzTo)
dateStr
- the date time stringtzFrom
- the original time zonetzTo
- the target time zonepublic static ApiExpression fromUnixtime(Object unixtime)
unixtime
- The unix timestamp with numeric type.public static ApiExpression fromUnixtime(Object unixtime, Object format)
unixtime
- The unix timestamp with numeric type.format
- The format of the string.public static ApiExpression unixTimestamp()
public static ApiExpression unixTimestamp(Object timestampStr)
timestampStr
- The date time string.public static ApiExpression unixTimestamp(Object timestampStr, Object format)
timestampStr
- The date time string.format
- The format of the date time string.public static ApiExpression array(Object head, Object... tail)
public static ApiExpression row(Object head, Object... tail)
public static ApiExpression map(Object key, Object value, Object... tail)
table.select(
map(
"key1", 1,
"key2", 2,
"key3", 3
))
Note keys and values should have the same types for all entries.
public static ApiExpression mapFromArrays(Object key, Object value)
table.select(
mapFromArrays(
array("key1", "key2", "key3"),
array(1, 2, 3)
))
Note both arrays should have the same length.
public static ApiExpression rowInterval(Long rows)
public static ApiExpression pi()
public static ApiExpression e()
public static ApiExpression rand()
public static ApiExpression rand(Object seed)
public static ApiExpression randInteger(Object bound)
public static ApiExpression randInteger(Object seed, Object bound)
public static ApiExpression concat(Object string, Object... strings)
public static ApiExpression atan2(Object y, Object x)
public static ApiExpression negative(Object v)
public static ApiExpression concatWs(Object separator, Object string, Object... strings)
Note: this function does not skip empty strings. However, it does skip any NULL values after the separator argument.
public static ApiExpression uuid()
public static ApiExpression nullOf(DataType dataType)
e.g. nullOf(DataTypes.INT())
public static ApiExpression nullOf(TypeInformation<?> typeInfo)
nullOf(DataType)
instead which uses the new type system
based on DataTypes
. Please make sure to use either the old or the new type system
consistently to avoid unintended behavior. See the website documentation for more
information.public static ApiExpression log(Object value)
public static ApiExpression log(Object base, Object value)
public static ApiExpression sourceWatermark()
Schema
.
This is a marker function that doesn't have concrete runtime implementation. It can only
be used as a single expression in Schema.Builder.watermark(String, Expression)
. The
declaration will be pushed down into a table source that implements the SupportsSourceWatermark
interface. The source will emit system-defined watermarks
afterwards.
Please check the documentation whether the connector supports source watermarks.
public static ApiExpression ifThenElse(Object condition, Object ifTrue, Object ifFalse)
e.g. ifThenElse($("f0") > 5, "A", "B") leads to "A"
condition
- boolean conditionifTrue
- expression to be evaluated if condition holdsifFalse
- expression to be evaluated if condition does not holdpublic static ApiExpression coalesce(Object... args)
If all arguments are NULL, it returns NULL as well. The return type is the least restrictive, common type of all of its arguments. The return type is nullable if all arguments are nullable as well.
Examples:
// Returns "default"
coalesce(null, "default")
// Returns the first non-null value among f0 and f1, or "default" if f0 and f1 are both null
coalesce($("f0"), $("f1"), "default")
args
- the input expressions.public static ApiExpression withAllColumns()
This expression is a synonym of $("*"). It is semantically equal to SELECT *
in
SQL when used in a projection.
Example:
tab.select(withAllColumns())
public static ApiExpression withColumns(Object head, Object... tail)
A range can either be index-based or name-based. Indices start at 1 and boundaries are inclusive.
e.g. withColumns(range("b", "c")) or withColumns($("*"))
public static ApiExpression withoutColumns(Object head, Object... tail)
A range can either be index-based or name-based. Indices start at 1 and boundaries are inclusive.
e.g. withoutColumns(range("b", "c")) or withoutColumns($("c"))
public static ApiExpression jsonObject(JsonOnNull onNull, Object... keyValues)
is an even-numbered list of alternating key/value pairs. Note that keys
must be non-NULL
string literals, while values may be arbitrary expressions.
This function returns a JSON string. The onNull
behavior defines how to
treat NULL
values.
Values which are created from another JSON construction function call (jsonObject
,
jsonArray
) are inserted directly rather than as a string. This allows building nested
JSON structures.
Examples:
// {}
jsonObject(JsonOnNull.NULL)
// "{\"K1\":\"V1\",\"K2\":\"V2\"}"
// {"K1":"V1","K2":"V2"}
jsonObject(JsonOnNull.NULL, "K1", "V1", "K2", "V2")
// Expressions as values
jsonObject(JsonOnNull.NULL, "orderNo", $("orderId"))
// ON NULL
jsonObject(JsonOnNull.NULL, "K1", nullOf(DataTypes.STRING())) // "{\"K1\":null}"
jsonObject(JsonOnNull.ABSENT, "K1", nullOf(DataTypes.STRING())) // "{}"
// {"K1":{"K2":"V"}}
jsonObject(JsonOnNull.NULL, "K1", jsonObject(JsonOnNull.NULL, "K2", "V"))
jsonArray(JsonOnNull, Object...)
public static ApiExpression jsonObjectAgg(JsonOnNull onNull, Object keyExpr, Object valueExpr)
The key expression must return a non-nullable character string. Value expressions can be
arbitrary, including other JSON functions. If a value is NULL
, the onNull
behavior defines what to do.
Note that keys must be unique. If a key occurs multiple times, an error will be thrown.
This function is currently not supported in OVER
windows.
Examples:
// "{\"Apple\":2,\"Banana\":17,\"Orange\":0}"
orders.select(jsonObjectAgg(JsonOnNull.NULL, $("product"), $("cnt")))
public static ApiExpression jsonString(Object value)
This function returns a JSON string containing the serialized value. If the value is
null
, the function returns null
.
Examples:
// null
jsonString(nullOf(DataTypes.INT()))
jsonString(1) // "1"
jsonString(true) // "true"
jsonString("Hello, World!") // "\"Hello, World!\""
jsonString(Arrays.asList(1, 2)) // "[1,2]"
public static ApiExpression jsonArray(JsonOnNull onNull, Object... values)
This function returns a JSON string. The values can be arbitrary expressions. The onNull
behavior defines how to treat NULL
values.
Elements which are created from another JSON construction function call (jsonObject
, jsonArray
) are inserted directly rather than as a string. This allows
building nested JSON structures.
Examples:
// "[]"
jsonArray(JsonOnNull.NULL)
// "[1,\"2\"]"
jsonArray(JsonOnNull.NULL, 1, "2")
// Expressions as values
jsonArray(JsonOnNull.NULL, $("orderId"))
// ON NULL
jsonArray(JsonOnNull.NULL, nullOf(DataTypes.STRING())) // "[null]"
jsonArray(JsonOnNull.ABSENT, nullOf(DataTypes.STRING())) // "[]"
// "[[1]]"
jsonArray(JsonOnNull.NULL, jsonArray(JsonOnNull.NULL, 1))
jsonObject(JsonOnNull, Object...)
public static ApiExpression jsonArrayAgg(JsonOnNull onNull, Object itemExpr)
Item expressions can be arbitrary, including other JSON functions. If a value is NULL
, the onNull
behavior defines what to do.
This function is currently not supported in OVER
windows, unbounded session
windows, or hop windows.
Examples:
// "[\"Apple\",\"Banana\",\"Orange\"]"
orders.select(jsonArrayAgg(JsonOnNull.NULL, $("product")))
public static ApiExpression call(String path, Object... arguments)
Moreover each function can either be a temporary function or permanent one (which is stored in an external catalog).
Based on that two properties the resolution order for looking up a function based on the
provided functionName
is following:
TableEnvironment.useCatalog(String)
,
TableEnvironment.useDatabase(String)
,
TableEnvironment.createTemporaryFunction(java.lang.String, java.lang.Class<? extends org.apache.flink.table.functions.UserDefinedFunction>)
,
TableEnvironment.createTemporarySystemFunction(java.lang.String, java.lang.Class<? extends org.apache.flink.table.functions.UserDefinedFunction>)
public static ApiExpression call(UserDefinedFunction function, Object... arguments)
For functions that have been registered before and are identified by a name, use call(String, Object...)
.
public static ApiExpression call(Class<? extends UserDefinedFunction> function, Object... arguments)
For functions that have been registered before and are identified by a name, use call(String, Object...)
.
public static ApiExpression callSql(String sqlExpression)
The given string is parsed and translated into an Expression
during planning. Only
the translated expression is evaluated during runtime.
Note: Currently, calls are limited to simple scalar expressions. Calls to aggregate or table-valued functions are not supported. Sub-queries are also not allowed.
Copyright © 2014–2024 The Apache Software Foundation. All rights reserved.