From 3a22d9a02468e531b079c8c8d019c408e5715089 Mon Sep 17 00:00:00 2001 From: t0m3x Date: Fri, 23 Aug 2024 16:59:06 +0200 Subject: [PATCH] Fixed `Energy.toString` and mandatory parquet value omission Before this change, a lower energy value was printed in KJoule and a higher one in Joule. This change reverses it. Before this change no exception was thrown when omitting value in a required parquet field while exporting, resulting in unreadable parquet file. Now exception is thrown while exporting. --- .../src/main/kotlin/org/opendc/common/units/Energy.kt | 2 +- .../org/opendc/trace/util/parquet/exporter/Exporter.kt | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/opendc-common/src/main/kotlin/org/opendc/common/units/Energy.kt b/opendc-common/src/main/kotlin/org/opendc/common/units/Energy.kt index 467192a01..22803b866 100644 --- a/opendc-common/src/main/kotlin/org/opendc/common/units/Energy.kt +++ b/opendc-common/src/main/kotlin/org/opendc/common/units/Energy.kt @@ -55,7 +55,7 @@ public value class Energy private constructor( override fun toString(): String = fmtValue() override fun fmtValue(fmt: String): String = - if (value >= 1000.0) { + if (value <= 1000.0) { "${toJoule().fmt(fmt)} Joule" } else { "${toKJoule().fmt(fmt)} KJoule" diff --git a/opendc-trace/opendc-trace-parquet/src/main/kotlin/org/opendc/trace/util/parquet/exporter/Exporter.kt b/opendc-trace/opendc-trace-parquet/src/main/kotlin/org/opendc/trace/util/parquet/exporter/Exporter.kt index 05f36530e..4f46f573f 100644 --- a/opendc-trace/opendc-trace-parquet/src/main/kotlin/org/opendc/trace/util/parquet/exporter/Exporter.kt +++ b/opendc-trace/opendc-trace-parquet/src/main/kotlin/org/opendc/trace/util/parquet/exporter/Exporter.kt @@ -32,6 +32,7 @@ import org.apache.parquet.schema.PrimitiveType.PrimitiveTypeName.DOUBLE import org.apache.parquet.schema.PrimitiveType.PrimitiveTypeName.FLOAT import org.apache.parquet.schema.PrimitiveType.PrimitiveTypeName.INT32 import org.apache.parquet.schema.PrimitiveType.PrimitiveTypeName.INT64 +import org.apache.parquet.schema.Type import org.apache.parquet.schema.Types import org.opendc.trace.util.parquet.ParquetDataWriter import java.io.File @@ -122,7 +123,13 @@ public class Exporter val valueToAdd: Any = column.getValue( record, - ) ?: return@forEachIndexed // Maybe add explicit check for optional fields + ) ?: let { + if (column.field.isRepetition(Type.Repetition.OPTIONAL)) { + return@forEachIndexed + } else { + throw RuntimeException("trying to insert null value in required column $column") + } + } startField(column.name, idx) when (column.primitiveTypeName) {