Skip to content

Commit 1f679ed

Browse files
huaxingaodbtsai
authored andcommitted
[SPARK-36556][SQL] Add DSV2 filters
Co-Authored-By: DB Tsai d_tsaiapple.com Co-Authored-By: Huaxin Gao huaxin_gaoapple.com ### What changes were proposed in this pull request? Add DSV2 Filters and use these in V2 codepath. ### Why are the changes needed? The motivation of adding DSV2 filters: 1. The values in V1 filters are Scala types. When translating catalyst `Expression` to V1 filers, we have to call `convertToScala` to convert from Catalyst types used internally in rows to standard Scala types, and later convert Scala types back to Catalyst types. This is very inefficient. In V2 filters, we use `Expression` for filter values, so the conversion from Catalyst types to Scala types and Scala types back to Catalyst types are avoided. 2. Improve nested column filter support. 3. Make the filters work better with the rest of the DSV2 APIs. ### Does this PR introduce _any_ user-facing change? Yes. The new V2 filters ### How was this patch tested? new test Closes #33803 from huaxingao/filter. Lead-authored-by: Huaxin Gao <[email protected]> Co-authored-by: DB Tsai <[email protected]> Signed-off-by: Liang-Chi Hsieh <[email protected]>
1 parent 7103a16 commit 1f679ed

22 files changed

+1210
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.spark.sql.connector.expressions.filter;
19+
20+
import java.util.Objects;
21+
22+
import org.apache.spark.annotation.Evolving;
23+
import org.apache.spark.sql.connector.expressions.NamedReference;
24+
25+
/**
26+
* A filter that always evaluates to {@code false}.
27+
*
28+
* @since 3.3.0
29+
*/
30+
@Evolving
31+
public final class AlwaysFalse extends Filter {
32+
33+
@Override
34+
public boolean equals(Object o) {
35+
if (this == o) return true;
36+
if (o == null || getClass() != o.getClass()) return false;
37+
return true;
38+
}
39+
40+
@Override
41+
public int hashCode() {
42+
return Objects.hash();
43+
}
44+
45+
@Override
46+
public String toString() { return "FALSE"; }
47+
48+
@Override
49+
public NamedReference[] references() { return EMPTY_REFERENCE; }
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.spark.sql.connector.expressions.filter;
19+
20+
import java.util.Objects;
21+
22+
import org.apache.spark.annotation.Evolving;
23+
import org.apache.spark.sql.connector.expressions.NamedReference;
24+
25+
/**
26+
* A filter that always evaluates to {@code true}.
27+
*
28+
* @since 3.3.0
29+
*/
30+
@Evolving
31+
public final class AlwaysTrue extends Filter {
32+
33+
@Override
34+
public boolean equals(Object o) {
35+
if (this == o) return true;
36+
if (o == null || getClass() != o.getClass()) return false;
37+
return true;
38+
}
39+
40+
@Override
41+
public int hashCode() {
42+
return Objects.hash();
43+
}
44+
45+
@Override
46+
public String toString() { return "TRUE"; }
47+
48+
@Override
49+
public NamedReference[] references() { return EMPTY_REFERENCE; }
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.spark.sql.connector.expressions.filter;
19+
20+
import org.apache.spark.annotation.Evolving;
21+
22+
/**
23+
* A filter that evaluates to {@code true} iff both {@code left} and {@code right} evaluate to
24+
* {@code true}.
25+
*
26+
* @since 3.3.0
27+
*/
28+
@Evolving
29+
public final class And extends BinaryFilter {
30+
31+
public And(Filter left, Filter right) {
32+
super(left, right);
33+
}
34+
35+
@Override
36+
public String toString() {
37+
return String.format("(%s) AND (%s)", left.describe(), right.describe());
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.spark.sql.connector.expressions.filter;
19+
20+
import java.util.Objects;
21+
22+
import org.apache.spark.annotation.Evolving;
23+
import org.apache.spark.sql.connector.expressions.Literal;
24+
import org.apache.spark.sql.connector.expressions.NamedReference;
25+
26+
/**
27+
* Base class for {@link EqualNullSafe}, {@link EqualTo}, {@link GreaterThan},
28+
* {@link GreaterThanOrEqual}, {@link LessThan}, {@link LessThanOrEqual}
29+
*
30+
* @since 3.3.0
31+
*/
32+
@Evolving
33+
abstract class BinaryComparison extends Filter {
34+
protected final NamedReference column;
35+
protected final Literal<?> value;
36+
37+
protected BinaryComparison(NamedReference column, Literal<?> value) {
38+
this.column = column;
39+
this.value = value;
40+
}
41+
42+
public NamedReference column() { return column; }
43+
public Literal<?> value() { return value; }
44+
45+
@Override
46+
public boolean equals(Object o) {
47+
if (this == o) return true;
48+
if (o == null || getClass() != o.getClass()) return false;
49+
BinaryComparison that = (BinaryComparison) o;
50+
return Objects.equals(column, that.column) && Objects.equals(value, that.value);
51+
}
52+
53+
@Override
54+
public int hashCode() {
55+
return Objects.hash(column, value);
56+
}
57+
58+
@Override
59+
public NamedReference[] references() { return new NamedReference[] { column }; }
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.spark.sql.connector.expressions.filter;
19+
20+
import java.util.Objects;
21+
22+
import org.apache.spark.annotation.Evolving;
23+
import org.apache.spark.sql.connector.expressions.NamedReference;
24+
25+
/**
26+
* Base class for {@link And}, {@link Or}
27+
*
28+
* @since 3.3.0
29+
*/
30+
@Evolving
31+
abstract class BinaryFilter extends Filter {
32+
protected final Filter left;
33+
protected final Filter right;
34+
35+
protected BinaryFilter(Filter left, Filter right) {
36+
this.left = left;
37+
this.right = right;
38+
}
39+
40+
public Filter left() { return left; }
41+
public Filter right() { return right; }
42+
43+
@Override
44+
public boolean equals(Object o) {
45+
if (this == o) return true;
46+
if (o == null || getClass() != o.getClass()) return false;
47+
BinaryFilter and = (BinaryFilter) o;
48+
return Objects.equals(left, and.left) && Objects.equals(right, and.right);
49+
}
50+
51+
@Override
52+
public int hashCode() {
53+
return Objects.hash(left, right);
54+
}
55+
56+
@Override
57+
public NamedReference[] references() {
58+
NamedReference[] refLeft = left.references();
59+
NamedReference[] refRight = right.references();
60+
NamedReference[] arr = new NamedReference[refLeft.length + refRight.length];
61+
System.arraycopy(refLeft, 0, arr, 0, refLeft.length);
62+
System.arraycopy(refRight, 0, arr, refLeft.length, refRight.length);
63+
return arr;
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.spark.sql.connector.expressions.filter;
19+
20+
import org.apache.spark.annotation.Evolving;
21+
import org.apache.spark.sql.connector.expressions.Literal;
22+
import org.apache.spark.sql.connector.expressions.NamedReference;
23+
24+
/**
25+
* Performs equality comparison, similar to {@link EqualTo}. However, this differs from
26+
* {@link EqualTo} in that it returns {@code true} (rather than NULL) if both inputs are NULL,
27+
* and {@code false} (rather than NULL) if one of the input is NULL and the other is not NULL.
28+
*
29+
* @since 3.3.0
30+
*/
31+
@Evolving
32+
public final class EqualNullSafe extends BinaryComparison {
33+
34+
public EqualNullSafe(NamedReference column, Literal<?> value) {
35+
super(column, value);
36+
}
37+
38+
@Override
39+
public String toString() { return this.column.describe() + " <=> " + value.describe(); }
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.spark.sql.connector.expressions.filter;
19+
20+
import org.apache.spark.annotation.Evolving;
21+
import org.apache.spark.sql.connector.expressions.Literal;
22+
import org.apache.spark.sql.connector.expressions.NamedReference;
23+
24+
/**
25+
* A filter that evaluates to {@code true} iff the {@code column} evaluates to a value
26+
* equal to {@code value}.
27+
*
28+
* @since 3.3.0
29+
*/
30+
@Evolving
31+
public final class EqualTo extends BinaryComparison {
32+
33+
public EqualTo(NamedReference column, Literal<?> value) {
34+
super(column, value);
35+
}
36+
37+
@Override
38+
public String toString() { return column.describe() + " = " + value.describe(); }
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.spark.sql.connector.expressions.filter;
19+
20+
import org.apache.spark.annotation.Evolving;
21+
import org.apache.spark.sql.connector.expressions.Expression;
22+
import org.apache.spark.sql.connector.expressions.NamedReference;
23+
24+
/**
25+
* Filter base class
26+
*
27+
* @since 3.3.0
28+
*/
29+
@Evolving
30+
public abstract class Filter implements Expression {
31+
32+
protected static final NamedReference[] EMPTY_REFERENCE = new NamedReference[0];
33+
34+
/**
35+
* Returns list of columns that are referenced by this filter.
36+
*/
37+
public abstract NamedReference[] references();
38+
39+
@Override
40+
public String describe() { return this.toString(); }
41+
}

0 commit comments

Comments
 (0)