Skip to content

Commit 5aaf95b

Browse files
committed
peng - hiden col solution 2
Signed-off-by: Jialiang Liang <jiallian@amazon.com>
1 parent 2041922 commit 5aaf95b

3 files changed

Lines changed: 28 additions & 9 deletions

File tree

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import static org.opensearch.sql.calcite.utils.PlanUtils.getRelation;
2424
import static org.opensearch.sql.calcite.utils.PlanUtils.getRexCall;
2525
import static org.opensearch.sql.calcite.utils.PlanUtils.transformPlanToAttachChild;
26+
import static org.opensearch.sql.expression.HighlightExpression.HIGHLIGHT_FIELD;
2627
import static org.opensearch.sql.utils.SystemIndexUtils.DATASOURCES_TABLE_NAME;
2728

2829
import com.google.common.base.Strings;
@@ -407,6 +408,15 @@ public RelNode visitProject(Project node, CalcitePlanContext context) {
407408
if (!context.isResolvingSubquery()) {
408409
context.setProjectVisited(true);
409410
}
411+
// When highlight is active, include _highlight in the projection so it survives
412+
// through the Calcite pipeline. This matches DSL behavior where _source filtering
413+
// does not affect highlights.
414+
if (CalcitePlanContext.getHighlightConfig() != null) {
415+
int hlIndex = currentFields.indexOf(HIGHLIGHT_FIELD);
416+
if (hlIndex >= 0) {
417+
expandedFields.add(context.relBuilder.field(hlIndex));
418+
}
419+
}
410420
context.relBuilder.project(expandedFields);
411421
}
412422
return context.relBuilder.peek();

docs/user/ppl/interfaces/endpoint.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,12 @@ Only the `address` field is highlighted. Rows where "Holmes" appears in other fi
155155
- Entries are `null` when a row has no highlight data for the requested fields.
156156
- The `highlights` array is **omitted entirely** when no `highlight` config is provided in the request (backward compatible).
157157

158-
### Notes
158+
### Limitations
159159

160160
- Highlighting is supported only in the Calcite engine.
161161
- The backend forwards the highlight config as-is to OpenSearch. The same highlighting behavior and limitations as [OpenSearch's highlighting API](https://opensearch.org/docs/latest/search-plugins/searching-data/highlight/) apply.
162162
- Piped commands (`where`, `sort`, `head`, `dedup`) narrow or reorder the result set but do not affect which terms are highlighted.
163+
- Highlighting works with **single-source queries only**. Joins (`| join`), subqueries, and multi-source queries are not supported — the `highlight` config is ignored in these cases.
163164

164165
## Explain
165166

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,15 @@ public void testHighlightWithWildcardFields() throws IOException {
4343
boolean foundHighlight = false;
4444
for (int i = 0; i < highlights.length(); i++) {
4545
if (!highlights.isNull(i)) {
46-
String hlStr = highlights.get(i).toString();
47-
if (hlStr.contains("<em>Street</em>")) {
48-
foundHighlight = true;
49-
break;
46+
JSONObject hl = highlights.getJSONObject(i);
47+
for (String key : hl.keySet()) {
48+
String fragment = hl.getJSONArray(key).getString(0);
49+
if (fragment.contains("<em>") && fragment.contains("Street")) {
50+
foundHighlight = true;
51+
break;
52+
}
5053
}
54+
if (foundHighlight) break;
5155
}
5256
}
5357
assertTrue("Expected at least one highlight with <em> tags", foundHighlight);
@@ -87,11 +91,15 @@ public void testHighlightWithCustomTags() throws IOException {
8791
boolean foundCustomTag = false;
8892
for (int i = 0; i < highlights.length(); i++) {
8993
if (!highlights.isNull(i)) {
90-
String hlStr = highlights.get(i).toString();
91-
if (hlStr.contains("<mark>")) {
92-
foundCustomTag = true;
93-
break;
94+
JSONObject hl = highlights.getJSONObject(i);
95+
for (String key : hl.keySet()) {
96+
String fragment = hl.getJSONArray(key).getString(0);
97+
if (fragment.contains("<mark>")) {
98+
foundCustomTag = true;
99+
break;
100+
}
94101
}
102+
if (foundCustomTag) break;
95103
}
96104
}
97105
assertTrue("Expected custom <mark> tags in highlights", foundCustomTag);

0 commit comments

Comments
 (0)