kunwp1 commented on code in PR #9683:
URL: https://github.com/apache/texera/pull/9683#discussion_r4690743948
##########
common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/aggregate/AggregateOpDescSpec.scala:
##########
@@ -120,52 +121,88 @@ class AggregateOpDescSpec extends AnyFlatSpec with
Matchers {
// fast rather than propagate a null-typed output.
val input = Schema().add("v", AttributeType.LONG)
assertThrows[Exception] {
- descWith(List.empty, aggOp(AggregationFunction.SUM, "", "total"))
- .getExternalOutputSchemas(Map(PortIdentity() -> input))
+ outputSchemasFor(input, List.empty, aggOp(AggregationFunction.SUM, "",
"total"))
}
}
- // The integers behind a timestamp column mean microseconds or nanoseconds
- // depending on the resolution it was read at, and a nanosecond total leaves
- // the range of a 64-bit integer after a handful of modern dates.
- it should "read a timestamp as epoch milliseconds at either resolution"
taggedAs NeedsPythonPackages in {
- val python = resolvePython().getOrElse(cancel("No runnable python
executable"))
- if (!canImportPandas(python)) cancel(s"'$python' cannot import pandas")
+ private def requirePandasPython(): String = {
+ val python = configuredPython().getOrElse(cancel("No runnable python
executable"))
+ if (!canImport(python, "pandas", "numpy")) cancel(s"'$python' cannot
import pandas")
+ python
+ }
Review Comment:
**Optional.** This `requirePandasPython()` is identical to the one in
`SetRows`, and `FilterSearchSpecSupport` and `IntersectOpDescSpec` repeat its
two lines inline. Since the PR shares fixtures, one `requirePython(modules:
String*)` in `PythonTestSupport` could serve all four. Note that this touches
`PythonTestSupport`, which the PR does not otherwise change, so it could also
be a follow-up.
##########
common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/FilterSearchSpecSupport.scala:
##########
@@ -0,0 +1,103 @@
+/*
+ * 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
+
+import org.apache.texera.amber.core.executor.{OpExecWithClassName,
OperatorExecutor}
+import org.apache.texera.amber.core.tuple.{Attribute, AttributeType, Schema,
Tuple}
+import org.apache.texera.amber.core.virtualidentity.{ExecutionIdentity,
WorkflowIdentity}
+import org.apache.texera.amber.core.workflow.PortIdentity
+import org.apache.texera.amber.util.JSONUtils.objectMapper
+import org.apache.texera.amber.util.PythonTestSupport.{canImport,
configuredPython, runSource}
+import org.scalatest.flatspec.AnyFlatSpec
+import org.scalatest.matchers.should.Matchers
+
+abstract class FilterSearchSpecSupport extends AnyFlatSpec with Matchers {
+
+ protected val bodyAttr: Attribute = new Attribute("body",
AttributeType.STRING)
+ protected val bodySchema: Schema = Schema().add(bodyAttr)
+
+ protected def tuple(text: String): Tuple =
+ Tuple.builder(bodySchema).add(bodyAttr, text).build()
+
+ protected def assertMatches(exec: OperatorExecutor, text: String): Unit = {
+ val t = tuple(text)
+ assert(exec.processTuple(t, port = 0).toList == List(t))
+ }
+
+ protected def assertMisses(exec: OperatorExecutor, text: String): Unit =
+ assert(exec.processTuple(tuple(text), port = 0).toList.isEmpty)
+
+ protected def assertPhysicalOpWired(op: LogicalOp, expectedClassName:
String): Unit = {
+ val physical = op.getPhysicalOp(WorkflowIdentity(1L),
ExecutionIdentity(1L))
+ physical.opExecInitInfo match {
+ case OpExecWithClassName(className, descString) =>
+ className shouldBe expectedClassName
+ descString should not be empty
+ case other => fail(s"expected OpExecWithClassName, got $other")
+ }
+ // Pin the actual port identities (not just counts).
+ physical.inputPorts.keySet shouldBe
op.operatorInfo.inputPorts.map(_.id).toSet
+ physical.outputPorts.keySet shouldBe
op.operatorInfo.outputPorts.map(_.id).toSet
+ }
+
+ protected def assertStandaloneParity[D <: StandaloneCodeGenerator](
+ prefix: String,
+ flagName: String,
+ cases: Seq[(String, Boolean, Seq[String])],
+ extraPreamble: Seq[String] = Nil
+ )(buildDesc: (String, Boolean) => D)(buildExec: String => OperatorExecutor):
Unit = {
+ val python = configuredPython().getOrElse(cancel("No runnable python
executable"))
+ if (!canImport(python, "pandas")) cancel(s"'$python' cannot import pandas")
+
+ val schema = Schema().add("v", AttributeType.STRING).add("i",
AttributeType.INTEGER)
+ val runs = cases.map {
+ case (needle, flag, values) =>
+ val desc = buildDesc(needle, flag)
+ val exec = buildExec(objectMapper.writeValueAsString(desc))
+ exec.open()
+ val kept = values.zipWithIndex.flatMap {
+ case (v, i) => exec.processTuple(Tuple(schema, Array[Any](v, i)), 0)
+ }
+ exec.close()
+ val label = s"'$needle' $flagName=$flag"
+ val engine = s"$label: ${kept.map(_.getFields(1)).mkString(" ")}"
+ val code = (desc.standaloneHelpers() :+
+ desc.generateStandaloneCode(Map(PortIdentity() ->
schema))).mkString("\n")
+ val script =
+ s"""in1df = pd.DataFrame({"v":
pd.Series(${objectMapper.writeValueAsString(
+ values
+ )}, dtype=object)})
+ |in1df["i"] = range(len(in1df))
+ |ns = {"pd": pd, "in1df": in1df}
+ |exec(${objectMapper.writeValueAsString(code)}, ns)
+ |print(${objectMapper.writeValueAsString(
+ label + ":"
+ )}, " ".join(str(i) for i in ns["out1df"]["i"]))""".stripMargin
+ (engine, script)
+ }
+
+ val driver = (("import pandas as pd" +: extraPreamble) ++
runs.map(_._2)).mkString("\n")
Review Comment:
**Optional.** Notice that the values reach Python as JSON source, so a
missing value reads as `null` and needs the `null = None` shim. Since only the
regex caller passes it through `extraPreamble`, a future substring case with a
`null` would fail with a `NameError` rather than compare the engines. One way
to avoid this is to define the shim in the driver itself, as the regex spec did
before, and drop the parameter:
```scala
// The values arrive as JSON arrays, so a missing one reads as null.
val driver = (Seq("import pandas as pd", "null = None") ++
runs.map(_._2)).mkString("\n")
```
--
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]