/** * Copyright (C) 2014-2016 LinkedIn Corp. (pinot-core@linkedin.com) * * Licensed 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 com.linkedin.pinot.core.operator.transform.function; import com.google.common.base.Preconditions; import com.linkedin.pinot.common.data.FieldSpec; import com.linkedin.pinot.core.common.BlockValSet; import com.linkedin.pinot.core.plan.DocIdSetPlanNode; import java.util.concurrent.TimeUnit; import javax.annotation.concurrent.NotThreadSafe; /** * This class implements the time conversion transform. * <ul> * <li> Inputs: </li> * <ul> * <li> Input time values </li> * <li> Input time unit as defined in {@link TimeUnit} </li> * <li> Desired output time unit as defined in {@link TimeUnit} </li> * </ul> * </ul> * * <ul> * <li> Outputs: </li> * <ul> * <li> Time values converted into desired time unit. </li> * </ul> * </ul> */ @NotThreadSafe public class TimeConversionTransform implements TransformFunction { private static final String TRANSFORM_NAME = "timeConvert"; private long[] _output = null; @Override public <T> T transform(int length, BlockValSet... input) { Preconditions.checkArgument(input.length == 3, TRANSFORM_NAME + " expects three arguments"); long[] inputTime = input[0].getLongValuesSV(); String[] inputTimeUnits = input[1].getStringValuesSV(); String[] outputTimeUnits = input[2].getStringValuesSV(); Preconditions.checkState(inputTime.length >= length && inputTimeUnits.length >= length, outputTimeUnits.length >= length); TimeUnit inputTimeUnit = TimeUnit.valueOf(inputTimeUnits[0]); TimeUnit outputTimeUnit = TimeUnit.valueOf(outputTimeUnits[0]); if (_output == null || _output.length < length) { _output = new long[Math.max(length, DocIdSetPlanNode.MAX_DOC_PER_CALL)]; } for (int i = 0; i < length; i++) { _output[i] = outputTimeUnit.convert(inputTime[i], inputTimeUnit); } return (T) _output; } @Override public FieldSpec.DataType getOutputType() { return FieldSpec.DataType.LONG; } @Override public String getName() { return TRANSFORM_NAME; } }