Skip to content

Commit 9953a5a

Browse files
authored
Do not remove nested fields in resolving AllFieldsExcludeMeta (#4708)
Signed-off-by: Lantao Jin <[email protected]>
1 parent 5517c1e commit 9953a5a

File tree

4 files changed

+86
-2
lines changed

4 files changed

+86
-2
lines changed

core/src/main/java/org/opensearch/sql/ast/expression/AllFields.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
public class AllFields extends UnresolvedExpression {
1919
public static final AllFields INSTANCE = new AllFields();
2020

21-
public AllFields() {}
21+
protected AllFields() {}
2222

2323
public static AllFields of() {
2424
return INSTANCE;

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,10 @@ private RelNode handleAllFieldsProject(Project node, CalcitePlanContext context)
380380
"Invalid field exclusion: operation would exclude all fields from the result set");
381381
}
382382
AllFields allFields = (AllFields) node.getProjectList().getFirst();
383-
tryToRemoveNestedFields(context);
383+
if (!(allFields instanceof AllFieldsExcludeMeta)) {
384+
// Should not remove nested fields for AllFieldsExcludeMeta.
385+
tryToRemoveNestedFields(context);
386+
}
384387
tryToRemoveMetaFields(context, allFields instanceof AllFieldsExcludeMeta);
385388
return context.relBuilder.peek();
386389
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ private static RexNode resolveInNonJoinCondition(
6565
log.debug("resolveInNonJoinCondition() called with nameNode={}", nameNode);
6666

6767
return resolveLambdaVariable(nameNode, context)
68+
.or(() -> resolveFieldDirectly(nameNode, context, 1))
6869
.or(() -> resolveFieldWithAlias(nameNode, context, 1))
6970
.or(() -> resolveFieldWithoutAlias(nameNode, context, 1))
7071
.or(() -> resolveRenamedField(nameNode, context))
@@ -88,6 +89,26 @@ private static String joinParts(List<String> parts, int start) {
8889
return joinParts(parts, start, parts.size() - start);
8990
}
9091

92+
private static Optional<RexNode> resolveFieldDirectly(
93+
QualifiedName nameNode, CalcitePlanContext context, int inputCount) {
94+
List<String> parts = nameNode.getParts();
95+
log.debug(
96+
"resolveFieldDirectly() called with nameNode={}, parts={}, inputCount={}",
97+
nameNode,
98+
parts,
99+
inputCount);
100+
101+
List<String> currentFields = context.relBuilder.peek().getRowType().getFieldNames();
102+
if (currentFields.contains(nameNode.toString())) {
103+
try {
104+
return Optional.of(context.relBuilder.field(nameNode.toString()));
105+
} catch (IllegalArgumentException e) {
106+
log.debug("resolveFieldDirectly() failed: {}", e.getMessage());
107+
}
108+
}
109+
return Optional.empty();
110+
}
111+
91112
private static Optional<RexNode> resolveFieldWithAlias(
92113
QualifiedName nameNode, CalcitePlanContext context, int inputCount) {
93114
List<String> parts = nameNode.getParts();
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
setup:
2+
- do:
3+
indices.create:
4+
index: test
5+
body:
6+
settings:
7+
number_of_shards: 1
8+
number_of_replicas: 0
9+
mappings:
10+
properties:
11+
a:
12+
properties:
13+
b:
14+
properties:
15+
c:
16+
type: keyword
17+
d:
18+
properties:
19+
e:
20+
properties:
21+
f:
22+
type: keyword
23+
num:
24+
type: integer
25+
26+
- do:
27+
query.settings:
28+
body:
29+
transient:
30+
plugins.calcite.enabled : true
31+
32+
---
33+
teardown:
34+
- do:
35+
query.settings:
36+
body:
37+
transient:
38+
plugins.calcite.enabled : false
39+
40+
---
41+
"Access struct fields after join":
42+
- skip:
43+
features:
44+
- headers
45+
- do:
46+
bulk:
47+
index: test
48+
refresh: true
49+
body:
50+
- '{"index": {}}'
51+
- '{"a": {"b": {"c": "/a/b/c"} }, "d": {"e": {"f": "/d/e/f"} }, "num": 10 }'
52+
- do:
53+
headers:
54+
Content-Type: 'application/json'
55+
ppl:
56+
body:
57+
query: 'source=test | eval N=d.e.f | join type=inner left=l, right=r ON l.N = r.F [ source=test | rename d.e.f as F | fields F ] | fields num, N, a.b.c, d.e.f'
58+
- match: {"total": 1}
59+
- match: { schema: [ { "name": "num", "type": "int" }, { "name": "N", "type": "string" }, { "name": "a.b.c", "type": "string" }, { "name": "d.e.f", "type": "string" } ] }
60+
- match: { datarows: [ [ 10, "/d/e/f", "/a/b/c", "/d/e/f"] ] }

0 commit comments

Comments
 (0)