You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by GitBox <gi...@apache.org> on 2019/05/21 02:37:53 UTC

[GitHub] [incubator-druid] gianm commented on a change in pull request #7695: Add TIMESTAMPDIFF sql support

gianm commented on a change in pull request #7695: Add TIMESTAMPDIFF sql support
URL: https://github.com/apache/incubator-druid/pull/7695#discussion_r285812299
 
 

 ##########
 File path: sql/src/main/java/org/apache/druid/sql/calcite/expression/builtin/TimeArithmeticOperatorConversion.java
 ##########
 @@ -70,44 +71,67 @@ public DruidExpression toDruidExpression(
       throw new IAE("Expected 2 args, got %s", operands.size());
     }
 
-    final RexNode timeRexNode = operands.get(0);
-    final RexNode shiftRexNode = operands.get(1);
+    final RexNode leftRexNode = operands.get(0);
+    final RexNode rightRexNode = operands.get(1);
 
-    final DruidExpression timeExpr = Expressions.toDruidExpression(plannerContext, rowSignature, timeRexNode);
-    final DruidExpression shiftExpr = Expressions.toDruidExpression(plannerContext, rowSignature, shiftRexNode);
+    final DruidExpression leftExpr = Expressions.toDruidExpression(plannerContext, rowSignature, leftRexNode);
+    final DruidExpression rightExpr = Expressions.toDruidExpression(plannerContext, rowSignature, rightRexNode);
 
-    if (timeExpr == null || shiftExpr == null) {
+    if (leftExpr == null || rightExpr == null) {
       return null;
     }
 
-    if (shiftRexNode.getType().getFamily() == SqlTypeFamily.INTERVAL_YEAR_MONTH) {
+    if (rightRexNode.getType().getFamily() == SqlTypeFamily.INTERVAL_YEAR_MONTH) {
       // timestamp_expr { + | - } <interval_expr> (year-month interval)
       // Period is a value in months.
       return DruidExpression.fromExpression(
           DruidExpression.functionCall(
               "timestamp_shift",
-              timeExpr,
-              shiftExpr.map(
+              leftExpr,
+              rightExpr.map(
                   simpleExtraction -> null,
                   expression -> StringUtils.format("concat('P', %s, 'M')", expression)
               ),
               DruidExpression.fromExpression(DruidExpression.numberLiteral(direction > 0 ? 1 : -1))
           )
       );
-    } else if (shiftRexNode.getType().getFamily() == SqlTypeFamily.INTERVAL_DAY_TIME) {
+    } else if (rightRexNode.getType().getFamily() == SqlTypeFamily.INTERVAL_DAY_TIME) {
       // timestamp_expr { + | - } <interval_expr> (day-time interval)
       // Period is a value in milliseconds. Ignore time zone.
       return DruidExpression.fromExpression(
           StringUtils.format(
               "(%s %s %s)",
-              timeExpr.getExpression(),
+              leftExpr.getExpression(),
               direction > 0 ? "+" : "-",
-              shiftExpr.getExpression()
+              rightExpr.getExpression()
           )
       );
+    } else if ((leftRexNode.getType().getFamily() == SqlTypeFamily.TIMESTAMP ||
+        leftRexNode.getType().getFamily() == SqlTypeFamily.DATE) &&
+        (rightRexNode.getType().getFamily() == SqlTypeFamily.TIMESTAMP ||
+        rightRexNode.getType().getFamily() == SqlTypeFamily.DATE)) {
+      final String exprstr;
+      if (call.getType().getSqlTypeName() == SqlTypeName.INTERVAL_MONTH ||
+          call.getType().getSqlTypeName() == SqlTypeName.INTERVAL_YEAR) {
+        exprstr = StringUtils.format(
+            "subtract_months(%s, %s)",
 
 Review comment:
   `subtract_months` should also take a timezone parameter, and this code here should pass down the context timezone. (See #7704 for a recent PR fixing this behavior for TIME_SHIFT.) The idea is that the month that the two instants are in, and therefore the expected result of `subtract_months`, could vary based on the timezone.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org