Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[spark] Support push down StringContains #3918

Merged
merged 1 commit into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public FileIndexResult visitEndsWith(FieldRef fieldRef, Object literal) {
return REMAIN;
}

@Override
public FileIndexResult visitContains(FieldRef fieldRef, Object literal) {
return REMAIN;
}

@Override
public FileIndexResult visitLessThan(FieldRef fieldRef, Object literal) {
return REMAIN;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public FileIndexResult visitEndsWith(FieldRef fieldRef, Object literal) {
return SKIP;
}

@Override
public FileIndexResult visitContains(FieldRef fieldRef, Object literal) {
return SKIP;
}

@Override
public FileIndexResult visitLessThan(FieldRef fieldRef, Object literal) {
return SKIP;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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.data.BinaryString;
import org.apache.paimon.types.DataType;

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

/** A {@link NullFalseLeafBinaryFunction} to evaluate {@code filter like '%abc%'}. */
public class Contains extends NullFalseLeafBinaryFunction {

public static final Contains INSTANCE = new Contains();

private Contains() {}

@Override
public boolean test(DataType type, Object field, Object patternLiteral) {
BinaryString fieldString = (BinaryString) field;
return fieldString.contains((BinaryString) patternLiteral);
}

@Override
public boolean test(
DataType type,
long rowCount,
Object min,
Object max,
Long nullCount,
Object patternLiteral) {
return true;
}

@Override
public Optional<LeafFunction> negate() {
return Optional.empty();
}

@Override
public <T> T visit(FunctionVisitor<T> visitor, FieldRef fieldRef, List<Object> literals) {
return visitor.visitContains(fieldRef, literals.get(0));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ default T visit(CompoundPredicate predicate) {

T visitEndsWith(FieldRef fieldRef, Object literal);

T visitContains(FieldRef fieldRef, Object literal);

T visitLessThan(FieldRef fieldRef, Object literal);

T visitGreaterOrEqual(FieldRef fieldRef, Object literal);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ public Boolean visitEndsWith(FieldRef fieldRef, Object literal) {
return false;
}

@Override
public Boolean visitContains(FieldRef fieldRef, Object literal) {
return false;
}

@Override
public Boolean visitLessThan(FieldRef fieldRef, Object literal) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ public Predicate endsWith(int idx, Object patternLiteral) {
return leaf(EndsWith.INSTANCE, idx, patternLiteral);
}

public Predicate contains(int idx, Object patternLiteral) {
return leaf(Contains.INSTANCE, idx, patternLiteral);
}

public Predicate leaf(NullFalseLeafBinaryFunction function, int idx, Object literal) {
DataField field = rowType.getFields().get(idx);
return new LeafPredicate(function, field.type(), idx, field.name(), singletonList(literal));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public Optional<OrcFilters.Predicate> visitEndsWith(FieldRef fieldRef, Object li
return Optional.empty();
}

@Override
public Optional<OrcFilters.Predicate> visitContains(FieldRef fieldRef, Object literal) {
return Optional.empty();
}

@Override
public Optional<OrcFilters.Predicate> visitLessThan(FieldRef fieldRef, Object literal) {
return convertBinary(fieldRef, literal, OrcFilters.LessThan::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ public FilterPredicate visitEndsWith(FieldRef fieldRef, Object literal) {
throw new UnsupportedOperationException();
}

@Override
public FilterPredicate visitContains(FieldRef fieldRef, Object literal) {
throw new UnsupportedOperationException();
}

@Override
public FilterPredicate visitIn(FieldRef fieldRef, List<Object> literals) {
throw new UnsupportedOperationException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.apache.spark.sql.sources.LessThanOrEqual;
import org.apache.spark.sql.sources.Not;
import org.apache.spark.sql.sources.Or;
import org.apache.spark.sql.sources.StringContains;
import org.apache.spark.sql.sources.StringEndsWith;
import org.apache.spark.sql.sources.StringStartsWith;

Expand Down Expand Up @@ -63,7 +64,8 @@ public class SparkFilterConverter {
"Or",
"Not",
"StringStartsWith",
"StringEndsWith");
"StringEndsWith",
"StringContains");

private final RowType rowType;
private final PredicateBuilder builder;
Expand Down Expand Up @@ -148,6 +150,11 @@ public Predicate convert(Filter filter) {
int index = fieldIndex(endsWith.attribute());
Object literal = convertLiteral(index, endsWith.value());
return builder.endsWith(index, literal);
} else if (filter instanceof StringContains) {
StringContains contains = (StringContains) filter;
int index = fieldIndex(contains.attribute());
Object literal = convertLiteral(index, contains.value());
return builder.contains(index, literal);
}

// TODO: AlwaysTrue, AlwaysFalse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.apache.spark.sql.sources.LessThan;
import org.apache.spark.sql.sources.LessThanOrEqual;
import org.apache.spark.sql.sources.Not;
import org.apache.spark.sql.sources.StringContains;
import org.apache.spark.sql.sources.StringEndsWith;
import org.apache.spark.sql.sources.StringStartsWith;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -167,6 +168,13 @@ public void testAll() {
boolean test1 = endsWithPre.test(10, min, max, new GenericArray(nullCount));
assertThat(test).isEqualTo(true);
assertThat(test1).isEqualTo(true);

// StringContains
StringContains stringContains = StringContains.apply("id", "aa");
Predicate contains = converter01.convert(stringContains);
assertThat(contains.test(row)).isEqualTo(true);
assertThat(contains.test(max)).isEqualTo(false);
assertThat(contains.test(min)).isEqualTo(true);
}

@Test
Expand Down
Loading