Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* 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.
*/
package org.apache.pinot.common.function.scalar;

import org.apache.pinot.spi.annotations.ScalarFunction;
import org.apache.pinot.spi.data.DateTimeFieldSpec;
import org.apache.pinot.spi.data.DateTimeFormatSpec;
import org.apache.pinot.spi.data.DateTimeGranularitySpec;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormatter;


/**
* Equivalent to {@code DateTimeConversionTransformFunction}.
*/
public class DateTimeConvert {
private DateTimeFormatSpec _inputFormatSpec;
private DateTimeFormatSpec _outputFormatSpec;
private DateTimeGranularitySpec _granularitySpec;

@ScalarFunction
public String dateTimeConvert(String timeValueStr, String inputFormatStr, String outputFormatStr,
String outputGranularityStr) {
if (_inputFormatSpec == null) {
_inputFormatSpec = new DateTimeFormatSpec(inputFormatStr);
_outputFormatSpec = new DateTimeFormatSpec(outputFormatStr);
_granularitySpec = new DateTimeGranularitySpec(outputGranularityStr);
}

long timeValueMs = _inputFormatSpec.fromFormatToMillis(timeValueStr);
if (_outputFormatSpec.getTimeFormat() == DateTimeFieldSpec.TimeFormat.SIMPLE_DATE_FORMAT) {
DateTimeFormatter outputFormatter = _outputFormatSpec.getDateTimeFormatter();
DateTime dateTime = new DateTime(timeValueMs, outputFormatter.getZone());
int size = _granularitySpec.getSize();
switch (_granularitySpec.getTimeUnit()) {
case MILLISECONDS:
dateTime = dateTime.withMillisOfSecond((dateTime.getMillisOfSecond() / size) * size);
break;
case SECONDS:
dateTime = dateTime.withSecondOfMinute((dateTime.getSecondOfMinute() / size) * size).secondOfMinute()
.roundFloorCopy();
break;
case MINUTES:
dateTime =
dateTime.withMinuteOfHour((dateTime.getMinuteOfHour() / size) * size).minuteOfHour().roundFloorCopy();
break;
case HOURS:
dateTime = dateTime.withHourOfDay((dateTime.getHourOfDay() / size) * size).hourOfDay().roundFloorCopy();
break;
case DAYS:
dateTime =
dateTime.withDayOfMonth(((dateTime.getDayOfMonth() - 1) / size) * size + 1).dayOfMonth().roundFloorCopy();
break;
default:
break;
}
return outputFormatter.print(dateTime);
} else {
long granularityMs = _granularitySpec.granularityToMillis();
long roundedTimeValueMs = timeValueMs / granularityMs * granularityMs;
return _outputFormatSpec.fromMillisToFormat(roundedTimeValueMs);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,9 @@
import org.apache.pinot.common.function.DateTimeUtils;
import org.apache.pinot.common.function.TimeZoneKey;
import org.apache.pinot.spi.annotations.ScalarFunction;
import org.apache.pinot.spi.data.DateTimeFieldSpec;
import org.apache.pinot.spi.data.DateTimeFormatSpec;
import org.apache.pinot.spi.data.DateTimeGranularitySpec;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.chrono.ISOChronology;
import org.joda.time.format.DateTimeFormatter;

/**
* Inbuilt date time related transform functions
Expand Down Expand Up @@ -680,50 +676,6 @@ private static long dateTrunc(String unit, long timeValue, TimeUnit inputTimeUni
.roundFloor(TimeUnit.MILLISECONDS.convert(timeValue, inputTimeUnit)), TimeUnit.MILLISECONDS);
}

/**
* Equivalent to {@code DateTimeConversionTransformFunction}. Both input and output are string type to support simple
* date format.
*/
@ScalarFunction
public static String dateTimeConvert(String timeValueStr, String inputFormatStr, String outputFormatStr,
String outputGranularityStr) {
long timeValueMs = new DateTimeFormatSpec(inputFormatStr).fromFormatToMillis(timeValueStr);
DateTimeFormatSpec outputFormat = new DateTimeFormatSpec(outputFormatStr);
DateTimeGranularitySpec granularitySpec = new DateTimeGranularitySpec(outputGranularityStr);
if (outputFormat.getTimeFormat() == DateTimeFieldSpec.TimeFormat.SIMPLE_DATE_FORMAT) {
DateTimeFormatter outputFormatter = outputFormat.getDateTimeFormatter();
DateTime dateTime = new DateTime(timeValueMs, outputFormatter.getZone());
int size = granularitySpec.getSize();
switch (granularitySpec.getTimeUnit()) {
case MILLISECONDS:
dateTime = dateTime.withMillisOfSecond((dateTime.getMillisOfSecond() / size) * size);
break;
case SECONDS:
dateTime = dateTime.withSecondOfMinute((dateTime.getSecondOfMinute() / size) * size).secondOfMinute()
.roundFloorCopy();
break;
case MINUTES:
dateTime =
dateTime.withMinuteOfHour((dateTime.getMinuteOfHour() / size) * size).minuteOfHour().roundFloorCopy();
break;
case HOURS:
dateTime = dateTime.withHourOfDay((dateTime.getHourOfDay() / size) * size).hourOfDay().roundFloorCopy();
break;
case DAYS:
dateTime = dateTime.withDayOfMonth(((dateTime.getDayOfMonth() - 1) / size) * size + 1).dayOfMonth()
.roundFloorCopy();
break;
default:
break;
}
return outputFormatter.print(dateTime);
} else {
long granularityMs = granularitySpec.granularityToMillis();
long roundedTimeValueMs = timeValueMs / granularityMs * granularityMs;
return outputFormat.fromMillisToFormat(roundedTimeValueMs);
}
}

/**
* Add a time period to the provided timestamp.
* e.g. timestampAdd('days', 10, NOW()) will add 10 days to the current timestamp and return the value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import static org.testng.Assert.assertEquals;


/**
* Tests the Pinot inbuilt transform functions
Expand All @@ -47,8 +48,8 @@ public class DateTimeFunctionsTest {
private void testFunction(String functionExpression, List<String> expectedArguments, GenericRow row,
Object expectedResult) {
InbuiltFunctionEvaluator evaluator = new InbuiltFunctionEvaluator(functionExpression);
Assert.assertEquals(evaluator.getArguments(), expectedArguments);
Assert.assertEquals(evaluator.evaluate(row), expectedResult);
assertEquals(evaluator.getArguments(), expectedArguments);
assertEquals(evaluator.evaluate(row), expectedResult);
}

@Test(dataProvider = "dateTimeFunctionsDataProvider")
Expand Down Expand Up @@ -567,4 +568,31 @@ private void testDateTimeConvert(Object timeValue, String inputFormatStr, String
testFunction(String.format("dateTimeConvert(timeCol, '%s', '%s', '%s')", inputFormatStr, outputFormatStr,
outputGranularityStr), arguments, row, expectedResult == null ? null : expectedResult.toString());
}

private void testMultipleInvocations(String functionExpression, List<GenericRow> rows, List<Object> expectedResults) {
InbuiltFunctionEvaluator evaluator = new InbuiltFunctionEvaluator(functionExpression);
int numInvocations = rows.size();
assertEquals(expectedResults.size(), numInvocations);
for (int i = 0; i < numInvocations; i++) {
assertEquals(evaluator.evaluate(rows.get(i)), expectedResults.get(i).toString());
}
}

@Test
public void testDateTimeConvertMultipleInvocations() {
String inputFormatStr = "SIMPLE_DATE_FORMAT|yyyyMMdd";
String outputFormatStr = "EPOCH|MILLISECONDS";
String outputGranularityStr = "1:DAYS";

List<GenericRow> rows = new ArrayList<>(10);
List<Object> expectedResults = new ArrayList<>(10);
for (int i = 0; i < 10; i++) {
GenericRow row = new GenericRow();
row.putValue("timeCol", 20200101 + i);
rows.add(row);
expectedResults.add(1577836800000L + 24 * 3600000 * i);
}
testMultipleInvocations(String.format("dateTimeConvert(timeCol, '%s', '%s', '%s')", inputFormatStr, outputFormatStr,
outputGranularityStr), rows, expectedResults);
}
}