Repository navigation
feat: Support SQL-NULL rows when shredding variant columns #445
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -66,6 +66,39 @@ public static (byte[] Metadata, IReadOnlyList<ShredResult> Rows) Shred( | |
| return (metadataBytes, results); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Shreds a column of nullable variant values. A <c>null</c> entry is a | ||
| /// SQL-NULL row, as opposed to <see cref="VariantValue.Null"/>, which is a | ||
| /// present variant null. SQL-NULL rows produce a <c>null</c> entry in the | ||
| /// returned rows, which <see cref="ShreddedVariantArrayBuilder.Build"/> turns | ||
| /// into a null element of the resulting array. | ||
| /// </summary> | ||
| public static (byte[] Metadata, IReadOnlyList<ShredResult> Rows) Shred( | ||
| IEnumerable<VariantValue?> values, | ||
| ShredSchema schema) | ||
|
Comment on lines
+76
to
+78
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keeping the overload. The only calls that become ambiguous are an untyped |
||
| { | ||
| if (values == null) throw new ArgumentNullException(nameof(values)); | ||
| if (schema == null) throw new ArgumentNullException(nameof(schema)); | ||
|
|
||
| List<VariantValue?> rows = values as List<VariantValue?> ?? new List<VariantValue?>(values); | ||
|
|
||
| VariantMetadataBuilder metadata = new VariantMetadataBuilder(); | ||
| foreach (VariantValue? row in rows) | ||
| { | ||
| if (row.HasValue) CollectFieldNames(row.Value, metadata); | ||
| } | ||
| byte[] metadataBytes = metadata.Build(out int[] idRemap); | ||
|
|
||
| ShredResult[] results = new ShredResult[rows.Count]; | ||
| for (int i = 0; i < rows.Count; i++) | ||
| { | ||
| VariantValue? row = rows[i]; | ||
| results[i] = row.HasValue ? Shred(row.Value, schema, metadata, idRemap) : null; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, fixed in 9155656. |
||
| } | ||
|
|
||
| return (metadataBytes, results); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Shreds a single variant value against a caller-managed metadata dictionary. | ||
| /// Use this when combining shredded columns with external metadata, or when | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| // 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. | ||
|
|
||
| using System.Collections.Generic; | ||
| using Apache.Arrow; | ||
| using Apache.Arrow.Operations.Shredding; | ||
| using Apache.Arrow.Scalars.Variant; | ||
| using Xunit; | ||
|
|
||
| namespace Apache.Arrow.Operations.Tests.Shredding | ||
| { | ||
| /// <summary> | ||
| /// Tests for SQL-NULL rows (a null <see cref="VariantValue"/>?) flowing through | ||
| /// inference, shredding and array assembly, and staying distinct from a present | ||
| /// <see cref="VariantValue.Null"/>. | ||
| /// </summary> | ||
| public class ShredNullRowTests | ||
| { | ||
| private static VariantValue Obj(int a) => VariantValue.FromObject( | ||
| new Dictionary<string, VariantValue> { ["a"] = VariantValue.FromInt32(a) }); | ||
|
|
||
| private static VariantArray ShredAndBuild(IReadOnlyList<VariantValue?> values, ShredSchema schema) | ||
| { | ||
| (byte[] metadata, IReadOnlyList<ShredResult> rows) = VariantShredder.Shred(values, schema); | ||
| return ShreddedVariantArrayBuilder.Build(schema, metadata, rows); | ||
| } | ||
|
|
||
| private static void AssertRows(IReadOnlyList<VariantValue?> expected, VariantArray array) | ||
| { | ||
| Assert.Equal(expected.Count, array.Length); | ||
| for (int i = 0; i < expected.Count; i++) | ||
| { | ||
| if (expected[i].HasValue) | ||
| { | ||
| Assert.False(array.IsNull(i), $"row {i} should be valid"); | ||
| Assert.Equal(expected[i].Value, array.GetLogicalVariantValue(i)); | ||
| } | ||
| else | ||
| { | ||
| Assert.True(array.IsNull(i), $"row {i} should be null"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SqlNullRow_IsDistinctFromVariantNull() | ||
| { | ||
| var values = new List<VariantValue?> { Obj(1), null, VariantValue.Null, Obj(3) }; | ||
|
|
||
| ShredSchema schema = new ShredSchemaInferer().Infer(values, ShredOptions.Default); | ||
| VariantArray array = ShredAndBuild(values, schema); | ||
|
|
||
| Assert.Equal(ShredType.Object, schema.TypedValueType); | ||
| Assert.Equal(1, array.NullCount); | ||
| Assert.Equal(1, array.StorageArray.NullCount); | ||
| AssertRows(values, array); | ||
| Assert.False(array.IsNull(2)); | ||
| Assert.True(array.GetLogicalVariantValue(2).IsNull); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SqlNullRow_Unshredded() | ||
| { | ||
| var values = new List<VariantValue?> { VariantValue.FromString("x"), null, VariantValue.FromInt32(5) }; | ||
| AssertRows(values, ShredAndBuild(values, ShredSchema.Unshredded())); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SqlNullRow_Primitive() | ||
| { | ||
| var values = new List<VariantValue?> { null, VariantValue.FromInt32(1), VariantValue.FromString("residual"), null }; | ||
| VariantArray array = ShredAndBuild(values, ShredSchema.Primitive(ShredType.Int32)); | ||
| Assert.Equal(2, array.NullCount); | ||
| AssertRows(values, array); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SqlNullRow_Array() | ||
| { | ||
| var values = new List<VariantValue?> | ||
| { | ||
| VariantValue.FromArray(VariantValue.FromInt32(1), VariantValue.FromInt32(2)), | ||
| null, | ||
| VariantValue.FromArray(VariantValue.FromInt32(3)), | ||
| }; | ||
| AssertRows(values, ShredAndBuild(values, ShredSchema.ForArray(ShredSchema.Primitive(ShredType.Int32)))); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void AllRowsNull() | ||
| { | ||
| var values = new List<VariantValue?> { null, null }; | ||
| ShredSchema schema = new ShredSchemaInferer().Infer(values); | ||
| Assert.Equal(ShredType.None, schema.TypedValueType); | ||
|
|
||
| VariantArray array = ShredAndBuild(values, schema); | ||
| Assert.Equal(2, array.NullCount); | ||
| AssertRows(values, array); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void NoNullRows_ProducesNoValidityBuffer() | ||
| { | ||
| var values = new List<VariantValue?> { Obj(1), Obj(2) }; | ||
| VariantArray array = ShredAndBuild(values, new ShredSchemaInferer().Infer(values)); | ||
| Assert.Equal(0, array.NullCount); | ||
| Assert.True(array.StorageArray.NullBitmapBuffer.IsEmpty); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SlicedArray_KeepsValidityAligned() | ||
| { | ||
| var values = new List<VariantValue?> { Obj(1), null, Obj(3), null, Obj(5) }; | ||
| VariantArray array = ShredAndBuild(values, new ShredSchemaInferer().Infer(values)); | ||
|
|
||
| var sliced = (VariantArray)ArrowArrayFactory.Slice(array, 1, 3); | ||
| AssertRows(new List<VariantValue?> { null, Obj(3), null }, sliced); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Infer_IgnoresNullRows() | ||
| { | ||
| // With nulls counted, Int32 would appear in only a third of the rows. | ||
| var values = new List<VariantValue?> { VariantValue.FromInt32(1), null, null, null, VariantValue.FromInt32(2), null }; | ||
| ShredSchema schema = new ShredSchemaInferer().Infer(values); | ||
| Assert.Equal(ShredType.Int32, schema.TypedValueType); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Shred_ReturnsNullEntryForNullRow() | ||
| { | ||
| var values = new List<VariantValue?> { Obj(1), null }; | ||
| (byte[] metadata, IReadOnlyList<ShredResult> rows) = VariantShredder.Shred(values, ShredSchema.Unshredded()); | ||
| Assert.NotNull(metadata); | ||
| Assert.NotNull(rows[0]); | ||
| Assert.Null(rows[1]); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Reconstruct_RoundTripsNullRows() | ||
| { | ||
| var values = new List<VariantValue?> { Obj(1), null, VariantValue.Null, VariantValue.FromString("s") }; | ||
| ShredSchema schema = new ShredSchemaInferer().Infer(values); | ||
| (byte[] metadata, IReadOnlyList<ShredResult> rows) = VariantShredder.Shred(values, schema); | ||
|
|
||
| for (int i = 0; i < values.Count; i++) | ||
| { | ||
| Assert.Equal(values[i], VariantUnshredder.Reconstruct(rows[i], schema, metadata)); | ||
| } | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keeping the overload. The only calls that become ambiguous are an untyped
nullordefaultliteral forvalues, and those calls could only ever throwArgumentNullException, so nothing that does useful work is affected. A null in a typed variable, arrays and collection expressions (including[v, null]) all still resolve correctly, and already-compiled callers stay bound to the original method.VariantArray.Builder.AppendRangealready uses the sameVariantValue/VariantValue?overload pair, so a separate name would be less consistent with the rest of the library. I've updated the PR description to explain this.