Skip to content

Commit 6b2e491

Browse files
committed
Utilize coercion
Signed-off-by: Tomoyuki Morita <[email protected]>
1 parent 9a2b08e commit 6b2e491

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,13 @@ public static boolean hasString(List<RexNode> rexNodeList) {
182182
.anyMatch(t -> t == ExprCoreType.STRING);
183183
}
184184

185+
public static boolean hasAny(List<RexNode> rexNodeList) {
186+
return rexNodeList.stream()
187+
.map(RexNode::getType)
188+
.map(OpenSearchTypeFactory::convertRelDataTypeToExprType)
189+
.anyMatch(t -> t == ExprCoreType.UNDEFINED);
190+
}
191+
185192
private static final Set<ExprType> NUMBER_TYPES = ExprCoreType.numberTypes();
186193

187194
private static final List<CoercionRule> COMMON_COERCION_RULES =

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ static List<RexNode> validateFunctionArgs(
468468
List<RexNode> fields = new ArrayList<>();
469469
fields.add(field);
470470
fields.addAll(argList);
471-
if (CoercionUtils.hasString(fields)) {
471+
if (CoercionUtils.hasString(fields) || CoercionUtils.hasAny(fields)) {
472472
coercionNodes = CoercionUtils.castArguments(rexBuilder, signature.typeChecker(), fields);
473473
}
474474
if (coercionNodes == null) {

integ-test/src/test/java/org/opensearch/sql/calcite/standalone/CalciteDynamicFieldsAggregationIT.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,7 @@ public void testStatsByDynamicField() throws IOException {
9393
public void testStatsWithDynamicField() throws IOException {
9494
String query =
9595
source(
96-
TEST_INDEX_DYNAMIC,
97-
"stats avg(cast(salary as int)) as avg by department | fields department, avg");
96+
TEST_INDEX_DYNAMIC, "stats avg(salary) as avg by department | fields department, avg");
9897

9998
assertExplainYaml(
10099
query,
@@ -168,8 +167,7 @@ public void testEventstatsWithDynamicField() throws IOException {
168167
String query =
169168
source(
170169
TEST_INDEX_DYNAMIC,
171-
"eventstats avg(cast(salary as int)) as"
172-
+ " avg by department | fields id, city, department, avg");
170+
"eventstats avg(salary) as avg by department " + "| fields id, city, department, avg");
173171

174172
assertExplainYaml(
175173
query,

0 commit comments

Comments
 (0)