blob: 6e68bcdcd4118ce96d2e6b10d5e1501adc901089 [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.calcite.sql.fun;
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.sql.SqlFunction;
import org.apache.calcite.sql.SqlFunctionCategory;
import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.SqlOperatorBinding;
import org.apache.calcite.sql.SqlSyntax;
import org.apache.calcite.sql.type.OperandTypes;
import org.apache.calcite.sql.type.SqlOperandTypeChecker;
import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.calcite.sql.type.SqlTypeUtil;
import org.apache.calcite.sql.validate.SqlMonotonicity;
import static org.apache.calcite.sql.validate.SqlNonNullableAccessors.getOperandLiteralValueOrThrow;
import static org.apache.calcite.util.Static.RESOURCE;
/**
* Base class for time functions such as "LOCALTIME", "LOCALTIME(n)".
*/
public class SqlAbstractTimeFunction extends SqlFunction {
//~ Static fields/initializers ---------------------------------------------
private static final SqlOperandTypeChecker OTC_CUSTOM =
OperandTypes.POSITIVE_INTEGER_LITERAL.or(OperandTypes.NILADIC);
//~ Instance fields --------------------------------------------------------
private final SqlTypeName typeName;
//~ Constructors -----------------------------------------------------------
protected SqlAbstractTimeFunction(String name, SqlTypeName typeName) {
super(name, SqlKind.OTHER_FUNCTION, null, null, OTC_CUSTOM,
SqlFunctionCategory.TIMEDATE);
this.typeName = typeName;
}
//~ Methods ----------------------------------------------------------------
@Override public SqlSyntax getSyntax() {
return SqlSyntax.FUNCTION_ID;
}
@Override public RelDataType inferReturnType(
SqlOperatorBinding opBinding) {
// REVIEW jvs 20-Feb-2005: Need to take care of time zones.
int precision = 0;
if (opBinding.getOperandCount() == 1) {
RelDataType type = opBinding.getOperandType(0);
if (SqlTypeUtil.isNumeric(type)) {
precision = getOperandLiteralValueOrThrow(opBinding, 0, Integer.class);
}
}
assert precision >= 0;
final int maxPrecision = opBinding.getTypeFactory().getTypeSystem().getMaxPrecision(typeName);
if (precision > maxPrecision) {
throw opBinding.newError(
RESOURCE.argumentMustBeValidPrecision(
opBinding.getOperator().getName(), 0,
maxPrecision));
}
return opBinding.getTypeFactory().createSqlType(typeName, precision);
}
// All of the time functions are increasing. Not strictly increasing.
@Override public SqlMonotonicity getMonotonicity(SqlOperatorBinding call) {
return SqlMonotonicity.INCREASING;
}
// Plans referencing context variables should never be cached
@Override public boolean isDynamicFunction() {
return true;
}
}