Skip to content

Commit 6d37ceb

Browse files
committed
Spark: Support recursive delegate unwrapping to find ExtendedParser in parser chains
1 parent 7a9079b commit 6d37ceb

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

spark/v3.5/spark/src/main/java/org/apache/iceberg/spark/ExtendedParser.java

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ public NullOrder nullOrder() {
5252
}
5353

5454
static List<RawOrderField> parseSortOrder(SparkSession spark, String orderString) {
55-
if (spark.sessionState().sqlParser() instanceof ExtendedParser) {
56-
ExtendedParser parser = (ExtendedParser) spark.sessionState().sqlParser();
55+
ExtendedParser extParser = findParser(spark.sessionState().sqlParser(), ExtendedParser.class);
56+
if (extParser != null) {
5757
try {
58-
return parser.parseSortOrder(orderString);
58+
return extParser.parseSortOrder(orderString);
5959
} catch (AnalysisException e) {
6060
throw new IllegalArgumentException(
6161
String.format("Unable to parse sortOrder: %s", orderString), e);
@@ -66,5 +66,42 @@ static List<RawOrderField> parseSortOrder(SparkSession spark, String orderString
6666
}
6767
}
6868

69+
static <T> T findParser(ParserInterface parser, Class<T> clazz) {
70+
ParserInterface current = parser;
71+
while (current != null) {
72+
if (clazz.isInstance(current)) {
73+
return clazz.cast(current);
74+
}
75+
Object next = null;
76+
try {
77+
for (String methodName : new String[]{"delegate", "getDelegate"}) {
78+
try {
79+
java.lang.reflect.Method delegateMethod = current.getClass().getMethod(methodName);
80+
next = delegateMethod.invoke(current);
81+
break;
82+
} catch (NoSuchMethodException ignore) {
83+
// go on
84+
}
85+
}
86+
if (next == null) {
87+
try {
88+
java.lang.reflect.Field delegateField = current.getClass().getDeclaredField("delegate");
89+
delegateField.setAccessible(true);
90+
next = delegateField.get(current);
91+
} catch (NoSuchFieldException ignore) {
92+
// No delegate
93+
}
94+
}
95+
if (next == null || next == current) {
96+
break;
97+
}
98+
current = (ParserInterface) next;
99+
} catch (Exception e) {
100+
break;
101+
}
102+
}
103+
return null;
104+
}
105+
69106
List<RawOrderField> parseSortOrder(String orderString) throws AnalysisException;
70107
}

0 commit comments

Comments
 (0)