carloea2 commented on code in PR #8364: URL: https://github.com/apache/texera/pull/8364#discussion_r4606430713
########## workflow-compiling-service/src/test/scala/org/apache/texera/amber/translator/verify/SourceCategoryRunner.scala: ########## @@ -0,0 +1,556 @@ +/* + * 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.translator.verify + +import org.apache.texera.amber.core.tuple.{AttributeType, Schema, Tuple} +import org.apache.texera.amber.core.workflow.PortIdentity +import org.apache.texera.amber.operator.LogicalOp +import org.apache.texera.amber.operator.source.SourceOperatorDescriptor +import org.apache.texera.amber.operator.source.fetcher.URLFetcherOpDesc +import org.apache.texera.amber.operator.source.scan.{FileAttributeType, ScanSourceOpDesc} +import org.apache.texera.amber.operator.source.scan.csv.CSVScanSourceOpDesc +import org.apache.texera.amber.operator.source.scan.csvOld.CSVOldScanSourceOpDesc +import org.apache.texera.amber.operator.source.scan.file.FileScanSourceOpDesc +import org.apache.texera.amber.operator.source.scan.text.TextInputSourceOpDesc +import com.fasterxml.jackson.databind.node.ObjectNode +import org.apache.texera.amber.util.JSONUtils.objectMapper + +import java.nio.charset.StandardCharsets +import java.nio.file.{Files, Path} +import scala.collection.mutable +import scala.jdk.CollectionConverters._ +import scala.util.Try + +/** + * Per-category test runner for source operators (operators with no input + * ports — they read from an external resource and emit tuples). + * + * Dispatch is auto-first. A scan source declares the format it reads via + * [[ScanSourceOpDesc.fileTypeName]], and where [[encoderByFileType]] knows that + * format the shared [[CanonicalSourceFixture]] is encoded into it with no + * per-operator code at all: a newly registered file-scan source in a known + * format is verified the moment it appears in [[LogicalOp]]'s `@JsonSubTypes`. + * A source that cannot take the shared table keeps a hand-written + * [[SourceHandler]] instead: the text family emits a single `line` column, and + * some carry their data inline. Anything else is flagged, never silently + * skipped. + */ +object SourceCategoryRunner { + + /** + * The curated tier: sources that keep a hand-written handler because they + * can't go through the shared-fixture + encoder (auto) path — their output + * isn't the shared table (text-family, single `line` column) or + * their data is inline config rather than a file. Mirrors the transform + * side's [[CuratedHandlers]] (hand-written vs auto-generated fixture). + */ + private val curatedHandlersByClass: Map[Class[_ <: LogicalOp], SourceHandler] = + Seq[SourceHandler](TextInputHandler, FileScanSourceHandler) + .map(h => h.opDescClass -> h) + .toMap + + /** + * The auto tier. A scan source declares the file format it reads via + * [[ScanSourceOpDesc.fileTypeName]] ("CSV", "JSONL", "Arrow", …). Map that + * tag to the [[CanonicalSourceFixture]] encoder that writes a file in that + * format. Any source whose `fileTypeName` is a key here runs with zero + * per-operator code, so a newly added file-scan source in a known format is + * verified the moment it is registered in `@JsonSubTypes` — no handler, no + * edit here. (ParallelCSV also declares "CSV" and would be covered for free, + * but it is currently commented out of `@JsonSubTypes`, so the suite doesn't + * enumerate it.) + * + * An encoder is handed the op it writes for, because a source's config states + * how the bytes look as well as how to read them: a delimiter, like an + * encoding, has to be in the file before a reader can be asked to honour it. + */ + private val encoderByFileType: Map[String, (Path, ScanSourceOpDesc, Seq[Tuple]) => Path] = Map( + "CSV" -> ((dir, op, rows) => + CanonicalSourceFixture.writeCsv(dir, op.fileEncoding.getCharset, delimiterOf(op), rows) + ), + "CSVOld" -> ((dir, op, rows) => + CanonicalSourceFixture.writeCsv(dir, op.fileEncoding.getCharset, delimiterOf(op), rows) + ), + "JSONL" -> ((dir, op, rows) => + CanonicalSourceFixture.writeJsonl(dir, op.fileEncoding.getCharset, rows) + ), + // Arrow is binary and its descriptor declares fileEncoding ignored, so the + // charset a variant asks for has nothing to apply to. + "Arrow" -> ((dir, _, rows) => CanonicalSourceFixture.writeArrow(dir, rows)), + "Parquet" -> ((dir, _, rows) => CanonicalSourceFixture.writeParquet(dir, rows)) + ) + + /** The delimiter a CSV variant reads with, resolved as the readers resolve it: + * the first character, and a comma when the field is empty or unset. + */ + private def delimiterOf(op: ScanSourceOpDesc): Char = + (op match { + case csv: CSVScanSourceOpDesc => csv.customDelimiter + case csvOld: CSVOldScanSourceOpDesc => csvOld.customDelimiter + case _ => None + }).filter(_.nonEmpty).map(_.charAt(0)).getOrElse(',') + + /** + * Sources this runner cannot verify, with the honest reason. Mirrors + * `TransformVerificationRunner.knownIssues`: the reason surfaces in the + * ignored test's name and the coverage table. + */ + private val knownIssues: Map[Class[_ <: LogicalOp], String] = Map( + classOf[URLFetcherOpDesc] -> + ("live-network source: the operator fetches a real URL over the network, so its " + + "output is non-deterministic and depends on external connectivity — it cannot be " + + "verified against a fixed fixture in isolation") + ) + + /** The format tag a source declares, or `None` if it isn't an instantiable + * ScanSourceOpDesc (non-scan sources, or ones that fail to construct). + */ + private def declaredFileType(opDescClass: Class[_ <: LogicalOp]): Option[String] = + Try(opDescClass.getDeclaredConstructor().newInstance()).toOption.collect { + case scan: ScanSourceOpDesc => scan.fileTypeName + }.flatten + + /** Whether this runner is the one to verify `opDescClass`: a source with no + * input port. A source that reads its file names from an input port, as File + * Scan does, is fed a table like a transform instead. + */ + def runsAsSource(opDescClass: Class[_ <: LogicalOp]): Boolean = + classOf[SourceOperatorDescriptor].isAssignableFrom(opDescClass) && + Try(opDescClass.getDeclaredConstructor().newInstance().operatorInfo.inputPorts.isEmpty) + .getOrElse(true) + + def canRun(opDescClass: Class[_ <: LogicalOp]): Boolean = + curatedHandlersByClass.contains(opDescClass) || + declaredFileType(opDescClass).exists(encoderByFileType.contains) + + /** + * Tier label for a runnable source, mirroring the transform side's + * auto/curated distinction: `"curated source"` when a hand-written + * [[SourceHandler]] serves it, else `"auto source"` (a declared-format scan + * source fixtured by an [[encoderByFileType]] encoder with zero per-op code). + */ + def tier(opDescClass: Class[_ <: LogicalOp]): String = + if (curatedHandlersByClass.contains(opDescClass)) "curated source" else "auto source" + + /** Why a non-runnable source is flagged: a specific known issue, an + * unsupported declared format, or no handler/format match at all. + */ + def flagReason(opDescClass: Class[_ <: LogicalOp]): String = + knownIssues.getOrElse( + opDescClass, + declaredFileType(opDescClass) match { + case Some(fileType) => + s"unsupported source format '$fileType' — no encoder registered in SourceCategoryRunner" + case None => "no source handler registered yet" + } + ) + + private def newScanSource(opDescClass: Class[_ <: LogicalOp]): ScanSourceOpDesc = + opDescClass.getDeclaredConstructor().newInstance() match { + case s: ScanSourceOpDesc => s + case other => + throw new IllegalArgumentException( + s"${opDescClass.getSimpleName} has no curated handler and is not a " + + s"ScanSourceOpDesc (${other.getClass.getName})" + ) + } + + /** + * Every configuration of one source worth running, as (label, op, its own + * directory). + * + * Each variant gets a directory of its own holding its OWN copy of the fixture, + * because the generated script reads the file by bare name (`pd.read_csv( + * "sample.csv")`) out of the directory it runs in. Two variants wanting two + * different `sample.csv` files cannot share one. + */ + private def variantsFor( + opDescClass: Class[_ <: LogicalOp], + testRoot: Path + ): Seq[(String, LogicalOp, Path)] = { + // Punctuation collapses to '_', so two labels differing only in punctuation + // would name the same directory and share one fixture and one output dir. Fail + // loudly instead of letting a variant quietly run someone else's file. + val taken = mutable.Set.empty[String] + def dirFor(label: String): Path = { + val name = label.replaceAll("[^A-Za-z0-9]+", "_") + require(taken.add(name), s"two variants of $opDescClass both map to the directory '$name'") + Files.createDirectories(testRoot.resolve(name)) + } + + curatedHandlersByClass.get(opDescClass) match { + case Some(handler) => + val baseDir = dirFor("default") + val base = handler.makeOpDesc(baseDir) + // Every variant calls the handler AGAIN rather than reusing `base`: the handler + // writes its fixture into the directory it is given, and a second op carrying + // the first one's `fileName` would read a file outside the directory it runs in. + // No enum sweep: both curated sources are the text family, whose `attributeType` + // says how to PARSE the fixture (`alice` is not an integer) and whose + // `fileEncoding` describes its BYTES — flipping either without rewriting the + // fixture compares nothing but how the two paths fail. The auto branch below + // rewrites its fixture per variant and does sweep them. + ConfigGenerator + .fullVariantEditsOf(base, Map.empty, handler.rowCount, sweepEnums = false) + .fold( + reason => + throw new IllegalStateException( + s"cannot vary ${opDescClass.getSimpleName}: $reason" + ), + identity + ) + .map { variant => + if (variant.at.isEmpty) ("default", base, baseDir) + else { + val dir = dirFor(variant.label) + val op = ConfigGenerator + .applyVariant(handler.makeOpDesc(dir), variant) + .fold( + reason => + throw new IllegalStateException( + s"cannot build ${opDescClass.getSimpleName} variant '${variant.label}': $reason" + ), + identity + ) + (variant.label, op, dir) + } + } ++ handler.scenarios.map { + case (label, write) => + val dir = dirFor(label) + (label, write(dir), dir) + } + case None => + val fileType = declaredFileType(opDescClass).getOrElse("") + val encoder = encoderByFileType.getOrElse( + fileType, + throw new IllegalArgumentException( + s"No encoder for ${opDescClass.getSimpleName} (fileTypeName='$fileType')" + ) + ) + def bare( + label: String, + rows: Seq[Tuple], + window: (Option[Int], Option[Int]) = (None, None) + ): (String, LogicalOp, Path) = { + val dir = dirFor(label) + val op = newScanSource(opDescClass) + op.fileName = Some(encoder(dir, op, rows).toUri.toString) + op.offset = window._1 + op.limit = window._2 + (label, op, dir) + } + // The nulls variant takes the bare config rather than crossing with the + // others: how a reader treats an empty cell is a property of the reader, + // and multiplying it across every knob buys runtime, not signal. + // + // The windows are the ones the generator never fills, since it fills a + // knob with a value the property editor accepts: a limit of no rows, which + // still has columns to declare, a negative bound, which only a plan posted + // to the API can carry and which pandas reads from the end, and bounds + // whose sum is past what an Int holds. + val rows = CanonicalSourceFixture.rows + Seq( + bare("default", rows), + bare("nulls", CanonicalSourceFixture.holedRows), + bare("window: limit 0", rows, (None, Some(0))), + bare("window: negative offset", rows, (Some(-1), None)), + bare("window: negative limit", rows, (None, Some(-1))), + bare("window: negative", rows, (Some(-1), Some(-1))), + bare("window: long end", rows, (Some(1), Some(Int.MaxValue))), + bare("window: largest", rows, (Some(Int.MaxValue), Some(Int.MaxValue))) + ) ++ skippedBadLine(opDescClass, fileType, encoder, dirFor) ++ + generatedVariants(opDescClass, encoder, dirFor) + } + } + + /** A JSONL file whose first line is no JSON at all, read past it. The executor + * drops and takes the raw lines before it parses any, so a line outside the + * window is never read, however malformed. The other formats have no line a + * reader refuses to parse. + */ + private def skippedBadLine( + opDescClass: Class[_ <: LogicalOp], + fileType: String, + encoder: (Path, ScanSourceOpDesc, Seq[Tuple]) => Path, + dirFor: String => Path + ): Seq[(String, LogicalOp, Path)] = + if (fileType != "JSONL") Seq.empty + else { + val label = "window: skips a bad line" + val dir = dirFor(label) + val op = newScanSource(opDescClass) + val path = encoder(dir, op, CanonicalSourceFixture.rows) + val body = new String(Files.readAllBytes(path), op.fileEncoding.getCharset) + Files.write(path, ("not json at all\n" + body).getBytes(op.fileEncoding.getCharset)) + op.fileName = Some(path.toUri.toString) + op.offset = Some(1) + Seq((label, op, dir)) + } + + /** + * The variants the shared [[ConfigGenerator]] derives from the operator's own + * fields — the base config with every knob filled, plus one per enum branch + * (`hasHeader`, JSONL's `flatten`). Nothing to register per operator: a knob + * added to a source is swept the day it is added. + * + * `fileEncoding` is swept like any other enum, and the fixture FOLLOWS it: each + * variant's file is written in the charset that variant declares. Encoding is a + * statement about the bytes, so a UTF_16 config over a file left in UTF-8 would + * only compare how each path fails. A CSV delimiter is swept the same way and + * followed the same way: a semicolon config over a comma file reads each line as + * one field on both paths, and agreeing on that tests no semicolon at all. + * + * Variants that serialize identically are dropped — an operator that ignores + * `fileEncoding` (Arrow declares `@JsonIgnoreProperties`) would otherwise run the + * same config three times. + */ + private def generatedVariants( + opDescClass: Class[_ <: LogicalOp], + encoder: (Path, ScanSourceOpDesc, Seq[Tuple]) => Path, + dirFor: String => Path + ): Seq[(String, LogicalOp, Path)] = { + val seen = mutable.Set.empty[String] + ConfigGenerator + .generateVariants(opDescClass, Map.empty, CanonicalSourceFixture.rows.size) + .fold( + reason => + throw new IllegalStateException( + s"cannot auto-configure ${opDescClass.getSimpleName}: $reason" + ), + identity + ) + .flatMap { + case (label, op) => + val scan = op.asInstanceOf[ScanSourceOpDesc] + val shape = objectMapper.valueToTree[ObjectNode](scan) + shape.remove("fileName") // every variant reads its own copy of the file + if (!seen.add(shape.toString)) None + else { + // "default" is already the bare newInstance config above; this one is + // the generator's, which additionally fills limit and offset. + val name = if (label == "default") "auto-base" else label + val dir = dirFor(name) + scan.fileName = Some(encoder(dir, scan, CanonicalSourceFixture.rows).toUri.toString) + Some((name, scan: LogicalOp, dir)) + } + } + } + + /** Runs the parity test for the operator, once per variant. Throws on mismatch. */ + def run(opDescClass: Class[_ <: LogicalOp]): Unit = { + val testRoot = Files.createTempDirectory(s"op-behavior-${opDescClass.getSimpleName}-") Review Comment: The seven passing source checks leave about 4 MiB of fixtures. Please reuse the cleanup from #8362 for successful source runs and keep the artifacts for failures. -- 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]
