From ad883e143ff5e07ef961cf3f177876dc59290528 Mon Sep 17 00:00:00 2001 From: nobigo Date: Wed, 28 Aug 2024 09:12:27 +0800 Subject: [PATCH] [CALCITE-6548] Return type of COVAR_SAMP, COVAR_POP and VAR_SAMP should be nullable --- .../java/org/apache/calcite/sql/type/ReturnTypes.java | 7 +++++-- .../resources/org/apache/calcite/test/RelOptRulesTest.xml | 4 ++-- .../java/org/apache/calcite/test/SqlOperatorTest.java | 8 ++++---- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/org/apache/calcite/sql/type/ReturnTypes.java b/core/src/main/java/org/apache/calcite/sql/type/ReturnTypes.java index bab91ec22c5..68778256c05 100644 --- a/core/src/main/java/org/apache/calcite/sql/type/ReturnTypes.java +++ b/core/src/main/java/org/apache/calcite/sql/type/ReturnTypes.java @@ -1402,7 +1402,8 @@ private static RelDataType multivalentStringWithSepSumPrecision( typeFactory.getTypeSystem().deriveAvgAggType(typeFactory, opBinding.getOperandType(0)); if (opBinding.getGroupCount() == 0 || opBinding.hasFilter() - || opBinding.getOperator().kind == SqlKind.STDDEV_SAMP) { + || opBinding.getOperator().getKind() == SqlKind.STDDEV_SAMP + || opBinding.getOperator().getKind() == SqlKind.VAR_SAMP) { return typeFactory.createTypeWithNullability(relDataType, true); } else { return relDataType; @@ -1414,7 +1415,9 @@ private static RelDataType multivalentStringWithSepSumPrecision( final RelDataType relDataType = typeFactory.getTypeSystem().deriveCovarType(typeFactory, opBinding.getOperandType(0), opBinding.getOperandType(1)); - if (opBinding.getGroupCount() == 0 || opBinding.hasFilter()) { + if (opBinding.getGroupCount() == 0 || opBinding.hasFilter() + || opBinding.getOperator().kind == SqlKind.COVAR_POP + || opBinding.getOperator().kind == SqlKind.COVAR_SAMP) { return typeFactory.createTypeWithNullability(relDataType, true); } else { return relDataType; diff --git a/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml b/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml index bf3276d22cb..0963020e2c3 100644 --- a/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml +++ b/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml @@ -1521,7 +1521,7 @@ LogicalAggregate(group=[{0}], EXPR$1=[STDDEV_POP($1)], EXPR$2=[AVG($1)], EXPR$3=