chenhao-db commented on code in PR #56756: URL: https://github.com/apache/spark/pull/56756#discussion_r3478443044
########## sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/json/EmbeddedArrayJsonDataSource.scala: ########## @@ -0,0 +1,316 @@ +/* + * 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.execution.datasources.json + +import java.io.{InputStream, InputStreamReader, Reader} +import java.nio.charset.StandardCharsets + +import org.apache.hadoop.conf.Configuration +import org.apache.hadoop.fs.FileStatus + +import org.apache.spark.SparkException +import org.apache.spark.sql.SparkSession +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.catalyst.json.{CreateJacksonParser, JacksonParser, JSONOptions} +import org.apache.spark.sql.catalyst.util.FailureSafeParser +import org.apache.spark.sql.errors.QueryExecutionErrors +import org.apache.spark.sql.execution.datasources.{CodecStreams, PartitionedFile} +import org.apache.spark.sql.types.StructType +import org.apache.spark.unsafe.types.UTF8String + +/** + * A [[JsonDataSource]] for the `explodeEmbeddedArray` option. The input JSON files are expected + * to be object documents with a top-level array-valued field named by the option, i.e. + * `{..., "<fieldName>": [record1, record2, ...], ...}`. An [[EmbeddedArraySplitter]] streams the + * records out of the array one at a time, and each record is parsed by the regular JSON parsing + * logic as if it were an independent input record, producing one row per record. Neither the + * whole array text nor the result rows are ever buffered in memory. + */ +object EmbeddedArrayJsonDataSource extends JsonDataSource { + // The embedded array spans the whole file, so the file cannot be split. + override val isSplitable: Boolean = false + + override protected def infer( + sparkSession: SparkSession, + inputPaths: Seq[FileStatus], + parsedOptions: JSONOptions): StructType = { + // Unreachable: `inferSchema` always returns a single variant column when + // `explodeEmbeddedArray` is set. + throw SparkException.internalError( + "The schema is always a single variant column when `explodeEmbeddedArray` is set.") + } + + override def readFile( + conf: Configuration, + file: PartitionedFile, + parser: JacksonParser, + schema: StructType): Iterator[InternalRow] = { + val stream = CodecStreams.createInputStreamWithCloseResource(conf, file.toPath) + parseStream(stream, parser, schema) + } + + override protected def readStream( + in: InputStream, + parser: JacksonParser, + schema: StructType): Iterator[InternalRow] = { + parseStream(in, parser, schema) + } + + private def parseStream( + stream: InputStream, + parser: JacksonParser, + schema: StructType): Iterator[InternalRow] = { + val encoding = parser.options.encoding.getOrElse(StandardCharsets.UTF_8.name()) Review Comment: It is a bit difficult to support auto-detection here, so I choose to keep code unchanged and explicitly document it. The reason it differs from the other readers: every other JSON path hands the raw byte stream to Jackson (`createParser(InputStream)`), and Jackson's `ByteSourceJsonBootstrapper` does the BOM + byte-pattern charset detection internally — Spark never sees that logic. `EmbeddedArrayJsonDataSource` is the exception because the `EmbeddedArraySplitter` scans *characters* to find record boundaries, so it needs a decoded `Reader` and has to choose the charset before Jackson is involved. Mirroring auto-detection would mean reimplementing Jackson's internal detection in Spark, which isn't worth it for this, and Jackson's bootstrapper isn't reusable as stable API. -- 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]
