diff --git a/core/src/main/java/org/opensearch/sql/calcite/CalciteRelNodeVisitor.java b/core/src/main/java/org/opensearch/sql/calcite/CalciteRelNodeVisitor.java index f1bc5fd6a0..280085968b 100644 --- a/core/src/main/java/org/opensearch/sql/calcite/CalciteRelNodeVisitor.java +++ b/core/src/main/java/org/opensearch/sql/calcite/CalciteRelNodeVisitor.java @@ -3331,6 +3331,17 @@ private void flattenParsedPattern( private void buildExpandRelNode( RexInputRef arrayFieldRex, String arrayFieldName, String alias, CalcitePlanContext context) { + // Validate that the field is an array type. + // Only nested array fields are supported by expand command. + RelDataType fieldType = arrayFieldRex.getType(); + if (fieldType.getSqlTypeName() != SqlTypeName.ARRAY) { + throw new UnsupportedOperationException( + String.format( + "Expand command only supports nested array fields. " + + "Field '%s' has type '%s' which is not a nested array.", + arrayFieldName, fieldType.getSqlTypeName())); + } + // 3. Capture the outer row in a CorrelationId Holder correlVariable = Holder.empty(); context.relBuilder.variable(correlVariable::set); diff --git a/integ-test/src/yamlRestTest/resources/rest-api-spec/test/issues/5065.yml b/integ-test/src/yamlRestTest/resources/rest-api-spec/test/issues/5065.yml new file mode 100644 index 0000000000..422adffc7f --- /dev/null +++ b/integ-test/src/yamlRestTest/resources/rest-api-spec/test/issues/5065.yml @@ -0,0 +1,45 @@ +setup: + - do: + indices.create: + index: test-idx-5065 + body: + settings: + number_of_shards: 1 + mappings: + properties: + nums: + type: long + - do: + index: + index: test-idx-5065 + id: 1 + refresh: true + body: + nums: [1, 2, 3] + - do: + query.settings: + body: + transient: + plugins.calcite.enabled: true +--- +teardown: + - do: + query.settings: + body: + transient: + plugins.calcite.enabled: false + - do: + indices.delete: + index: test-idx-5065 + ignore_unavailable: true + +--- +"Expand on scalar type should fail with clear error message": + - do: + catch: bad_request + ppl: + body: + query: 'source=test-idx-5065 | expand nums' + - match: { error.type: "UnsupportedOperationException" } + - match: { error.reason: "/.*Expand command only works on array types.*/" } + - match: { error.details: "/.*nums.*BIGINT.*/" }