OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
org.apache.calcite.sql2rel.StandardConvertletTable.RegrCovarianceConvertlet Class Reference
+ Inheritance diagram for org.apache.calcite.sql2rel.StandardConvertletTable.RegrCovarianceConvertlet:
+ Collaboration diagram for org.apache.calcite.sql2rel.StandardConvertletTable.RegrCovarianceConvertlet:

Public Member Functions

RexNode convertCall (SqlRexContext cx, SqlCall call)
 

Package Functions

 RegrCovarianceConvertlet (SqlKind kind)
 

Private Member Functions

SqlNode expandRegrSzz (final SqlNode arg1, final SqlNode arg2, final RelDataType avgType, final SqlRexContext cx, boolean variance)
 
SqlNode expandCovariance (final SqlNode arg0Input, final SqlNode arg1Input, final SqlNode dependent, final RelDataType varType, final SqlRexContext cx, boolean biased)
 
SqlNode getCastedSqlNode (SqlNode argInput, RelDataType varType, SqlParserPos pos, RexNode argRex)
 

Private Attributes

final SqlKind kind
 

Detailed Description

Convertlet that handles

COVAR_POP

,

COVAR_SAMP

,

REGR_SXX

,

REGR_SYY

windowed aggregate functions.

Definition at line 1043 of file StandardConvertletTable.java.

Constructor & Destructor Documentation

org.apache.calcite.sql2rel.StandardConvertletTable.RegrCovarianceConvertlet.RegrCovarianceConvertlet ( SqlKind  kind)
inlinepackage

Definition at line 1046 of file StandardConvertletTable.java.

Member Function Documentation

RexNode org.apache.calcite.sql2rel.StandardConvertletTable.RegrCovarianceConvertlet.convertCall ( SqlRexContext  cx,
SqlCall  call 
)
inline

Definition at line 1050 of file StandardConvertletTable.java.

References run_benchmark_import.type.

1050  {
1051  assert call.operandCount() == 2;
1052  final SqlNode arg1 = call.operand(0);
1053  final SqlNode arg2 = call.operand(1);
1054  final SqlNode expr;
1055  final RelDataType type = cx.getValidator().getValidatedNodeType(call);
1056  switch (kind) {
1057  case COVAR_POP:
1058  expr = expandCovariance(arg1, arg2, null, type, cx, true);
1059  break;
1060  case COVAR_SAMP:
1061  expr = expandCovariance(arg1, arg2, null, type, cx, false);
1062  break;
1063  case REGR_SXX:
1064  expr = expandRegrSzz(arg2, arg1, type, cx, true);
1065  break;
1066  case REGR_SYY:
1067  expr = expandRegrSzz(arg1, arg2, type, cx, true);
1068  break;
1069  default:
1070  throw Util.unexpected(kind);
1071  }
1072  RexNode rex = cx.convertExpression(expr);
1073  return cx.getRexBuilder().ensureType(type, rex, true);
1074  }
SqlNode expandRegrSzz(final SqlNode arg1, final SqlNode arg2, final RelDataType avgType, final SqlRexContext cx, boolean variance)
SqlNode expandCovariance(final SqlNode arg0Input, final SqlNode arg1Input, final SqlNode dependent, final RelDataType varType, final SqlRexContext cx, boolean biased)
SqlNode org.apache.calcite.sql2rel.StandardConvertletTable.RegrCovarianceConvertlet.expandCovariance ( final SqlNode  arg0Input,
final SqlNode  arg1Input,
final SqlNode  dependent,
final RelDataType  varType,
final SqlRexContext  cx,
boolean  biased 
)
inlineprivate

Definition at line 1091 of file StandardConvertletTable.java.

1096  {
1097  // covar_pop(x1, x2) ==>
1098  // (sum(x1 * x2) - sum(x2) * sum(x1) / count(x1, x2))
1099  // / count(x1, x2)
1100  //
1101  // covar_samp(x1, x2) ==>
1102  // (sum(x1 * x2) - sum(x1) * sum(x2) / count(x1, x2))
1103  // / (count(x1, x2) - 1)
1104  final SqlParserPos pos = SqlParserPos.ZERO;
1105  final SqlLiteral nullLiteral = SqlLiteral.createNull(SqlParserPos.ZERO);
1106 
1107  final RexNode arg0Rex = cx.convertExpression(arg0Input);
1108  final RexNode arg1Rex = cx.convertExpression(arg1Input);
1109 
1110  final SqlNode arg0 = getCastedSqlNode(arg0Input, varType, pos, arg0Rex);
1111  final SqlNode arg1 = getCastedSqlNode(arg1Input, varType, pos, arg1Rex);
1112  final SqlNode argSquared = SqlStdOperatorTable.MULTIPLY.createCall(pos, arg0, arg1);
1113  final SqlNode sumArgSquared;
1114  final SqlNode sum0;
1115  final SqlNode sum1;
1116  final SqlNode count;
1117  if (dependent == null) {
1118  sumArgSquared = SqlStdOperatorTable.SUM.createCall(pos, argSquared);
1119  sum0 = SqlStdOperatorTable.SUM.createCall(pos, arg0, arg1);
1120  sum1 = SqlStdOperatorTable.SUM.createCall(pos, arg1, arg0);
1121  count = SqlStdOperatorTable.REGR_COUNT.createCall(pos, arg0, arg1);
1122  } else {
1123  sumArgSquared = SqlStdOperatorTable.SUM.createCall(pos, argSquared, dependent);
1124  sum0 = SqlStdOperatorTable.SUM.createCall(
1125  pos, arg0, Objects.equals(dependent, arg0Input) ? arg1 : dependent);
1126  sum1 = SqlStdOperatorTable.SUM.createCall(
1127  pos, arg1, Objects.equals(dependent, arg1Input) ? arg0 : dependent);
1128  count = SqlStdOperatorTable.REGR_COUNT.createCall(
1129  pos, arg0, Objects.equals(dependent, arg0Input) ? arg1 : dependent);
1130  }
1131 
1132  final SqlNode sumSquared = SqlStdOperatorTable.MULTIPLY.createCall(pos, sum0, sum1);
1133  final SqlNode countCasted =
1134  getCastedSqlNode(count, varType, pos, cx.convertExpression(count));
1135 
1136  final SqlNode avgSumSquared =
1137  SqlStdOperatorTable.DIVIDE.createCall(pos, sumSquared, countCasted);
1138  final SqlNode diff =
1139  SqlStdOperatorTable.MINUS.createCall(pos, sumArgSquared, avgSumSquared);
1140  SqlNode denominator;
1141  if (biased) {
1142  denominator = countCasted;
1143  } else {
1144  final SqlNumericLiteral one = SqlLiteral.createExactNumeric("1", pos);
1145  denominator = new SqlCase(SqlParserPos.ZERO,
1146  countCasted,
1147  SqlNodeList.of(
1148  SqlStdOperatorTable.EQUALS.createCall(pos, countCasted, one)),
1149  SqlNodeList.of(getCastedSqlNode(nullLiteral, varType, pos, null)),
1150  SqlStdOperatorTable.MINUS.createCall(pos, countCasted, one));
1151  }
1152 
1153  return SqlStdOperatorTable.DIVIDE.createCall(pos, diff, denominator);
1154  }
SqlNode getCastedSqlNode(SqlNode argInput, RelDataType varType, SqlParserPos pos, RexNode argRex)
SqlNode org.apache.calcite.sql2rel.StandardConvertletTable.RegrCovarianceConvertlet.expandRegrSzz ( final SqlNode  arg1,
final SqlNode  arg2,
final RelDataType  avgType,
final SqlRexContext  cx,
boolean  variance 
)
inlineprivate

Definition at line 1076 of file StandardConvertletTable.java.

1080  {
1081  final SqlParserPos pos = SqlParserPos.ZERO;
1082  final SqlNode count = SqlStdOperatorTable.REGR_COUNT.createCall(pos, arg1, arg2);
1083  final SqlNode varPop =
1084  expandCovariance(arg1, variance ? arg1 : arg2, arg2, avgType, cx, true);
1085  final RexNode varPopRex = cx.convertExpression(varPop);
1086  final SqlNode varPopCast;
1087  varPopCast = getCastedSqlNode(varPop, avgType, pos, varPopRex);
1088  return SqlStdOperatorTable.MULTIPLY.createCall(pos, varPopCast, count);
1089  }
SqlNode expandCovariance(final SqlNode arg0Input, final SqlNode arg1Input, final SqlNode dependent, final RelDataType varType, final SqlRexContext cx, boolean biased)
SqlNode getCastedSqlNode(SqlNode argInput, RelDataType varType, SqlParserPos pos, RexNode argRex)
SqlNode org.apache.calcite.sql2rel.StandardConvertletTable.RegrCovarianceConvertlet.getCastedSqlNode ( SqlNode  argInput,
RelDataType  varType,
SqlParserPos  pos,
RexNode  argRex 
)
inlineprivate

Definition at line 1156 of file StandardConvertletTable.java.

1157  {
1158  SqlNode arg;
1159  if (argRex != null && !argRex.getType().equals(varType)) {
1160  arg = SqlStdOperatorTable.CAST.createCall(
1161  pos, argInput, SqlTypeUtil.convertTypeToSpec(varType));
1162  } else {
1163  arg = argInput;
1164  }
1165  return arg;
1166  }

Member Data Documentation

final SqlKind org.apache.calcite.sql2rel.StandardConvertletTable.RegrCovarianceConvertlet.kind
private

The documentation for this class was generated from the following file: