fix(historian): refuse aggregates not valid for the node's DataType up front (#897)
v2-ci / unit-tests (push) Has been cancelled

TimeAverage / Total on a Boolean node over the Gateway backend (AVEVA discrete tag, live
SysPulse) went to the gateway since native TimeAverage/Total became default-on (#694). AVEVA
refuses both modes at StartQuery; the gateway reports that only as Internal /
HistorianServerOperationException (the same answer as a historian storage fault), so the
node answered BadHistoryOperationUnsupported and the read counted as a backend failure.

HistoryRead-Processed now checks the node's scalar DataType before calling any backend and
answers BadAggregateNotSupported (Part 11 6.5.4: "If the Aggregate is not valid for the Node
then the StatusCode shall be Bad_AggregateNotSupported"). Nothing is sent, nothing is counted.
- Shared, every backend: HistoryAggregateDataTypes (Core.Abstractions) applies Part 13
  5.4.2.3 Table 50: the numeric-only aggregates (Average, TimeAverage(2), Total(2), Min/Max,
  ActualTime, Range, Delta, DeltaBounds, Interpolative, stddev/variance) on a String or
  DateTime node are refused. Client-visible: the SQL backends used to answer every interval
  BadAggregateInvalidInputs.
- Per backend: IHistorianDataSource.SupportsAggregateFor(aggregate, TypeCode?) (default true;
  every production backend declares it, guard-tested). Gateway: false for native
  TimeAverage/Total on a Boolean node. SqlServer/QuestDb: true, since they serve Boolean as
  0/1 (stepped), so Boolean stays served there, as do the Gateway's raw-evaluated aggregates.

The residual case (a node declared numeric whose AVEVA tag is discrete) cannot be told apart
from a historian fault on the wire and still counts; that needs the gateway to surface
AVEVA's native error code as a deterministic status.

Tests: HistoryAggregateDataTypesTests, guard test, node-manager String/DateTime and per-type
refusals, Gateway capability matrix, DiscreteTagTimeWeightedWireTests (production Gateway
backend over an AVEVA-shaped fake: unsent, 0 health failures, positive controls). Each
mutation (no check, no shared rule, no backend hint, Gateway hint true, shared rule always
true, SQL inheriting the default) fails at least one. Live gate re-run read-only against
AVEVA 2023 R2 via a private HistorianGateway fa9cf31 container: SysPulse TimeAverage on the
wire = BadAggregateNotSupported, health failures unchanged; #801 gate 244 cases pass.

Claude-Session: https://claude.ai/code/session_01GT67ACmpMR7b9LGRPLijMi
This commit is contained in:
Joseph Doherty
2026-11-13 00:41:32 -05:00
parent 2e537dc6ca
commit ee39c169df
17 changed files with 506 additions and 6 deletions
@@ -111,6 +111,22 @@ public sealed class HistorianDataSourceReverseEventsGuardTests
}
}
[Fact]
public void Every_implementation_declares_its_per_type_aggregate_support_itself()
{
// lmxopcua#897: the default (true) would let a Gateway wrapper send AVEVA native TimeAverage / Total for a Boolean
// node, which AVEVA refuses at StartQuery — BadHistoryOperationUnsupported and a backend failure, not the up-front
// BadAggregateNotSupported.
foreach (var type in ProductionImplementations())
{
var map = type.GetInterfaceMap(typeof(IHistorianDataSource));
var method = map.TargetMethods[Array.FindIndex(map.InterfaceMethods,
m => m.Name == nameof(IHistorianDataSource.SupportsAggregateFor))];
method.DeclaringType.ShouldBe(type,
$"{type.FullName} must declare SupportsAggregateFor itself, not inherit the interface default");
}
}
[Fact]
public void Every_implementation_declares_its_raw_page_limit_itself()
{
@@ -0,0 +1,62 @@
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
namespace ZB.MOM.WW.OtOpcUa.Core.Abstractions.Tests.Historian;
/// <summary>
/// lmxopcua#897: the shared Part 13 §5.4.2.3 Table 50 rule — which aggregates apply to a node's DataType — that every
/// backend's HistoryRead-Processed is checked against before the backend is called.
/// </summary>
public sealed class HistoryAggregateDataTypesTests
{
public static TheoryData<HistoryAggregateType> NumericOnly() => [.. HistoryAggregateDataTypes.NumericOnly];
public static TheoryData<HistoryAggregateType> AnyType() =>
[.. Enum.GetValues<HistoryAggregateType>().Where(a => !HistoryAggregateDataTypes.NumericOnly.Contains(a))];
[Fact]
public void The_numeric_only_set_is_Table_50s()
{
HistoryAggregateDataTypes.NumericOnly.ShouldBe(
[
HistoryAggregateType.Average, HistoryAggregateType.Minimum, HistoryAggregateType.Maximum,
HistoryAggregateType.Total, HistoryAggregateType.Interpolative, HistoryAggregateType.TimeAverage,
HistoryAggregateType.Delta, HistoryAggregateType.Range, HistoryAggregateType.MinimumActualTime,
HistoryAggregateType.MaximumActualTime, HistoryAggregateType.DeltaBounds,
HistoryAggregateType.StandardDeviationPopulation, HistoryAggregateType.VariancePopulation,
HistoryAggregateType.TimeAverage2, HistoryAggregateType.Total2,
],
ignoreOrder: true);
// Table 50 "all types" / "numeric or Boolean" members stay out of it.
foreach (var a in new[]
{
HistoryAggregateType.Count, HistoryAggregateType.Start, HistoryAggregateType.End,
HistoryAggregateType.StartBound, HistoryAggregateType.EndBound, HistoryAggregateType.DurationGood,
HistoryAggregateType.DurationBad, HistoryAggregateType.PercentGood, HistoryAggregateType.PercentBad,
HistoryAggregateType.WorstQuality, HistoryAggregateType.AnnotationCount,
HistoryAggregateType.NumberOfTransitions,
})
HistoryAggregateDataTypes.NumericOnly.ShouldNotContain(a);
}
[Theory]
[MemberData(nameof(NumericOnly))]
public void A_numeric_only_aggregate_does_not_apply_to_a_String_or_DateTime_node(HistoryAggregateType a)
{
HistoryAggregateDataTypes.AppliesTo(a, TypeCode.String).ShouldBeFalse();
HistoryAggregateDataTypes.AppliesTo(a, TypeCode.DateTime).ShouldBeFalse();
// ...but to every numeric node, to Boolean (served as 0/1 where a backend can) and to an unknown type.
foreach (var t in new TypeCode?[] { TypeCode.Double, TypeCode.Single, TypeCode.Int32, TypeCode.UInt16, TypeCode.Boolean, null })
HistoryAggregateDataTypes.AppliesTo(a, t).ShouldBeTrue($"{a} on {t?.ToString() ?? "an unknown type"}");
}
[Theory]
[MemberData(nameof(AnyType))]
public void Every_other_aggregate_applies_to_any_node(HistoryAggregateType a)
{
foreach (var t in new TypeCode?[] { TypeCode.String, TypeCode.DateTime, TypeCode.Boolean, TypeCode.Double, null })
HistoryAggregateDataTypes.AppliesTo(a, t).ShouldBeTrue($"{a} on {t?.ToString() ?? "an unknown type"}");
}
}