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

[core] Introduce InPredicateVisitor to refactor Predicate handle #4486

Merged
merged 7 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
@@ -0,0 +1,53 @@
/*
* 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 java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/** A utils to handle {@link Predicate}. */
public class InPredicateVisitor {

/**
* Method for handling with CompoundPredicate.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method for handling with In CompoundPredicate

*
* @param predicate CompoundPredicate to traverse handle
* @param leafName LeafPredicate name
*/
public static Optional<List<Object>> extractInElements(Predicate predicate, String leafName) {
if (!(predicate instanceof CompoundPredicate)) {
return Optional.empty();
}

CompoundPredicate compoundPredicate = (CompoundPredicate) predicate;
List<Object> leafValues = new ArrayList<>();
List<Predicate> children = compoundPredicate.children();
for (Predicate leaf : children) {
if (leaf instanceof LeafPredicate
&& (((LeafPredicate) leaf).function() instanceof Equal)
&& leaf.visit(LeafPredicateExtractor.INSTANCE).get(leafName) != null) {
leafValues.add(((LeafPredicate) leaf).literals().get(0));
} else {
return Optional.empty();
}
}
return Optional.of(leafValues);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.paimon.predicate.Equal;
import org.apache.paimon.predicate.GreaterOrEqual;
import org.apache.paimon.predicate.GreaterThan;
import org.apache.paimon.predicate.InPredicateVisitor;
import org.apache.paimon.predicate.LeafPredicate;
import org.apache.paimon.predicate.LeafPredicateExtractor;
import org.apache.paimon.predicate.LessOrEqual;
Expand Down Expand Up @@ -229,17 +230,11 @@ public InnerTableRead withFilter(Predicate predicate) {

// optimize for IN filter
if ((compoundPredicate.function()) instanceof Or) {
List<Predicate> children = compoundPredicate.children();
for (Predicate leaf : children) {
if (leaf instanceof LeafPredicate
&& (((LeafPredicate) leaf).function() instanceof Equal)
&& leaf.visit(LeafPredicateExtractor.INSTANCE).get(leafName)
!= null) {
schemaIds.add((Long) ((LeafPredicate) leaf).literals().get(0));
} else {
schemaIds.clear();
break;
}
Optional<List<Object>> leafs =
InPredicateVisitor.extractInElements(predicate, leafName);
if (leafs.isPresent()) {
leafs.get().stream()
.forEach(leaf -> schemaIds.add(Long.parseLong(leaf.toString())));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

InPredicateVisitor.extractInElements(predicate, leafName) .ifPresent(leafs -> leafs .forEach(leaf -> schemaIds.add(Long.parseLong(leaf.toString()))));

}
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
import org.apache.paimon.predicate.Equal;
import org.apache.paimon.predicate.GreaterOrEqual;
import org.apache.paimon.predicate.GreaterThan;
import org.apache.paimon.predicate.InPredicateVisitor;
import org.apache.paimon.predicate.LeafPredicate;
import org.apache.paimon.predicate.LeafPredicateExtractor;
import org.apache.paimon.predicate.LessOrEqual;
import org.apache.paimon.predicate.LessThan;
import org.apache.paimon.predicate.Or;
import org.apache.paimon.predicate.Predicate;
import org.apache.paimon.reader.RecordReader;
import org.apache.paimon.table.FileStoreTable;
Expand Down Expand Up @@ -231,18 +231,11 @@ public InnerTableRead withFilter(Predicate predicate) {
}

// optimize for IN filter
if ((compoundPredicate.function()) instanceof Or) {
for (Predicate leaf : children) {
if (leaf instanceof LeafPredicate
&& (((LeafPredicate) leaf).function() instanceof Equal)
&& leaf.visit(LeafPredicateExtractor.INSTANCE).get(leafName)
!= null) {
snapshotIds.add((Long) ((LeafPredicate) leaf).literals().get(0));
} else {
snapshotIds.clear();
break;
}
}
Optional<List<Object>> leafs =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why here has no judge of : (compoundPredicate.function() instanceof Or) ?
But SchemasTable and TagsTable both judged?

InPredicateVisitor.extractInElements(predicate, leafName);
if (leafs.isPresent()) {
leafs.get().stream()
.forEach(leaf -> snapshotIds.add(Long.parseLong(leaf.toString())));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

InPredicateVisitor.extractInElements(predicate, leafName)
.ifPresent(leafs -> leafs
.forEach(leaf -> snapshotIds.add(Long.parseLong(leaf.toString()))));

}
} else {
handleLeafPredicate(predicate, leafName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.paimon.fs.Path;
import org.apache.paimon.predicate.CompoundPredicate;
import org.apache.paimon.predicate.Equal;
import org.apache.paimon.predicate.InPredicateVisitor;
import org.apache.paimon.predicate.LeafPredicate;
import org.apache.paimon.predicate.LeafPredicateExtractor;
import org.apache.paimon.predicate.Or;
Expand Down Expand Up @@ -239,25 +240,18 @@ public RecordReader<InternalRow> createReader(Split split) {
CompoundPredicate compoundPredicate = (CompoundPredicate) predicate;
// optimize for IN filter
if ((compoundPredicate.function()) instanceof Or) {
List<Predicate> children = compoundPredicate.children();
for (Predicate leaf : children) {
if (leaf instanceof LeafPredicate
&& (((LeafPredicate) leaf).function() instanceof Equal
&& ((LeafPredicate) leaf).literals().get(0)
instanceof BinaryString)
&& predicate
.visit(LeafPredicateExtractor.INSTANCE)
.get(TAG_NAME)
!= null) {
String equalValue =
((LeafPredicate) leaf).literals().get(0).toString();
if (tagManager.tagExists(equalValue)) {
predicateMap.put(equalValue, tagManager.tag(equalValue));
}
} else {
predicateMap.clear();
break;
}
Optional<List<Object>> leafs =
InPredicateVisitor.extractInElements(predicate, TAG_NAME);
if (leafs.isPresent()) {
leafs.get().stream()
.forEach(
leaf -> {
if (tagManager.tagExists(leaf.toString())) {
predicateMap.put(
leaf.toString(),
tagManager.tag(leaf.toString()));
}
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

InPredicateVisitor.extractInElements(predicate, TAG_NAME) .ifPresent(leafs -> leafs .forEach( leaf -> { String leftName = leaf.toString(); if (tagManager.tagExists(leftName)) { predicateMap.put( leftName, tagManager.tag(leftName)); } }));

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public void testSchemasTable() {
result =
sql(
"SELECT schema_id, fields, partition_keys, "
+ "primary_keys, options, `comment` FROM T$schemas where schema_id>0 and schema_id<3");
+ "primary_keys, options, `comment` FROM T$schemas where schema_id>0 and schema_id<3 order by schema_id");
assertThat(result.toString())
.isEqualTo(
"[+I[1, [{\"id\":0,\"name\":\"a\",\"type\":\"INT NOT NULL\"},"
Expand All @@ -312,7 +312,7 @@ public void testSchemasTable() {
result =
sql(
"SELECT schema_id, fields, partition_keys, "
+ "primary_keys, options, `comment` FROM T$schemas where schema_id in (1, 3)");
+ "primary_keys, options, `comment` FROM T$schemas where schema_id in (1, 3) order by schema_id");
assertThat(result.toString())
.isEqualTo(
"[+I[1, [{\"id\":0,\"name\":\"a\",\"type\":\"INT NOT NULL\"},"
Expand Down
Loading