Skip to content

Commit c1666ee

Browse files
committed
Review comments - simplify FlattenOperator.flattenExprValueAtPath.
Signed-off-by: currantw <[email protected]>
1 parent db96c51 commit c1666ee

File tree

1 file changed

+20
-28
lines changed

1 file changed

+20
-28
lines changed

core/src/main/java/org/opensearch/sql/planner/physical/FlattenOperator.java

+20-28
Original file line numberDiff line numberDiff line change
@@ -55,44 +55,36 @@ public ExprValue next() {
5555
*/
5656
private static ExprValue flattenExprValueAtPath(ExprValue rootExprValue, String path) {
5757

58-
Matcher matcher = ExprValueUtils.QUALIFIED_NAME_SEPARATOR_PATTERN.matcher(path);
5958
Map<String, ExprValue> exprValueMap = ExprValueUtils.getTupleValue(rootExprValue);
6059

61-
// [A] Flatten nested struct value
62-
// -------------------------------
63-
64-
if (matcher.find()) {
65-
String currentPathComponent = path.substring(0, matcher.start());
66-
String remainingPath = path.substring(matcher.end());
67-
68-
if (!exprValueMap.containsKey(currentPathComponent)) {
69-
return rootExprValue;
70-
}
71-
72-
ExprValue childExprValue = exprValueMap.get(currentPathComponent);
73-
if (childExprValue.isNull() || childExprValue.isMissing()) {
74-
return rootExprValue;
75-
}
76-
77-
ExprValue flattenedExprValue =
78-
flattenExprValueAtPath(exprValueMap.get(currentPathComponent), remainingPath);
79-
exprValueMap.put(currentPathComponent, flattenedExprValue);
80-
return ExprTupleValue.fromExprValueMap(exprValueMap);
81-
}
82-
83-
// [B] Flatten child struct value
84-
// ------------------------------
60+
// Get current path component.
61+
Matcher matcher = ExprValueUtils.QUALIFIED_NAME_SEPARATOR_PATTERN.matcher(path);
62+
boolean fieldIsNested = matcher.find();
63+
String currentPathComponent = fieldIsNested ? path.substring(0, matcher.start()) : path;
8564

86-
if (!exprValueMap.containsKey(path)) {
65+
// Check for undefined, null, or missing values.
66+
if (!exprValueMap.containsKey(currentPathComponent)) {
8767
return rootExprValue;
8868
}
8969

90-
ExprValue childExprValue = exprValueMap.get(path);
70+
ExprValue childExprValue = exprValueMap.get(currentPathComponent);
9171
if (childExprValue.isNull() || childExprValue.isMissing()) {
9272
return rootExprValue;
9373
}
9474

95-
exprValueMap.putAll(ExprValueUtils.getTupleValue(childExprValue));
75+
// Get flattened values and add them to the field map.
76+
Map<String, ExprValue> flattenedExprValueMap;
77+
if (fieldIsNested) {
78+
String remainingPath = path.substring(matcher.end());
79+
flattenedExprValueMap =
80+
Map.of(
81+
currentPathComponent,
82+
flattenExprValueAtPath(exprValueMap.get(currentPathComponent), remainingPath));
83+
} else {
84+
flattenedExprValueMap = ExprValueUtils.getTupleValue(childExprValue);
85+
}
86+
87+
exprValueMap.putAll(flattenedExprValueMap);
9688
return ExprTupleValue.fromExprValueMap(exprValueMap);
9789
}
9890
}

0 commit comments

Comments
 (0)