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

Public Member Functions

RexNode convertCall (SqlRexContext cx, SqlCall call)
 

Package Functions

 AvgVarianceConvertlet (SqlKind kind)
 

Private Member Functions

SqlNode expandAvg (final SqlNode arg, final RelDataType avgType, final SqlRexContext cx)
 
SqlNode expandVariance (final SqlNode argInput, final RelDataType varType, final SqlRexContext cx, boolean biased, boolean sqrt)
 
SqlNode getCastedSqlNode (SqlNode argInput, RelDataType varType, SqlParserPos pos, RexNode argRex)
 

Private Attributes

final SqlKind kind
 

Detailed Description

Convertlet that handles

and

VARIANCE

windowed aggregate functions.

Definition at line 1173 of file StandardConvertletTable.java.

Constructor & Destructor Documentation

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

Definition at line 1176 of file StandardConvertletTable.java.

Member Function Documentation

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

Definition at line 1180 of file StandardConvertletTable.java.

References AVG, and run_benchmark_import.type.

1180  {
1181  assert call.operandCount() == 1;
1182  final SqlNode arg = call.operand(0);
1183  final SqlNode expr;
1184  final RelDataType type = cx.getValidator().getValidatedNodeType(call);
1185  switch (kind) {
1186  case AVG:
1187  expr = expandAvg(arg, type, cx);
1188  break;
1189  case STDDEV_POP:
1190  expr = expandVariance(arg, type, cx, true, true);
1191  break;
1192  case STDDEV_SAMP:
1193  expr = expandVariance(arg, type, cx, false, true);
1194  break;
1195  case VAR_POP:
1196  expr = expandVariance(arg, type, cx, true, false);
1197  break;
1198  case VAR_SAMP:
1199  expr = expandVariance(arg, type, cx, false, false);
1200  break;
1201  default:
1202  throw Util.unexpected(kind);
1203  }
1204  RexNode rex = cx.convertExpression(expr);
1205  return cx.getRexBuilder().ensureType(type, rex, true);
1206  }
SqlNode expandVariance(final SqlNode argInput, final RelDataType varType, final SqlRexContext cx, boolean biased, boolean sqrt)
SqlNode expandAvg(final SqlNode arg, final RelDataType avgType, final SqlRexContext cx)
SqlNode org.apache.calcite.sql2rel.StandardConvertletTable.AvgVarianceConvertlet.expandAvg ( final SqlNode  arg,
final RelDataType  avgType,
final SqlRexContext  cx 
)
inlineprivate

Definition at line 1208 of file StandardConvertletTable.java.

1209  {
1210  final SqlParserPos pos = SqlParserPos.ZERO;
1211  final SqlNode sum = SqlStdOperatorTable.SUM.createCall(pos, arg);
1212  final RexNode sumRex = cx.convertExpression(sum);
1213  final SqlNode sumCast;
1214  sumCast = getCastedSqlNode(sum, avgType, pos, sumRex);
1215  final SqlNode count = SqlStdOperatorTable.COUNT.createCall(pos, arg);
1216  return SqlStdOperatorTable.DIVIDE.createCall(pos, sumCast, count);
1217  }
SqlNode getCastedSqlNode(SqlNode argInput, RelDataType varType, SqlParserPos pos, RexNode argRex)
SqlNode org.apache.calcite.sql2rel.StandardConvertletTable.AvgVarianceConvertlet.expandVariance ( final SqlNode  argInput,
final RelDataType  varType,
final SqlRexContext  cx,
boolean  biased,
boolean  sqrt 
)
inlineprivate

Definition at line 1219 of file StandardConvertletTable.java.

References run_benchmark_import.result.

1223  {
1224  // stddev_pop(x) ==>
1225  // power(
1226  // (sum(x * x) - sum(x) * sum(x) / count(x))
1227  // / count(x),
1228  // .5)
1229  //
1230  // stddev_samp(x) ==>
1231  // power(
1232  // (sum(x * x) - sum(x) * sum(x) / count(x))
1233  // / (count(x) - 1),
1234  // .5)
1235  //
1236  // var_pop(x) ==>
1237  // (sum(x * x) - sum(x) * sum(x) / count(x))
1238  // / count(x)
1239  //
1240  // var_samp(x) ==>
1241  // (sum(x * x) - sum(x) * sum(x) / count(x))
1242  // / (count(x) - 1)
1243  final SqlParserPos pos = SqlParserPos.ZERO;
1244 
1245  final SqlNode arg =
1246  getCastedSqlNode(argInput, varType, pos, cx.convertExpression(argInput));
1247 
1248  final SqlNode argSquared = SqlStdOperatorTable.MULTIPLY.createCall(pos, arg, arg);
1249  final SqlNode argSquaredCasted = getCastedSqlNode(
1250  argSquared, varType, pos, cx.convertExpression(argSquared));
1251  final SqlNode sumArgSquared =
1252  SqlStdOperatorTable.SUM.createCall(pos, argSquaredCasted);
1253  final SqlNode sumArgSquaredCasted = getCastedSqlNode(
1254  sumArgSquared, varType, pos, cx.convertExpression(sumArgSquared));
1255  final SqlNode sum = SqlStdOperatorTable.SUM.createCall(pos, arg);
1256  final SqlNode sumCasted =
1257  getCastedSqlNode(sum, varType, pos, cx.convertExpression(sum));
1258  final SqlNode sumSquared =
1259  SqlStdOperatorTable.MULTIPLY.createCall(pos, sumCasted, sumCasted);
1260  final SqlNode sumSquaredCasted = getCastedSqlNode(
1261  sumSquared, varType, pos, cx.convertExpression(sumSquared));
1262  final SqlNode count = SqlStdOperatorTable.COUNT.createCall(pos, arg);
1263  final SqlNode countCasted =
1264  getCastedSqlNode(count, varType, pos, cx.convertExpression(count));
1265  final SqlNode avgSumSquared =
1266  SqlStdOperatorTable.DIVIDE.createCall(pos, sumSquaredCasted, countCasted);
1267  final SqlNode avgSumSquaredCasted = getCastedSqlNode(
1268  avgSumSquared, varType, pos, cx.convertExpression(avgSumSquared));
1269  final SqlNode diff = SqlStdOperatorTable.MINUS.createCall(
1270  pos, sumArgSquaredCasted, avgSumSquaredCasted);
1271  final SqlNode diffCasted =
1272  getCastedSqlNode(diff, varType, pos, cx.convertExpression(diff));
1273  final SqlNode denominator;
1274  if (biased) {
1275  denominator = countCasted;
1276  } else {
1277  final SqlNumericLiteral one = SqlLiteral.createExactNumeric("1", pos);
1278  final SqlLiteral nullLiteral = SqlLiteral.createNull(SqlParserPos.ZERO);
1279  denominator = new SqlCase(SqlParserPos.ZERO,
1280  count,
1281  SqlNodeList.of(SqlStdOperatorTable.EQUALS.createCall(pos, count, one)),
1282  SqlNodeList.of(getCastedSqlNode(nullLiteral, varType, pos, null)),
1283  SqlStdOperatorTable.MINUS.createCall(pos, count, one));
1284  }
1285  final SqlNode div =
1286  SqlStdOperatorTable.DIVIDE.createCall(pos, diffCasted, denominator);
1287  final SqlNode divCasted =
1288  getCastedSqlNode(div, varType, pos, cx.convertExpression(div));
1289 
1290  SqlNode result = div;
1291  if (sqrt) {
1292  final SqlNumericLiteral half = SqlLiteral.createExactNumeric("0.5", pos);
1293  result = SqlStdOperatorTable.POWER.createCall(pos, divCasted, half);
1294  }
1295  return result;
1296  }
SqlNode getCastedSqlNode(SqlNode argInput, RelDataType varType, SqlParserPos pos, RexNode argRex)
SqlNode org.apache.calcite.sql2rel.StandardConvertletTable.AvgVarianceConvertlet.getCastedSqlNode ( SqlNode  argInput,
RelDataType  varType,
SqlParserPos  pos,
RexNode  argRex 
)
inlineprivate

Definition at line 1298 of file StandardConvertletTable.java.

1299  {
1300  SqlNode arg;
1301  if (argRex != null && !argRex.getType().equals(varType)) {
1302  arg = SqlStdOperatorTable.CAST.createCall(
1303  pos, argInput, SqlTypeUtil.convertTypeToSpec(varType));
1304  } else {
1305  arg = argInput;
1306  }
1307  return arg;
1308  }

Member Data Documentation

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

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