Skip to content

Commit 85e0b31

Browse files
committed
Display PPL types when failing to resolve a function
Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
1 parent 139d858 commit 85e0b31

3 files changed

Lines changed: 23 additions & 14 deletions

File tree

core/src/main/java/org/opensearch/sql/exception/ExpressionEvaluationException.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77

88
/** Exception for Expression Evaluation. */
99
public class ExpressionEvaluationException extends QueryEngineException {
10+
1011
public ExpressionEvaluationException(String message) {
1112
super(message);
1213
}
14+
15+
public ExpressionEvaluationException(String message, Throwable cause) {
16+
super(message, cause);
17+
}
1318
}

core/src/main/java/org/opensearch/sql/expression/function/PPLFuncImpTable.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -146,26 +146,29 @@ public RexNode resolve(
146146
}
147147
}
148148
} catch (Exception e) {
149-
throw new IllegalArgumentException(
149+
throw new ExpressionEvaluationException(
150150
String.format(
151151
"Cannot resolve function: %s, arguments: %s, caused by: %s",
152-
functionName, argTypes, e.getMessage()),
152+
functionName, getActualSignature(argTypes), e.getMessage()),
153153
e);
154154
}
155-
StringJoiner joiner = new StringJoiner(",");
155+
StringJoiner allowedSignatures = new StringJoiner(",");
156156
for (var implement : implementList) {
157-
joiner.add(implement.getKey().typeChecker().getAllowedSignatures());
157+
allowedSignatures.add(implement.getKey().typeChecker().getAllowedSignatures());
158158
}
159-
String actualSignature =
160-
"["
161-
+ argTypes.stream()
162-
.map(OpenSearchTypeFactory::convertRelDataTypeToExprType)
163-
.map(Objects::toString)
164-
.collect(Collectors.joining(","))
165-
+ "]";
166159
throw new ExpressionEvaluationException(
167160
String.format(
168-
"%s function expects {%s}, but got %s", functionName, joiner, actualSignature));
161+
"%s function expects {%s}, but got %s",
162+
functionName, allowedSignatures, getActualSignature(argTypes)));
163+
}
164+
165+
private static String getActualSignature(List<RelDataType> argTypes) {
166+
return "["
167+
+ argTypes.stream()
168+
.map(OpenSearchTypeFactory::convertRelDataTypeToExprType)
169+
.map(Objects::toString)
170+
.collect(Collectors.joining(","))
171+
+ "]";
169172
}
170173

171174
@SuppressWarnings({"UnusedReturnValue", "SameParameterValue"})

ppl/src/test/java/org/opensearch/sql/ppl/calcite/CalcitePPLFunctionTypeTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,11 @@ public void testIfWithWrongType() {
8787
String pplIncompatibleType =
8888
"source=EMP | eval if_name = if(EMPNO > 6, 'Jack', 1) | fields if_name";
8989
Throwable t2 =
90-
Assert.assertThrows(IllegalArgumentException.class, () -> getRelNode(pplIncompatibleType));
90+
Assert.assertThrows(
91+
ExpressionEvaluationException.class, () -> getRelNode(pplIncompatibleType));
9192
verifyErrorMessageContains(
9293
t2,
93-
"Cannot resolve function: IF, arguments: [BOOLEAN, VARCHAR, INTEGER], caused by: Can't find"
94+
"Cannot resolve function: IF, arguments: [BOOLEAN,STRING,INTEGER], caused by: Can't find"
9495
+ " leastRestrictive type for [VARCHAR, INTEGER]");
9596
}
9697

0 commit comments

Comments
 (0)