aglinxinyuan commented on code in PR #6076: URL: https://github.com/apache/texera/pull/6076#discussion_r3518165108
########## common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/source/scan/json/JSONLScanSourceOpExecSpec.scala: ########## @@ -0,0 +1,83 @@ +/* + * 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.texera.amber.operator.source.scan.json + +import org.apache.texera.amber.operator.source.scan.FileDecodingMethod +import org.apache.texera.amber.util.JSONUtils.objectMapper +import org.scalatest.flatspec.AnyFlatSpec + +import java.net.URI +import java.nio.charset.StandardCharsets +import java.nio.file.Files + +class JSONLScanSourceOpExecSpec extends AnyFlatSpec { + + private def writeJsonl(lines: String*): URI = { + val path = Files.createTempFile("jsonl-scan-", ".jsonl") + path.toFile.deleteOnExit() + Files.write(path, lines.mkString("\n").getBytes(StandardCharsets.UTF_8)) + path.toFile.toURI + } + + private def descString( + uri: URI, + flatten: Boolean = false, + limit: Option[Int] = None, + offset: Option[Int] = None + ): String = { + val desc = new JSONLScanSourceOpDesc + desc.setResolvedFileName(uri) + desc.fileEncoding = FileDecodingMethod.UTF_8 + desc.flatten = flatten + desc.limit = limit + desc.offset = offset + objectMapper.writeValueAsString(desc) + } + + private def drain(exec: JSONLScanSourceOpExec): List[Seq[Any]] = { + exec.open() + try exec.produceTuple().map(_.getFields.toSeq).toList + finally exec.close() + } + + "JSONLScanSourceOpExec" should "read each JSON line, ordering fields by sorted attribute name" in { + val exec = new JSONLScanSourceOpExec( + descString(writeJsonl("""{"id":1,"name":"a"}""", """{"id":2,"name":"b"}""")) + ) Review Comment: Good point — the JSON objects now list keys as name-then-id, so the id-then-name assertion fails if the executor ever preserved input order. ########## common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/source/scan/csv/ParallelCSVScanSourceOpExecSpec.scala: ########## @@ -0,0 +1,104 @@ +/* + * 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.texera.amber.operator.source.scan.csv + +import org.apache.texera.amber.util.JSONUtils.objectMapper +import org.scalatest.flatspec.AnyFlatSpec + +import java.nio.charset.StandardCharsets +import java.nio.file.Files + +class ParallelCSVScanSourceOpExecSpec extends AnyFlatSpec { + + private def writeCsv(content: String): String = { + val file = Files.createTempFile("parallel-csv-", ".csv") + file.toFile.deleteOnExit() + Files.write(file, content.getBytes(StandardCharsets.UTF_8)) + file.toFile.toURI.toString + } + + private def descString(uri: String, delimiter: String = ",", header: Boolean = true): String = { + val desc = new ParallelCSVScanSourceOpDesc() + desc.fileName = Some(uri) + desc.customDelimiter = Some(delimiter) + desc.hasHeader = header + desc.setResolvedFileName(new java.net.URI(uri)) + objectMapper.writeValueAsString(desc) + } + + private def drain(exec: ParallelCSVScanSourceOpExec): List[Seq[Any]] = { + exec.open() + try exec.produceTuple().map(_.getFields.toSeq).toList + finally exec.close() + } + + "ParallelCSVScanSourceOpExec" should "read every data row of a headered file" in { + val exec = + new ParallelCSVScanSourceOpExec(descString(writeCsv("id,name,age\n1,Alice,30\n2,Bob,25\n"))) + val rows = drain(exec) + assert(rows.size == 2) + assert(rows.head == Seq(1, "Alice", 30)) + assert(rows(1) == Seq(2, "Bob", 25)) + } + + it should "not skip the first row when the file has no header" in { + val exec = + new ParallelCSVScanSourceOpExec( + descString(writeCsv("1,Alice,30\n2,Bob,25\n"), header = false) + ) + assert(drain(exec).size == 2) + } + + it should "pad short rows with trailing nulls to the schema width" in { + // the wide first data row fixes the schema at 3 columns; the short row is padded + val exec = new ParallelCSVScanSourceOpExec(descString(writeCsv("a,b,c\n1,2,3\n4,5\n"))) + val rows = drain(exec) + assert(rows.size == 2) + assert(rows(1).length == 3) + assert(rows(1)(2) == null) + } + + it should "discard all-null (blank) lines" in { + val exec = + new ParallelCSVScanSourceOpExec(descString(writeCsv("name,city\nAlice,NYC\n\nBob,LA\n"))) + val rows = drain(exec) + assert(rows.size == 2) + assert(rows.map(_.head) == Seq("Alice", "Bob")) + } + + it should "partition a headerless file across workers by byte range" in { + // Two workers over the same file: worker 0 (idx != workerCount-1) takes the + // first byte-range chunk; worker 1 seeks past its chunk start and drops the + // leading partial line. Exact boundary rows depend on the block reader + // reading one line past the boundary, so assert only that the union covers + // all rows without duplication. + val uri = writeCsv("1,aaaa\n2,bbbb\n3,cccc\n4,dddd\n") Review Comment: Fixed — the fixture now uses uneven row widths so totalBytes/2 (=12) lands inside the second row, so worker 1 skips a genuine partial line rather than a full one. -- 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]
