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

Public Member Functions

RexNode convertCall (SqlRexContext cx, SqlCall call)
 

Detailed Description

Convertlet that converts

GREATEST

and

LEAST

.

Definition at line 1333 of file StandardConvertletTable.java.

Member Function Documentation

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

Definition at line 1334 of file StandardConvertletTable.java.

References run_benchmark_import.type.

1334  {
1335  // Translate
1336  // GREATEST(a, b, c, d)
1337  // to
1338  // CASE
1339  // WHEN a IS NULL OR b IS NULL OR c IS NULL OR d IS NULL
1340  // THEN NULL
1341  // WHEN a > b AND a > c AND a > d
1342  // THEN a
1343  // WHEN b > c AND b > d
1344  // THEN b
1345  // WHEN c > d
1346  // THEN c
1347  // ELSE d
1348  // END
1349  final RexBuilder rexBuilder = cx.getRexBuilder();
1350  final RelDataType type = cx.getValidator().getValidatedNodeType(call);
1351  final SqlBinaryOperator op;
1352  switch (call.getKind()) {
1353  case GREATEST:
1354  op = SqlStdOperatorTable.GREATER_THAN;
1355  break;
1356  case LEAST:
1357  op = SqlStdOperatorTable.LESS_THAN;
1358  break;
1359  default:
1360  throw new AssertionError();
1361  }
1362  final List<RexNode> exprs = convertExpressionList(
1363  cx, call.getOperandList(), SqlOperandTypeChecker.Consistency.NONE);
1364  final List<RexNode> list = new ArrayList<>();
1365  final List<RexNode> orList = new ArrayList<>();
1366  for (RexNode expr : exprs) {
1367  orList.add(rexBuilder.makeCall(SqlStdOperatorTable.IS_NULL, expr));
1368  }
1369  list.add(RexUtil.composeDisjunction(rexBuilder, orList));
1370  list.add(rexBuilder.makeNullLiteral(type));
1371  for (int i = 0; i < exprs.size() - 1; i++) {
1372  RexNode expr = exprs.get(i);
1373  final List<RexNode> andList = new ArrayList<>();
1374  for (int j = i + 1; j < exprs.size(); j++) {
1375  final RexNode expr2 = exprs.get(j);
1376  andList.add(rexBuilder.makeCall(op, expr, expr2));
1377  }
1378  list.add(RexUtil.composeConjunction(rexBuilder, andList));
1379  list.add(expr);
1380  }
1381  list.add(exprs.get(exprs.size() - 1));
1382  return rexBuilder.makeCall(type, SqlStdOperatorTable.CASE, list);
1383  }
static List< RexNode > convertExpressionList(SqlRexContext cx, List< SqlNode > nodes, SqlOperandTypeChecker.Consistency consistency)

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