Skip to content

Instantly share code, notes, and snippets.

@westonpace
Created March 29, 2023 17:57
Show Gist options
  • Save westonpace/caa76e0c2aa461505cf4c77c645f4147 to your computer and use it in GitHub Desktop.
Save westonpace/caa76e0c2aa461505cf4c77c645f4147 to your computer and use it in GitHub Desktop.
Example of creating a StructArray
// 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 Apache.Arrow.Ipc;
using Apache.Arrow.Types;
using System.Collections.Generic;
using System.IO;
using Xunit;
namespace Apache.Arrow.Tests
{
public class StructArrayTests
{
public class Point
{
public double X { get; set; }
public double Y { get; set; }
}
public class ScoredPoint
{
public Point Location { get; set; }
public int Score { get; set; }
}
[Fact]
public void Example()
{
Field.Builder fieldBuilder = new Field.Builder();
List<Field> pointFields = new List<Field>();
pointFields.Add(fieldBuilder.Name("x").DataType(DoubleType.Default).Nullable(true).Build());
pointFields.Add(fieldBuilder.Name("y").DataType(DoubleType.Default).Nullable(true).Build());
StructType pointType = new StructType(pointFields);
List<Field> scoredPointFields = new List<Field>();
scoredPointFields.Add(fieldBuilder.Name("location").DataType(pointType).Nullable(true).Build());
scoredPointFields.Add(fieldBuilder.Name("score").DataType(Int32Type.Default).Nullable(true).Build());
StructType scoredPointType = new StructType(scoredPointFields);
List<ScoredPoint> inputData = new List<ScoredPoint> {
new ScoredPoint {
Location = new Point { X = 0, Y = 0 }, Score = 100
},
new ScoredPoint {
Location = new Point { X = 10, Y = 5 }, Score = 10
}
};
int numRows = inputData.Count;
// Skipping nulls for this example
int numNulls = 0;
// Normally you woudln't need to manually set the offset because you'd be using a
// StructArray.Builder but it seems a StructArray.Builder doesn't exist so we will
// create the array directly. We can always use 0 for offset (this gets set when
// an array is created by slicing)
int offset = 0;
// In a struct array, each field is its own array.
DoubleArray.Builder xBuilder = new DoubleArray.Builder();
DoubleArray.Builder yBuilder = new DoubleArray.Builder();
Int32Array.Builder scoreBuilder = new Int32Array.Builder();
// I can't figure out how to specify null for the validity bitmap so we will just
// create one that is "all-valid" since we don't have any nulls for this example.
ArrowBuffer.BitmapBuilder nullBitmap = new ArrowBuffer.BitmapBuilder();
// Converting from row-major to column-major
foreach (var scoredPoint in inputData)
{
xBuilder.Append(scoredPoint.Location.X);
yBuilder.Append(scoredPoint.Location.Y);
scoreBuilder.Append(scoredPoint.Score);
nullBitmap.Append(true);
}
ArrowBuffer nullBuffer = nullBitmap.Build();
// Order matters here, it needs to match the order specified when we created the type (e.g. pointType)
List<Array> pointArrays = new List<Array> { xBuilder.Build(), yBuilder.Build() };
StructArray points = new StructArray(pointType, numRows, pointArrays, nullBuffer, numNulls, offset);
List<Array> scoredPointArrays = new List<Array> { points, scoreBuilder.Build() };
// We can reuse the null buffer
StructArray scoredPoints = new StructArray(scoredPointType, numRows, scoredPointArrays, nullBuffer, numNulls, offset);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment