jiayuasu commented on code in PR #2673: URL: https://github.com/apache/sedona/pull/2673#discussion_r2851271844
########## spark/common/src/main/scala/org/apache/spark/sql/sedona_sql/io/raster/RasterTable.scala: ########## @@ -0,0 +1,94 @@ +/* + * 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.sedona_sql.io.raster + +import org.apache.hadoop.fs.FileStatus +import org.apache.spark.sql.SparkSession +import org.apache.spark.sql.connector.catalog.SupportsRead +import org.apache.spark.sql.connector.catalog.SupportsWrite +import org.apache.spark.sql.connector.catalog.TableCapability +import org.apache.spark.sql.connector.read.ScanBuilder +import org.apache.spark.sql.connector.write.LogicalWriteInfo +import org.apache.spark.sql.connector.write.WriteBuilder +import org.apache.spark.sql.execution.datasources.v2.FileTable +import org.apache.spark.sql.execution.datasources.FileFormat +import org.apache.spark.sql.sedona_sql.UDT.RasterUDT +import org.apache.spark.sql.types.IntegerType +import org.apache.spark.sql.types.StructField +import org.apache.spark.sql.types.StructType +import org.apache.spark.sql.util.CaseInsensitiveStringMap + +import java.util.{Set => JSet} + +case class RasterTable( + name: String, + sparkSession: SparkSession, + options: CaseInsensitiveStringMap, + paths: Seq[String], + userSpecifiedSchema: Option[StructType], + rasterOptions: RasterOptions, + fallbackFileFormat: Class[_ <: FileFormat]) + extends FileTable(sparkSession, options, paths, userSpecifiedSchema) + with SupportsRead + with SupportsWrite { + + override def inferSchema(files: Seq[FileStatus]): Option[StructType] = + Some(userSpecifiedSchema.getOrElse(RasterTable.inferSchema(rasterOptions))) + + override def formatName: String = "Raster" + + override def capabilities(): JSet[TableCapability] = + java.util.EnumSet.of(TableCapability.BATCH_READ) + + override def newScanBuilder(options: CaseInsensitiveStringMap): ScanBuilder = { + RasterScanBuilder(sparkSession, fileIndex, schema, dataSchema, options, rasterOptions) + } + + override def newWriteBuilder(info: LogicalWriteInfo): WriteBuilder = + // Note: this code path will never be taken, since Spark will always fall back to V1 + // data source when writing to File source v2. See SPARK-28396: File source v2 write + // path is currently broken. + null Review Comment: Not an issue ########## spark/common/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/raster/RasterConstructors.scala: ########## @@ -107,39 +109,35 @@ private[apache] case class RS_TileExplode(children: Seq[Expression]) override def eval(input: InternalRow): TraversableOnce[InternalRow] = { val raster = arguments.rasterExpr.toRaster(input) - try { - val bandIndices = arguments.bandIndicesExpr.eval(input).asInstanceOf[ArrayData] match { - case null => null - case value: Any => value.toIntArray - } - val tileWidth = arguments.tileWidthExpr.eval(input).asInstanceOf[Int] - val tileHeight = arguments.tileHeightExpr.eval(input).asInstanceOf[Int] - val padWithNoDataValue = arguments.padWithNoDataExpr.eval(input).asInstanceOf[Boolean] - val noDataValue = arguments.noDataValExpr.eval(input) match { - case null => Double.NaN - case value: Integer => value.toDouble - case value: Decimal => value.toDouble - case value: Float => value.toDouble - case value: Double => value - case value: Any => - throw new IllegalArgumentException( - "Unsupported class for noDataValue: " + value.getClass) - } - val tiles = RasterConstructors.generateTiles( - raster, - bandIndices, - tileWidth, - tileHeight, - padWithNoDataValue, - noDataValue) - tiles.map { tile => - val gridCoverage2D = tile.getCoverage - val row = InternalRow(tile.getTileX, tile.getTileY, gridCoverage2D.serialize) - gridCoverage2D.dispose(true) - row - } - } finally { - raster.dispose(true) + val bandIndices = arguments.bandIndicesExpr.eval(input).asInstanceOf[ArrayData] match { + case null => null + case value: Any => value.toIntArray + } + val tileWidth = arguments.tileWidthExpr.eval(input).asInstanceOf[Int] + val tileHeight = arguments.tileHeightExpr.eval(input).asInstanceOf[Int] + val padWithNoDataValue = arguments.padWithNoDataExpr.eval(input).asInstanceOf[Boolean] + val noDataValue = arguments.noDataValExpr.eval(input) match { + case null => Double.NaN + case value: Integer => value.toDouble + case value: Decimal => value.toDouble + case value: Float => value.toDouble + case value: Double => value + case value: Any => + throw new IllegalArgumentException("Unsupported class for noDataValue: " + value.getClass) + } + val tileIterator = RasterConstructors.generateTiles( + raster, + bandIndices, + tileWidth, + tileHeight, + padWithNoDataValue, + noDataValue) + tileIterator.setAutoDisposeSource(true) + tileIterator.asScala.map { tile => + val gridCoverage2D = tile.getCoverage + val row = InternalRow(tile.getTileX, tile.getTileY, gridCoverage2D.serialize) + gridCoverage2D.dispose(true) + row Review Comment: Not an issue -- 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]
