blob: ad39d147615979de9b952e60b9a7d62ee6198b1a [file] [log] [blame]
/**
* 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.tajo.engine.function.datetime;
import org.apache.tajo.catalog.Column;
import org.apache.tajo.common.TajoDataTypes;
import org.apache.tajo.datum.DateDatum;
import org.apache.tajo.datum.Datum;
import org.apache.tajo.datum.NullDatum;
import org.apache.tajo.plan.function.GeneralFunction;
import org.apache.tajo.engine.function.annotation.Description;
import org.apache.tajo.engine.function.annotation.ParamTypes;
import org.apache.tajo.storage.Tuple;
import org.apache.tajo.util.datetime.DateTimeFormat;
import org.apache.tajo.util.datetime.TimeMeta;
@Description(
functionName = "to_date",
description = "Convert string to date. Format should be a SQL standard format string.",
example = "> SELECT to_date('2014-01-01', 'YYYY-MM-DD');\n"
+ "2014-01-01",
returnType = TajoDataTypes.Type.DATE,
paramTypes = {@ParamTypes(paramTypes = {TajoDataTypes.Type.TEXT, TajoDataTypes.Type.TEXT})}
)
public class ToDate extends GeneralFunction {
public ToDate() {
super(new Column[]{
new Column("string", TajoDataTypes.Type.TEXT),
new Column("format", TajoDataTypes.Type.TEXT)});
}
@Override
public Datum eval(Tuple params) {
if(params.isBlankOrNull(0) || params.isBlankOrNull(1)) {
return NullDatum.get();
}
String value = params.getText(0);
String pattern = params.getText(1);
TimeMeta tm = DateTimeFormat.parseDateTime(value, pattern);
return new DateDatum(tm);
}
}