Skip to content

Commit d055163

Browse files
authored
[BugFix] Return all columns (struct and nested fields) listed when using head (#5518)
Signed-off-by: Du Tran <quangdutran809@gmail.com>
1 parent 7dca5cc commit d055163

3 files changed

Lines changed: 21 additions & 10 deletions

File tree

core/src/main/java/org/opensearch/sql/calcite/CalciteRelNodeVisitor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,8 +516,9 @@ private RelNode handleAllFieldsProject(Project node, CalcitePlanContext context)
516516
"Invalid field exclusion: operation would exclude all fields from the result set");
517517
}
518518
AllFields allFields = (AllFields) node.getProjectList().getFirst();
519-
if (!(allFields instanceof AllFieldsExcludeMeta)) {
520-
// Should not remove nested fields for AllFieldsExcludeMeta.
519+
if (!(allFields instanceof AllFieldsExcludeMeta) && !context.isProjectVisited()) {
520+
// Should not remove nested fields for AllFieldsExcludeMeta
521+
// and when no explicit project has already curated the schema
521522
tryToRemoveNestedFields(context);
522523
}
523524
tryToRemoveMetaFields(context, allFields instanceof AllFieldsExcludeMeta);

docs/user/ppl/cmd/fields.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ fetched rows / total rows = 3/3
198198

199199
## Example 8: Selecting all fields
200200

201-
The following query selects all fields defined in the index schema using `` `*` ``. Fields with null values are included in the result set:
202-
201+
The following query selects all fields defined in the index schema using `` `*` ``. Struct fields are returned alongside their flattened sub-fields, and fields with null values are included in the result set:
202+
203203
```ppl
204204
source=otellogs
205205
| where severityText = 'WARN'
@@ -211,11 +211,11 @@ The query returns the following results:
211211

212212
```text
213213
fetched rows / total rows = 1/1
214-
+----------+------------------+---------------------+-------------------------------------------------------------------------------------------------------------------------------------------+--------------+--------------------------------------------------------------------------------------------------------------------------------------+-------+------------+------------------------+----------------+---------------------+----------------------------------------------------------------------------------------+
215-
| spanId | traceId | @timestamp | instrumentationScope | severityText | resource | flags | attributes | droppedAttributesCount | severityNumber | time | body |
216-
|----------+------------------+---------------------+-------------------------------------------------------------------------------------------------------------------------------------------+--------------+--------------------------------------------------------------------------------------------------------------------------------------+-------+------------+------------------------+----------------+---------------------+----------------------------------------------------------------------------------------|
217-
| span0003 | abcd1234efgh5678 | 2024-02-01 09:12:00 | {'name': 'go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc', 'droppedAttributesCount': 0, 'version': '0.49.0'} | WARN | {'attributes': {'service': {'name': 'product-catalog'}, 'host': {'name': 'productcatalog-7c9d-zn4p2'}}, 'droppedAttributesCount': 0} | 0 | {} | 0 | 13 | 2024-02-01 09:12:00 | Slow query detected: SELECT * FROM products WHERE category = 'electronics' took 3200ms |
218-
+----------+------------------+---------------------+-------------------------------------------------------------------------------------------------------------------------------------------+--------------+--------------------------------------------------------------------------------------------------------------------------------------+-------+------------+------------------------+----------------+---------------------+----------------------------------------------------------------------------------------+
214+
+----------+------------------+---------------------+-------------------------------------------------------------------------------------------------------------------------------------------+---------------------------------------------+-----------------------------------------------------------------------------+------------------------------+--------------+--------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------+---------------------------------------+-------------------------------+-----------------------------+----------------------------------+---------------------------------+-------+------------+------------------------+----------------+---------------------+----------------------------------------------------------------------------------------+
215+
| spanId | traceId | @timestamp | instrumentationScope | instrumentationScope.droppedAttributesCount | instrumentationScope.name | instrumentationScope.version | severityText | resource | resource.attributes | resource.attributes.host | resource.attributes.host.name | resource.attributes.service | resource.attributes.service.name | resource.droppedAttributesCount | flags | attributes | droppedAttributesCount | severityNumber | time | body |
216+
|----------+------------------+---------------------+-------------------------------------------------------------------------------------------------------------------------------------------+---------------------------------------------+-----------------------------------------------------------------------------+------------------------------+--------------+--------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------+---------------------------------------+-------------------------------+-----------------------------+----------------------------------+---------------------------------+-------+------------+------------------------+----------------+---------------------+----------------------------------------------------------------------------------------|
217+
| span0003 | abcd1234efgh5678 | 2024-02-01 09:12:00 | {'name': 'go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc', 'droppedAttributesCount': 0, 'version': '0.49.0'} | 0 | go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc | 0.49.0 | WARN | {'attributes': {'service': {'name': 'product-catalog'}, 'host': {'name': 'productcatalog-7c9d-zn4p2'}}, 'droppedAttributesCount': 0} | {'service': {'name': 'product-catalog'}, 'host': {'name': 'productcatalog-7c9d-zn4p2'}} | {'name': 'productcatalog-7c9d-zn4p2'} | productcatalog-7c9d-zn4p2 | {'name': 'product-catalog'} | product-catalog | 0 | 0 | {} | 0 | 13 | 2024-02-01 09:12:00 | Slow query detected: SELECT * FROM products WHERE category = 'electronics' took 3200ms |
218+
+----------+------------------+---------------------+-------------------------------------------------------------------------------------------------------------------------------------------+---------------------------------------------+-----------------------------------------------------------------------------+------------------------------+--------------+--------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------+---------------------------------------+-------------------------------+-----------------------------+----------------------------------+---------------------------------+-------+------------+------------------------+----------------+---------------------+----------------------------------------------------------------------------------------+
219219
```
220220

221221

integ-test/src/test/java/org/opensearch/sql/calcite/remote/CalciteEvalCommandIT.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package org.opensearch.sql.calcite.remote;
77

88
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_BANK;
9+
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_DEEP_NESTED;
910
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_TELEMETRY;
1011
import static org.opensearch.sql.util.MatcherUtils.rows;
1112
import static org.opensearch.sql.util.MatcherUtils.schema;
@@ -29,6 +30,7 @@ public void init() throws Exception {
2930

3031
loadIndex(Index.BANK);
3132
loadIndex(Index.TELEMETRY);
33+
loadIndex(Index.DEEP_NESTED);
3234

3335
// Pre-create test_eval through the helper so the analytics-engine compatibility run
3436
// (tests.analytics.parquet_indices=true) provisions it as a parquet-backed composite
@@ -160,7 +162,7 @@ public void testEvalDottedNamePreservesStructParent_ImplicitProject() throws IOE
160162
// longer dropped by `shouldOverrideField`'s prefix branch.
161163
JSONObject result =
162164
executeQuery("source=test_eval_agent | fields agent | eval `agent.name` = 'test'");
163-
verifySchema(result, schema("agent", "struct"));
165+
verifySchema(result, schema("agent", "struct"), schema("agent.name", "string"));
164166
}
165167

166168
@Test
@@ -233,4 +235,12 @@ public void testEvalStringConcatenationWithExistingData() throws IOException {
233235
rows("Hattie", "Bond", "Hattie Bond"),
234236
rows("Nanette", "Bates", "Nanette Bates"));
235237
}
238+
239+
@Test
240+
public void testStruckFieldAndSubFieldWithHead() throws IOException {
241+
JSONObject result =
242+
executeQuery(
243+
String.format("source=%s | fields city.name, city | head", TEST_INDEX_DEEP_NESTED));
244+
verifySchema(result, schema("city.name", "string"), schema("city", "struct"));
245+
}
236246
}

0 commit comments

Comments
 (0)