HeartSaVioR commented on code in PR #59908: URL: https://github.com/apache/spark/pull/59908#discussion_r4419619624
########## sql/catalyst/src/main/java/org/apache/spark/sql/connector/read/ExternalLookupReader.java: ########## @@ -0,0 +1,64 @@ +/* + * 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.read; + +import java.io.Closeable; +import java.io.IOException; + +import org.apache.spark.annotation.Evolving; +import org.apache.spark.sql.catalyst.InternalRow; +import org.apache.spark.sql.connector.metric.CustomTaskMetric; + +/** + * A task-local client that executes lookup requests. + * <ul> + * <li>Calls to a reader and its cursors are sequential, but may come from different threads.</li> + * <li>Requests are not snapshot boundaries: results may mix source versions.</li> + * <li>Retried or speculative tasks may repeat a request; duplicates must be tolerated.</li> + * <li>Implementations should respond to thread interruption if their client supports it.</li> + * </ul> + * + * @since 4.4.0 + */ +@Evolving +public interface ExternalLookupReader extends Closeable { + + /** + * Looks up {@code uniqueParameters} and returns a cursor over all matches. For example: + * <pre>{@code + * uniqueParameters: [(1), (2), (3)] + * cursor yields: (0, a), (0, b), (2, c) // (inputOrdinal, value row) + * => row 0 has two matches, row 1 is a miss, row 2 has one match + * }</pre> + * Spark guarantees that {@code uniqueParameters} is non-null, non-empty and has at most + * {@link ExternalLookup#maxParameterRowsPerRequest()} non-null rows. Field {@code i} of each row Review Comment: > [!NOTE] > **DISCLAIMER:** This comment was written by an LLM, Claude Opus 5.5 (`claude-opus-5-5`, 1M context), posting on behalf of @HeartSaVioR. Adopted the suggested wording in `ExternalLookupReader.lookupBatch`: Spark guarantees that `uniqueParameters` is non-null and holds between 1 and `maxParameterRowsPerRequest()` rows, none of them null. Fixed in [795fc14c3d5](https://github.com/apache/spark/pull/59908/commits/795fc14c3d55a9ba9feac1244a257a5678755222). ########## sql/catalyst/src/test/scala/org/apache/spark/sql/connector/read/ExternalLookupApiSuite.scala: ########## @@ -0,0 +1,74 @@ +/* + * 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.read + +import scala.reflect.ClassTag + +import org.apache.spark.{SparkConf, SparkFunSuite, SparkUnsupportedOperationException} +import org.apache.spark.serializer.JavaSerializer +import org.apache.spark.sql.connector.expressions.{Expression, FieldReference} +import org.apache.spark.sql.connector.expressions.filter.Predicate +import org.apache.spark.sql.types.{IntegerType, StructType} + +class ExternalLookupApiSuite extends SparkFunSuite { + + private val offeredPredicate: Predicate = + new Predicate("=", Array[Expression]( + FieldReference.column("id"), new LookupParameter(IntegerType))) + + private def javaRoundTrip[T: ClassTag](value: T): T = { + val serializer = new JavaSerializer(new SparkConf()).newInstance() + serializer.deserialize[T](serializer.serialize(value)) + } + + test("a lookup parameter is a leaf rendered as ?") { Review Comment: > [!NOTE] > **DISCLAIMER:** This comment was written by an LLM, Claude Opus 5.5 (`claude-opus-5-5`, 1M context), posting on behalf of @HeartSaVioR. Added `candidates of the same shape are distinct because each has its own parameter` to `ExternalLookupApiSuite`. It builds two `id = ?` predicates from distinct `LookupParameter`s and asserts that each equals itself and that the two are not equal (and neither are their parameters). Only `equals` is asserted, not hash codes. Giving `LookupParameter` value-based equality would fail it. Fixed in [795fc14c3d5](https://github.com/apache/spark/pull/59908/commits/795fc14c3d55a9ba9feac1244a257a5678755222). ########## sql/catalyst/src/main/scala/org/apache/spark/sql/internal/connector/ToStringSQLBuilder.scala: ########## @@ -58,6 +59,7 @@ class ToStringSQLBuilder extends V2ExpressionSQLBuilder with Serializable { // until StackOverflowError. Render it as a function call instead. override protected def visitUnexpectedExpr(expr: Expression): String = expr match { case e: GeneralScalarExpression => visitSQLFunction(e.name(), e.children()) + case p: LookupParameter => p.describe() Review Comment: > [!NOTE] > **DISCLAIMER:** This comment was written by an LLM, Claude Opus 5.5 (`claude-opus-5-5`, 1M context), posting on behalf of @HeartSaVioR. `ToStringSQLBuilder` now overrides the expression-level `visitBinaryComparison(name, le, re)`. When the name is `<=>` and an operand is a `LookupParameter`, it renders `l <=> r`; otherwise it delegates to `super`. So `id <=> ?` shows one marker, and a null-safe comparison with a literal keeps its expanded form. The new test `a null-safe candidate renders its parameter once; a literal comparison still expands` covers both, plus `id >= ?` and the reversed `? <=> id`. `V2PredicateSuite` (29 tests) still passes unchanged. Fixed in [795fc14c3d5](https://github.com/apache/spark/pull/59908/commits/795fc14c3d55a9ba9feac1244a257a5678755222). -- 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]
