Skip to content

API: Compute truncate decimal result precision based on lowest value bound - #12969

Closed
lliangyu-lin wants to merge 4 commits into
apache:mainfrom
lliangyu-lin:truncate-decimal-result-type
Closed

lliangyu-lin wants to merge 4 commits into
apache:mainfrom
lliangyu-lin:truncate-decimal-result-type

Conversation

@lliangyu-lin

@lliangyu-lin lliangyu-lin commented May 4, 2025 •

Copy link
Copy Markdown
Contributor

Description

When creating a table with truncate decimal partition, the result of applying the truncate decimal may be outside the bound of decimal precision.
Ex:

  • CREATE TABLE prod.db.sample (a DECIMAL(5,4)) USING iceberg PARTITIONED BY (TRUNCATE(10,a)
    The edge cases are -9.9991 to -9.9999. These values are legal values for the column, and the truncated result for these values are -10.0000. Since partition field type is using the same as source type (in this case DECIMAL(5,4)), -10.0000 does not fit into DECIMAL(5,4) and causing write failures and leaving orphaned data files.
  • Truncate(9, Decimal(5,4)) = Decimal(5,4) because the lowest truncated value is the same as lowest value for Decimal(5,4)

The attempt fix is to compute the lowest possible truncated value (only lower bound will cause overflow), and use the max of the truncated value precision and the source type precision.

Fixes: #12915

@github-actions github-actions Bot added the API label May 4, 2025
@lliangyu-lin

Copy link
Copy Markdown
Contributor Author

Hi @amogh-jahagirdar @nastra, could you help take a look on the change, if this is something we should fix?


@Override
public Type getResultType(Type sourceType) {
if (sourceType.typeId() != Type.TypeID.DECIMAL) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we would use a Precondition here for the check

BigDecimal minTrunc = minInput.divide(unit, 0, RoundingMode.FLOOR).multiply(unit);
int finalPrecision = Math.max(minTrunc.precision(), precision);

return Types.DecimalType.of(finalPrecision, scale);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A question here, if we just always just add 1 to precision will be ok? Feels like that is a bit simpler. Not sure if we have any rough edges if we do that but it would make the code consistent rather than behaving differently in certain combos.

@RussellSpitzer RussellSpitzer May 14, 2025 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Either way this will require a spec change since we currently do not specify that the result type of "truncate" can be different)

Current Spec Details

Transform name Description Source types Result type
truncate[W] Value truncated to width W (see below) int, long, decimal, string, binary Source type
Type Config Truncate specification Examples
decimal W, width (no scale) scaled_W = decimal(W, scale(v)) v - (v % scaled_W) [1, 2] W=50, s=2: 10.65 → 10.50
The remainder, v % W, must be positive. For languages where % can produce negative values, the correct truncate function is: v - (((v % W) + W) % W)
The width, W, used to truncate decimal values is applied using the scale of the decimal column to avoid additional (and potentially conflicting) parameters.

@lliangyu-lin lliangyu-lin May 16, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RussellSpitzer Thank you for the reviews! There are some extreme cases where +1 on the precision is not enough as shown in the added unit tests. Although if we restrict the input, I think +1 precision will fix the issue.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wondering also as an alternative, is it worse if we just never allow the minimum truncated value to be outside of the type range? This is probably another spec change and I think precision changing is probably better (I don't think engines will actually break if we change precision unless we exceed it's inmemory type bounds ...) but I just want to explore this option.

So like if we defined

    Types.DecimalType sourceType = Types.DecimalType.of(5, 3);
    SerializableFunction<Object, Object> trunc1000 = Truncate.get(1000).bind(sourceType);

To produce values normally except when the outcome would be -100.000 and in that case we return -99.000?

The function looks a little messier there ... I'm trying to think if that's easier for engines to deal with than type changing, but my gut says no.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It sounds like a more general problem for me, for example over or underflows can happen for other types, like integer or long too. For example Truncate.get(10).bind(Types.IntegerType.get()).apply(Integer.MIN_VALUE) will underflow, instead of the expected -2147483650 it is going to be 2147483646. Here, underflow looks inevitable without changing the type, the result is outside of the 32 bit range.

It can also overflow, Truncate.get(Integer.MAX_VALUE).bind(Types.IntegerType.get()).apply(1) will result in 2 instead of 0. Note also, that the two expressions allowed by truncate behave differently in regard with overflow: v - (v % W) won't overflow, but v - (((v % W) + W) % W) can, therefore truncate can give different result in different languages. The over or underflow depends on both the value and the width.

What do you think, are these some extreme edge cases, or something possibly relevant?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I get wrong query results with truncation on int types. Similar issue #13105

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for raising the overflow / underflow issue for other types. Definitely feel like it's a general problem for truncate on numeric values. What do you think about if we just capping to lowest / highest possible values when detect there is underflow / overflow? Ex: Truncate.get(10).bind(Types.IntegerType.get()).apply(Integer.MIN_VALUE) = Integer.MIN_VALUE

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That feels like the solution to me. That will not mistakenly skip data files when pushing down the filters.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capping it might work, though for positive values, overflows are avoidable I think, when v - (v % W) is used instead of v - (((v % W) + W) % W) for positive values. Nevertheless, this sounds like a breaking change for me, probably better to discuss it with community first?


// Truncate unit = 10000.00, possible truncated outputs -10000.00, 0
// Needs 3 extra precision widening
SerializableFunction<Object, Object> trunc1000000 = Truncate.get(1000000).bind(sourceType);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be allowed? The truncate function is using a unit which can't be expressed in the source type?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel this should not be allowed, but currently iceberg doesn't make any validations to input. This validation probably needs to be done in table creation level and not during the truncate computation level.
@rdblue Do you think this should be a valid case that iceberg should handle?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mentioned this in my hypothetical above, but the alternative is to change the function so that the only allowed output with this kind of truncation would be 0. It's probably better though if we error out on people trying to create this transform though IMHO

@lliangyu-lin lliangyu-lin May 20, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me try to explore the option mentioned, to cap the underflow value to the lowest possible truncated value within the range. I definitely agree that having different precisions based on input isn't very consistent and elegant.

@github-actions

Copy link
Copy Markdown

This pull request has been marked as stale due to 30 days of inactivity. It will be closed in 1 week if no further activity occurs. If you think that’s incorrect or this pull request requires a review, please simply write any comment. If closed, you can revive the PR at any time and @mention a reviewer or discuss it on the [email protected] list. Thank you for your contributions.

@github-actions github-actions Bot added the stale label Jun 29, 2025
@github-actions

github-actions Bot commented Jul 7, 2025

Copy link
Copy Markdown

This pull request has been closed due to lack of activity. This is not a judgement on the merit of the PR in any way. It is just a way of keeping the PR queue manageable. If you think that is incorrect, or the pull request requires review, you can revive the PR at any time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Iceberg Truncate Transform on Numeric type

4 participants