HyukjinKwon commented on code in PR #56756: URL: https://github.com/apache/spark/pull/56756#discussion_r3478224485
########## 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: `parseStream` resolves the charset as `parser.options.encoding.getOrElse(UTF_8)` and wraps the stream in an `InputStreamReader` with it. For a non-UTF-8 JSON file where the user didn't set the `encoding` option, this forces UTF-8 rather than using the charset detection the regular (multiline) JSON reader applies. Question: is encoding auto-detection intended to be supported with `explodeEmbeddedArray`? If so, this path would need to mirror it; if not (explicit `encoding` required for non-UTF-8), a line in the option docs would set expectations. The explicit `\uFEFF` BOM skip in `findEmbeddedArray` suggests encoding was considered, hence a question rather than an assertion. Non-blocking. -- 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]
