@@ -386,10 +386,45 @@ public SqlNode visit(SqlIdentifier id) {
386386 SqlValidator validator = context .getValidator ();
387387 if (rewritten != null ) {
388388 try {
389- log .debug ("Before validation: {}" , rewritten );
390389 validator .validate (rewritten );
391- log .debug ("After validation: {}" , rewritten );
392390 } catch (CalciteContextException e ) {
391+ /*
392+ Special handling for nested window functions that fail validation due to a Calcite bug.
393+ Only CalcitePPLEventstatsIT#testMultipleEventstatsWithNullBucket should be caught by this check.
394+
395+ <p><b>Calcite Bug (v1.41):</b> {@link SqlImplementor.Result#containsOver()} at
396+ SqlImplementor.java:L2145 only checks {@link SqlBasicCall} nodes for window functions,
397+ missing other {@link SqlCall} subclasses like {@link SqlCase}. This causes it to fail
398+ detecting window functions inside CASE expressions.
399+
400+ <p><b>Impact:</b> When nested window functions exist (e.g., from double eventstats),
401+ Calcite's {@link RelToSqlConverter} doesn't create the necessary subquery boundary
402+ because {@code containsOver()} returns false for expressions like:
403+
404+ <pre>{@code
405+ * CASE WHEN ... THEN (SUM(age) OVER (...)) END
406+ * }</pre>
407+
408+ <p>This results in invalid SQL with nested aggregations:
409+
410+ <pre>{@code
411+ * SUM(CASE WHEN ... THEN (SUM(age) OVER (...)) END) OVER (...)
412+ * }</pre>
413+
414+ <p>Which fails validation with "Aggregate expressions cannot be nested".
415+
416+ <p><b>Workaround:</b> When this specific error occurs, we bypass validation and return
417+ the unvalidated RelNode. The query will still execute correctly in the target engine
418+ (OpenSearch/Spark SQL) even though Calcite's validator rejects it.
419+
420+ <p><b>TODO:</b> Remove this workaround when upgrading to a Calcite version that fixes the
421+ bug, or implement a proper fix by post-processing the SqlNode to wrap inner window
422+ functions in subqueries.
423+ */
424+ if (e .getMessage () != null
425+ && e .getMessage ().contains ("Aggregate expressions cannot be nested" )) {
426+ return relNode ;
427+ }
393428 throw new ExpressionEvaluationException (e .getMessage (), e );
394429 }
395430 } else {
0 commit comments