diff --git a/core/src/main/java/io/substrait/expression/ExpressionCreator.java b/core/src/main/java/io/substrait/expression/ExpressionCreator.java index 3738feed3..f7fe5fd63 100644 --- a/core/src/main/java/io/substrait/expression/ExpressionCreator.java +++ b/core/src/main/java/io/substrait/expression/ExpressionCreator.java @@ -18,50 +18,132 @@ import java.util.UUID; import java.util.concurrent.TimeUnit; +/** + * Utility class for creating Substrait expression objects. + * + *
This class provides factory methods for constructing various types of expressions, literals,
+ * and function invocations used in Substrait query plans.
+ */
public class ExpressionCreator {
private ExpressionCreator() {}
+ /**
+ * Creates a null literal expression with a specific type.
+ *
+ * @param t the type of the null literal
+ * @return a NullLiteral expression
+ */
public static Expression.NullLiteral typedNull(Type t) {
return Expression.NullLiteral.builder().type(t).build();
}
+ /**
+ * Creates a boolean literal expression.
+ *
+ * @param nullable whether the literal can be null
+ * @param value the boolean value
+ * @return a BoolLiteral expression
+ */
public static Expression.BoolLiteral bool(boolean nullable, boolean value) {
return Expression.BoolLiteral.builder().nullable(nullable).value(value).build();
}
+ /**
+ * Creates an 8-bit integer literal expression.
+ *
+ * @param nullable whether the literal can be null
+ * @param value the integer value
+ * @return an I8Literal expression
+ */
public static Expression.I8Literal i8(boolean nullable, int value) {
return Expression.I8Literal.builder().nullable(nullable).value(value).build();
}
+ /**
+ * Creates a 16-bit integer literal expression.
+ *
+ * @param nullable whether the literal can be null
+ * @param value the integer value
+ * @return an I16Literal expression
+ */
public static Expression.I16Literal i16(boolean nullable, int value) {
return Expression.I16Literal.builder().nullable(nullable).value(value).build();
}
+ /**
+ * Creates a 32-bit integer literal expression.
+ *
+ * @param nullable whether the literal can be null
+ * @param value the integer value
+ * @return an I32Literal expression
+ */
public static Expression.I32Literal i32(boolean nullable, int value) {
return Expression.I32Literal.builder().nullable(nullable).value(value).build();
}
+ /**
+ * Creates a 64-bit integer literal expression.
+ *
+ * @param nullable whether the literal can be null
+ * @param value the long value
+ * @return an I64Literal expression
+ */
public static Expression.I64Literal i64(boolean nullable, long value) {
return Expression.I64Literal.builder().nullable(nullable).value(value).build();
}
+ /**
+ * Creates a 32-bit floating point literal expression.
+ *
+ * @param nullable whether the literal can be null
+ * @param value the float value
+ * @return an FP32Literal expression
+ */
public static Expression.FP32Literal fp32(boolean nullable, float value) {
return Expression.FP32Literal.builder().nullable(nullable).value(value).build();
}
+ /**
+ * Creates a 64-bit floating point literal expression.
+ *
+ * @param nullable whether the literal can be null
+ * @param value the double value
+ * @return an FP64Literal expression
+ */
public static Expression.FP64Literal fp64(boolean nullable, double value) {
return Expression.FP64Literal.builder().nullable(nullable).value(value).build();
}
+ /**
+ * Creates a string literal expression.
+ *
+ * @param nullable whether the literal can be null
+ * @param value the string value
+ * @return a StrLiteral expression
+ */
public static Expression.StrLiteral string(boolean nullable, String value) {
return Expression.StrLiteral.builder().nullable(nullable).value(value).build();
}
+ /**
+ * Creates a binary literal expression from a ByteString.
+ *
+ * @param nullable whether the literal can be null
+ * @param value the ByteString value
+ * @return a BinaryLiteral expression
+ */
public static Expression.BinaryLiteral binary(boolean nullable, ByteString value) {
return Expression.BinaryLiteral.builder().nullable(nullable).value(value).build();
}
+ /**
+ * Creates a binary literal expression from a byte array.
+ *
+ * @param nullable whether the literal can be null
+ * @param value the byte array value
+ * @return a BinaryLiteral expression
+ */
public static Expression.BinaryLiteral binary(boolean nullable, byte[] value) {
return Expression.BinaryLiteral.builder()
.nullable(nullable)
@@ -69,11 +151,23 @@ public static Expression.BinaryLiteral binary(boolean nullable, byte[] value) {
.build();
}
+ /**
+ * Creates a date literal expression.
+ *
+ * @param nullable whether the literal can be null
+ * @param value the date value as days since epoch
+ * @return a DateLiteral expression
+ */
public static Expression.DateLiteral date(boolean nullable, int value) {
return Expression.DateLiteral.builder().nullable(nullable).value(value).build();
}
/**
+ * Creates a time literal expression.
+ *
+ * @param nullable whether the literal can be null
+ * @param value the time value in microseconds since midnight
+ * @return a TimeLiteral expression
* @deprecated Time is deprecated in favor of PrecisionTime
*/
@Deprecated
@@ -81,6 +175,14 @@ public static Expression.TimeLiteral time(boolean nullable, long value) {
return Expression.TimeLiteral.builder().nullable(nullable).value(value).build();
}
+ /**
+ * Creates a precision time literal expression.
+ *
+ * @param nullable whether the literal can be null
+ * @param value the time value
+ * @param precision the precision of the time
+ * @return a PrecisionTimeLiteral expression
+ */
public static Expression.PrecisionTimeLiteral precisionTime(
boolean nullable, long value, int precision) {
return Expression.PrecisionTimeLiteral.builder()
@@ -108,6 +210,11 @@ public static Expression.PrecisionTimeLiteral precisionTime(boolean nullable, Lo
}
/**
+ * Creates a timestamp literal expression.
+ *
+ * @param nullable whether the literal can be null
+ * @param value the timestamp value in microseconds since epoch
+ * @return a TimestampLiteral expression
* @deprecated Timestamp is deprecated in favor of PrecisionTimestamp
*/
@Deprecated
@@ -116,6 +223,11 @@ public static Expression.TimestampLiteral timestamp(boolean nullable, long value
}
/**
+ * Creates a timestamp literal expression from a LocalDateTime.
+ *
+ * @param nullable whether the literal can be null
+ * @param value the LocalDateTime value (interpreted as UTC)
+ * @return a TimestampLiteral expression
* @deprecated Timestamp is deprecated in favor of PrecisionTimestamp
*/
@Deprecated
@@ -127,6 +239,17 @@ public static Expression.TimestampLiteral timestamp(boolean nullable, LocalDateT
}
/**
+ * Creates a timestamp literal expression from date/time components.
+ *
+ * @param nullable whether the literal can be null
+ * @param year the year
+ * @param month the month (1-12)
+ * @param dayOfMonth the day of month (1-31)
+ * @param hour the hour (0-23)
+ * @param minute the minute (0-59)
+ * @param second the second (0-59)
+ * @param micros the microseconds (0-999999)
+ * @return a TimestampLiteral expression
* @deprecated Timestamp is deprecated in favor of PrecisionTimestamp
*/
@Deprecated
@@ -146,6 +269,11 @@ public static Expression.TimestampLiteral timestamp(
}
/**
+ * Creates a timestamp with timezone literal expression.
+ *
+ * @param nullable whether the literal can be null
+ * @param value the timestamp value in microseconds since epoch
+ * @return a TimestampTZLiteral expression
* @deprecated TimestampTZ is deprecated in favor of PrecisionTimestampTZ
*/
@Deprecated
@@ -154,6 +282,11 @@ public static Expression.TimestampTZLiteral timestampTZ(boolean nullable, long v
}
/**
+ * Creates a timestamp with timezone literal expression from an Instant.
+ *
+ * @param nullable whether the literal can be null
+ * @param value the Instant value
+ * @return a TimestampTZLiteral expression
* @deprecated TimestampTZ is deprecated in favor of PrecisionTimestampTZ
*/
@Deprecated
@@ -164,6 +297,14 @@ public static Expression.TimestampTZLiteral timestampTZ(boolean nullable, Instan
return timestampTZ(nullable, epochMicro);
}
+ /**
+ * Creates a precision timestamp literal expression.
+ *
+ * @param nullable whether the literal can be null
+ * @param value the timestamp value
+ * @param precision the precision of the timestamp
+ * @return a PrecisionTimestampLiteral expression
+ */
public static Expression.PrecisionTimestampLiteral precisionTimestamp(
boolean nullable, long value, int precision) {
return Expression.PrecisionTimestampLiteral.builder()
@@ -193,6 +334,14 @@ public static Expression.PrecisionTimestampLiteral precisionTimestamp(
return precisionTimestamp(nullable, epochNano, 9);
}
+ /**
+ * Creates a precision timestamp with timezone literal expression.
+ *
+ * @param nullable whether the literal can be null
+ * @param value the timestamp value
+ * @param precision the precision of the timestamp
+ * @return a PrecisionTimestampTZLiteral expression
+ */
public static Expression.PrecisionTimestampTZLiteral precisionTimestampTZ(
boolean nullable, long value, int precision) {
return Expression.PrecisionTimestampTZLiteral.builder()
@@ -202,6 +351,14 @@ public static Expression.PrecisionTimestampTZLiteral precisionTimestampTZ(
.build();
}
+ /**
+ * Creates an interval year literal expression.
+ *
+ * @param nullable whether the literal can be null
+ * @param years the number of years
+ * @param months the number of months
+ * @return an IntervalYearLiteral expression
+ */
public static Expression.IntervalYearLiteral intervalYear(
boolean nullable, int years, int months) {
return Expression.IntervalYearLiteral.builder()
@@ -211,10 +368,28 @@ public static Expression.IntervalYearLiteral intervalYear(
.build();
}
+ /**
+ * Creates an interval day literal expression with default subseconds and precision.
+ *
+ * @param nullable whether the literal can be null
+ * @param days the number of days
+ * @param seconds the number of seconds
+ * @return an IntervalDayLiteral expression
+ */
public static Expression.IntervalDayLiteral intervalDay(boolean nullable, int days, int seconds) {
return intervalDay(nullable, days, seconds, 0, 0);
}
+ /**
+ * Creates an interval day literal expression with subseconds and precision.
+ *
+ * @param nullable whether the literal can be null
+ * @param days the number of days
+ * @param seconds the number of seconds
+ * @param subseconds the subsecond component
+ * @param precision the precision of subseconds
+ * @return an IntervalDayLiteral expression
+ */
public static Expression.IntervalDayLiteral intervalDay(
boolean nullable, int days, int seconds, long subseconds, int precision) {
return Expression.IntervalDayLiteral.builder()
@@ -226,6 +401,18 @@ public static Expression.IntervalDayLiteral intervalDay(
.build();
}
+ /**
+ * Creates an interval compound literal expression.
+ *
+ * @param nullable whether the literal can be null
+ * @param years the number of years
+ * @param months the number of months
+ * @param days the number of days
+ * @param seconds the number of seconds
+ * @param subseconds the subsecond component
+ * @param precision the precision of subseconds
+ * @return an IntervalCompoundLiteral expression
+ */
public static Expression.IntervalCompoundLiteral intervalCompound(
boolean nullable,
int years,
@@ -245,6 +432,13 @@ public static Expression.IntervalCompoundLiteral intervalCompound(
.build();
}
+ /**
+ * Creates a UUID literal expression from a ByteString.
+ *
+ * @param nullable whether the literal can be null
+ * @param uuid the UUID value as a ByteString
+ * @return a UUIDLiteral expression
+ */
public static Expression.UUIDLiteral uuid(boolean nullable, ByteString uuid) {
ByteBuffer bb = uuid.asReadOnlyByteBuffer();
return Expression.UUIDLiteral.builder()
@@ -253,22 +447,58 @@ public static Expression.UUIDLiteral uuid(boolean nullable, ByteString uuid) {
.build();
}
+ /**
+ * Creates a UUID literal expression from a UUID object.
+ *
+ * @param nullable whether the literal can be null
+ * @param uuid the UUID value
+ * @return a UUIDLiteral expression
+ */
public static Expression.UUIDLiteral uuid(boolean nullable, UUID uuid) {
return Expression.UUIDLiteral.builder().nullable(nullable).value(uuid).build();
}
+ /**
+ * Creates a fixed-length character literal expression.
+ *
+ * @param nullable whether the literal can be null
+ * @param str the string value
+ * @return a FixedCharLiteral expression
+ */
public static Expression.FixedCharLiteral fixedChar(boolean nullable, String str) {
return Expression.FixedCharLiteral.builder().nullable(nullable).value(str).build();
}
+ /**
+ * Creates a variable-length character literal expression.
+ *
+ * @param nullable whether the literal can be null
+ * @param str the string value
+ * @param len the maximum length
+ * @return a VarCharLiteral expression
+ */
public static Expression.VarCharLiteral varChar(boolean nullable, String str, int len) {
return Expression.VarCharLiteral.builder().nullable(nullable).value(str).length(len).build();
}
+ /**
+ * Creates a fixed-length binary literal expression from a ByteString.
+ *
+ * @param nullable whether the literal can be null
+ * @param bytes the ByteString value
+ * @return a FixedBinaryLiteral expression
+ */
public static Expression.FixedBinaryLiteral fixedBinary(boolean nullable, ByteString bytes) {
return Expression.FixedBinaryLiteral.builder().nullable(nullable).value(bytes).build();
}
+ /**
+ * Creates a fixed-length binary literal expression from a byte array.
+ *
+ * @param nullable whether the literal can be null
+ * @param bytes the byte array value
+ * @return a FixedBinaryLiteral expression
+ */
public static Expression.FixedBinaryLiteral fixedBinary(boolean nullable, byte[] bytes) {
return Expression.FixedBinaryLiteral.builder()
.nullable(nullable)
@@ -276,6 +506,15 @@ public static Expression.FixedBinaryLiteral fixedBinary(boolean nullable, byte[]
.build();
}
+ /**
+ * Creates a decimal literal expression from a ByteString.
+ *
+ * @param nullable whether the literal can be null
+ * @param value the ByteString value containing the decimal in two's complement format
+ * @param precision the precision of the decimal
+ * @param scale the scale of the decimal
+ * @return a DecimalLiteral expression
+ */
public static Expression.DecimalLiteral decimal(
boolean nullable, ByteString value, int precision, int scale) {
return Expression.DecimalLiteral.builder()
@@ -286,6 +525,15 @@ public static Expression.DecimalLiteral decimal(
.build();
}
+ /**
+ * Creates a decimal literal expression from a BigDecimal.
+ *
+ * @param nullable whether the literal can be null
+ * @param value the BigDecimal value
+ * @param precision the precision of the decimal
+ * @param scale the scale of the decimal
+ * @return a DecimalLiteral expression
+ */
public static Expression.DecimalLiteral decimal(
boolean nullable, BigDecimal value, int precision, int scale) {
byte[] twosComplement = DecimalUtil.encodeDecimalIntoBytes(value, scale, 16);
@@ -298,11 +546,26 @@ public static Expression.DecimalLiteral decimal(
.build();
}
+ /**
+ * Creates a map literal expression.
+ *
+ * @param nullable whether the literal can be null
+ * @param values the map of key-value pairs
+ * @return a MapLiteral expression
+ */
public static Expression.MapLiteral map(
boolean nullable, Map Note: This class cannot be used to construct an empty list. To create an empty list, use
+ * Note: This method cannot be used to construct an empty list. To create an empty list, use
* {@link ExpressionCreator#emptyList(boolean, Type)} which returns an {@link
* Expression.EmptyListLiteral}.
+ *
+ * @param nullable whether the literal can be null
+ * @param values the expression values in the nested list
+ * @return a NestedList expression
*/
public static Expression.NestedList nestedList(boolean nullable, List Use {@link Expression.ScalarFunctionInvocation#builder()} directly to specify other
+ * parameters, e.g. options
+ *
+ * @param declaration the function declaration
+ * @param outputType the output type of the function
+ * @param arguments the function arguments
+ * @return a ScalarFunctionInvocation expression
*/
public static Expression.ScalarFunctionInvocation scalarFunction(
SimpleExtension.ScalarFunctionVariant declaration,
@@ -480,8 +857,18 @@ public static Expression.ScalarFunctionInvocation scalarFunction(
}
/**
- * Use {@link AggregateFunctionInvocation#builder()} directly to specify other parameters, e.g.
+ * Creates an aggregate function invocation.
+ *
+ * Use {@link AggregateFunctionInvocation#builder()} directly to specify other parameters, e.g.
* options
+ *
+ * @param declaration the function declaration
+ * @param outputType the output type of the function
+ * @param phase the aggregation phase
+ * @param sort the sort fields
+ * @param invocation the aggregation invocation type
+ * @param arguments the function arguments
+ * @return an AggregateFunctionInvocation
*/
public static AggregateFunctionInvocation aggregateFunction(
SimpleExtension.AggregateFunctionVariant declaration,
@@ -500,6 +887,17 @@ public static AggregateFunctionInvocation aggregateFunction(
.build();
}
+ /**
+ * Creates an aggregate function invocation with varargs.
+ *
+ * @param declaration the function declaration
+ * @param outputType the output type of the function
+ * @param phase the aggregation phase
+ * @param sort the sort fields
+ * @param invocation the aggregation invocation type
+ * @param arguments the function arguments
+ * @return an AggregateFunctionInvocation
+ */
public static AggregateFunctionInvocation aggregateFunction(
SimpleExtension.AggregateFunctionVariant declaration,
Type outputType,
@@ -512,8 +910,22 @@ public static AggregateFunctionInvocation aggregateFunction(
}
/**
- * Use {@link Expression.WindowFunctionInvocation#builder()} directly to specify other parameters,
- * e.g. options
+ * Creates a window function invocation.
+ *
+ * Use {@link Expression.WindowFunctionInvocation#builder()} directly to specify other
+ * parameters, e.g. options
+ *
+ * @param declaration the function declaration
+ * @param outputType the output type of the function
+ * @param phase the aggregation phase
+ * @param sort the sort fields
+ * @param invocation the aggregation invocation type
+ * @param partitionBy the partition by expressions
+ * @param boundsType the window bounds type
+ * @param lowerBound the lower bound of the window
+ * @param upperBound the upper bound of the window
+ * @param arguments the function arguments
+ * @return a WindowFunctionInvocation expression
*/
public static Expression.WindowFunctionInvocation windowFunction(
SimpleExtension.WindowFunctionVariant declaration,
@@ -541,8 +953,20 @@ public static Expression.WindowFunctionInvocation windowFunction(
}
/**
- * Use {@link ConsistentPartitionWindow.WindowRelFunctionInvocation#builder()} directly to specify
- * other parameters, e.g. options
+ * Creates a window relation function invocation.
+ *
+ * Use {@link ConsistentPartitionWindow.WindowRelFunctionInvocation#builder()} directly to
+ * specify other parameters, e.g. options
+ *
+ * @param declaration the function declaration
+ * @param outputType the output type of the function
+ * @param phase the aggregation phase
+ * @param invocation the aggregation invocation type
+ * @param boundsType the window bounds type
+ * @param lowerBound the lower bound of the window
+ * @param upperBound the upper bound of the window
+ * @param arguments the function arguments
+ * @return a WindowRelFunctionInvocation
*/
public static ConsistentPartitionWindow.WindowRelFunctionInvocation windowRelFunction(
SimpleExtension.WindowFunctionVariant declaration,
@@ -565,6 +989,21 @@ public static ConsistentPartitionWindow.WindowRelFunctionInvocation windowRelFun
.build();
}
+ /**
+ * Creates a window function invocation with varargs.
+ *
+ * @param declaration the function declaration
+ * @param outputType the output type of the function
+ * @param phase the aggregation phase
+ * @param sort the sort fields
+ * @param invocation the aggregation invocation type
+ * @param partitionBy the partition by expressions
+ * @param boundsType the window bounds type
+ * @param lowerBound the lower bound of the window
+ * @param upperBound the upper bound of the window
+ * @param arguments the function arguments
+ * @return a WindowFunctionInvocation expression
+ */
public static Expression.WindowFunctionInvocation windowFunction(
SimpleExtension.WindowFunctionVariant declaration,
Type outputType,
@@ -589,6 +1028,14 @@ public static Expression.WindowFunctionInvocation windowFunction(
Arrays.asList(arguments));
}
+ /**
+ * Creates a cast expression.
+ *
+ * @param type the target type to cast to
+ * @param expression the expression to cast
+ * @param failureBehavior the failure behavior for the cast
+ * @return a Cast expression
+ */
public static Expression cast(
Type type, Expression expression, Expression.FailureBehavior failureBehavior) {
return Expression.Cast.builder()