-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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-4924] REGR_SXX and similar aggregate functions return the wr… #3928
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,12 +16,25 @@ | |
*/ | ||
package org.apache.calcite.rel.type; | ||
|
||
import org.apache.calcite.runtime.CalciteException; | ||
import org.apache.calcite.runtime.Resources; | ||
import org.apache.calcite.sql.SqlKind; | ||
import org.apache.calcite.sql.SqlOperator; | ||
import org.apache.calcite.sql.SqlOperatorBinding; | ||
import org.apache.calcite.sql.SqlUtil; | ||
import org.apache.calcite.sql.fun.SqlStdOperatorTable; | ||
import org.apache.calcite.sql.parser.SqlParserPos; | ||
import org.apache.calcite.sql.type.BasicSqlType; | ||
import org.apache.calcite.sql.type.SqlTypeFamily; | ||
import org.apache.calcite.sql.type.SqlTypeName; | ||
import org.apache.calcite.sql.validate.SqlValidatorException; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
|
||
import org.checkerframework.checker.nullness.qual.Nullable; | ||
|
||
import java.util.List; | ||
|
||
/** Default implementation of | ||
* {@link org.apache.calcite.rel.type.RelDataTypeSystem}, | ||
* providing parameters from the SQL standard. | ||
|
@@ -255,8 +268,72 @@ | |
return argumentType; | ||
} | ||
|
||
@Override public RelDataType deriveCovarType(RelDataTypeFactory typeFactory, | ||
@Override public RelDataType deriveCovarType(RelDataTypeFactory typeFactory, SqlKind sqlKind, | ||
RelDataType arg0Type, RelDataType arg1Type) { | ||
switch (sqlKind) { | ||
case REGR_SXX: | ||
// REGR_SXX(x, y) → REGR_COUNT(x, y) * VAR_POP(y) | ||
|
||
// REGR_COUNT(x, y) | ||
RelDataType regrCountTypeSXX = | ||
getOperatorRelDataType(typeFactory, SqlStdOperatorTable.REGR_COUNT, arg0Type, arg1Type); | ||
// VAR_POP(y) | ||
RelDataType varPopTypeY = | ||
getOperatorRelDataType(typeFactory, SqlStdOperatorTable.VAR_POP, arg1Type); | ||
// REGR_COUNT(x, y) * VAR_POP(y) | ||
return getOperatorRelDataType( | ||
typeFactory, SqlStdOperatorTable.MULTIPLY, regrCountTypeSXX, varPopTypeY); | ||
|
||
case REGR_SYY: | ||
// REGR_SYY(x, y) → REGR_COUNT(x, y) * VAR_POP(x) | ||
|
||
// REGR_COUNT(x, y) | ||
RelDataType regrCountTypeSYY = | ||
getOperatorRelDataType(typeFactory, SqlStdOperatorTable.REGR_COUNT, arg0Type, arg1Type); | ||
// VAR_POP(x) | ||
RelDataType varPopTypeX = | ||
getOperatorRelDataType(typeFactory, SqlStdOperatorTable.VAR_POP, arg0Type); | ||
// REGR_COUNT(x, y) * VAR_POP(y) | ||
return getOperatorRelDataType( | ||
typeFactory, SqlStdOperatorTable.MULTIPLY, regrCountTypeSYY, varPopTypeX); | ||
|
||
case COVAR_POP: | ||
case COVAR_SAMP: | ||
// COVAR_POP(x, y) → (SUM(x * y) - SUM(x) * SUM(y) / REGR_COUNT(x, y)) / REGR_COUNT(x, y) | ||
// COVAR_SAMP(x, y) → (SUM(x * y) - SUM(x) * SUM(y) / REGR_COUNT(x, y)) / | ||
// CASE REGR_COUNT(x, y) WHEN 1 THEN NULL ELSE REGR_COUNT(x, y) - 1 END | ||
|
||
// (x * y) | ||
RelDataType multiplyXY = | ||
getOperatorRelDataType(typeFactory, SqlStdOperatorTable.MULTIPLY, arg0Type, arg1Type); | ||
// SUM(x * y) | ||
RelDataType sumMultiplyXY = | ||
getOperatorRelDataType(typeFactory, SqlStdOperatorTable.SUM, multiplyXY); | ||
// SUM(x) | ||
RelDataType sumX = getOperatorRelDataType(typeFactory, SqlStdOperatorTable.SUM, arg0Type); | ||
// SUM(y) | ||
RelDataType sumY = getOperatorRelDataType(typeFactory, SqlStdOperatorTable.SUM, arg1Type); | ||
// SUM(x) * SUM(y) | ||
RelDataType multiplySumXSumY = | ||
getOperatorRelDataType(typeFactory, SqlStdOperatorTable.MULTIPLY, sumX, sumY); | ||
// REGR_COUNT(x, y) | ||
RelDataType regrCountXY = | ||
getOperatorRelDataType(typeFactory, SqlStdOperatorTable.REGR_COUNT, arg0Type, arg1Type); | ||
// SUM(x) * SUM(y) / REGR_COUNT(x, y) | ||
RelDataType divide = | ||
getOperatorRelDataType(typeFactory, | ||
SqlStdOperatorTable.DIVIDE, multiplySumXSumY, regrCountXY); | ||
// SUM(x * y) - SUM(x) * SUM(y) / REGR_COUNT(x, y) | ||
RelDataType minus = | ||
getOperatorRelDataType(typeFactory, SqlStdOperatorTable.MINUS, sumMultiplyXY, divide); | ||
// (sum(x * y) - sum(x) * sum(y) / regr_count(x, y)) / regr_count(x, y) | ||
RelDataType relDataType = | ||
getOperatorRelDataType(typeFactory, SqlStdOperatorTable.DIVIDE, minus, regrCountXY); | ||
if (sqlKind == SqlKind.COVAR_POP) { | ||
return relDataType; | ||
} | ||
return typeFactory.createTypeWithNullability(relDataType, true); | ||
} | ||
return arg0Type; | ||
} | ||
|
||
|
@@ -278,4 +355,37 @@ | |
return false; | ||
} | ||
|
||
/** | ||
* Implementation of the {@link SqlOperatorBinding} interface. | ||
*/ | ||
public static class TypeCallBinding extends SqlOperatorBinding { | ||
private final List<RelDataType> operands; | ||
|
||
public TypeCallBinding(RelDataTypeFactory typeFactory, | ||
SqlOperator sqlOperator, List<RelDataType> operands) { | ||
super(typeFactory, sqlOperator); | ||
this.operands = operands; | ||
} | ||
|
||
@Override public int getOperandCount() { | ||
return operands.size(); | ||
} | ||
|
||
@Override public RelDataType getOperandType(int ordinal) { | ||
return operands.get(ordinal); | ||
} | ||
|
||
@Override public CalciteException newError( | ||
Resources.ExInst<SqlValidatorException> e) { | ||
return SqlUtil.newContextException(SqlParserPos.ZERO, e); | ||
} | ||
} | ||
|
||
private RelDataType getOperatorRelDataType(RelDataTypeFactory typeFactory, | ||
SqlOperator sqlOperator, RelDataType... argumentTypes) { | ||
ImmutableList<RelDataType> operatorTypeList = ImmutableList.copyOf(argumentTypes); | ||
TypeCallBinding operatorBinding = | ||
new TypeCallBinding(typeFactory, sqlOperator, operatorTypeList); | ||
return sqlOperator.getReturnTypeInference().inferReturnType(operatorBinding); | ||
Check failure on line 389 in core/src/main/java/org/apache/calcite/rel/type/RelDataTypeSystemImpl.java GitHub Actions / CheckerFramework (JDK 11)
Check failure on line 389 in core/src/main/java/org/apache/calcite/rel/type/RelDataTypeSystemImpl.java GitHub Actions / CheckerFramework (JDK 11)
Check failure on line 389 in core/src/main/java/org/apache/calcite/rel/type/RelDataTypeSystemImpl.java GitHub Actions / CheckerFramework (JDK 11), oldest Guava
Check failure on line 389 in core/src/main/java/org/apache/calcite/rel/type/RelDataTypeSystemImpl.java GitHub Actions / CheckerFramework (JDK 11), oldest Guava
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2937,7 +2937,7 @@ from "scott".emp; | |
+-----------------------+----------------------+---------------+---------------+ | ||
| COVAR_POP(COMM, COMM) | COVAR_SAMP(SAL, SAL) | VAR_POP(COMM) | VAR_SAMP(SAL) | | ||
+-----------------------+----------------------+---------------+---------------+ | ||
| 272500.0000 | 1398313.8736 | 272500.0000 | 1398313.8736 | | ||
| 272500.0000000000 | 1398313.873626374 | 272500.0000 | 1398313.8736 | | ||
+-----------------------+----------------------+---------------+---------------+ | ||
(1 row) | ||
|
||
|
@@ -2981,16 +2981,41 @@ group by MONTH(HIREDATE); | |
| 1 | | | 1201250.0000 | | ||
| 11 | | | | | ||
| 12 | | | 1510833.3333 | | ||
| 2 | -35000.0000 | 10000.0000 | 831458.3333 | | ||
| 4 | | | | | ||
| 5 | | | | | ||
| 6 | | | | | ||
| 9 | -175000.0000 | 490000.0000 | 31250.0000 | | ||
| 2 | -35000.00000000000 | 10000.0000 | 831458.3333 | | ||
| 9 | -175000.0000000000 | 490000.0000 | 31250.0000 | | ||
+-------+-----------------------+---------------+---------------+ | ||
(8 rows) | ||
|
||
!ok | ||
|
||
# [CALCITE-4924] REGR_SXX and similar aggregate functions return the wrong data type | ||
SELECT | ||
MONTH(HIREDATE) as "MONTH", | ||
covar_samp(SAL, COMM) as "COVAR_SAMP(SAL, COMM)", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Compared to the previous one, this test can remove the cast. |
||
covar_pop(SAL, COMM) as "COVAR_POP(SAL, COMM)", | ||
regr_syy(SAL, COMM) as "REGR_SYY(COMM)", | ||
regr_sxx(SAL, COMM) as "REGR_SXX(SAL)" | ||
from "scott".emp | ||
group by MONTH(HIREDATE); | ||
+-------+-----------------------+----------------------+----------------+---------------+ | ||
| MONTH | COVAR_SAMP(SAL, COMM) | COVAR_POP(SAL, COMM) | REGR_SYY(COMM) | REGR_SXX(SAL) | | ||
+-------+-----------------------+----------------------+----------------+---------------+ | ||
| 1 | | | | | | ||
| 11 | | | | | | ||
| 12 | | | | | | ||
| 2 | -35000.00000000000 | -17500.00000000000 | 61250.00 | 20000.00 | | ||
| 4 | | | | | | ||
| 5 | | | | | | ||
| 6 | | | | | | ||
| 9 | -175000.0000000000 | -87500.00000000000 | 31250.00 | 980000.00 | | ||
+-------+-----------------------+----------------------+----------------+---------------+ | ||
(8 rows) | ||
|
||
!ok | ||
|
||
# [CALCITE-2224] WITHIN GROUP clause for aggregate functions | ||
select deptno, collect(empno) within group (order by empno asc) as empnos | ||
from "scott".emp | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,9 +15,17 @@ | |
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
!use post | ||
values 1; | ||
!use scott | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This example is for review. |
||
select regr_sxx(SAL, COMM) from "scott".emp; | ||
EXPR$0 | ||
1 | ||
1090000.00 | ||
!ok | ||
|
||
EnumerableCalc(expr#0..2=[{inputs}], expr#3=[CAST($t0):DECIMAL(19, 2)], expr#4=[0], expr#5=[=($t2, $t4)], expr#6=[null:INTEGER], expr#7=[*($t1, $t1)], expr#8=[/($t7, $t2)], expr#9=[CASE($t5, $t6, $t8)], expr#10=[CAST($t9):DECIMAL(19, 2)], expr#11=[-($t3, $t10)], EXPR$0=[$t11]) | ||
EnumerableAggregate(group=[{}], agg#0=[SUM($1) FILTER $2], agg#1=[SUM($0) FILTER $2], agg#2=[REGR_COUNT($0) FILTER $2]) | ||
EnumerableCalc(expr#0..7=[{inputs}], expr#8=[CAST($t6):DECIMAL(19, 2)], expr#9=[*($t8, $t8)], expr#10=[IS NOT NULL($t8)], expr#11=[CAST($t5):DECIMAL(19, 2)], expr#12=[IS NOT NULL($t11)], expr#13=[AND($t10, $t12)], COMM=[$t6], $f8=[$t9], $f9=[$t13]) | ||
EnumerableTableScan(table=[[scott, EMP]]) | ||
!plan | ||
|
||
|
||
# End dummy.iq |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if this is a reasonable implementation, but from the test cases, it seems feasible.