-
Notifications
You must be signed in to change notification settings - Fork 3k
Spark - Implement FunctionCatalog and Truncate #5305
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
70ea489
Spark - Implement FunctionCatalog and Truncate
kbendick b77a5b3
Remove test for assertion error on bad truncation width now that we d…
kbendick 1b5a0e5
Allow for empty namespace to resolve functions to match assumptions m…
kbendick 24efcba
Run spotless over iceberg-spark-3.3 and iceberg-api
kbendick 093682c
Fix invalid input type for truncateByte to avoid cast to short
kbendick 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
77 changes: 77 additions & 0 deletions
77
api/src/main/java/org/apache/iceberg/util/TruncateUtil.java
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,77 @@ | ||
| /* | ||
| * 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>See also {@linkplain UnicodeUtil#truncateString(CharSequence, int)} and {@link | ||
| * BinaryUtil#truncateBinary(ByteBuffer, int)} | ||
| */ | ||
| public class TruncateUtil { | ||
|
|
||
| private TruncateUtil() {} | ||
|
|
||
| public static ByteBuffer truncateByteBuffer(int width, ByteBuffer value) { | ||
| ByteBuffer ret = value.duplicate(); | ||
| ret.limit(Math.min(value.limit(), value.position() + width)); | ||
| return ret; | ||
| } | ||
|
|
||
| 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); | ||
| } | ||
| } |
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
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
48 changes: 48 additions & 0 deletions
48
spark/v3.3/spark/src/main/java/org/apache/iceberg/spark/functions/SparkFunctions.java
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,48 @@ | ||
| /* | ||
| * 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.spark.functions; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; | ||
| import org.apache.spark.sql.connector.catalog.functions.UnboundFunction; | ||
|
|
||
| public class SparkFunctions { | ||
|
|
||
| private SparkFunctions() {} | ||
|
|
||
| private static final Map<String, UnboundFunction> FUNCTIONS = | ||
| ImmutableMap.of("truncate", new TruncateFunction()); | ||
|
|
||
| private static final List<String> FUNCTION_NAMES = ImmutableList.copyOf(FUNCTIONS.keySet()); | ||
|
|
||
| // Functions that are added to all Iceberg catalogs should be accessed with either the `system` | ||
| // namespace or no namespace at all, so a list of names alone is returned. | ||
| public static List<String> list() { | ||
| return FUNCTION_NAMES; | ||
| } | ||
|
|
||
| public static UnboundFunction load(String name) { | ||
| // function resolution is case insensitive to match the existing Spark behavior for functions | ||
| UnboundFunction func = FUNCTIONS.get(name.toLowerCase(Locale.ROOT)); | ||
| return func; | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
There's
BinaryUtil#truncateBinary(ByteBuffer, int), but it doesn't exactly have the same code as is used here and it does aPreconditionscheck on thewidthon every call to it, which isn't needed here.I can try to merge the two implementations in a follow up if we want.