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

Public Member Functions

RexNode convertCall (SqlRexContext cx, SqlCall call)
 

Detailed Description

Convertlet that handles the

TIMESTAMPDIFF

function.

Definition at line 1427 of file StandardConvertletTable.java.

Member Function Documentation

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

Definition at line 1428 of file StandardConvertletTable.java.

1428  {
1429  // TIMESTAMPDIFF(unit, t1, t2)
1430  // => (t2 - t1) UNIT
1431  final RexBuilder rexBuilder = cx.getRexBuilder();
1432  final SqlLiteral unitLiteral = call.operand(0);
1433  TimeUnit unit = unitLiteral.symbolValue(TimeUnit.class);
1434  BigDecimal multiplier = BigDecimal.ONE;
1435  BigDecimal divider = BigDecimal.ONE;
1436  SqlTypeName sqlTypeName =
1437  unit == TimeUnit.NANOSECOND ? SqlTypeName.BIGINT : SqlTypeName.INTEGER;
1438  switch (unit) {
1439  case MICROSECOND:
1440  case MILLISECOND:
1441  case NANOSECOND:
1442  case WEEK:
1443  multiplier = BigDecimal.valueOf(DateTimeUtils.MILLIS_PER_SECOND);
1444  divider = unit.multiplier;
1445  unit = TimeUnit.SECOND;
1446  break;
1447  case QUARTER:
1448  divider = unit.multiplier;
1449  unit = TimeUnit.MONTH;
1450  break;
1451  }
1452  final SqlIntervalQualifier qualifier =
1453  new SqlIntervalQualifier(unit, null, SqlParserPos.ZERO);
1454  final RexNode op2 = cx.convertExpression(call.operand(2));
1455  final RexNode op1 = cx.convertExpression(call.operand(1));
1456  final RelDataType intervalType = cx.getTypeFactory().createTypeWithNullability(
1457  cx.getTypeFactory().createSqlIntervalType(qualifier),
1458  op1.getType().isNullable() || op2.getType().isNullable());
1459  final RexCall rexCall = (RexCall) rexBuilder.makeCall(
1460  intervalType, SqlStdOperatorTable.MINUS_DATE, ImmutableList.of(op2, op1));
1461  final RelDataType intType = cx.getTypeFactory().createTypeWithNullability(
1462  cx.getTypeFactory().createSqlType(sqlTypeName),
1463  SqlTypeUtil.containsNullable(rexCall.getType()));
1464  RexNode e = rexBuilder.makeCast(intType, rexCall);
1465  return rexBuilder.multiplyDivide(e, multiplier, divider);
1466  }

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