-
Notifications
You must be signed in to change notification settings - Fork 2
Add network_bucket column type for efficient BigQuery lookups #12
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e743aa7
Do not lint testdata/MaxMind-DB
horgh e4ca579
Reflow changelog entry
horgh a34a964
Add network_bucket column type for efficient BigQuery lookups
horgh 32bc23a
Support IPv6 buckets being integer
horgh 9ae7146
Add network_bucket column support for CSV output
horgh e498ee4
Improve docs
horgh 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| # BigQuery with Network Bucketing | ||
|
|
||
| BigQuery performs full table scans for range queries like | ||
| `WHERE start_int <= ip AND end_int >= ip`. Use a `network_bucket` column to | ||
| enable efficient lookups. | ||
|
|
||
| **Note:** The BigQuery table must be clustered on the `network_bucket` column | ||
| for efficient querying. | ||
|
|
||
| **Important:** The bucket size in your queries must match the configured bucket | ||
| size. The examples below use the default `/16` bucket size. If you configured a | ||
| different `ipv4_bucket_size` or `ipv6_bucket_size`, adjust the second argument | ||
| to `NET.IP_TRUNC()` accordingly. | ||
|
|
||
| ## IPv4 Lookup | ||
|
|
||
| For IPv4, the bucket is int64. Use `NET.IP_TRUNC()` to get the bucket and | ||
| `NET.IPV4_TO_INT64()` to convert to the integer type: | ||
|
|
||
| ```sql | ||
| -- Using default ipv4_bucket_size = 16 | ||
| SELECT * | ||
| FROM `project.dataset.geoip_v4` | ||
| WHERE network_bucket = NET.IPV4_TO_INT64(NET.IP_TRUNC(NET.IP_FROM_STRING('203.0.113.100'), 16)) | ||
| AND NET.IPV4_TO_INT64(NET.IP_FROM_STRING('203.0.113.100')) BETWEEN start_int AND end_int; | ||
| ``` | ||
|
|
||
| ## IPv6 Lookup | ||
|
|
||
| The query depends on your `ipv6_bucket_type` configuration. | ||
|
|
||
| **Note:** For IPv6 files, `start_int` and `end_int` columns are stored as | ||
| 16-byte binary values, not integers. The comparison with `NET.IP_FROM_STRING()` | ||
| works because it also returns BYTES. | ||
|
|
||
| **Using default `ipv6_bucket_type = "string"` (hex string):** | ||
|
|
||
| ```sql | ||
| -- Using default ipv6_bucket_size = 16 | ||
| SELECT * | ||
| FROM `project.dataset.geoip_v6` | ||
| WHERE network_bucket = TO_HEX(NET.IP_TRUNC(NET.IP_FROM_STRING('2001:db8::1'), 16)) | ||
| AND NET.IP_FROM_STRING('2001:db8::1') BETWEEN start_int AND end_int; | ||
| ``` | ||
|
|
||
| **Using `ipv6_bucket_type = "int"` (60-bit int64):** | ||
|
|
||
| ```sql | ||
| -- Using default ipv6_bucket_size = 16 | ||
| SELECT * | ||
| FROM `project.dataset.geoip_v6` | ||
| WHERE network_bucket = CAST(CONCAT('0x', SUBSTR( | ||
| TO_HEX(NET.IP_TRUNC(NET.IP_FROM_STRING('2001:db8::1'), 16)), 1, 15 | ||
| )) AS INT64) | ||
| AND NET.IP_FROM_STRING('2001:db8::1') BETWEEN start_int AND end_int; | ||
| ``` | ||
|
|
||
| The int type expression extracts the first 60 bits (15 hex chars) of the | ||
| truncated IPv6 address as an integer. | ||
|
|
||
| ## Why Bucketing Helps | ||
|
|
||
| Without bucketing, BigQuery must scan every row to check the range condition. | ||
| With bucketing: | ||
|
|
||
| 1. BigQuery first filters by exact match on `network_bucket` | ||
| 2. Only matching bucket rows are checked for the range condition | ||
| 3. Result: Query scans only rows in the matching bucket instead of the entire | ||
| table | ||
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.
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.