Skip to content

Commit

Permalink
first version
Browse files Browse the repository at this point in the history
  • Loading branch information
Zouxxyy committed Mar 15, 2024
1 parent bc2e1af commit 5fdb0cd
Show file tree
Hide file tree
Showing 16 changed files with 146 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@

import static org.apache.paimon.predicate.CompareUtils.compareLiteral;

/** A {@link NullFalseLeafBinaryFunction} to eval equal. */
public class Equal extends NullFalseLeafBinaryFunction {
/** A {@link NullFalseLeafOneLiteralFunction} to eval equal. */
public class Equal extends NullFalseLeafOneLiteralFunction {

public static final Equal INSTANCE = new Equal();

private Equal() {}

@Override
public boolean test(DataType type, Object field, Object literal) {
public boolean test0(DataType type, Object field, Object literal) {
return compareLiteral(type, literal, field) == 0;
}

@Override
public boolean test(
public boolean test0(
DataType type, long rowCount, Object min, Object max, Long nullCount, Object literal) {
return compareLiteral(type, literal, min) >= 0 && compareLiteral(type, literal, max) <= 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@

import static org.apache.paimon.predicate.CompareUtils.compareLiteral;

/** A {@link NullFalseLeafBinaryFunction} to eval greater or equal. */
public class GreaterOrEqual extends NullFalseLeafBinaryFunction {
/** A {@link NullFalseLeafOneLiteralFunction} to eval greater or equal. */
public class GreaterOrEqual extends NullFalseLeafOneLiteralFunction {

public static final GreaterOrEqual INSTANCE = new GreaterOrEqual();

private GreaterOrEqual() {}

@Override
public boolean test(DataType type, Object field, Object literal) {
public boolean test0(DataType type, Object field, Object literal) {
return compareLiteral(type, literal, field) <= 0;
}

@Override
public boolean test(
public boolean test0(
DataType type, long rowCount, Object min, Object max, Long nullCount, Object literal) {
return compareLiteral(type, literal, max) <= 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@
import static org.apache.paimon.predicate.CompareUtils.compareLiteral;

/** A {@link LeafFunction} to eval greater. */
public class GreaterThan extends NullFalseLeafBinaryFunction {
public class GreaterThan extends NullFalseLeafOneLiteralFunction {

public static final GreaterThan INSTANCE = new GreaterThan();

private GreaterThan() {}

@Override
public boolean test(DataType type, Object field, Object literal) {
public boolean test0(DataType type, Object field, Object literal) {
return compareLiteral(type, literal, field) < 0;
}

@Override
public boolean test(
public boolean test0(
DataType type, long rowCount, Object min, Object max, Long nullCount, Object literal) {
return compareLiteral(type, literal, max) < 0;
}
Expand Down
12 changes: 3 additions & 9 deletions paimon-common/src/main/java/org/apache/paimon/predicate/In.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import static org.apache.paimon.predicate.CompareUtils.compareLiteral;

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

private static final long serialVersionUID = 1L;

Expand All @@ -35,10 +35,7 @@ public class In extends LeafFunction {
private In() {}

@Override
public boolean test(DataType type, Object field, List<Object> literals) {
if (field == null) {
return false;
}
public boolean test0(DataType type, Object field, List<Object> literals) {
for (Object literal : literals) {
if (literal != null && compareLiteral(type, literal, field) == 0) {
return true;
Expand All @@ -48,16 +45,13 @@ public boolean test(DataType type, Object field, List<Object> literals) {
}

@Override
public boolean test(
public boolean test0(
DataType type,
long rowCount,
Object min,
Object max,
Long nullCount,
List<Object> literals) {
if (nullCount != null && rowCount == nullCount) {
return false;
}
for (Object literal : literals) {
if (literal != null
&& compareLiteral(type, literal, min) >= 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import java.util.List;
import java.util.Optional;

/** A {@link NullFalseLeafBinaryFunction} to eval is not null. */
/** A {@link LeafUnaryFunction} to eval is not null. */
public class IsNotNull extends LeafUnaryFunction {

public static final IsNotNull INSTANCE = new IsNotNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import java.util.List;
import java.util.Optional;

/** A {@link NullFalseLeafBinaryFunction} to eval is null. */
/** A {@link LeafUnaryFunction} to eval is null. */
public class IsNull extends LeafUnaryFunction {

public static final IsNull INSTANCE = new IsNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.Optional;

import static org.apache.paimon.utils.InternalRowUtils.get;
import static org.apache.paimon.utils.Preconditions.checkArgument;

/** Leaf node of a {@link Predicate} tree. Compares a field in the row with literals. */
public class LeafPredicate implements Predicate {
Expand Down Expand Up @@ -100,14 +101,16 @@ public boolean test(
Object min = get(minValues, fieldIndex, type);
Object max = get(maxValues, fieldIndex, type);
Long nullCount = nullCounts.isNullAt(fieldIndex) ? null : nullCounts.getLong(fieldIndex);
if (nullCount == null || rowCount != nullCount) {
// not all null
// min or max is null
// unknown stats
if (min == null || max == null) {
return true;
}

checkArgument(rowCount >= 0, "rowCount must >= 0");
checkArgument(
nullCount == null || (nullCount >= 0 && nullCount <= rowCount),
"nullCount must >= 0 and <= rowCount");

if (rowCount == 0) {
return false;
}

return function.test(type, rowCount, min, max, nullCount, literals);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@

import static org.apache.paimon.predicate.CompareUtils.compareLiteral;

/** A {@link NullFalseLeafBinaryFunction} to eval less or equal. */
public class LessOrEqual extends NullFalseLeafBinaryFunction {
/** A {@link NullFalseLeafOneLiteralFunction} to eval less or equal. */
public class LessOrEqual extends NullFalseLeafOneLiteralFunction {

public static final LessOrEqual INSTANCE = new LessOrEqual();

private LessOrEqual() {}

@Override
public boolean test(DataType type, Object field, Object literal) {
public boolean test0(DataType type, Object field, Object literal) {
return compareLiteral(type, literal, field) >= 0;
}

@Override
public boolean test(
public boolean test0(
DataType type, long rowCount, Object min, Object max, Long nullCount, Object literal) {
return compareLiteral(type, literal, min) >= 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@

import static org.apache.paimon.predicate.CompareUtils.compareLiteral;

/** A {@link NullFalseLeafBinaryFunction} to eval less or equal. */
public class LessThan extends NullFalseLeafBinaryFunction {
/** A {@link NullFalseLeafOneLiteralFunction} to eval less or equal. */
public class LessThan extends NullFalseLeafOneLiteralFunction {

public static final LessThan INSTANCE = new LessThan();

private LessThan() {}

@Override
public boolean test(DataType type, Object field, Object literal) {
public boolean test0(DataType type, Object field, Object literal) {
return compareLiteral(type, literal, field) > 0;
}

@Override
public boolean test(
public boolean test0(
DataType type, long rowCount, Object min, Object max, Long nullCount, Object literal) {
return compareLiteral(type, literal, min) > 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@

import static org.apache.paimon.predicate.CompareUtils.compareLiteral;

/** A {@link NullFalseLeafBinaryFunction} to eval not equal. */
public class NotEqual extends NullFalseLeafBinaryFunction {
/** A {@link NullFalseLeafOneLiteralFunction} to eval not equal. */
public class NotEqual extends NullFalseLeafOneLiteralFunction {

public static final NotEqual INSTANCE = new NotEqual();

private NotEqual() {}

@Override
public boolean test(DataType type, Object field, Object literal) {
public boolean test0(DataType type, Object field, Object literal) {
return compareLiteral(type, literal, field) != 0;
}

@Override
public boolean test(
public boolean test0(
DataType type, long rowCount, Object min, Object max, Long nullCount, Object literal) {
return compareLiteral(type, literal, min) != 0 || compareLiteral(type, literal, max) != 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import static org.apache.paimon.predicate.CompareUtils.compareLiteral;

/** A {@link LeafFunction} to eval not in. */
public class NotIn extends LeafFunction {
public class NotIn extends NullFalseLeafFunction {

private static final long serialVersionUID = 1L;

Expand All @@ -35,10 +35,7 @@ public class NotIn extends LeafFunction {
private NotIn() {}

@Override
public boolean test(DataType type, Object field, List<Object> literals) {
if (field == null) {
return false;
}
public boolean test0(DataType type, Object field, List<Object> literals) {
for (Object literal : literals) {
if (literal == null || compareLiteral(type, literal, field) == 0) {
return false;
Expand All @@ -48,16 +45,13 @@ public boolean test(DataType type, Object field, List<Object> literals) {
}

@Override
public boolean test(
public boolean test0(
DataType type,
long rowCount,
Object min,
Object max,
Long nullCount,
List<Object> literals) {
if (nullCount != null && rowCount == nullCount) {
return false;
}
for (Object literal : literals) {
if (literal == null
|| (compareLiteral(type, literal, min) == 0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* 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.paimon.predicate;

import org.apache.paimon.types.DataType;

import java.util.List;

/** Field and literals in this function can't be null. */
public abstract class NullFalseLeafFunction extends LeafFunction {
private static final long serialVersionUID = 1L;

public abstract boolean test0(DataType type, Object field, List<Object> literals);

public abstract boolean test0(
DataType type,
long rowCount,
Object min,
Object max,
Long nullCount,
List<Object> literals);

@Override
public boolean test(DataType type, Object field, List<Object> literals) {
if (field == null) {
return false;
}

// Only when the count of literals is one we can return quickly checking for null
if (literals.size() == 1 && literals.get(0) == null) {
return false;
}

return test0(type, field, literals);
}

@Override
public boolean test(
DataType type,
long rowCount,
Object min,
Object max,
Long nullCount,
List<Object> literals) {
// All null
if (nullCount != null && rowCount == nullCount) {
return false;
}

// Only when the count of literals is one we can quickly check for null
if (literals.size() == 1 && literals.get(0) == null) {
return false;
}

// todo: support one of min and max is not null
if (min == null || max == null) {
return true;
}

return test0(type, rowCount, min, max, nullCount, literals);
}
}
Loading

0 comments on commit 5fdb0cd

Please sign in to comment.