You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@arrow.apache.org by Sergii Mikhtoniuk <sm...@kamu.dev> on 2021/12/03 22:38:19 UTC
Hashing and equivalence of datasets
Hi,
I'm working on a data processing tool that guarantees reproducibility /
determinism of operations. It's a frequent task for me to verify that one
dataset (Table) is equivalent to another.
I didn't find any functions related to computing hash sums in Arrow, but
wondering if anyone knows existing implementations?
If I were to implement a hashing over chunked arrays myself, does Arrow
guarantee that any sort of padding between aligned values is zeroed-out, so
that hashes are perfectly stable?
Bonus question: Has anyone seen hashing algorithms for tabular data that
can check for equivalence (rather than equality)? i.e. I consider datasets
equivalent if they contain the same set of records, but not necessarily in
the same order.
Thank you!
- Sergii
Re: Hashing and equivalence of datasets
Posted by Sergii Mikhtoniuk <sm...@kamu.dev>.
In case someone stumbles upon this thread in future - I decided to continue
using logical hashing for now.
I've created a new Rust crate `arrow-digest` [1] that implements the stable
hashing for Arrow arrays and record batches and tries hard to hide the
encoding-related differences. The crate's README describes the hashing
algorithm if someone finds it useful and wants to implement it in another
language.
I'll continue to expand the set of supported types as I'm integrating it
into the decentralized data processing tool I'm working on [2]. Any
feedback on the algorithm would be much appreciated!
In the long term, I'm not sure logical hashing is the best way forward - a
subset of Parquet that makes some efficiency sacrifices just to make file
layout deterministic might be a better choice for content-addressability.
[1] https://github.com/sergiimk/arrow-digest
[2] https://github.com/kamu-data/kamu-cli
Cheers,
Sergii
Re: Hashing and equivalence of datasets
Posted by Jorge Cardoso Leitão <jo...@gmail.com>.
Hi,
I think that unfortunately parquet is underdetermined, for example,
RLE-hybrid encoding: whether to use a RLE or bitpacked run in RLE-hybrid
encoding is left for implementations to decide: an implementation may only
use bitpacked runs, while other may use a combination. This leads to
different binaries, although with equal semantics. RLE hybrid-encoding is
commonly used to store validities / definition levels and repetition
levels, so it is likely that you hit it in normal uses of parquet.
Best,
Jorge
On Sat, Dec 4, 2021 at 9:44 PM Sergii Mikhtoniuk <sm...@kamu.dev>
wrote:
> Thanks everyone for suggestions,
>
> Hashing a _serialized_ representation is what I'm doing now (CSV, yuck),
> but no format is strict enough it seems to guarantee hash stability - my
> hashes may differ depending on implementation or even drift with a new
> version of the same library.
>
> Arrow standardizes the exact memory layout of data making it already more
> strict than any serialization format, so that's why I was hoping to use it
> directly (which looks very doable, but a bit more work than I hoped).
>
> Another possibility I've been asking around in the Parquet community is
> whether I can achieve stable hashing with it. Assuming I:
> - Sort all records
> - Disable statistics
> - Write all data in a single row group
> ... would that yield a reproducible binary representation?
>
> Parquet spec is very vague. From what I could gather it doesn't produce
> value gaps [1, 2, ?, 4] for nullable data, but I could not confirm if there
> are any other sources of non-determinism in there.
>
> Cheers,
> Sergii
>
Re: Hashing and equivalence of datasets
Posted by Sergii Mikhtoniuk <sm...@kamu.dev>.
Thanks everyone for suggestions,
Hashing a _serialized_ representation is what I'm doing now (CSV, yuck),
but no format is strict enough it seems to guarantee hash stability - my
hashes may differ depending on implementation or even drift with a new
version of the same library.
Arrow standardizes the exact memory layout of data making it already more
strict than any serialization format, so that's why I was hoping to use it
directly (which looks very doable, but a bit more work than I hoped).
Another possibility I've been asking around in the Parquet community is
whether I can achieve stable hashing with it. Assuming I:
- Sort all records
- Disable statistics
- Write all data in a single row group
... would that yield a reproducible binary representation?
Parquet spec is very vague. From what I could gather it doesn't produce
value gaps [1, 2, ?, 4] for nullable data, but I could not confirm if there
are any other sources of non-determinism in there.
Cheers,
Sergii
Re: Hashing and equivalence of datasets
Posted by Marnix van den Broek <ma...@bundlesandbatches.io>.
You could also let Arrow handle these cases by hashing a serialized
RecordBatch of your data selection. I'm not sure if it yields the exact
same bytesequence in every implementation, though. I would expect that to
be the case, but haven't tested it yet.
Best,
Marnix
On Sat, Dec 4, 2021 at 7:02 AM Jorge Cardoso Leitão <
jorgecarleitao@gmail.com> wrote:
> AFAIK hashing in this context needs to be done on a slot by slot basis,
> just like array equality, as any item on a null slot has a value on the
> buffer that is undetermined.
>
> E.g. the layout of a primitive array [1, 2, None, 4] is two buffer
> regions:
> * [1, 2, ?, 4] and
> * [true, true, false, true] (in bitmap)
>
> The question mark can be any number. Hashing needs to skip the "?", which
> is achieved by iterating over [(1, true), (2, true), (?, false), (4, true)]
> and using a unique hash for the false case (representing the None)
>
> Best,
> Jorge
>
>
>
> On Sat, Dec 4, 2021 at 6:26 AM Weston Pace <we...@gmail.com> wrote:
>
>> One possibility could be to calculate the hash of the logical data
>> when serializing and then put the hash in the metadata.
>>
>> > I'm not even sure this can actually happen ... After all buffers should
>> only carry primitive types (not some complex structs) and they all seem to
>> be 16/32/64/128 bit long and should produce "gapless" buffers.
>>
>> Arrow buffers are aligned on 8 or 64 byte boundaries and there is a
>> preference to align on 64 byte boundaries. So I think gaps/padding is
>> a real possibility.
>>
>> On Fri, Dec 3, 2021 at 3:05 PM Sergii Mikhtoniuk <sm...@kamu.dev>
>> wrote:
>> >
>> > Apologies for the confusion, I was using wrong terminology. When I was
>> talking about "array chunks" I meant Buffers - contiguous slices of memory
>> with nullability, offsets, or value data.
>> >
>> > If Arrow is not explicit about Buffers having to be memset to zero
>> before use - whenever the size of the vale is not a multiple of its
>> alignment we would have garbage in between, messing up the stability of a
>> buffer-wise hash.
>> >
>> > I'm not even sure this can actually happen ... After all buffers should
>> only carry primitive types (not some complex structs) and they all seem to
>> be 16/32/64/128 bit long and should produce "gapless" buffers.
>>
>
Re: Hashing and equivalence of datasets
Posted by Jorge Cardoso Leitão <jo...@gmail.com>.
AFAIK hashing in this context needs to be done on a slot by slot basis,
just like array equality, as any item on a null slot has a value on the
buffer that is undetermined.
E.g. the layout of a primitive array [1, 2, None, 4] is two buffer regions:
* [1, 2, ?, 4] and
* [true, true, false, true] (in bitmap)
The question mark can be any number. Hashing needs to skip the "?", which
is achieved by iterating over [(1, true), (2, true), (?, false), (4, true)]
and using a unique hash for the false case (representing the None)
Best,
Jorge
On Sat, Dec 4, 2021 at 6:26 AM Weston Pace <we...@gmail.com> wrote:
> One possibility could be to calculate the hash of the logical data
> when serializing and then put the hash in the metadata.
>
> > I'm not even sure this can actually happen ... After all buffers should
> only carry primitive types (not some complex structs) and they all seem to
> be 16/32/64/128 bit long and should produce "gapless" buffers.
>
> Arrow buffers are aligned on 8 or 64 byte boundaries and there is a
> preference to align on 64 byte boundaries. So I think gaps/padding is
> a real possibility.
>
> On Fri, Dec 3, 2021 at 3:05 PM Sergii Mikhtoniuk <sm...@kamu.dev>
> wrote:
> >
> > Apologies for the confusion, I was using wrong terminology. When I was
> talking about "array chunks" I meant Buffers - contiguous slices of memory
> with nullability, offsets, or value data.
> >
> > If Arrow is not explicit about Buffers having to be memset to zero
> before use - whenever the size of the vale is not a multiple of its
> alignment we would have garbage in between, messing up the stability of a
> buffer-wise hash.
> >
> > I'm not even sure this can actually happen ... After all buffers should
> only carry primitive types (not some complex structs) and they all seem to
> be 16/32/64/128 bit long and should produce "gapless" buffers.
>
Re: Hashing and equivalence of datasets
Posted by Weston Pace <we...@gmail.com>.
One possibility could be to calculate the hash of the logical data
when serializing and then put the hash in the metadata.
> I'm not even sure this can actually happen ... After all buffers should only carry primitive types (not some complex structs) and they all seem to be 16/32/64/128 bit long and should produce "gapless" buffers.
Arrow buffers are aligned on 8 or 64 byte boundaries and there is a
preference to align on 64 byte boundaries. So I think gaps/padding is
a real possibility.
On Fri, Dec 3, 2021 at 3:05 PM Sergii Mikhtoniuk <sm...@kamu.dev> wrote:
>
> Apologies for the confusion, I was using wrong terminology. When I was talking about "array chunks" I meant Buffers - contiguous slices of memory with nullability, offsets, or value data.
>
> If Arrow is not explicit about Buffers having to be memset to zero before use - whenever the size of the vale is not a multiple of its alignment we would have garbage in between, messing up the stability of a buffer-wise hash.
>
> I'm not even sure this can actually happen ... After all buffers should only carry primitive types (not some complex structs) and they all seem to be 16/32/64/128 bit long and should produce "gapless" buffers.
Re: Hashing and equivalence of datasets
Posted by Sergii Mikhtoniuk <sm...@kamu.dev>.
Apologies for the confusion, I was using wrong terminology. When I was
talking about "array chunks" I meant Buffers - contiguous slices of memory
with nullability, offsets, or value data.
If Arrow is not explicit about Buffers having to be memset to zero before
use - whenever the size of the vale is not a multiple of its alignment we
would have garbage in between, messing up the stability of a buffer-wise
hash.
I'm not even sure this can actually happen ... After all buffers should
only carry primitive types (not some complex structs) and they all seem to
be 16/32/64/128 bit long and should produce "gapless" buffers.
Re: Hashing and equivalence of datasets
Posted by Niranda Perera <ni...@gmail.com>.
Ah! I think I understand your usecase now. I don't think it would be that
trivial to feed blocks of memory to sha-256. Arrow Arrays consist of
multiple buffers (validity, data, offset) and the number of buffers depends
on the data type. AFAIU there's no guarantee about how these buffers are
located in physical memory. So, two identical tables A, B could have
different memory footprints in the physical memory.
Having said that, how about comparing sha-256 buffer-wise? (there could be
some issues if you have null-values)
On Fri, Dec 3, 2021 at 7:11 PM Sergii Mikhtoniuk <sm...@kamu.dev>
wrote:
> Thank you for your response, Niranda.
>
> Re. padding what I had in mind is accessing individual blocks of memory
> (array chunks) and feeding them directly into sha-256. For an equality
> check it would not matter that I'm hashing column-wise instead of row-wise.
> But if there can be gaps between individual values in a chunk and if they
> can be filled with uninitialized memory - I'd have no choice but to
> traverse and hash one value at a time. Equivalence would of course require
> a different approach.
>
> Using `set difference` would not work in my case as I want two parties to
> be able to check if their datasets are identical without needing to
> transfer or disclose data. Computing hashes and only comparing those seems
> like the only way.
>
> I wonder if there would be interest in generalizing ARROW-8991 to support
> multiple hashing algorithms.
>
> I was surprised to see how little hashing of structured data comes up on
> the web. Storing datasets in content-addressable systems like IPFS may in
> future make Parquet a poor format choice as it adds a lot of "entropy" into
> data that is hard to control and can produce different binary layout for
> the same dataset every time it's serialized.
>
> Best,
> - Sergii
>
--
Niranda Perera
https://niranda.dev/
@n1r44 <https://twitter.com/N1R44>
Re: Hashing and equivalence of datasets
Posted by Sergii Mikhtoniuk <sm...@kamu.dev>.
Thank you for your response, Niranda.
Re. padding what I had in mind is accessing individual blocks of memory
(array chunks) and feeding them directly into sha-256. For an equality
check it would not matter that I'm hashing column-wise instead of row-wise.
But if there can be gaps between individual values in a chunk and if they
can be filled with uninitialized memory - I'd have no choice but to
traverse and hash one value at a time. Equivalence would of course require
a different approach.
Using `set difference` would not work in my case as I want two parties to
be able to check if their datasets are identical without needing to
transfer or disclose data. Computing hashes and only comparing those seems
like the only way.
I wonder if there would be interest in generalizing ARROW-8991 to support
multiple hashing algorithms.
I was surprised to see how little hashing of structured data comes up on
the web. Storing datasets in content-addressable systems like IPFS may in
future make Parquet a poor format choice as it adds a lot of "entropy" into
data that is hard to control and can produce different binary layout for
the same dataset every time it's serialized.
Best,
- Sergii
Re: Hashing and equivalence of datasets
Posted by Niranda Perera <ni...@gmail.com>.
Hey Sergii,
AFAIK latest arrow compute dev work contains row-wise hash value
calculation (for hash aggregations, group-by, etc). But I don't think it is
exposed as a Compute API yet. This has been discussed here [1].
I have come across a similar requirement like yours in our project [2],
where we wanted to provide set operations such as (union, intersection,
set-difference, etc).
I didn't quite understand what you mean by 'padding between aligned
values'. Are you referring to the null-values in an array (i.e. whether
null values are zeroed out in the data buffers)? Since the arrow format is
columnar, you have to access i'th value of each array separately and create
a composite hash for each row. ATM you can not get a 'row-view' of a table.
As per equivalence, in Cylon we check `len(A.set_difference(B)) == 0`,
where A, B tables can have duplicated rows. set_difference, simply creates
a hash_table using A and queries each row of B against it.
[1] https://issues.apache.org/jira/browse/ARROW-8991
[2] https://github.com/cylondata/cylon
On Fri, Dec 3, 2021 at 5:38 PM Sergii Mikhtoniuk <sm...@kamu.dev>
wrote:
> Hi,
>
> I'm working on a data processing tool that guarantees reproducibility /
> determinism of operations. It's a frequent task for me to verify that one
> dataset (Table) is equivalent to another.
>
> I didn't find any functions related to computing hash sums in Arrow, but
> wondering if anyone knows existing implementations?
>
> If I were to implement a hashing over chunked arrays myself, does Arrow
> guarantee that any sort of padding between aligned values is zeroed-out, so
> that hashes are perfectly stable?
>
> Bonus question: Has anyone seen hashing algorithms for tabular data that
> can check for equivalence (rather than equality)? i.e. I consider datasets
> equivalent if they contain the same set of records, but not necessarily in
> the same order.
>
> Thank you!
> - Sergii
>
--
Niranda Perera
https://niranda.dev/
@n1r44 <https://twitter.com/N1R44>