sunchao commented on a change in pull request #33803: URL: https://github.com/apache/spark/pull/33803#discussion_r695084243
########## File path: sql/catalyst/src/main/java/org/apache/spark/sql/connector/expressions/filter/In.java ########## @@ -0,0 +1,84 @@ +/* + * 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.spark.sql.connector.expressions.filter; + +import org.apache.spark.annotation.Evolving; +import org.apache.spark.sql.connector.expressions.FieldReference; +import org.apache.spark.sql.connector.expressions.Literal; +import org.apache.spark.sql.connector.expressions.NamedReference; + +import java.util.Arrays; +import java.util.stream.Stream; + +/** + * A filter that evaluates to `true` iff the field evaluates to one of the values in the array. + * + * @since 3.3.0 + */ +@Evolving +public final class In extends Filter { + private final FieldReference column; + private final Literal[] values; + + public In(FieldReference column, Literal[] values) { + this.column = column; + this.values = values; + } + + public FieldReference column() { return column; } + public Literal[] values() { return values; } + + public int hashCode() { Review comment: implement only `hashCode` but not `equals`? and why only this implement it? ########## File path: sql/catalyst/src/main/java/org/apache/spark/sql/connector/expressions/filter/In.java ########## @@ -0,0 +1,84 @@ +/* + * 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.spark.sql.connector.expressions.filter; + +import org.apache.spark.annotation.Evolving; +import org.apache.spark.sql.connector.expressions.FieldReference; +import org.apache.spark.sql.connector.expressions.Literal; +import org.apache.spark.sql.connector.expressions.NamedReference; + +import java.util.Arrays; +import java.util.stream.Stream; + +/** + * A filter that evaluates to `true` iff the field evaluates to one of the values in the array. + * + * @since 3.3.0 + */ +@Evolving +public final class In extends Filter { + private final FieldReference column; + private final Literal[] values; Review comment: ditto ########## File path: sql/catalyst/src/main/java/org/apache/spark/sql/connector/expressions/filter/EqualTo.java ########## @@ -0,0 +1,58 @@ +/* + * 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.spark.sql.connector.expressions.filter; + +import org.apache.spark.annotation.Evolving; +import org.apache.spark.sql.connector.expressions.FieldReference; +import org.apache.spark.sql.connector.expressions.Literal; +import org.apache.spark.sql.connector.expressions.NamedReference; + +/** + * A filter that evaluates to `true` iff the field evaluates to a value + * equal to `value`. + * + * @since 3.3.0 + */ +@Evolving +public final class EqualTo extends Filter { + private final FieldReference column; + private final Literal value; Review comment: should this be parameterized `Literal<T>`? ########## File path: sql/core/src/test/scala/org/apache/spark/sql/execution/datasources/v2/V2FiltersSuite.scala ########## @@ -0,0 +1,109 @@ +/* Review comment: can we test the `describe` of the filters too? ########## File path: sql/catalyst/src/main/java/org/apache/spark/sql/connector/expressions/filter/In.java ########## @@ -0,0 +1,84 @@ +/* + * 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.spark.sql.connector.expressions.filter; + +import org.apache.spark.annotation.Evolving; +import org.apache.spark.sql.connector.expressions.FieldReference; +import org.apache.spark.sql.connector.expressions.Literal; +import org.apache.spark.sql.connector.expressions.NamedReference; + +import java.util.Arrays; +import java.util.stream.Stream; + +/** + * A filter that evaluates to `true` iff the field evaluates to one of the values in the array. + * + * @since 3.3.0 + */ +@Evolving +public final class In extends Filter { + private final FieldReference column; + private final Literal[] values; + + public In(FieldReference column, Literal[] values) { + this.column = column; + this.values = values; + } + + public FieldReference column() { return column; } + public Literal[] values() { return values; } + + public int hashCode() { + int h = column.hashCode(); + for (Literal v : values) { + h *= 41; + h += v.hashCode(); + } + return h; + } + + @Override + public String toString() { + StringBuilder str = new StringBuilder(); + for (Literal v : values) { + str.append(v.describe()).append(", "); + } + String res = ""; + if (!str.toString().isEmpty()) { + res = str.substring(0, str.length() - 2); + } + return column.describe() + " in: (" + res + ")"; + } + + @Override + public String describe() { return this.toString(); } + + @Override + public NamedReference[] references() { + Stream<NamedReference> stream = Stream.of(); + NamedReference[] arr = new NamedReference[1]; + arr[0] = column; + stream = Stream.concat(stream, Arrays.stream(arr)); + for (Literal value : values) { Review comment: hmm is this necessary? `values` are literals. ########## File path: sql/catalyst/src/main/java/org/apache/spark/sql/connector/expressions/filter/Filter.java ########## @@ -0,0 +1,43 @@ +/* + * 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.spark.sql.connector.expressions.filter; + +import org.apache.spark.annotation.Evolving; +import org.apache.spark.sql.connector.expressions.Expression; +import org.apache.spark.sql.connector.expressions.NamedReference; + +/** + * Filter base class + * + * @since 3.3.0 + */ +@Evolving +public abstract class Filter implements Expression { + /** + * Returns list of columns that are referenced by this filter. + */ + public abstract NamedReference[] references(); + + protected NamedReference[] findReferences(Object filter) { + if (filter instanceof Filter) { + return ((Filter) filter).references(); + } else { + return new NamedReference[0]; Review comment: nit: we can define a static variable `EMPTY_REFERENCE` so that we don't need to allocate new object each time here. ########## File path: sql/catalyst/src/main/java/org/apache/spark/sql/connector/expressions/filter/EqualTo.java ########## @@ -0,0 +1,58 @@ +/* + * 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.spark.sql.connector.expressions.filter; + +import org.apache.spark.annotation.Evolving; +import org.apache.spark.sql.connector.expressions.FieldReference; +import org.apache.spark.sql.connector.expressions.Literal; +import org.apache.spark.sql.connector.expressions.NamedReference; + +/** + * A filter that evaluates to `true` iff the field evaluates to a value + * equal to `value`. + * + * @since 3.3.0 + */ +@Evolving +public final class EqualTo extends Filter { + private final FieldReference column; + private final Literal value; + + public EqualTo(FieldReference column, Literal value) { + this.column = column; + this.value = value; + } + + public FieldReference column() { return column; } + public Literal value() { return value; } + + @Override + public String toString() { return column.describe() + " = " + value.describe(); } + + @Override + public String describe() { return this.toString(); } Review comment: can we move this to `Filter` if all sub-classes are sharing the same impl? ########## File path: sql/catalyst/src/main/java/org/apache/spark/sql/connector/expressions/filter/Not.java ########## @@ -0,0 +1,44 @@ +/* + * 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.spark.sql.connector.expressions.filter; + +import org.apache.spark.annotation.Evolving; +import org.apache.spark.sql.connector.expressions.NamedReference; + +/** + * A filter that evaluates to `true` iff `child` is evaluated to `false`. + * + * @since 3.3.0 + */ +@Evolving +public final class Not extends Filter { + private final Filter child; + + public Not(Filter child) { this.child = child; } + + public Filter child() { return child; } + + @Override + public String toString() { return child.describe() + " Not"; } Review comment: why "NOT" is at the end? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
