Skip to content

[BugFix] Return all columns (struct and nested fields) listed when using head#5518

Open
quangdutran wants to merge 1 commit into
opensearch-project:mainfrom
quangdutran:main
Open

[BugFix] Return all columns (struct and nested fields) listed when using head#5518
quangdutran wants to merge 1 commit into
opensearch-project:mainfrom
quangdutran:main

Conversation

@quangdutran

Copy link
Copy Markdown

Description

When both a struct parent and the nested ones are projected in the query, appending | head must not drop the nested.

Explanation

Query with head has a Project wrapper like Project(Head (Project([agent.id, agent.name, agent])))

// source=big5 | fields agent.id agent.name agent | head

Seems like outer Project hits tryToRemoveNestedFields so nested fields are removed. Without head, there is no wrapper, hence nested fields stay.

tryToRemoveMetaFields has one run condition: There is no other project ever visited in the main query
So I try to apply the same with tryToRemoveNestedFields

Related Issues

Resolves #5507

Check List

  • New functionality includes testing.
  • New functionality has been documented.
  • New functionality has javadoc added.
  • New functionality has a user manual doc added.
  • New PPL command checklist all confirmed.
  • API changes companion pull request created.
  • Commits are signed per the DCO using --signoff or -s.
  • Public documentation issue/PR created.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

…when using head

Signed-off-by: Du Tran <quangdutran809@gmail.com>
@quangdutran

Copy link
Copy Markdown
Author

Test result:

image

@github-actions

github-actions Bot commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

🧪 PR contains tests
🔒 No security concerns identified
✅ No TODO sections
🔀 No multiple PR themes
⚡ Recommended focus areas for review

Typo in Method Name

Method name testStruckFieldAndSubFieldWithHead contains a typo. 'Struck' should be 'Struct' to accurately describe testing struct fields. This affects code readability and searchability.

public void testStruckFieldAndSubFieldWithHead() throws IOException {
Missing Data Verification

The test verifies schema but does not verify actual data rows returned by the query. Without data verification, the test cannot confirm that both the struct parent and nested fields are correctly returned in the result set, which is the core bug being fixed.

public void testStruckFieldAndSubFieldWithHead() throws IOException {
  JSONObject result =
          executeQuery(
                  String.format("source=%s | fields city.name, city | head", TEST_INDEX_DEEP_NESTED));
  verifySchema(result, schema("city.name", "string"), schema("city", "struct"));
}

@github-actions

github-actions Bot commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
General
Fix typo in test method name

The test method name contains a typo: "Struck" should be "Struct". This typo could
cause confusion when reading test reports or searching for struct-related tests.
Rename the method to testStructFieldAndSubFieldWithHead for clarity.

integ-test/src/test/java/org/opensearch/sql/calcite/remote/CalciteEvalCommandIT.java [239-244]

 @Test
-public void testStruckFieldAndSubFieldWithHead() throws IOException {
+public void testStructFieldAndSubFieldWithHead() throws IOException {
   JSONObject result =
           executeQuery(
                   String.format("source=%s | fields city.name, city | head", TEST_INDEX_DEEP_NESTED));
   verifySchema(result, schema("city.name", "string"), schema("city", "struct"));
 }
Suggestion importance[1-10]: 5

__

Why: The suggestion correctly identifies a typo in the method name testStruckFieldAndSubFieldWithHead where "Struck" should be "Struct". This improves code readability and maintainability, though it's a minor issue that doesn't affect functionality.

Low
Add data row verification to test

The test only verifies the schema but doesn't verify the actual data rows returned.
Add verifyDataRows to ensure the query returns expected data, not just the correct
schema structure. This would make the test more comprehensive and catch potential
data retrieval issues.

integ-test/src/test/java/org/opensearch/sql/calcite/remote/CalciteEvalCommandIT.java [239-244]

 @Test
 public void testStruckFieldAndSubFieldWithHead() throws IOException {
   JSONObject result =
           executeQuery(
                   String.format("source=%s | fields city.name, city | head", TEST_INDEX_DEEP_NESTED));
   verifySchema(result, schema("city.name", "string"), schema("city", "struct"));
+  verifyDataRows(result);
 }
Suggestion importance[1-10]: 3

__

Why: While adding verifyDataRows could make the test more comprehensive, the suggestion provides an incomplete implementation (calling verifyDataRows(result) without expected data rows). The current test may intentionally focus only on schema verification, making this a marginal improvement without proper implementation details.

Low

@penghuo penghuo added PPL Piped processing language bugFix labels Jun 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugFix PPL Piped processing language

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] head on struct removes other fields in that struct

2 participants