HIVE-17110 BucketCodec should enforce value ranges (Eugene Koifman, reviewed by Gopal V)
Project: http://git-wip-us.apache.org/repos/asf/hive/repo Commit: http://git-wip-us.apache.org/repos/asf/hive/commit/c396cd2b Tree: http://git-wip-us.apache.org/repos/asf/hive/tree/c396cd2b Diff: http://git-wip-us.apache.org/repos/asf/hive/diff/c396cd2b Branch: refs/heads/hive-14535 Commit: c396cd2b8bc46e512ccdf54af8817bb82cf5340e Parents: a3b4bf8 Author: Eugene Koifman <[email protected]> Authored: Tue Sep 26 08:51:02 2017 -0700 Committer: Eugene Koifman <[email protected]> Committed: Tue Sep 26 09:16:22 2017 -0700 ---------------------------------------------------------------------- .../apache/hadoop/hive/ql/io/BucketCodec.java | 23 +++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/hive/blob/c396cd2b/ql/src/java/org/apache/hadoop/hive/ql/io/BucketCodec.java ---------------------------------------------------------------------- diff --git a/ql/src/java/org/apache/hadoop/hive/ql/io/BucketCodec.java b/ql/src/java/org/apache/hadoop/hive/ql/io/BucketCodec.java index 343f41f..3af16e3 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/io/BucketCodec.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/io/BucketCodec.java @@ -86,11 +86,28 @@ public enum BucketCodec { } @Override public int encode(AcidOutputFormat.Options options) { - return this.version << 29 | options.getBucketId() << 16 | - (options.getStatementId() >= 0 ? options.getStatementId() : 0); + int statementId = options.getStatementId() >= 0 ? options.getStatementId() : 0; + + assert this.version >=0 && this.version <= MAX_VERSION + : "Version out of range: " + version; + if(!(options.getBucketId() >= 0 && options.getBucketId() <= MAX_BUCKET_ID)) { + throw new IllegalArgumentException("bucketId out of range: " + options.getBucketId()); + } + if(!(statementId >= 0 && statementId <= MAX_STATEMENT_ID)) { + throw new IllegalArgumentException("statementId out of range: " + statementId); + } + return this.version << (1 + NUM_BUCKET_ID_BITS + 4 + NUM_STATEMENT_ID_BITS) | + options.getBucketId() << (4 + NUM_STATEMENT_ID_BITS) | statementId; } }; - private static int TOP3BITS_MASK = 0b1110_0000_0000_0000_0000_0000_0000_0000; + private static final int TOP3BITS_MASK = 0b1110_0000_0000_0000_0000_0000_0000_0000; + private static final int NUM_VERSION_BITS = 3; + private static final int NUM_BUCKET_ID_BITS = 12; + private static final int NUM_STATEMENT_ID_BITS = 12; + private static final int MAX_VERSION = (1 << NUM_VERSION_BITS) - 1; + private static final int MAX_BUCKET_ID = (1 << NUM_BUCKET_ID_BITS) - 1; + private static final int MAX_STATEMENT_ID = (1 << NUM_STATEMENT_ID_BITS) - 1; + public static BucketCodec determineVersion(int bucket) { assert 7 << 29 == BucketCodec.TOP3BITS_MASK; //look at top 3 bits and return appropriate enum
