Copilot commented on code in PR #174: URL: https://github.com/apache/hbase-connectors/pull/174#discussion_r4480034501
########## spark/hbase-spark-pushdown/src/main/scala/org/apache/hadoop/hbase/spark/datasources/PredicatePushdownSupport.scala: ########## @@ -0,0 +1,68 @@ +/* + * 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.hadoop.hbase.spark.datasources + +import org.apache.spark.sql.sources._ +import org.apache.spark.sql.types._ +import org.apache.yetus.audience.InterfaceAudience + [email protected] +private[spark] object PredicatePushdownSupport { + + def isSupported( + filter: Filter, + dataTypeOf: String => Option[DataType], + isNaiveEncoder: Boolean): Boolean = { + def hasSafeLiteral(attr: String, value: Any): Boolean = { + (dataTypeOf(attr), value) match { + case (Some(BooleanType), _: Boolean) => true + case (Some(ByteType), _: Byte) => true + case (Some(ShortType), _: Short) => true + case (Some(IntegerType), _: Int) => true + case (Some(LongType), _: Long) => true + case (Some(StringType), _: String) => true + case (Some(BinaryType), _: Array[Byte]) => true + // shortcut: temporal, decimal and floating point comparisons stay in Spark until + // encoding, SQL comparison semantics and row-key pruning are all supported. + case _ => false + } + } + + // Byte has no comparison tag, and its ranges do not split signed values at zero. + def supportsRange(attr: String, value: Any): Boolean = + isNaiveEncoder && hasSafeLiteral(attr, value) && !dataTypeOf(attr).contains(ByteType) + + filter match { + case EqualTo(attr, value) => hasSafeLiteral(attr, value) + case LessThan(attr, value) => supportsRange(attr, value) + case LessThanOrEqual(attr, value) => supportsRange(attr, value) + case GreaterThan(attr, value) => supportsRange(attr, value) + case GreaterThanOrEqual(attr, value) => supportsRange(attr, value) + case StringStartsWith(attr, value) => value != null && dataTypeOf(attr).contains(StringType) + case IsNull(attr) => dataTypeOf(attr).isDefined Review Comment: `IsNull` pushdown is generally unsafe for sparse HBase columns unless the server-side filter implementation explicitly treats a missing cell as NULL (i.e., does not drop rows when the column is missing). Given the PR description notes unresolved NULL behavior, consider marking `IsNull` as unsupported (leave it residual in Spark) until the connector’s HBase filter guarantees correct semantics for missing qualifiers; `IsNotNull` can remain supported if missing columns are meant to be excluded. ########## spark/hbase-spark/src/test/scala/org/apache/hadoop/hbase/spark/DefaultSourceSuite.scala: ########## @@ -1534,4 +1535,182 @@ class DefaultSourceSuite case _: NullPointerException => fail("Unexpected NPE thrown") } } + + private def checkTypeAwarePredicates( + name: String, + dataType: String, + values: Seq[Any], + predicates: Seq[String], + rowKey: Boolean, + avro: Boolean = false): Unit = { + val testTable = TableName.valueOf(s"type_aware_${name}_${if (rowKey) "key" else "column"}") + val valueType = if (avro) "\"avro\":\"valueSchema\"" else s""""type":"$dataType"""" + val valueFamily = if (rowKey) "rowkey" else columnFamily + val rowKeyName = if (rowKey) "value" else "key" + val keyColumn = if (rowKey) "" else """"key":{"cf":"rowkey", "col":"key", "type":"string"},""" + val json = s"""{ + |"table":{"namespace":"default", "name":"${testTable.getNameAsString}"}, + |"rowkey":"$rowKeyName", + |"columns":{ + |$keyColumn + |"value":{"cf":"$valueFamily", "col":"value", $valueType}, + |"id":{"cf":"$columnFamily", "col":"id", "type":"string"}, + |"safe":{"cf":"$columnFamily", "col":"safe", "type":"int"} + |}}""".stripMargin + val options = Map( + HBaseTableCatalog.tableCatalog -> json, + "valueSchema" -> """{"type":"record","name":"TypeAwareValue","fields":[{"name":"number","type":"int"}]}""") + new HBaseContext(sc, TEST_UTIL.getConfiguration) + val field = HBaseTableCatalog(options).getField("value") Review Comment: This creates a new `HBaseContext` on every `checkTypeAwarePredicates` invocation and does not retain/close it. Repeated instantiation can leak resources (threads/connections) and slow down the suite. Prefer creating/reusing a single `HBaseContext` in suite setup (`beforeAll`) or using an existing shared instance already managed by the suite. ########## spark4/hbase-spark4/src/main/scala/org/apache/hadoop/hbase/spark/datasources/HBaseScanBuilder.scala: ########## @@ -57,33 +57,25 @@ class HBaseScanBuilder(schema: StructType, properties: Map[String, String]) override def pushFilters(filters: Array[Filter]): Array[Filter] = { val hasCompositeRowKey = catalog.getRowKey.size > 1 - def isSupported(f: Filter): Boolean = f match { - case EqualTo(attr, _) => isSupportedField(attr) - case LessThan(attr, _) => isSupportedField(attr) - case GreaterThan(attr, _) => isSupportedField(attr) - case LessThanOrEqual(attr, _) => isSupportedField(attr) - case GreaterThanOrEqual(attr, _) => isSupportedField(attr) - case StringStartsWith(attr, _) => isSupportedField(attr) - case IsNull(attr) => isSupportedField(attr) - case IsNotNull(attr) => isSupportedField(attr) - case Or(left, right) => isSupported(left) && isSupported(right) - case And(left, right) => isSupported(left) && isSupported(right) - case _ => false - } - - def isSupportedField(attr: String): Boolean = { - catalog.sMap.map.get(attr) match { - case Some(field) => !(hasCompositeRowKey && field.isRowKey) - case None => false - } - } + def dataTypeOf(attr: String): Option[DataType] = + catalog.sMap.map + .get(attr) + .filter( + field => + !(hasCompositeRowKey && field.isRowKey) && + field.avroSchema.isEmpty && field.serdes.isEmpty) + .map(_.dt) val supported = new ListBuffer[Filter]() val unsupported = new ListBuffer[Filter]() - filters.foreach { f => - if (isSupported(f)) supported += f - else unsupported += f + filters.foreach { + f => + if (PredicatePushdownSupport.isSupported( + f, + dataTypeOf, + encoder.getClass == classOf[NaiveEncoder])) supported += f Review Comment: Using `encoder.getClass == classOf[NaiveEncoder]` as a capability check is brittle and hard to extend (e.g., a verified encoder that subclasses `NaiveEncoder` will be treated as unverified). Prefer an explicit capability signal (e.g., a marker trait implemented only by encoders that guarantee Spark-compatible ordering/range semantics, or a method on the encoder interface) so correctness gating doesn’t depend on exact runtime class equality. If you keep the conservative behavior, consider documenting this constraint at the capability boundary. ########## spark4/hbase-spark4/src/test/scala/org/apache/hadoop/hbase/spark/datasources/HBaseTableProviderSuite.scala: ########## @@ -1013,4 +1043,181 @@ class HBaseTableProviderSuite extends AnyFunSuite with BeforeAndAfterAll with Lo connection.close() } } + + private def checkTypeAwarePredicates( + name: String, + dataType: String, + values: Seq[Any], + predicates: Seq[String], + rowKey: Boolean, + avro: Boolean = false): Unit = { + val testTable = TableName.valueOf(s"type_aware_${name}_${if (rowKey) "key" else "column"}") Review Comment: The test table name is deterministic. If tests run concurrently (within the suite, across suites sharing a mini-cluster, or under parallel test execution/retries), this can cause flaky failures due to table name collisions. Consider adding a unique suffix (e.g., per-test UUID / nanotime) or using the test framework’s test name to ensure uniqueness, while still cleaning up in the `finally` block. -- 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]
