Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you think this file needs test? seems it only defined three static schemas. Can we skip it?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's check the coverage about this change. I kind of don't believe the current numbers shown.

Copy link
Copy Markdown
Contributor Author

@aglinxinyuan aglinxinyuan May 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair concern. The motivation is that downstream readers deserialize this schema positionally and cast each slot to a concrete type, so a silent reorder/retype would break consumers without any local-file change being obvious in review (the OG Copilot thread above made the same point). Pinning the column list + types here lets a CI red flag catch the drift instead of waiting for a runtime cast failure.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expanded the spec in 743cfb5 so it goes beyond redeclaring the schema. New runtimeStatisticsSchema coverage: stable name → index mapping for positional readers; unknown-name lookup throws and the error message names the missing column; containsAttribute returns false for unknown names; column names are unique; toRawSchemafromRawSchema round-trips names + types intact (the cross-language serialization contract that Python and external consumers actually depend on); singleton-val identity. Parallel coverage added for consoleMessagesSchema. 12 tests total, all passing.

Let me know if this changes your mind on closing — happy to either keep it or close if it still feels like ceremony.

* 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.core.storage.result

import org.apache.texera.amber.core.tuple.{AttributeType, Schema}
import org.scalatest.flatspec.AnyFlatSpec

class ResultSchemaSpec extends AnyFlatSpec {

// The expected (name, type) layout of runtimeStatisticsSchema, in the order
// production code declares it. Multiple tests below depend on this list, so
// it lives here as a single source of truth for the spec.
private val runtimeStatsLayout: List[(String, AttributeType)] = List(
"operatorId" -> AttributeType.STRING,
"time" -> AttributeType.TIMESTAMP,
"inputTupleCnt" -> AttributeType.LONG,
"inputTupleSize" -> AttributeType.LONG,
"outputTupleCnt" -> AttributeType.LONG,
"outputTupleSize" -> AttributeType.LONG,
"dataProcessingTime" -> AttributeType.LONG,
"controlProcessingTime" -> AttributeType.LONG,
"idleTime" -> AttributeType.LONG,
"numWorkers" -> AttributeType.INTEGER,
"status" -> AttributeType.INTEGER
)

"ResultSchema.runtimeStatisticsSchema" should "list its columns in the declared order" in {
val actualNames =
ResultSchema.runtimeStatisticsSchema.getAttributes.map(_.getName)
assert(actualNames == runtimeStatsLayout.map(_._1))
}

it should "pin every runtime-statistics column to its expected type" in {
val schema = ResultSchema.runtimeStatisticsSchema
// Downstream readers deserialize positionally and cast each slot, so each
// column type matters. Pin all of them, not just a sample.
runtimeStatsLayout.foreach {
case (name, expectedType) =>
assert(
schema.getAttribute(name).getType == expectedType,
s"$name expected $expectedType, got ${schema.getAttribute(name).getType}"
)
}
}

it should "expose a stable name → index mapping for positional readers" in {
val schema = ResultSchema.runtimeStatisticsSchema
runtimeStatsLayout.zipWithIndex.foreach {
case ((name, _), expectedIndex) =>
assert(
schema.getIndex(name) == expectedIndex,
s"$name expected index $expectedIndex, got ${schema.getIndex(name)}"
)
}
}

it should "throw on lookup of an unknown attribute name" in {
val schema = ResultSchema.runtimeStatisticsSchema
val ex = intercept[RuntimeException] {
schema.getAttribute("not-a-runtime-stats-column")
}
assert(ex.getMessage.contains("not-a-runtime-stats-column"))
assert(!schema.containsAttribute("not-a-runtime-stats-column"))
}

it should "have unique column names" in {
val names = ResultSchema.runtimeStatisticsSchema.getAttributes.map(_.getName)
assert(names.distinct == names, s"duplicate column names: $names")
}

it should "round-trip via toRawSchema → fromRawSchema with stable names and types" in {
// The cross-language serialization contract that downstream Python /
// external consumers actually depend on. If a column type drifts so
// its `AttributeType.name()` no longer round-trips, this fails.
val original = ResultSchema.runtimeStatisticsSchema
val raw = original.toRawSchema
val restored = Schema.fromRawSchema(raw)
// Names + types must be preserved by the round-trip; column ORDER is not
// contractually guaranteed by `toRawSchema`'s `Map` return type, so we
// compare via the (name, type) set instead of full equality.
assert(
restored.getAttributes.map(a => a.getName -> a.getType).toSet ==
original.getAttributes.map(a => a.getName -> a.getType).toSet
)
assert(raw.keySet == runtimeStatsLayout.map(_._1).toSet)
assert(raw == runtimeStatsLayout.map { case (n, t) => n -> t.name() }.toMap)
}

it should "be a singleton val (same instance per access)" in {
// Pin the assumption that consumers can hold references without paying
// for repeated rebuilds, and that two reads produce structurally-equal
// schemas at minimum.
assert(ResultSchema.runtimeStatisticsSchema eq ResultSchema.runtimeStatisticsSchema)
}

"ResultSchema.consoleMessagesSchema" should "have a single STRING `message` column" in {
val schema = ResultSchema.consoleMessagesSchema
val attrs = schema.getAttributes
assert(attrs.size == 1)
assert(attrs.head.getName == "message")
assert(attrs.head.getType == AttributeType.STRING)
}

it should "place `message` at index 0" in {
assert(ResultSchema.consoleMessagesSchema.getIndex("message") == 0)
}

it should "throw on lookup of an unknown attribute name" in {
val ex = intercept[RuntimeException] {
ResultSchema.consoleMessagesSchema.getAttribute("not-a-console-column")
}
assert(ex.getMessage.contains("not-a-console-column"))
}

it should "round-trip to a {message: STRING} raw schema" in {
val raw = ResultSchema.consoleMessagesSchema.toRawSchema
assert(raw == Map("message" -> AttributeType.STRING.name()))
}

it should "be a singleton val (same instance per access)" in {
assert(ResultSchema.consoleMessagesSchema eq ResultSchema.consoleMessagesSchema)
}
}
Loading