-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[Iceberg v3] Geometry and Geography types #27893
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
Open
dain
wants to merge
2
commits into
master
Choose a base branch
from
user/dain/iceberg-geo
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,9 +24,11 @@ | |
| import org.locationtech.jts.io.WKBReader; | ||
| import org.locationtech.jts.io.WKBWriter; | ||
|
|
||
| import static com.google.common.base.Preconditions.checkArgument; | ||
| import static com.google.common.base.Verify.verify; | ||
| import static io.trino.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT; | ||
| import static java.lang.String.format; | ||
| import static java.util.Locale.ENGLISH; | ||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| /** | ||
|
|
@@ -36,6 +38,10 @@ | |
| public final class JtsGeometrySerde | ||
| { | ||
| private static final GeometryFactory GEOMETRY_FACTORY = new GeometryFactory(); | ||
| public static final int OGC_CRS84_SRID = 4326; | ||
|
|
||
| // EWKB flag for SRID presence (bit 29) | ||
| private static final int EWKB_SRID_FLAG = 0x20000000; | ||
|
|
||
| // WKB type codes (2D) | ||
| private static final int WKB_POINT = 1; | ||
|
|
@@ -184,4 +190,108 @@ public static Slice serializeBinaryOp(Geometry result, Geometry left, Geometry r | |
| result.setSRID(validateAndGetSrid(left, right)); | ||
| return serialize(result); | ||
| } | ||
|
|
||
| /** | ||
| * Extract SRID from EWKB without full parsing. | ||
| * Returns 0 if no SRID is embedded. | ||
| */ | ||
| public static int extractSrid(Slice ewkb) | ||
| { | ||
| if (ewkb.length() < 9) { | ||
| return 0; | ||
| } | ||
| boolean bigEndian = ewkb.getByte(0) == 0; | ||
| int type = ewkb.getInt(1); | ||
| if (bigEndian) { | ||
| type = Integer.reverseBytes(type); | ||
| } | ||
| if ((type & EWKB_SRID_FLAG) == 0) { | ||
| return 0; | ||
| } | ||
| int srid = ewkb.getInt(5); | ||
|
Comment on lines
+204
to
+211
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we do a single getLong instead?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It turned out more complex. |
||
| if (bigEndian) { | ||
| srid = Integer.reverseBytes(srid); | ||
| } | ||
| return srid; | ||
| } | ||
|
|
||
| /** | ||
| * Strip SRID from EWKB to produce standard WKB. | ||
| * If the input is already standard WKB (no SRID flag), returns it unchanged. | ||
| */ | ||
| public static Slice ewkbToWkb(Slice ewkb) | ||
| { | ||
| if (ewkb.length() < 9) { | ||
| return ewkb; | ||
| } | ||
| boolean bigEndian = ewkb.getByte(0) == 0; | ||
| int type = ewkb.getInt(1); | ||
| if (bigEndian) { | ||
| type = Integer.reverseBytes(type); | ||
| } | ||
| if ((type & EWKB_SRID_FLAG) == 0) { | ||
| return ewkb; | ||
| } | ||
|
|
||
| // Strip SRID flag and 4 SRID bytes | ||
| int newType = type & ~EWKB_SRID_FLAG; | ||
| Slice wkb = Slices.allocate(ewkb.length() - 4); | ||
| wkb.setByte(0, ewkb.getByte(0)); // endianness | ||
| wkb.setInt(1, bigEndian ? Integer.reverseBytes(newType) : newType); | ||
| wkb.setBytes(5, ewkb, 9, ewkb.length() - 9); // geometry data | ||
| return wkb; | ||
| } | ||
|
|
||
| /** | ||
| * Convert a CRS string to an SRID integer. | ||
| * Supports formats: | ||
| * - "EPSG:XXXX" → XXXX | ||
| * - "OGC:CRS84" or "CRS84" → 4326 (WGS84) | ||
| */ | ||
| public static int crsToSrid(String crs) | ||
| { | ||
| if (crs == null || crs.isEmpty()) { | ||
| return 0; | ||
| } | ||
| String upperCrs = crs.toUpperCase(ENGLISH); | ||
| if (upperCrs.equals("OGC:CRS84") || upperCrs.equals("CRS84")) { | ||
| return OGC_CRS84_SRID; // WGS84 | ||
| } | ||
| if (upperCrs.startsWith("EPSG:")) { | ||
| try { | ||
| int srid = Integer.parseInt(crs.substring(5)); | ||
| if (srid <= 0) { | ||
| throw new IllegalArgumentException("Invalid EPSG code: " + crs); | ||
| } | ||
| return srid; | ||
| } | ||
| catch (NumberFormatException e) { | ||
| throw new IllegalArgumentException("Invalid EPSG code: " + crs); | ||
| } | ||
| } | ||
| throw new IllegalArgumentException("Unsupported CRS format: " + crs); | ||
| } | ||
|
dain marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Inject SRID into WKB to produce EWKB. | ||
| */ | ||
| public static Slice wkbToEwkb(Slice wkb, int srid) | ||
| { | ||
| checkArgument(wkb.length() >= 5, "WKB too short"); | ||
| boolean bigEndian = wkb.getByte(0) == 0; | ||
| int type = wkb.getInt(1); | ||
| if (bigEndian) { | ||
| type = Integer.reverseBytes(type); | ||
| } | ||
| checkArgument((type & EWKB_SRID_FLAG) == 0, "Input already has SRID flag set (expected WKB, got EWKB)"); | ||
|
|
||
| // Add SRID flag | ||
| int newType = type | EWKB_SRID_FLAG; | ||
| Slice ewkb = Slices.allocate(wkb.length() + 4); | ||
| ewkb.setByte(0, wkb.getByte(0)); // endianness | ||
| ewkb.setInt(1, bigEndian ? Integer.reverseBytes(newType) : newType); | ||
| ewkb.setInt(5, bigEndian ? Integer.reverseBytes(srid) : srid); | ||
| ewkb.setBytes(9, wkb, 5, wkb.length() - 5); // geometry data | ||
| return ewkb; | ||
| } | ||
| } | ||
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
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
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
Oops, something went wrong.
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.
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.
Validate EWKB/WKB byte-order marker before parsing.
Right now any marker other than
0is treated as little-endian. Malformed inputs should fail fast instead of being parsed with a guessed endianness.🛠️ Proposed fix
public static int extractSrid(Slice ewkb) { if (ewkb.length() < 9) { return 0; } - boolean bigEndian = ewkb.getByte(0) == 0; + byte byteOrder = ewkb.getByte(0); + checkArgument(byteOrder == 0 || byteOrder == 1, "invalid WKB endianness: %s", byteOrder); + boolean bigEndian = byteOrder == 0; int type = ewkb.getInt(1); if (bigEndian) { type = Integer.reverseBytes(type); } @@ public static Slice ewkbToWkb(Slice ewkb) { if (ewkb.length() < 9) { return ewkb; } - boolean bigEndian = ewkb.getByte(0) == 0; + byte byteOrder = ewkb.getByte(0); + checkArgument(byteOrder == 0 || byteOrder == 1, "invalid WKB endianness: %s", byteOrder); + boolean bigEndian = byteOrder == 0; int type = ewkb.getInt(1); if (bigEndian) { type = Integer.reverseBytes(type); } @@ public static Slice wkbToEwkb(Slice wkb, int srid) { checkArgument(wkb.length() >= 5, "WKB too short"); - boolean bigEndian = wkb.getByte(0) == 0; + byte byteOrder = wkb.getByte(0); + checkArgument(byteOrder == 0 || byteOrder == 1, "invalid WKB endianness: %s", byteOrder); + boolean bigEndian = byteOrder == 0; int type = wkb.getInt(1); if (bigEndian) { type = Integer.reverseBytes(type); }Also applies to: 227-229, 281-282
🤖 Prompt for AI Agents