DateCeilFloorMonotonic.java
// 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.doris.nereids.trees.expressions.functions;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.literal.IntegerLikeLiteral;
import org.apache.doris.nereids.trees.expressions.literal.Literal;
/** Monotonicity and rounding relation of date/time ceil and floor functions. */
public interface DateCeilFloorMonotonic extends RoundingMonotonic {
@Override
default boolean isMonotonic(Literal lower, Literal upper) {
switch (arity()) {
case 1:
return true;
case 2:
return !(child(0) instanceof Literal) && child(1) instanceof Literal;
case 3:
return !(child(0) instanceof Literal) && child(1) instanceof Literal && child(2) instanceof Literal;
default:
return false;
}
}
@Override
default boolean isPositive() {
return true;
}
@Override
default int getMonotonicFunctionChildIndex() {
return 0;
}
@Override
default boolean isRoundingRelationGuaranteed() {
if (arity() == 1) {
return true;
}
if (arity() == 2 && getArgument(1).getDataType().isDateLikeType()) {
return true;
}
return (arity() == 2 || arity() == 3) && isPositiveIntegerLiteral(getArgument(1));
}
private boolean isPositiveIntegerLiteral(Expression expression) {
return expression instanceof IntegerLikeLiteral
&& ((IntegerLikeLiteral) expression).getBigDecimalValue().signum() > 0;
}
}