diff --git common/src/java/org/apache/hadoop/hive/conf/HiveConf.java common/src/java/org/apache/hadoop/hive/conf/HiveConf.java index d26573e..8637142 100644 --- common/src/java/org/apache/hadoop/hive/conf/HiveConf.java +++ common/src/java/org/apache/hadoop/hive/conf/HiveConf.java @@ -550,6 +550,9 @@ // Define the default block padding HIVE_ORC_DEFAULT_BLOCK_PADDING("hive.exec.orc.default.block.padding", true), + // Define the tolerance for block padding. By default, 5% of stripe size is + // tolerated. For 256MB stripe size, maximum padding will be ~13MB. + HIVE_ORC_BLOCK_PADDING_TOLERANCE("hive.exec.orc.block.padding.tolerance", 0.05f), // Define the default compression codec for ORC file HIVE_ORC_DEFAULT_COMPRESS("hive.exec.orc.default.compress", "ZLIB"), HIVE_ORC_INCLUDE_FILE_FOOTER_IN_SPLITS("hive.orc.splits.include.file.footer", false), diff --git conf/hive-default.xml.template conf/hive-default.xml.template index 8a74e4e..f7af01e 100644 --- conf/hive-default.xml.template +++ conf/hive-default.xml.template @@ -1962,6 +1962,14 @@ + hive.exec.orc.block.padding.tolerance + 0.05 + + Define the tolerance for block padding. + + + + hive.exec.orc.default.compress ZLIB diff --git ql/src/java/org/apache/hadoop/hive/ql/io/orc/OrcFile.java ql/src/java/org/apache/hadoop/hive/ql/io/orc/OrcFile.java index 7c542c1..cde66d3 100644 --- ql/src/java/org/apache/hadoop/hive/ql/io/orc/OrcFile.java +++ ql/src/java/org/apache/hadoop/hive/ql/io/orc/OrcFile.java @@ -221,6 +221,7 @@ public static Reader createReader(Path path, private MemoryManager memoryManagerValue; private Version versionValue; private WriterCallback callback; + private float paddingTolerance; WriterOptions(Configuration conf) { configuration = conf; @@ -250,6 +251,9 @@ public static Reader createReader(Path path, } else { versionValue = Version.byName(versionName); } + paddingTolerance = + conf.getFloat(HiveConf.ConfVars.HIVE_ORC_BLOCK_PADDING_TOLERANCE.varname, + HiveConf.ConfVars.HIVE_ORC_BLOCK_PADDING_TOLERANCE.defaultFloatVal); } /** @@ -301,6 +305,14 @@ public WriterOptions blockPadding(boolean value) { } /** + * Sets the tolerance for block padding. + */ + public WriterOptions paddingTolerance(float value) { + paddingTolerance = value; + return this; + } + + /** * Sets the generic compression that is used to compress the data. */ public WriterOptions compress(CompressionKind value) { @@ -370,7 +382,7 @@ public static Writer createWriter(Path path, opts.stripeSizeValue, opts.compressValue, opts.bufferSizeValue, opts.rowIndexStrideValue, opts.memoryManagerValue, opts.blockPaddingValue, - opts.versionValue, opts.callback); + opts.versionValue, opts.callback, opts.paddingTolerance); } /** diff --git ql/src/java/org/apache/hadoop/hive/ql/io/orc/WriterImpl.java ql/src/java/org/apache/hadoop/hive/ql/io/orc/WriterImpl.java index b3ab96f..d3ba88a 100644 --- ql/src/java/org/apache/hadoop/hive/ql/io/orc/WriterImpl.java +++ ql/src/java/org/apache/hadoop/hive/ql/io/orc/WriterImpl.java @@ -97,13 +97,15 @@ private final FileSystem fs; private final Path path; - private final long stripeSize; + private long adjustedStripeSize; + private final long defaultStripeSize; private final int rowIndexStride; private final CompressionKind compress; private final CompressionCodec codec; private final boolean addBlockPadding; private final int bufferSize; private final long blockSize; + private final float paddingTolerance; // the streams that make up the current stripe private final Map streams = new TreeMap(); @@ -146,7 +148,8 @@ MemoryManager memoryManager, boolean addBlockPadding, OrcFile.Version version, - OrcFile.WriterCallback callback) throws IOException { + OrcFile.WriterCallback callback, + float paddingTolerance) throws IOException { this.fs = fs; this.path = path; this.conf = conf; @@ -162,11 +165,13 @@ public Writer getWriter() { } else { callbackContext = null; } - this.stripeSize = stripeSize; + this.adjustedStripeSize = stripeSize; + this.defaultStripeSize = stripeSize; this.version = version; this.addBlockPadding = addBlockPadding; // pick large block size to minimize block over or under hangs this.blockSize = Math.min(MAX_BLOCK_SIZE, 2 * stripeSize); + this.paddingTolerance = paddingTolerance; this.compress = compress; this.bufferSize = bufferSize; this.rowIndexStride = rowIndexStride; @@ -211,7 +216,7 @@ static CompressionCodec createCodec(CompressionKind kind) { @Override public synchronized boolean checkMemory(double newScale) throws IOException { - long limit = (long) Math.round(stripeSize * newScale); + long limit = (long) Math.round(adjustedStripeSize * newScale); long size = estimateStripeSize(); if (LOG.isDebugEnabled()) { LOG.debug("ORC writer " + path + " size = " + size + " limit = " + @@ -1802,10 +1807,17 @@ private void flushStripe() throws IOException { // Do we need to pad the file so the stripe doesn't straddle a block // boundary? long start = rawWriter.getPos(); - long stripeSize = indexSize + dataSize + footer.getSerializedSize(); + long currentStripeSize = indexSize + dataSize + footer.getSerializedSize(); + long available = blockSize - (start % blockSize); + float availRatio = (float) available / (float) adjustedStripeSize; + if (availRatio > 0.0f && availRatio < 1.0f && availRatio > paddingTolerance) { + // adjust default stripe size to fit into remaining space + adjustedStripeSize = (long) (availRatio * adjustedStripeSize); + } if (addBlockPadding && - stripeSize < blockSize && - (start % blockSize) + stripeSize > blockSize) { + currentStripeSize < blockSize && + (start % blockSize) + currentStripeSize > blockSize && + availRatio < paddingTolerance ) { long padding = blockSize - (start % blockSize); byte[] pad = new byte[(int) Math.min(HDFS_BUFFER_SIZE, padding)]; start += padding; @@ -1814,6 +1826,9 @@ private void flushStripe() throws IOException { rawWriter.write(pad, 0, writeLen); padding -= writeLen; } + + // after padding reset the default stripe size + adjustedStripeSize = defaultStripeSize; } // write out the data streams diff --git ql/src/test/resources/orc-file-dump-dictionary-threshold.out ql/src/test/resources/orc-file-dump-dictionary-threshold.out index 0a06481..f6e7a50 100644 --- ql/src/test/resources/orc-file-dump-dictionary-threshold.out +++ ql/src/test/resources/orc-file-dump-dictionary-threshold.out @@ -90,15 +90,15 @@ Stripes: Encoding column 1: DIRECT_V2 Encoding column 2: DIRECT_V2 Encoding column 3: DIRECT_V2 - Stripe: offset: 1800000 data: 177935 rows: 1000 tail: 67 index: 813 - Stream: column 0 section ROW_INDEX start: 1800000 length 10 - Stream: column 1 section ROW_INDEX start: 1800010 length 36 - Stream: column 2 section ROW_INDEX start: 1800046 length 39 - Stream: column 3 section ROW_INDEX start: 1800085 length 728 - Stream: column 1 section DATA start: 1800813 length 4007 - Stream: column 2 section DATA start: 1804820 length 8007 - Stream: column 3 section DATA start: 1812827 length 164661 - Stream: column 3 section LENGTH start: 1977488 length 1260 + Stripe: offset: 1751794 data: 177935 rows: 1000 tail: 67 index: 813 + Stream: column 0 section ROW_INDEX start: 1751794 length 10 + Stream: column 1 section ROW_INDEX start: 1751804 length 36 + Stream: column 2 section ROW_INDEX start: 1751840 length 39 + Stream: column 3 section ROW_INDEX start: 1751879 length 728 + Stream: column 1 section DATA start: 1752607 length 4007 + Stream: column 2 section DATA start: 1756614 length 8007 + Stream: column 3 section DATA start: 1764621 length 164661 + Stream: column 3 section LENGTH start: 1929282 length 1260 Encoding column 0: DIRECT Encoding column 1: DIRECT_V2 Encoding column 2: DIRECT_V2 diff --git ql/src/test/resources/orc-file-dump.out ql/src/test/resources/orc-file-dump.out index 7a5b907..f46a89b 100644 --- ql/src/test/resources/orc-file-dump.out +++ ql/src/test/resources/orc-file-dump.out @@ -80,30 +80,30 @@ Stripes: Encoding column 1: DIRECT_V2 Encoding column 2: DIRECT_V2 Encoding column 3: DICTIONARY_V2 - Stripe: offset: 200000 data: 63796 rows: 5000 tail: 74 index: 123 - Stream: column 0 section ROW_INDEX start: 200000 length 10 - Stream: column 1 section ROW_INDEX start: 200010 length 35 - Stream: column 2 section ROW_INDEX start: 200045 length 39 - Stream: column 3 section ROW_INDEX start: 200084 length 39 - Stream: column 1 section DATA start: 200123 length 20029 - Stream: column 2 section DATA start: 220152 length 40035 - Stream: column 3 section DATA start: 260187 length 3574 - Stream: column 3 section LENGTH start: 263761 length 25 - Stream: column 3 section DICTIONARY_DATA start: 263786 length 133 + Stripe: offset: 191881 data: 63796 rows: 5000 tail: 74 index: 123 + Stream: column 0 section ROW_INDEX start: 191881 length 10 + Stream: column 1 section ROW_INDEX start: 191891 length 35 + Stream: column 2 section ROW_INDEX start: 191926 length 39 + Stream: column 3 section ROW_INDEX start: 191965 length 39 + Stream: column 1 section DATA start: 192004 length 20029 + Stream: column 2 section DATA start: 212033 length 40035 + Stream: column 3 section DATA start: 252068 length 3574 + Stream: column 3 section LENGTH start: 255642 length 25 + Stream: column 3 section DICTIONARY_DATA start: 255667 length 133 Encoding column 0: DIRECT Encoding column 1: DIRECT_V2 Encoding column 2: DIRECT_V2 Encoding column 3: DICTIONARY_V2 - Stripe: offset: 263993 data: 12940 rows: 1000 tail: 71 index: 123 - Stream: column 0 section ROW_INDEX start: 263993 length 10 - Stream: column 1 section ROW_INDEX start: 264003 length 36 - Stream: column 2 section ROW_INDEX start: 264039 length 39 - Stream: column 3 section ROW_INDEX start: 264078 length 38 - Stream: column 1 section DATA start: 264116 length 4007 - Stream: column 2 section DATA start: 268123 length 8007 - Stream: column 3 section DATA start: 276130 length 768 - Stream: column 3 section LENGTH start: 276898 length 25 - Stream: column 3 section DICTIONARY_DATA start: 276923 length 133 + Stripe: offset: 255874 data: 12940 rows: 1000 tail: 71 index: 123 + Stream: column 0 section ROW_INDEX start: 255874 length 10 + Stream: column 1 section ROW_INDEX start: 255884 length 36 + Stream: column 2 section ROW_INDEX start: 255920 length 39 + Stream: column 3 section ROW_INDEX start: 255959 length 38 + Stream: column 1 section DATA start: 255997 length 4007 + Stream: column 2 section DATA start: 260004 length 8007 + Stream: column 3 section DATA start: 268011 length 768 + Stream: column 3 section LENGTH start: 268779 length 25 + Stream: column 3 section DICTIONARY_DATA start: 268804 length 133 Encoding column 0: DIRECT Encoding column 1: DIRECT_V2 Encoding column 2: DIRECT_V2