Skip to content

Commit 9178001

Browse files
authored
[BugFix] Fix rename with wildcard applying on hidden fields (opensearch-project#5099) (opensearch-project#5350)
* [BugFix] Fix rename with wildcard applying on hidden/metadata fields (opensearch-project#5099) Signed-off-by: Songkan Tang <songkant@amazon.com> * Fix test assertion for rename wildcard excluding hidden/metadata fields Update testCrossClusterRenameFullWildcard to expect only 3 user fields instead of 9 (including 6 metadata fields), aligning the test with the corrected behavior from PR opensearch-project#5350 where rename * no longer renames hidden/metadata fields. Signed-off-by: Songkan Tang <songkant@amazon.com> --------- Signed-off-by: Songkan Tang <songkant@amazon.com>
1 parent ee28e3d commit 9178001

4 files changed

Lines changed: 100 additions & 17 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,10 @@ public RelNode visitRename(Rename node, CalcitePlanContext context) {
670670
}
671671

672672
List<String> matchingFields = WildcardRenameUtils.matchFieldNames(sourcePattern, newNames);
673+
// Exclude metadata fields from wildcard rename (issue #5099)
674+
if (WildcardRenameUtils.isWildcardPattern(sourcePattern)) {
675+
matchingFields.removeIf(this::isMetadataField);
676+
}
673677

674678
for (String fieldName : matchingFields) {
675679
String newName =

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,41 @@ public void testRenameFullWildcard() throws IOException {
199199
verifyDataRows(result, rows("Jake", 70), rows("Hello", 30), rows("John", 25), rows("Jane", 20));
200200
}
201201

202+
@Test
203+
public void testRenameFullWildcardExcludesMetadataFields() throws IOException {
204+
JSONObject result =
205+
executeQuery(String.format("source = %s | rename * as old_*", TEST_INDEX_STATE_COUNTRY));
206+
verifySchema(
207+
result,
208+
schema("old_name", "string"),
209+
schema("old_age", "int"),
210+
schema("old_state", "string"),
211+
schema("old_country", "string"),
212+
schema("old_year", "int"),
213+
schema("old_month", "int"));
214+
verifyDataRows(
215+
result,
216+
rows("Jake", "USA", "California", 4, 2023, 70),
217+
rows("Hello", "USA", "New York", 4, 2023, 30),
218+
rows("John", "Canada", "Ontario", 4, 2023, 25),
219+
rows("Jane", "Canada", "Quebec", 4, 2023, 20));
220+
}
221+
222+
@Test
223+
public void testRenamePartialWildcardExcludesMetadataFields() throws IOException {
224+
JSONObject result =
225+
executeQuery(String.format("source = %s | rename _* as meta_*", TEST_INDEX_STATE_COUNTRY));
226+
verifySchema(
227+
result,
228+
schema("name", "string"),
229+
schema("age", "int"),
230+
schema("state", "string"),
231+
schema("country", "string"),
232+
schema("year", "int"),
233+
schema("month", "int"));
234+
verifyStandardDataRows(result);
235+
}
236+
202237
@Test
203238
public void testRenameMultipleWildcards() throws IOException {
204239
JSONObject result =

integ-test/src/test/java/org/opensearch/sql/security/CalciteCrossClusterSearchIT.java

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -262,27 +262,12 @@ public void testCrossClusterRenameFullWildcard() throws IOException {
262262
JSONObject result =
263263
executeQuery(String.format("search source=%s | rename * as old_*", TEST_INDEX_DOG_REMOTE));
264264
verifyColumn(
265-
result,
266-
columnName("old_dog_name"),
267-
columnName("old_holdersName"),
268-
columnName("old_age"),
269-
columnName("old__id"),
270-
columnName("old__index"),
271-
columnName("old__score"),
272-
columnName("old__maxscore"),
273-
columnName("old__sort"),
274-
columnName("old__routing"));
265+
result, columnName("old_dog_name"), columnName("old_holdersName"), columnName("old_age"));
275266
verifySchema(
276267
result,
277268
schema("old_dog_name", "string"),
278269
schema("old_holdersName", "string"),
279-
schema("old_age", "bigint"),
280-
schema("old__id", "string"),
281-
schema("old__index", "string"),
282-
schema("old__score", "float"),
283-
schema("old__maxscore", "float"),
284-
schema("old__sort", "bigint"),
285-
schema("old__routing", "string"));
270+
schema("old_age", "bigint"));
286271
}
287272

288273
@Test
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
setup:
2+
- do:
3+
query.settings:
4+
body:
5+
transient:
6+
plugins.calcite.enabled: true
7+
- do:
8+
indices.create:
9+
index: issue5099
10+
body:
11+
settings:
12+
number_of_shards: 1
13+
number_of_replicas: 0
14+
mappings:
15+
properties:
16+
name:
17+
type: keyword
18+
age:
19+
type: integer
20+
- do:
21+
bulk:
22+
refresh: true
23+
body:
24+
- '{"index": {"_index": "issue5099", "_id": "1"}}'
25+
- '{"name": "Alice", "age": 30}'
26+
- '{"index": {"_index": "issue5099", "_id": "2"}}'
27+
- '{"name": "Bob", "age": 25}'
28+
29+
---
30+
teardown:
31+
- do:
32+
indices.delete:
33+
index: issue5099
34+
ignore_unavailable: true
35+
- do:
36+
query.settings:
37+
body:
38+
transient:
39+
plugins.calcite.enabled: false
40+
41+
---
42+
"Issue 5099: rename with wildcard should not apply on hidden fields":
43+
- skip:
44+
features:
45+
- headers
46+
- allowed_warnings
47+
- do:
48+
allowed_warnings:
49+
- 'Loading the fielddata on the _id field is deprecated and will be removed in future versions. If you require sorting or aggregating on this field you should also include the id in the body of your documents, and map this field as a keyword field that has [doc_values] enabled'
50+
headers:
51+
Content-Type: 'application/json'
52+
ppl:
53+
body:
54+
query: source=issue5099 | rename * as old_*
55+
56+
- match: { total: 2 }
57+
- length: { schema: 2 }
58+
- match: { schema: [ { name: "old_name", type: "string" }, { name: "old_age", type: "int" } ] }
59+
- length: { datarows: 2 }

0 commit comments

Comments
 (0)