xuzifu666 commented on code in PR #5015:
URL: https://github.com/apache/calcite/pull/5015#discussion_r3406919205
##########
core/src/test/java/org/apache/calcite/rel/logical/LogicalWindowTest.java:
##########
@@ -84,4 +95,101 @@ public class LogicalWindowTest {
assertThat(updated.getConstants(), hasSize(1));
assertSame(newConstants.get(0), updated.getConstants().get(0));
}
+
+ /** Test case of
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-5929">[CALCITE-5929]
+ * Improve LogicalWindow print plan to add the constant value</a>. */
+ @Test void testComputeDisplayStringWithLiteralConstant() {
+ // Test that computeDisplayString() correctly expands literal constants
+ // in window bounds (e.g., "10 PRECEDING" instead of "$1 PRECEDING")
+ final MockRelOptPlanner planner = new MockRelOptPlanner(Contexts.empty());
+ final SqlTypeFactoryImpl typeFactory =
+ new
SqlTypeFactoryImpl(org.apache.calcite.rel.type.RelDataTypeSystem.DEFAULT);
+ final RexBuilder rexBuilder = new RexBuilder(typeFactory);
+ final RelOptCluster cluster = RelOptCluster.create(planner, rexBuilder);
+ final RelTraitSet traitSet = RelTraitSet.createEmpty();
+ final RelNode relNode = new AbstractRelNode(cluster, traitSet) {
+ };
+
+ // Create a literal constant: 10
+ final RexLiteral literalTen =
+ rexBuilder.makeExactLiteral(java.math.BigDecimal.TEN,
+ typeFactory.createSqlType(SqlTypeName.BIGINT));
+ final List<RexLiteral> constants = Collections.singletonList(literalTen);
+
+ // Create window bounds: 10 PRECEDING to CURRENT ROW
+ // The offset is RexInputRef(1) which maps to constants[0] = 10
+ final int inputFieldCount = 1;
+ final RexInputRef offsetRef = new RexInputRef(inputFieldCount,
literalTen.getType());
+ final RexWindowBound lowerBound = RexWindowBounds.preceding(offsetRef);
+
+ // Create a window group with this bound
+ final List<Window.RexWinAggCall> aggCalls = new ArrayList<>();
+ final Group group =
+ new Group(ImmutableBitSet.of(),
+ true, // isRows
+ lowerBound,
+ RexWindowBounds.CURRENT_ROW,
+ RexWindowExclusion.EXCLUDE_NO_OTHER,
+ RelCollations.EMPTY,
+ aggCalls);
+
+ // Call computeDisplayString and verify it expands "10 PRECEDING"
+ final String displayString = group.computeDisplayString(constants,
inputFieldCount);
+ assertThat(displayString, containsString("10 PRECEDING"));
+ assertThat(displayString, containsString("CURRENT ROW"));
+ }
+
+ @Test void testComputeDisplayStringWithConstantExpression() {
+ // Test that computeDisplayString() correctly handles constant expressions
+ // (not just literals) in window bounds. For example, when a window bound
+ // contains RexCall representing an expression like 5+5.
+ final MockRelOptPlanner planner = new MockRelOptPlanner(Contexts.empty());
+ final SqlTypeFactoryImpl typeFactory =
+ new
SqlTypeFactoryImpl(org.apache.calcite.rel.type.RelDataTypeSystem.DEFAULT);
+ final RexBuilder rexBuilder = new RexBuilder(typeFactory);
+ final RelOptCluster cluster = RelOptCluster.create(planner, rexBuilder);
+ final RelTraitSet traitSet = RelTraitSet.createEmpty();
+ final RelNode relNode = new AbstractRelNode(cluster, traitSet) {
+ };
+
+ // Create a constant expression: 5 + 5
+ final RexLiteral five =
+ rexBuilder.makeExactLiteral(java.math.BigDecimal.valueOf(5),
+ typeFactory.createSqlType(SqlTypeName.BIGINT));
+ final SqlOperator plusOp = SqlStdOperatorTable.PLUS;
+ final RexCall addExpr =
+ (RexCall) rexBuilder.makeCall(plusOp, five, five);
+
+ // Test that expandBound() correctly handles both literals and expressions.
+ // Although the API accepts List<RexLiteral>, at runtime constants can
include
+ // expressions like RexCall(+, 5, 5). We use an unchecked cast to simulate
this.
+ @SuppressWarnings("unchecked")
+ final List<RexLiteral> constants =
+ (List<RexLiteral>) (List<?>) Collections.singletonList(addExpr);
+
+ // Create window bounds with RexInputRef pointing to this expression
+ final int inputFieldCount = 1;
+ final RexInputRef offsetRef = new RexInputRef(inputFieldCount,
addExpr.getType());
+ final RexWindowBound lowerBound = RexWindowBounds.preceding(offsetRef);
+
+ // Create a window group
+ final List<Window.RexWinAggCall> aggCalls = new ArrayList<>();
+ final Group group =
+ new Group(ImmutableBitSet.of(),
+ true, // isRows
+ lowerBound,
+ RexWindowBounds.CURRENT_ROW,
+ RexWindowExclusion.EXCLUDE_NO_OTHER,
+ RelCollations.EMPTY,
+ aggCalls);
+
+ // Call computeDisplayString and verify it correctly renders the
expression.
+ // Since the constant is RexCall(+, 5, 5), expandBound() should call
toString()
+ // on it (the non-literal branch), which returns the digest representation.
+ final String displayString = group.computeDisplayString(constants,
inputFieldCount);
+ assertThat(displayString, containsString("PRECEDING"));
+ assertThat(displayString, containsString("CURRENT ROW"));
Review Comment:
I had added a new test to verify the expression ```5+5``` is shown as
```+(5:BIGINT, 5:BIGINT) PRECEDING```, not as unexpanded ```$1 PRECEDING```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]