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

org.apache.iceberg.expressions.Evaluator 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.apache.iceberg.expressions;

import java.io.Serializable;
import java.util.Comparator;
import java.util.Set;
import org.apache.iceberg.StructLike;
import org.apache.iceberg.expressions.ExpressionVisitors.BoundVisitor;
import org.apache.iceberg.types.Types.StructType;
import org.apache.iceberg.util.NaNUtil;

/**
 * Evaluates an {@link Expression} for data described by a {@link StructType}.
 * 

* Data rows must implement {@link StructLike} and are passed to {@link #eval(StructLike)}. *

* This class is thread-safe. */ public class Evaluator implements Serializable { private final Expression expr; public Evaluator(StructType struct, Expression unbound) { this.expr = Binder.bind(struct, unbound, true); } public Evaluator(StructType struct, Expression unbound, boolean caseSensitive) { this.expr = Binder.bind(struct, unbound, caseSensitive); } public boolean eval(StructLike data) { return new EvalVisitor().eval(data); } private class EvalVisitor extends BoundVisitor { private StructLike struct; private boolean eval(StructLike row) { this.struct = row; return ExpressionVisitors.visitEvaluator(expr, this); } @Override public Boolean alwaysTrue() { return true; } @Override public Boolean alwaysFalse() { return false; } @Override public Boolean not(Boolean result) { return !result; } @Override public Boolean and(Boolean leftResult, Boolean rightResult) { return leftResult && rightResult; } @Override public Boolean or(Boolean leftResult, Boolean rightResult) { return leftResult || rightResult; } @Override public Boolean isNull(Bound valueExpr) { return valueExpr.eval(struct) == null; } @Override public Boolean notNull(Bound valueExpr) { return valueExpr.eval(struct) != null; } @Override public Boolean isNaN(Bound valueExpr) { return NaNUtil.isNaN(valueExpr.eval(struct)); } @Override public Boolean notNaN(Bound valueExpr) { return !NaNUtil.isNaN(valueExpr.eval(struct)); } @Override public Boolean lt(Bound valueExpr, Literal lit) { Comparator cmp = lit.comparator(); return cmp.compare(valueExpr.eval(struct), lit.value()) < 0; } @Override public Boolean ltEq(Bound valueExpr, Literal lit) { Comparator cmp = lit.comparator(); return cmp.compare(valueExpr.eval(struct), lit.value()) <= 0; } @Override public Boolean gt(Bound valueExpr, Literal lit) { Comparator cmp = lit.comparator(); return cmp.compare(valueExpr.eval(struct), lit.value()) > 0; } @Override public Boolean gtEq(Bound valueExpr, Literal lit) { Comparator cmp = lit.comparator(); return cmp.compare(valueExpr.eval(struct), lit.value()) >= 0; } @Override public Boolean eq(Bound valueExpr, Literal lit) { Comparator cmp = lit.comparator(); return cmp.compare(valueExpr.eval(struct), lit.value()) == 0; } @Override public Boolean notEq(Bound valueExpr, Literal lit) { return !eq(valueExpr, lit); } @Override public Boolean in(Bound valueExpr, Set literalSet) { return literalSet.contains(valueExpr.eval(struct)); } @Override public Boolean notIn(Bound valueExpr, Set literalSet) { return !in(valueExpr, literalSet); } @Override public Boolean startsWith(Bound valueExpr, Literal lit) { T evalRes = valueExpr.eval(struct); return evalRes != null && ((String) evalRes).startsWith((String) lit.value()); } @Override public Boolean notStartsWith(Bound valueExpr, Literal lit) { return !startsWith(valueExpr, lit); } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy