Skip to content

fix: correct Unpivot's null tests for int and int? members - #188

Open
Peter Shrosbree (pcshrosbree) wants to merge 1 commit into
microsoft:masterfrom
pcshrosbree:upstream/unpivot-nulls
Open

Peter Shrosbree (pcshrosbree) wants to merge 1 commit into
microsoft:masterfrom
pcshrosbree:upstream/unpivot-nulls

Conversation

@pcshrosbree

Copy link
Copy Markdown

TL;DR: Unpivot skipped int input members, and handled int? properties the wrong way round: a property with a value was skipped, and a null one threw. This fixes the null tests that Unpivot builds for each input member, and adds eight regression tests, each run in row mode and with the default settings.

Fixes #185.

Unpivot builds a null test for each input member and skips the member when the test is true. Two of those tests were wrong, both in the UnpivotEnumerable constructor in PivotExtensions.cs:

  • When the value field's type is not a class, such as int, a member that is not Nullable<T> got the test o => true (line 327 and line 363). So an int field or property was never emitted, although an int? field was.
  • For an int? property, the test was IsTrue(HasValue) (line 313), where the field branch has IsFalse(HasValue). A property with a value was skipped, and a null one threw InvalidOperationException from Value.

Line 313 now uses IsFalse. When the value field's type is not a class, lines 327 and 363 now call a helper that chooses the null test by the input member's type. A non-nullable value-type member is never null. This branch also handles reference-type input members when the output value field has an interface type. For example, a string input member can supply an IComparable value field. Reference-type input members in this branch are compared with null by reference (Expression.ReferenceEqual). This skips null values and prevents a user-defined == operator from making a non-null value look null. Applying o => false to every member in this branch would instead emit a null string as a value. The branch used when the value field's type is a class is unchanged.

The new UnpivotNullTests has eight tests, each run in two test classes: one uses row mode, and the other uses the default settings. They cover int fields and properties, int? fields with values, int? properties with values and with nulls, and an IComparable value field with string members and with members of a type that defines its own ==. On this branch's base, with only the test file added, seven tests fail in each class; the int? fields case already worked. With the fix, all 16 runs pass. I ran them on netcoreapp3.1, rolled forward to .NET 10.0.13 on Linux x64, because only .NET 10 runtimes are installed here.

The second test class uses the default settings rather than columnar mode with fallback disabled. Unpivot passes a captured delegate to SelectMany, which columnar code generation cannot compile, so a columnar query with Unpivot falls back to row execution.

This PR and #187 both edit PivotExtensions.cs, in different methods. They merge without conflicts, and the tests of both pass on the merged tree.

Note

Prepared with AI assistance (Anthropic Claude, OpenAI Codex). I reviewed it before posting.

Unpivot builds a null test for each input member and skips the member
when the test is true. Two of those tests were wrong.

When the value field's type is not a class, such as int, a member that
is not Nullable<T> got the test o => true. So an int field or property
was always skipped, although an int? field was emitted. For an input
member that is not Nullable<T>, the test now returns true only if the
member has a reference type and its value is null. It compares the
reference with null, so a user-defined == operator on the member's type
cannot make a value look null. The same null test
handles reference-type input members when the value field has an
interface type. For example, if the
value field's type is IComparable and the input member is a string, a
null string is still skipped.

For an int? property, the test was IsTrue(HasValue), where the field
branch uses IsFalse(HasValue). A property with a value was skipped, and
a null one threw InvalidOperationException from Value. It now uses
IsFalse, as the field branch does.

UnpivotNullTests runs eight tests, in row mode and with the default
settings. Before this change seven of them fail in both; the int?
fields case already worked. After it, all pass.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unpivot skips int input fields and handles int? properties the wrong way round

1 participant