[C++] Choose the Correct Value in ArrayBuilder::AppendEmptyValue() for Floating-Point Types
Describe the enhancement requested
ArrayBuilder::AppendEmptyValue() was introduced and implemented in ARROW-25393 to write an arbitrary, well-defined value during StructBuilder::AppendNull(), as discussed here. In that context, the value written to the child builder is irrelevant because the parent entry is null.
At that time, RunEndEncodedBuilder did not yet exist; it was implemented about three years later in ARROW-32688. As a result, the current implementation does not appear to consider that the value written by AppendEmptyValue() may later become part of the value stream observed by RunEndEncodedBuilder.
this can be problematic for RunEndEncodedBuilder with floating-point values, as it can produce a zero value, which cannot be considered an empty value (Case 1). Even worse, it can be mistakenly confused with actual floating-point values (see the example below).
Case 1
auto ree_type = run_end_encoded(int32(), float32());
auto int32_builder = std::make_shared<Int32Builder>(pool_);
auto float_builder = std::make_shared<FloatBuilder>(pool_);
RunEndEncodedBuilder builder(pool_, int32_builder, float_builder, ree_type);
ASSERT_OK(builder.AppendEmptyValues(3));
ASSERT_OK(builder.AppendScalar(**MakeScalar(float32(), 3), 3));
ASSERT_OK_AND_ASSIGN(auto array, builder.Finish());
ARROW_LOGGER_INFO("", array->ToString());
Output:
-- run_ends:
[
3,
6
]
-- values:
[
0,
3
]
Case 2
auto ree_type = run_end_encoded(int32(), float32());
auto int32_builder = std::make_shared<Int32Builder>(pool_);
auto float_builder = std::make_shared<FloatBuilder>(pool_);
RunEndEncodedBuilder builder(pool_, int32_builder, float_builder, ree_type);
ASSERT_OK(builder.AppendEmptyValues(3));
ASSERT_OK(builder.AppendScalar(**MakeScalar(float32(), 0), 3));
ASSERT_OK_AND_ASSIGN(auto array, builder.Finish());
ARROW_LOGGER_INFO("", array->ToString());
Output:
-- run_ends:
[
3,
6
]
-- values:
[
0,
0
]
One possible solution would be to use NaN as the placeholder value for floating-point builders instead of 0.0f.
Component(s)
C++
take
@goel-skd, thanks for your interest and contribution. I was planning to work on this issue myself, so would you mind picking up another issue instead? I really appreciate your help.
By the way, this is just a proposal and it needs to be reviewed and verified by an Arrow core developer.
Sure, please re-assign😊
@pitrou, would it be useful to change the default empty value for floatTypes?
this can be problematic for RunEndEncodedBuilder with floating-point values, as it can produce a zero value, which cannot be considered an empty value (Case 1).
This is the same behavior as other numeric types, so I don't understand the problem.
Even worse, it can be mistakenly confused with actual floating-point values (see the example below).
That would be a bug in RunEndEncodedBuilder::AppendEmptyValues then? Let's make sure that "empty" values don't get compressed with a run of non-empty values.
One possible solution would be to use NaN as the placeholder value for floating-point builders instead of 0.0f.
That would only fix the issue until https://github.com/apache/arrow/issues/50517 is fixed, right?
One possible solution would be to use NaN as the placeholder value for floating-point builders instead of 0.0f.
That would only fix the issue until #50517 is fixed, right?
Yes, that's correct. However, at this stage, I would like to know whether this suggestion is useful as a possible solution or direction.
Even worse, it can be mistakenly confused with actual floating-point values (see the example below).
That would be a bug in
RunEndEncodedBuilder::AppendEmptyValuesthen? Let's make sure that "empty" values don't get compressed with a run of non-empty values.
Sorry, I think I conveyed my suggestion incorrectly. The current logic in RunEndEncodedBuilder already prevents merging AppendEmptyValues with non-empty values. The problem is that the value produced by AppendEmptyValues is indistinguishable from a legitimate value with the same value. For example, both an empty value and an explicitly appended 0.0 are represented as 0.0 in different consecutive runs, so the run-end value stream alone cannot tell whether a particular value came from AppendEmptyValues or from a normal append.
For example:
TEST(TestRunEndEncodedBuilder, AppendScalar) {
auto ree_type = run_end_encoded(int32(), float32());
auto run_end_builder = std::make_shared<Int32Builder>();
auto value_builder = std::make_shared<FloatBuilder>();
RunEndEncodedBuilder ree_builder(default_memory_pool(), run_end_builder, value_builder,
ree_type);
ASSERT_OK(ree_builder.AppendEmptyValues(10));
ASSERT_OK(ree_builder.AppendScalar(**MakeScalar(float32(), 0), 10));
ASSERT_OK_AND_ASSIGN(auto result, ree_builder.Finish());
ARROW_LOGGER_INFO("", result->ToString());
}
This produces:
-- run_ends:
[
10,
20
]
-- values:
[
0,
0
]
The two runs are kept separate, but looking at the values alone, there is no way to distinguish the first 0 produced by AppendEmptyValues from the second 0 produced by AppendScalar.
this can be problematic for RunEndEncodedBuilder with floating-point values, as it can produce a zero value, which cannot be considered an empty value (Case 1).
This is the same behavior as other numeric types, so I don't understand the problem.
Integer types do not have NaN, so there is no analogous special value that could be used as a placeholder, and 0 is currently used. Floating-point types, however, do have NaN, which could potentially serve as a distinguishable placeholder.
As I mentioned above, AppendEmptyValue() was originally introduced for StructBuilder::AppendNull(), where the parent validity bitmap marks the value as null, so the value written to the child builder does not matter. In RunEndEncodedBuilder::AppendEmptyValue(), however, no validity bitmap is set for the value, so it is important to write a value that can distinguish an empty value from a regular value. For floating-point types, NaN could potentially serve this purpose.
The two runs are kept separate, but looking at the values alone, there is no way to distinguish the first
0produced byAppendEmptyValuesfrom the second0produced byAppendScalar.
Sure, but as I said above I think it's just a bug in RunEndEncodedBuilder. It should keep track of whether the last value was "empty" or not.