Skip to content

Commit

Permalink
DRILL-3610: Fix TIMESTAMPADD and TIMESTAMPDIFF functions
Browse files Browse the repository at this point in the history
  • Loading branch information
vvysotskyi committed Nov 8, 2018
1 parent 0109c8b commit 2af1cf0
Show file tree
Hide file tree
Showing 8 changed files with 556 additions and 134 deletions.
1 change: 1 addition & 0 deletions exec/java-exec/src/main/codegen/data/DateIntervalFunc.tdd
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
{intervals: ["Interval", "IntervalDay", "IntervalYear", "Int", "BigInt"] },
{truncInputTypes: ["Date", "TimeStamp", "Time", "Interval", "IntervalDay", "IntervalYear"] },
{truncUnits : ["Second", "Minute", "Hour", "Day", "Month", "Year", "Week", "Quarter", "Decade", "Century", "Millennium" ] },
{timestampDiffUnits : ["Nanosecond", "Microsecond", "Second", "Minute", "Hour", "Day", "Month", "Year", "Week", "Quarter"] },

{
varCharToDate: [
Expand Down
23 changes: 18 additions & 5 deletions exec/java-exec/src/main/codegen/data/MathFunctionTypes.tdd
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
{input1: "UInt4", input2: "UInt4", outputType: "UInt4", castType: "int"},
{input1: "UInt8", input2: "UInt8", outputType: "UInt8", castType: "long"}
]
},
},
{className: "Subtract", funcName: "subtract", op: "-", types: [
{input1: "Int", input2: "Int", outputType: "Int", castType: "int"},
{input1: "BigInt", input2: "BigInt", outputType: "BigInt", castType: "long"},
Expand All @@ -42,7 +42,7 @@
{input1: "UInt8", input2: "UInt8", outputType: "UInt8", castType: "long"}
]
},
{className: "Multiply", funcName: "multiply", op: "*", types: [
{className: "Multiply", funcName: "multiply", op: "*", types: [
{input1: "Int", input2: "Int", outputType: "Int", castType: "int"},
{input1: "BigInt", input2: "BigInt", outputType: "BigInt", castType: "long"},
{input1: "Float4", input2: "Float4", outputType: "Float4", castType: "float"},
Expand All @@ -54,8 +54,8 @@
{input1: "UInt4", input2: "UInt4", outputType: "UInt4", castType: "int"},
{input1: "UInt8", input2: "UInt8", outputType: "UInt8", castType: "long"}
]
},
{className: "Divide", funcName: "divide", op: "/", types: [
},
{className: "Divide", funcName: "divide", op: "/", types: [
{input1: "Int", input2: "Int", outputType: "Int", castType: "int"},
{input1: "BigInt", input2: "BigInt", outputType: "BigInt", castType: "long"},
{input1: "Float4", input2: "Float4", outputType: "Float4", castType: "float"},
Expand All @@ -67,6 +67,19 @@
{input1: "UInt4", input2: "UInt4", outputType: "UInt4", castType: "int"},
{input1: "UInt8", input2: "UInt8", outputType: "UInt8", castType: "long"}
]
}
},
{className: "DivideInt", funcName: "/int", op: "/", types: [
{input1: "Int", input2: "Int", outputType: "Int", castType: "int"},
{input1: "BigInt", input2: "BigInt", outputType: "Int", castType: "int"},
{input1: "Float4", input2: "Float4", outputType: "Int", castType: "int"},
{input1: "Float8", input2: "Float8", outputType: "Int", castType: "int"},
{input1: "SmallInt", input2: "SmallInt", outputType: "Int", castType: "int"},
{input1: "TinyInt", input2: "TinyInt", outputType: "Int", castType: "int"},
{input1: "UInt1", input2: "UInt1", outputType: "Int", castType: "int"},
{input1: "UInt2", input2: "UInt2", outputType: "Int", castType: "int"},
{input1: "UInt4", input2: "UInt4", outputType: "Int", castType: "int"},
{input1: "UInt8", input2: "UInt8", outputType: "Int", castType: "int"}
]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,20 +129,22 @@ public void eval() {
}
}

@SuppressWarnings("unused")
@FunctionTemplate(names = {"divide", "div"}, scope = FunctionTemplate.FunctionScope.SIMPLE, nulls=NullHandling.NULL_IF_NULL)
public static class ${intervaltype}${numerictype}DivideFunction implements DrillSimpleFunc {
@SuppressWarnings("unused")
@FunctionTemplate(names = {"divide", "div"<#if numerictype == "Int">, "/int"</#if>},
scope = FunctionTemplate.FunctionScope.SIMPLE,
nulls = NullHandling.NULL_IF_NULL)
public static class ${intervaltype}${numerictype}DivideFunction implements DrillSimpleFunc {
@Param ${intervaltype}Holder left;
@Param ${numerictype}Holder right;
@Output IntervalHolder out;

public void setup() {
}
public void setup() {
}

public void eval() {
<@intervalNumericArithmeticBlock left="left" right="right" temp = "temp" op = "/" out = "out" intervaltype=intervaltype />
}
public void eval() {
<@intervalNumericArithmeticBlock left="left" right="right" temp = "temp" op = "/" out = "out" intervaltype=intervaltype />
}
}
}
</#list>
</#list>
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
<@pp.dropOutputFile />
<#assign className="GTimestampDiff"/>

<@pp.changeOutputFile name="/org/apache/drill/exec/expr/fn/impl/${className}.java"/>

<#include "/@includes/license.ftl"/>

package org.apache.drill.exec.expr.fn.impl;

import org.apache.drill.exec.expr.DrillSimpleFunc;
import org.apache.drill.exec.expr.annotations.FunctionTemplate;
import org.apache.drill.exec.expr.annotations.FunctionTemplate.NullHandling;
import org.apache.drill.exec.expr.annotations.Output;
import org.apache.drill.exec.expr.annotations.Workspace;
import org.apache.drill.exec.expr.annotations.Param;
import org.apache.drill.exec.expr.holders.*;
import org.apache.drill.exec.record.RecordBatch;

/*
* This class is generated using freemarker and the ${.template_name} template.
*/

public class ${className} {

<#list dateIntervalFunc.timestampDiffUnits as unit>

<#list dateIntervalFunc.dates as fromUnit>
<#list dateIntervalFunc.dates as toUnit>

@FunctionTemplate(name = "timestampdiff${unit}",
scope = FunctionTemplate.FunctionScope.SIMPLE,
nulls = FunctionTemplate.NullHandling.NULL_IF_NULL)
public static class TimestampDiff${unit}${fromUnit}To${toUnit} implements DrillSimpleFunc {

@Param ${fromUnit}Holder left;
@Param ${toUnit}Holder right;
@Output BigIntHolder out;

public void setup() {
}

public void eval() {
<#if unit == "Nanosecond">
out.value = (right.value - left.value) * 1000000;
<#elseif unit == "Microsecond">
out.value = (right.value - left.value) * 1000;
<#elseif unit == "Second">
out.value = (right.value - left.value) / org.apache.drill.exec.vector.DateUtilities.secondsToMillis;
<#elseif unit == "Minute">
out.value = (right.value - left.value) / org.apache.drill.exec.vector.DateUtilities.minutesToMillis;
<#elseif unit == "Hour">
out.value = (right.value - left.value) / org.apache.drill.exec.vector.DateUtilities.hoursToMillis;
<#elseif unit == "Day">
out.value = (right.value - left.value) / org.apache.drill.exec.vector.DateUtilities.daysToStandardMillis;
<#elseif unit == "Week">
out.value = (right.value - left.value) / 604800000; // 7 * 24 * 60 * 60 * 1000
<#elseif unit == "Month" || unit == "Quarter" || unit == "Year">
long timeMilliseconds = left.value % org.apache.drill.exec.vector.DateUtilities.daysToStandardMillis
- right.value % org.apache.drill.exec.vector.DateUtilities.daysToStandardMillis;

java.time.Period between = java.time.Period.between(
java.time.Instant.ofEpochMilli(left.value).atZone(java.time.ZoneOffset.UTC).toLocalDate(),
java.time.Instant.ofEpochMilli(right.value).atZone(java.time.ZoneOffset.UTC).toLocalDate());
int days = between.getDays();
if (timeMilliseconds < 0 && days > 0) {
// in the case of negative time value increases left operand days value
between = java.time.Period.between(
java.time.Instant.ofEpochMilli(left.value + org.apache.drill.exec.vector.DateUtilities.daysToStandardMillis).atZone(java.time.ZoneOffset.UTC).toLocalDate(),
java.time.Instant.ofEpochMilli(right.value).atZone(java.time.ZoneOffset.UTC).toLocalDate());
} else if (timeMilliseconds > 0 && days < 0) {
// in the case of negative days value decreases it for the right operand
between = java.time.Period.between(
java.time.Instant.ofEpochMilli(left.value - org.apache.drill.exec.vector.DateUtilities.daysToStandardMillis).atZone(java.time.ZoneOffset.UTC).toLocalDate(),
java.time.Instant.ofEpochMilli(right.value).atZone(java.time.ZoneOffset.UTC).toLocalDate());
}
int months = between.getMonths() + between.getYears() * org.apache.drill.exec.vector.DateUtilities.yearsToMonths;

<#if unit == "Month">
out.value = months;
<#elseif unit == "Quarter">
out.value = months / 4;
<#elseif unit == "Year">
out.value = months / org.apache.drill.exec.vector.DateUtilities.yearsToMonths;
</#if>
</#if>
}
}
</#list>
</#list>

</#list>
}

0 comments on commit 2af1cf0

Please sign in to comment.