All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.dinky.shaded.paimon.predicate.In Maven / Gradle / Ivy

The newest version!
/*
 * 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.dinky.shaded.paimon.predicate;

import org.dinky.shaded.paimon.format.FieldStats;
import org.dinky.shaded.paimon.types.DataType;

import java.util.List;
import java.util.Optional;

import static org.dinky.shaded.paimon.predicate.CompareUtils.compareLiteral;

/** A {@link LeafFunction} to eval in. */
public class In extends LeafFunction {

    private static final long serialVersionUID = 1L;

    public static final In INSTANCE = new In();

    private In() {}

    @Override
    public boolean test(DataType type, Object field, List literals) {
        if (field == null) {
            return false;
        }
        for (Object literal : literals) {
            if (literal != null && compareLiteral(type, literal, field) == 0) {
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean test(
            DataType type, long rowCount, FieldStats fieldStats, List literals) {
        Long nullCount = fieldStats.nullCount();
        if (nullCount != null && rowCount == nullCount) {
            return false;
        }
        for (Object literal : literals) {
            if (literal != null
                    && compareLiteral(type, literal, fieldStats.minValue()) >= 0
                    && compareLiteral(type, literal, fieldStats.maxValue()) <= 0) {
                return true;
            }
        }
        return false;
    }

    @Override
    public Optional negate() {
        return Optional.of(NotIn.INSTANCE);
    }

    @Override
    public  T visit(FunctionVisitor visitor, FieldRef fieldRef, List literals) {
        return visitor.visitIn(fieldRef, literals);
    }
}