wchevreuil commented on code in PR #162:
URL: https://github.com/apache/hbase-connectors/pull/162#discussion_r3958968330


##########
spark4/hbase-spark4/src/main/scala/org/apache/hadoop/hbase/spark/datasources/HBasePartitionReader.scala:
##########
@@ -0,0 +1,317 @@
+/*
+ * 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.hadoop.fs.Path
+import org.apache.hadoop.hbase.{CellUtil, HBaseConfiguration, TableName}
+import org.apache.hadoop.hbase.client.{Result, ResultScanner, Scan}
+import org.apache.hadoop.hbase.spark.{AndLogicExpression, 
DynamicLogicExpression,
+  EqualLogicExpression, GreaterThanLogicExpression, 
GreaterThanOrEqualLogicExpression,
+  HBaseConnectionCache, IsNullLogicExpression, LessThanLogicExpression,
+  LessThanOrEqualLogicExpression, Logging, OrLogicExpression, 
PassThroughLogicExpression,
+  PushdownMappedField, SmartConnection, SparkSQLPushDownFilter, 
StartsWithLogicExpression}
+import org.apache.hadoop.hbase.util.Bytes
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.catalyst.expressions.GenericInternalRow
+import org.apache.spark.sql.connector.read.PartitionReader
+import org.apache.spark.sql.sources._
+import org.apache.spark.sql.types._
+import org.apache.spark.unsafe.types.UTF8String
+import org.apache.yetus.audience.InterfaceAudience
+import scala.collection.mutable.ListBuffer
+import scala.jdk.CollectionConverters._
+
+/**
+ * This is a new class in the spark4 module. Extends 
PartitionReader[InternalRow] for reading data from HBase regions.
+ * The actual execution: opens an HBase scanner on the partition's range, 
attaches the SparkSQLPushDownFilter,
+ * reads Result objects, and converts them to InternalRow. Implements 
next()/get()/close().
+ *
+ *
+ * In the spark 3 DS V1 model, this logic was inside DefaultSource.buildScan()
+ * which returned an RDD[Row] with its own compute() method.
+ *
+ * @param partition
+ * @param requiredSchema
+ * @param properties
+ * @param catalog
+ * @param pushedFilters
+ * @param encoderClsName
+ * @param usePushDownColumnFilter
+ */
[email protected]
+class HBasePartitionReader(
+    partition: HBaseInputPartition,
+    requiredSchema: StructType,
+    properties: Map[String, String],
+    catalog: HBaseTableCatalog,
+    pushedFilters: Array[Filter],
+    encoderClsName: String,
+    usePushDownColumnFilter: Boolean)
+    extends PartitionReader[InternalRow]
+    with Logging {
+
+  private val conf = HBaseConfiguration.create()
+  private val configResources = 
properties.get(HBaseSparkConf.HBASE_CONFIG_LOCATION)
+  configResources.foreach(_.split(",").foreach(r => conf.addResource(new 
Path(r))))
+
+  private val connection: SmartConnection = 
HBaseConnectionCache.getConnection(conf)
+  private val tableName = s"${catalog.namespace}:${catalog.name}"
+  private val table = connection.getTable(TableName.valueOf(tableName))
+
+  private val scanner: ResultScanner = {
+    val scan = new Scan()
+
+    if (partition.startRow != null && partition.startRow.nonEmpty) {
+      scan.withStartRow(partition.startRow)
+    }
+    if (partition.stopRow != null && partition.stopRow.nonEmpty) {
+      scan.withStopRow(partition.stopRow)
+    }
+
+    val blockCacheEnable = properties
+      .get(HBaseSparkConf.QUERY_CACHEBLOCKS)
+      .map(_.toBoolean)
+      .getOrElse(HBaseSparkConf.DEFAULT_QUERY_CACHEBLOCKS)
+    scan.setCacheBlocks(blockCacheEnable)
+
+    properties.get(HBaseSparkConf.QUERY_CACHEDROWS).map(_.toInt).foreach { 
rows =>
+      if (rows > 0) scan.setCaching(rows)
+    }
+    properties.get(HBaseSparkConf.QUERY_BATCHSIZE).map(_.toInt).foreach { 
batch =>
+      if (batch > 0) scan.setBatch(batch)
+    }
+
+    val requiredFields = 
requiredSchema.fieldNames.map(catalog.sMap.getField(_))
+    val filterFields = extractFilterFields(pushedFilters)
+    val scanFields = (requiredFields ++ 
filterFields).distinct.filterNot(_.isRowKey)
+
+    scanFields.foreach { f =>
+      scan.addColumn(f.cfBytes, f.colBytes)
+    }
+
+    if (usePushDownColumnFilter && pushedFilters.nonEmpty) {
+      val valueArray = buildValueArray()
+      val dynamicLogicExpression = buildDynamicLogicExpression()
+      if (dynamicLogicExpression != null) {
+        val allFilterFields = (requiredFields ++ filterFields).distinct
+        val columnMappings = allFilterFields.map { field =>
+          new PushdownMappedField {
+            override def colName(): String = field.colName
+            override def cfBytes(): Array[Byte] = field.cfBytes
+            override def colBytes(): Array[Byte] = field.colBytes
+          }
+        }
+        val pushDownFilter = new SparkSQLPushDownFilter(
+          dynamicLogicExpression,
+          valueArray,
+          columnMappings.toList.asJava,
+          encoderClsName)
+        scan.setFilter(pushDownFilter)
+      }
+    }
+
+    table.getScanner(scan)
+  }
+
+  private var currentResult: Result = _
+
+  override def next(): Boolean = {
+    currentResult = scanner.next()
+    currentResult != null
+  }
+
+  override def get(): InternalRow = {
+    val fields = requiredSchema.fieldNames.map(catalog.sMap.getField(_))
+    val rowKey = currentResult.getRow
+    catalog.dynSetupRowKey(rowKey)
+    val keyFields = catalog.getRowKey

Review Comment:
   Addressing on next commit.



-- 
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]

Reply via email to