hazelcast-master/hazelcast-sql/src/main/java/com/hazelcast/sql/impl/expression/math/FloorCeilFunction.java
/*
 * Copyright 2023 Hazelcast Inc.
 *
 * Licensed under the Hazelcast Community License (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://hazelcast.com/hazelcast-community-license
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hazelcast.sql.impl.expression.math;

import com.hazelcast.jet.sql.impl.JetSqlSerializerHook;
import com.hazelcast.nio.ObjectDataInput;
import com.hazelcast.nio.ObjectDataOutput;
import com.hazelcast.sql.impl.QueryException;
import com.hazelcast.sql.impl.expression.Expression;
import com.hazelcast.sql.impl.expression.ExpressionEvalContext;
import com.hazelcast.sql.impl.expression.UniExpressionWithType;
import com.hazelcast.sql.impl.row.Row;
import com.hazelcast.sql.impl.type.QueryDataType;
import com.hazelcast.sql.impl.type.converter.Converter;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * Implementation of FLOOR/CEIL functions.
 */
public class FloorCeilFunction<T> extends UniExpressionWithType<T> {

    private boolean ceil;

    public FloorCeilFunction() {
        // No-op.
    }

    private FloorCeilFunction(Expression<?> operand, QueryDataType resultType, boolean ceil) {
        super(operand, resultType);

        this.ceil = ceil;
    }

    public static Expression<?> create(Expression<?> operand, QueryDataType resultType, boolean ceil) {
        QueryDataType operandType = operand.getType();

        // Non-fractional operand will not be changed.
        if (MathFunctionUtils.notFractional(operandType) && operandType == resultType) {
            return operand;
        }

        return new FloorCeilFunction<>(operand, resultType, ceil);
    }

    @SuppressWarnings("unchecked")
    @Override
    public T eval(Row row, ExpressionEvalContext context) {
        Object value = operand.eval(row, context);

        if (value == null) {
            return null;
        }

        return (T) floorCeil(value, operand.getType(), resultType, ceil);
    }

    @SuppressWarnings("checkstyle:AvoidNestedBlocks")
    private static Object floorCeil(Object operandValue, QueryDataType operandType, QueryDataType resultType, boolean ceil) {
        Converter operandConverter = operandType.getConverter();

        switch (resultType.getTypeFamily()) {
            case DECIMAL: {
                BigDecimal operand0 = operandConverter.asDecimal(operandValue);

                RoundingMode roundingMode = ceil ? RoundingMode.CEILING : RoundingMode.FLOOR;

                return operand0.setScale(0, roundingMode);
            }

            case REAL: {
                float operand0 = operandConverter.asReal(operandValue);

                return (float) (ceil ? Math.ceil(operand0) : Math.floor(operand0));
            }

            case DOUBLE: {
                double operand0 = operandConverter.asDouble(operandValue);

                return ceil ? Math.ceil(operand0) : Math.floor(operand0);
            }

            default:
                throw QueryException.error("Unexpected type: " + resultType);
        }
    }

    @Override
    public int getClassId() {
        return JetSqlSerializerHook.EXPRESSION_FLOOR_CEIL;
    }

    @Override
    public void writeData(ObjectDataOutput out) throws IOException {
        super.writeData(out);

        out.writeBoolean(ceil);
    }

    @Override
    public void readData(ObjectDataInput in) throws IOException {
        super.readData(in);

        ceil = in.readBoolean();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }

        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        if (!super.equals(o)) {
            return false;
        }

        FloorCeilFunction<?> that = (FloorCeilFunction<?>) o;

        return ceil == that.ceil;
    }

    @Override
    public int hashCode() {
        int result = super.hashCode();

        result = 31 * result + (ceil ? 1 : 0);

        return result;
    }

    @Override
    public String toString() {
        return getClass().getSimpleName() + "{operand=" + operand + ", ceil=" + ceil + '}';
    }
}
