-
Notifications
You must be signed in to change notification settings - Fork 24
feature: add selection, filtering, primitive comparison, formatting and limited casting #257
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mobiusklein
wants to merge
9
commits into
apache:main
Choose a base branch
from
mobiusklein:feature/add-operations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0ab5908
feature: add selection, filtering, primitive comparison, formatting a…
mobiusklein ea299d7
chore: add file header license
mobiusklein a66ad19
feature: add builders with capacity, IEnumerable impls
mobiusklein 9ab12a2
feature: refactor to use vector bits
mobiusklein 8f14079
feature: rework more numerical casts
mobiusklein 33e651a
chore: use ArgumentException
mobiusklein 3b1ed32
feature: add sql-like null handling option in comparisons
mobiusklein ebd1afb
chore: reorder import
mobiusklein 950dc55
change: start handling review feedback
mobiusklein File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/Apache.Arrow.Operations/Apache.Arrow.Operations.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <TargetFrameworks>net8.0</TargetFrameworks> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
|
Contributor
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. The other projects don't enable |
||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\Apache.Arrow\Apache.Arrow.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,305 @@ | ||
| // 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.Runtime.Intrinsics; | ||
|
|
||
| using Apache.Arrow.Memory; | ||
|
|
||
| namespace Apache.Arrow.Operations; | ||
|
|
||
| internal static class BitVectorOps | ||
| { | ||
| internal static ArrowBuffer AllOnes(int numBytes, MemoryAllocator? allocator = default) | ||
| { | ||
| var zeros = AllZeros(numBytes, allocator); | ||
| return OnesComplement(zeros, allocator); | ||
| } | ||
|
|
||
| internal static ArrowBuffer AllZeros(int numBytes, MemoryAllocator? allocator = default) | ||
| { | ||
| // Exploit that this uses new byte[...] to allocate the memory which necessarily | ||
| // zeros out everything. | ||
| var builder = new ArrowBuffer.BitmapBuilder(numBytes * 8); | ||
| builder.Set(numBytes * 8 - 1, false); | ||
| return builder.Build(allocator); | ||
| } | ||
|
|
||
| internal static ArrowBuffer OnesComplement(ArrowBuffer buffer, MemoryAllocator? allocator = default) | ||
| { | ||
| var builder = new ArrowBuffer.BitmapBuilder(buffer.Length * 8); | ||
| var store = builder.Span; | ||
| int offset = 0; | ||
| int size = buffer.Span.Length; | ||
|
|
||
| if (Vector512.IsHardwareAccelerated) | ||
| { | ||
| while ((size - offset) >= 64) | ||
| { | ||
| var part = buffer.Span.Slice(offset, 64); | ||
| Vector512<byte> vector = Vector512.Create(part); | ||
| vector = Vector512.OnesComplement(vector); | ||
| vector.CopyTo(store.Slice(offset, 64)); | ||
| offset += 64; | ||
| } | ||
| } | ||
| if (Vector256.IsHardwareAccelerated) | ||
| { | ||
| while ((size - offset) >= 32) | ||
| { | ||
| var part = buffer.Span.Slice(offset, 32); | ||
| Vector256<byte> vector = Vector256.Create(part); | ||
| vector = Vector256.OnesComplement(vector); | ||
| vector.CopyTo(store.Slice(offset, 32)); | ||
| offset += 32; | ||
| } | ||
| } | ||
| while ((size - offset) >= 16) | ||
| { | ||
| var part = buffer.Span.Slice(offset, 16); | ||
| Vector128<byte> vector = Vector128.Create(part); | ||
| vector = Vector128.OnesComplement(vector); | ||
| vector.CopyTo(store.Slice(offset, 16)); | ||
| offset += 16; | ||
| } | ||
| while ((size - offset) >= 8) | ||
| { | ||
| var part = buffer.Span.Slice(offset, 8); | ||
| Vector64<byte> vector = Vector64.Create(part); | ||
| vector = Vector64.OnesComplement(vector); | ||
| vector.CopyTo(store.Slice(offset, 8)); | ||
| offset += 8; | ||
| } | ||
| for (var i = offset; i < size; i++) | ||
| { | ||
| store[i] = (byte)~buffer.Span[i]; | ||
| } | ||
| return builder.Build(allocator); | ||
| } | ||
|
|
||
| internal static ArrowBuffer And(ArrowBuffer lhs, ArrowBuffer rhs, MemoryAllocator? allocator = default) | ||
| { | ||
| if (lhs.IsEmpty) | ||
| { | ||
| if (rhs.IsEmpty) | ||
| { | ||
| return ArrowBuffer.Empty; | ||
| } | ||
| else | ||
| { | ||
| return rhs; | ||
| } | ||
| } | ||
| else if (rhs.IsEmpty) return lhs; | ||
|
|
||
| var builder = new ArrowBuffer.BitmapBuilder(lhs.Length * 8); | ||
| var store = builder.Span; | ||
| int offset = 0; | ||
| int size = lhs.Span.Length; | ||
|
|
||
| if (Vector512.IsHardwareAccelerated) | ||
| { | ||
| while ((size - offset) >= 64) | ||
| { | ||
| var part = lhs.Span.Slice(offset, 64); | ||
| Vector512<byte> vlhs = Vector512.Create(part); | ||
| part = rhs.Span.Slice(offset, 64); | ||
| Vector512<byte> vrhs = Vector512.Create(part); | ||
| vlhs = vlhs & vrhs; | ||
| vlhs.CopyTo(store.Slice(offset, 64)); | ||
| offset += 64; | ||
| } | ||
| } | ||
| if (Vector256.IsHardwareAccelerated) | ||
| { | ||
| while ((size - offset) >= 32) | ||
| { | ||
| var part = lhs.Span.Slice(offset, 32); | ||
| Vector256<byte> vlhs = Vector256.Create(part); | ||
| part = rhs.Span.Slice(offset, 32); | ||
| Vector256<byte> vrhs = Vector256.Create(part); | ||
| vlhs = vlhs & vrhs; | ||
| vlhs.CopyTo(store.Slice(offset, 32)); | ||
| offset += 32; | ||
| } | ||
| } | ||
| while ((size - offset) >= 16) | ||
| { | ||
| var part = lhs.Span.Slice(offset, 16); | ||
| Vector128<byte> vlhs = Vector128.Create(part); | ||
| part = rhs.Span.Slice(offset, 16); | ||
| Vector128<byte> vrhs = Vector128.Create(part); | ||
| vlhs = vlhs & vrhs; | ||
| vlhs.CopyTo(store.Slice(offset, 16)); | ||
| offset += 16; | ||
| } | ||
| while ((size - offset) >= 8) | ||
| { | ||
| var part = lhs.Span.Slice(offset, 8); | ||
| Vector64<byte> vlhs = Vector64.Create(part); | ||
| part = rhs.Span.Slice(offset, 8); | ||
| Vector64<byte> vrhs = Vector64.Create(part); | ||
| vlhs = vlhs & vrhs; | ||
| vlhs.CopyTo(store.Slice(offset, 8)); | ||
| offset += 8; | ||
| } | ||
| for (var i = offset; i < size; i++) | ||
| { | ||
| store[i] = (byte)(lhs.Span[i] & rhs.Span[i]); | ||
| } | ||
| return builder.Build(allocator); | ||
| } | ||
|
|
||
| internal static ArrowBuffer Or(ArrowBuffer lhs, ArrowBuffer rhs, MemoryAllocator? allocator = default) | ||
| { | ||
| if (lhs.IsEmpty) | ||
| { | ||
| return lhs; | ||
| } | ||
| else if (rhs.IsEmpty) return rhs; | ||
|
|
||
| var builder = new ArrowBuffer.BitmapBuilder(lhs.Length * 8); | ||
| var store = builder.Span; | ||
| int offset = 0; | ||
| int size = lhs.Span.Length; | ||
|
|
||
| if (Vector512.IsHardwareAccelerated) | ||
| { | ||
| while ((size - offset) >= 64) | ||
| { | ||
| var part = lhs.Span.Slice(offset, 64); | ||
| Vector512<byte> vlhs = Vector512.Create(part); | ||
| part = rhs.Span.Slice(offset, 64); | ||
| Vector512<byte> vrhs = Vector512.Create(part); | ||
| vlhs = vlhs | vrhs; | ||
| vlhs.CopyTo(store.Slice(offset, 64)); | ||
| offset += 64; | ||
| } | ||
| } | ||
| if (Vector256.IsHardwareAccelerated) | ||
| { | ||
| while ((size - offset) >= 32) | ||
| { | ||
| var part = lhs.Span.Slice(offset, 32); | ||
| Vector256<byte> vlhs = Vector256.Create(part); | ||
| part = rhs.Span.Slice(offset, 32); | ||
| Vector256<byte> vrhs = Vector256.Create(part); | ||
| vlhs = vlhs | vrhs; | ||
| vlhs.CopyTo(store.Slice(offset, 32)); | ||
| offset += 32; | ||
| } | ||
| } | ||
| while ((size - offset) >= 16) | ||
| { | ||
| var part = lhs.Span.Slice(offset, 16); | ||
| Vector128<byte> vlhs = Vector128.Create(part); | ||
| part = rhs.Span.Slice(offset, 16); | ||
| Vector128<byte> vrhs = Vector128.Create(part); | ||
| vlhs = vlhs | vrhs; | ||
| vlhs.CopyTo(store.Slice(offset, 16)); | ||
| offset += 16; | ||
| } | ||
| while ((size - offset) >= 8) | ||
| { | ||
| var part = lhs.Span.Slice(offset, 8); | ||
| Vector64<byte> vlhs = Vector64.Create(part); | ||
| part = rhs.Span.Slice(offset, 8); | ||
| Vector64<byte> vrhs = Vector64.Create(part); | ||
| vlhs = vlhs | vrhs; | ||
| vlhs.CopyTo(store.Slice(offset, 8)); | ||
| offset += 8; | ||
| } | ||
| for (var i = offset; i < size; i++) | ||
| { | ||
| store[i] = (byte)(lhs.Span[i] | rhs.Span[i]); | ||
| } | ||
| return builder.Build(allocator); | ||
| } | ||
|
|
||
| internal static ArrowBuffer Xor(ArrowBuffer lhs, ArrowBuffer rhs, MemoryAllocator? allocator = default) | ||
| { | ||
| if (lhs.IsEmpty) | ||
| { | ||
| if (rhs.IsEmpty) | ||
| { | ||
| return ArrowBuffer.Empty; | ||
| } | ||
| else | ||
| { | ||
| return OnesComplement(rhs, allocator); | ||
| } | ||
| } | ||
| else if (rhs.IsEmpty) | ||
| { | ||
| return OnesComplement(lhs, allocator); | ||
| } | ||
| var builder = new ArrowBuffer.BitmapBuilder(lhs.Length * 8); | ||
| var store = builder.Span; | ||
| int offset = 0; | ||
| int size = lhs.Span.Length; | ||
|
|
||
| if (Vector512.IsHardwareAccelerated) | ||
| { | ||
| while ((size - offset) >= 64) | ||
| { | ||
| var part = lhs.Span.Slice(offset, 64); | ||
| Vector512<byte> vlhs = Vector512.Create(part); | ||
| part = rhs.Span.Slice(offset, 64); | ||
| Vector512<byte> vrhs = Vector512.Create(part); | ||
| vlhs = vlhs ^ vrhs; | ||
| vlhs.CopyTo(store.Slice(offset, 64)); | ||
| offset += 64; | ||
| } | ||
| } | ||
| if (Vector256.IsHardwareAccelerated) | ||
| { | ||
| while ((size - offset) >= 32) | ||
| { | ||
| var part = lhs.Span.Slice(offset, 32); | ||
| Vector256<byte> vlhs = Vector256.Create(part); | ||
| part = rhs.Span.Slice(offset, 32); | ||
| Vector256<byte> vrhs = Vector256.Create(part); | ||
| vlhs = vlhs ^ vrhs; | ||
| vlhs.CopyTo(store.Slice(offset, 32)); | ||
| offset += 32; | ||
| } | ||
| } | ||
| while ((size - offset) >= 16) | ||
| { | ||
| var part = lhs.Span.Slice(offset, 16); | ||
| Vector128<byte> vlhs = Vector128.Create(part); | ||
| part = rhs.Span.Slice(offset, 16); | ||
| Vector128<byte> vrhs = Vector128.Create(part); | ||
| vlhs = vlhs ^ vrhs; | ||
| vlhs.CopyTo(store.Slice(offset, 16)); | ||
| offset += 16; | ||
| } | ||
| while ((size - offset) >= 8) | ||
| { | ||
| var part = lhs.Span.Slice(offset, 8); | ||
| Vector64<byte> vlhs = Vector64.Create(part); | ||
| part = rhs.Span.Slice(offset, 8); | ||
| Vector64<byte> vrhs = Vector64.Create(part); | ||
| vlhs = vlhs ^ vrhs; | ||
| vlhs.CopyTo(store.Slice(offset, 8)); | ||
| offset += 8; | ||
| } | ||
|
|
||
| for (var i = offset; i < size; i++) | ||
| { | ||
| store[i] = (byte)(lhs.Span[i] ^ rhs.Span[i]); | ||
| } | ||
| return builder.Build(allocator); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can we target netstandard2.0 also or do you expect it to be hard to support?
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.
Okay, we obviously wouldn't be able to use the
INumber-based methods and would have to replace them with individual implementations by type. That could perhaps be done another time.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.
DataFrame from the Microsoft.Data.Analysis project could serve as an example. They still have a T4 macro for netstandard2.0, but switched to
INumber-based methods for other target frameworks.https://github.com/dotnet/machinelearning/blob/main/src/Microsoft.Data.Analysis/Computations/Arithmetic.netstandard.tt