Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion core/src/main/java/org/apache/druid/math/expr/ExprType.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ public static ExprType asArrayType(@Nullable ExprType elementType)

/**
* Given 2 'input' types, choose the most appropriate combined type, if possible
*
* arrays must be the same type
* if both types are {@link #STRING}, the output type will be preserved as string
* if both types are {@link #LONG}, the output type will be preserved as long
*
*/
@Nullable
public static ExprType operatorAutoTypeConversion(@Nullable ExprType type, @Nullable ExprType other)
Expand All @@ -191,14 +196,19 @@ public static ExprType operatorAutoTypeConversion(@Nullable ExprType type, @Null
return STRING;
}

// otherwise a decimal or integer number
return numericAutoTypeConversion(type, other);
}

/**
* Given 2 'input' types, choose the most appropriate combined type, if possible
*
* arrays must be the same type
* if either type is {@link #STRING}, the output type will be preserved as string
* if both types are {@link #LONG}, the output type will be preserved as long, otherwise {@link #DOUBLE}
*/
@Nullable
public static ExprType functionAutoTypeConversion(@Nullable ExprType type, @Nullable ExprType other)
public static ExprType doubleMathFunctionAutoTypeConversion(@Nullable ExprType type, @Nullable ExprType other)
{
if (type == null || other == null) {
// cannot auto conversion unknown types
Expand All @@ -219,7 +229,40 @@ public static ExprType functionAutoTypeConversion(@Nullable ExprType type, @Null
return numericAutoTypeConversion(type, other);
}

/**
* Given 2 'input' types, choose the most appropriate combined type, if possible
*
* arrays must be the same type
* if either type is {@link #STRING}, the output type will be preserved as string
* any number will be coerced to {@link #LONG}
*/
@Nullable
public static ExprType integerMathFunctionAutoTypeConversion(@Nullable ExprType type, @Nullable ExprType other)
{
if (type == null || other == null) {
// cannot auto conversion unknown types
return null;
}
// arrays cannot be auto converted
if (isArray(type) || isArray(other)) {
if (!type.equals(other)) {
throw new IAE("Cannot implicitly cast %s to %s", type, other);
}
return type;
}
// if either argument is a string, type becomes a string
if (STRING.equals(type) || STRING.equals(other)) {
return STRING;
}

// any number is long
return LONG;
}

/**
* Default best effort numeric type conversion. If both types are {@link #LONG}, returns {@link #LONG}, else
* {@link #DOUBLE}
*/
public static ExprType numericAutoTypeConversion(ExprType type, ExprType other)
{
// all numbers win over longs
Expand Down
Loading