Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CALCITE-6548] Return type of COVAR_SAMP, COVAR_POP and VAR_SAMP shou… #3935

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you sure about COVAR_POP?
Usually SAMP functions have the property that they return NULL for a non-empty set of size 1.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlrf/COVAR_POP.html. This function return type is nullable.

The function returns a value of type NUMBER. If the function is applied to an empty set, then it returns null.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all aggregates return null when applied to an empty set.
but if getGroupCount() != 0 the set can never be empty (because then there would be no aggregate at all)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is about aggregates that are null even when the set is NOT empty

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty set = set with all elements null

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will do some test for this.

|| opBinding.getOperator().kind == SqlKind.COVAR_SAMP) {
return typeFactory.createTypeWithNullability(relDataType, true);
} else {
return relDataType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1521,7 +1521,7 @@ LogicalAggregate(group=[{0}], EXPR$1=[STDDEV_POP($1)], EXPR$2=[AVG($1)], EXPR$3=
</Resource>
<Resource name="planAfter">
<![CDATA[
LogicalProject(NAME=[$0], EXPR$1=[CAST(POWER(/(-($1, /(*($2, $2), $3)), $3), 0.5:DECIMAL(2, 1))):INTEGER NOT NULL], EXPR$2=[CAST(/($2, $3)):INTEGER NOT NULL], EXPR$3=[CAST(POWER(/(-($1, /(*($2, $2), $3)), CASE(=($3, 1), null:BIGINT, -($3, 1))), 0.5:DECIMAL(2, 1))):INTEGER], EXPR$4=[CAST(/(-($1, /(*($2, $2), $3)), $3)):INTEGER NOT NULL], EXPR$5=[CAST(/(-($1, /(*($2, $2), $3)), CASE(=($3, 1), null:BIGINT, -($3, 1)))):INTEGER NOT NULL])
LogicalProject(NAME=[$0], EXPR$1=[CAST(POWER(/(-($1, /(*($2, $2), $3)), $3), 0.5:DECIMAL(2, 1))):INTEGER NOT NULL], EXPR$2=[CAST(/($2, $3)):INTEGER NOT NULL], EXPR$3=[CAST(POWER(/(-($1, /(*($2, $2), $3)), CASE(=($3, 1), null:BIGINT, -($3, 1))), 0.5:DECIMAL(2, 1))):INTEGER], EXPR$4=[CAST(/(-($1, /(*($2, $2), $3)), $3)):INTEGER NOT NULL], EXPR$5=[CAST(/(-($1, /(*($2, $2), $3)), CASE(=($3, 1), null:BIGINT, -($3, 1)))):INTEGER])
LogicalAggregate(group=[{0}], agg#0=[$SUM0($2)], agg#1=[$SUM0($1)], agg#2=[COUNT()])
LogicalProject(NAME=[$0], DEPTNO=[$1], $f2=[*($1, $1)])
LogicalProject(NAME=[$1], DEPTNO=[$0])
Expand Down Expand Up @@ -12133,7 +12133,7 @@ LogicalAggregate(group=[{0}], EXPR$1=[STDDEV_POP($1)], EXPR$2=[AVG($1)], EXPR$3=
</Resource>
<Resource name="planAfter">
<![CDATA[
LogicalProject(NAME=[$0], EXPR$1=[CAST(POWER(/(-($1, /(*($2, $2), $3)), $3), 0.5:DECIMAL(2, 1))):INTEGER NOT NULL], EXPR$2=[CAST(/($2, $3)):INTEGER NOT NULL], EXPR$3=[CAST(POWER(/(-($1, /(*($2, $2), $3)), CASE(=($3, 1), null:BIGINT, -($3, 1))), 0.5:DECIMAL(2, 1))):INTEGER], EXPR$4=[CAST(/(-($1, /(*($2, $2), $3)), $3)):INTEGER NOT NULL], EXPR$5=[CAST(/(-($1, /(*($2, $2), $3)), CASE(=($3, 1), null:BIGINT, -($3, 1)))):INTEGER NOT NULL])
LogicalProject(NAME=[$0], EXPR$1=[CAST(POWER(/(-($1, /(*($2, $2), $3)), $3), 0.5:DECIMAL(2, 1))):INTEGER NOT NULL], EXPR$2=[CAST(/($2, $3)):INTEGER NOT NULL], EXPR$3=[CAST(POWER(/(-($1, /(*($2, $2), $3)), CASE(=($3, 1), null:BIGINT, -($3, 1))), 0.5:DECIMAL(2, 1))):INTEGER], EXPR$4=[CAST(/(-($1, /(*($2, $2), $3)), $3)):INTEGER NOT NULL], EXPR$5=[CAST(/(-($1, /(*($2, $2), $3)), CASE(=($3, 1), null:BIGINT, -($3, 1)))):INTEGER])
LogicalAggregate(group=[{0}], agg#0=[$SUM0($2)], agg#1=[$SUM0($1)], agg#2=[COUNT()])
LogicalProject(NAME=[$0], DEPTNO=[$1], $f2=[*($1, $1)])
LogicalProject(NAME=[$1], DEPTNO=[$0])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14774,7 +14774,7 @@ void testTimestampDiff(boolean coercionEnabled) {
"DECIMAL(19, 9)");
f.checkType("covar_pop(CAST(NULL AS INTEGER),CAST(NULL AS INTEGER))",
"INTEGER");
f.checkAggType("covar_pop(1.5, 2.5)", "DECIMAL(2, 1) NOT NULL");
f.checkAggType("covar_pop(1.5, 2.5)", "DECIMAL(2, 1)");
if (!f.brokenTestsEnabled()) {
return;
}
Expand All @@ -14798,7 +14798,7 @@ void testTimestampDiff(boolean coercionEnabled) {
"DECIMAL(19, 9)");
f.checkType("covar_samp(CAST(NULL AS INTEGER),CAST(NULL AS INTEGER))",
"INTEGER");
f.checkAggType("covar_samp(1.5, 2.5)", "DECIMAL(2, 1) NOT NULL");
f.checkAggType("covar_samp(1.5, 2.5)", "DECIMAL(2, 1)");
if (!f.brokenTestsEnabled()) {
return;
}
Expand Down Expand Up @@ -14976,7 +14976,7 @@ void testTimestampDiff(boolean coercionEnabled) {
false);
f.checkType("var_samp(cast(null as varchar(2)))", "DECIMAL(19, 9)");
f.checkType("var_samp(CAST(NULL AS INTEGER))", "INTEGER");
f.checkAggType("var_samp(DISTINCT 1.5)", "DECIMAL(2, 1) NOT NULL");
f.checkAggType("var_samp(DISTINCT 1.5)", "DECIMAL(2, 1)");
final String[] values = {"0", "CAST(null AS FLOAT)", "3", "3"};
if (!f.brokenTestsEnabled()) {
return;
Expand Down Expand Up @@ -15004,7 +15004,7 @@ void testTimestampDiff(boolean coercionEnabled) {
false);
f.checkType("variance(cast(null as varchar(2)))", "DECIMAL(19, 9)");
f.checkType("variance(CAST(NULL AS INTEGER))", "INTEGER");
f.checkAggType("variance(DISTINCT 1.5)", "DECIMAL(2, 1) NOT NULL");
f.checkAggType("variance(DISTINCT 1.5)", "DECIMAL(2, 1)");
final String[] values = {"0", "CAST(null AS FLOAT)", "3", "3"};
if (!f.brokenTestsEnabled()) {
return;
Expand Down
Loading