forked from apache/iceberg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Spark 3.3: Support truncate in FunctionCatalog (apache#5431)
- Loading branch information
Showing
7 changed files
with
959 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
75 changes: 75 additions & 0 deletions
75
api/src/main/java/org/apache/iceberg/util/TruncateUtil.java
This file contains 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,75 @@ | ||
/* | ||
* 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. | ||
*/ | ||
package org.apache.iceberg.util; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.BigInteger; | ||
import java.nio.ByteBuffer; | ||
|
||
/** | ||
* Contains the logic for various {@code truncate} transformations for various types. | ||
* | ||
* <p>This utility class allows for the logic to be reused in different scenarios where input | ||
* validation is done at different times either in org.apache.iceberg.transforms.Truncate and within | ||
* defined SQL functions for different compute engines for usage in SQL. | ||
* | ||
* <p>In general, the inputs to the functions should have already been validated by the calling | ||
* code, as different classes use truncate with different preprocessing. This generally means that | ||
* the truncation width is positive and the value to truncate is non-null. | ||
* | ||
* <p>Thus, <b>none</b> of these utility functions validate their input. <i>It is the responsibility | ||
* of the calling code to validate input.</i> | ||
* | ||
* <p>See also {@linkplain UnicodeUtil#truncateString(CharSequence, int)} and {@link | ||
* BinaryUtil#truncateBinaryUnsafe(ByteBuffer, int)} for similar methods for Strings and | ||
* ByteBuffers. | ||
*/ | ||
public class TruncateUtil { | ||
|
||
private TruncateUtil() {} | ||
|
||
public static byte truncateByte(int width, byte value) { | ||
return (byte) (value - (((value % width) + width) % width)); | ||
} | ||
|
||
public static short truncateShort(int width, short value) { | ||
return (short) (value - (((value % width) + width) % width)); | ||
} | ||
|
||
public static int truncateInt(int width, int value) { | ||
return value - (((value % width) + width) % width); | ||
} | ||
|
||
public static long truncateLong(int width, long value) { | ||
return value - (((value % width) + width) % width); | ||
} | ||
|
||
public static BigDecimal truncateDecimal(BigInteger unscaledWidth, BigDecimal value) { | ||
BigDecimal remainder = | ||
new BigDecimal( | ||
value | ||
.unscaledValue() | ||
.remainder(unscaledWidth) | ||
.add(unscaledWidth) | ||
.remainder(unscaledWidth), | ||
value.scale()); | ||
|
||
return value.subtract(remainder); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
core/src/test/java/org/apache/iceberg/util/TestTruncateUtil.java
This file contains 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,35 @@ | ||
/* | ||
* 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. | ||
*/ | ||
package org.apache.iceberg.util; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class TestTruncateUtil { | ||
@Test | ||
public void testInvalidInputWidthBehavior() { | ||
Assertions.assertThatNoException() | ||
.as("Invalid width input shouldn't necessarily throw an exception as it's not validated") | ||
.isThrownBy(() -> TruncateUtil.truncateInt(-1, 100)); | ||
|
||
Assertions.assertThatException() | ||
.as("Invalid width input can possibly throw an exception") | ||
.isThrownBy(() -> TruncateUtil.truncateInt(0, 100)); | ||
} | ||
} |
This file contains 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
Oops, something went wrong.