@Test public void testVectorCanLoadEmptyBuffer() throws Exception { final DrillBuf empty = allocator.getEmpty(); testVectors(new VectorVerifier() { @Override public void verify(ValueVector vector) { final String hint = String.format("%s failed the test case", vector.getClass().getSimpleName()); final UserBitShared.SerializedField metadata = vector.getMetadata(); assertEquals(hint, 0, metadata.getBufferLength()); assertEquals(hint, 0, metadata.getValueCount()); vector.load(metadata, empty); assertEquals(hint, 0, vector.getValueCapacity()); assertEquals(hint, 0, vector.getAccessor().getValueCount()); vector.clear(); } }); }
@Override public DrillBuf[] getBuffers(boolean clear) { DrillBuf[] out; if (getBufferSize() == 0) { out = new DrillBuf[0]; } else { out = new DrillBuf[]{data}; if (clear) { data.readerIndex(0); data.retain(1); } } if (clear) { clear(); } return out; }
@Override public void load(SerializedField metadata, DrillBuf buffer) { final List<SerializedField> children = metadata.getChildList(); final SerializedField offsetField = children.get(0); offsets.load(offsetField, buffer); int bufOffset = offsetField.getBufferLength(); for (int i = 1; i < children.size(); i++) { final SerializedField child = children.get(i); final MaterializedField fieldDef = MaterializedField.create(child); ValueVector vector = getChild(fieldDef.getLastName()); if (vector == null) { // if we arrive here, we didn't have a matching vector. vector = TypeHelper.getNewVector(fieldDef, allocator); putChild(fieldDef.getLastName(), vector); } final int vectorLength = child.getBufferLength(); vector.load(child, buffer.slice(bufOffset, vectorLength)); bufOffset += vectorLength; } assert bufOffset == buffer.capacity(); }
@Override public void load(SerializedField metadata, DrillBuf buf) { final List<SerializedField> fields = metadata.getChildList(); valueCount = metadata.getValueCount(); int bufOffset = 0; for (final SerializedField child : fields) { final MaterializedField fieldDef = MaterializedField.create(child); ValueVector vector = getChild(fieldDef.getLastName()); if (vector == null) { // if we arrive here, we didn't have a matching vector. vector = TypeHelper.getNewVector(fieldDef, allocator); putChild(fieldDef.getLastName(), vector); } if (child.getValueCount() == 0) { vector.clear(); } else { vector.load(child, buf.slice(bufOffset, child.getBufferLength())); } bufOffset += child.getBufferLength(); } assert bufOffset == buf.capacity(); }
@Override public DrillBuf[] getBuffers(boolean clear) { List<DrillBuf> buffers = Lists.newArrayList(); for (ValueVector vector : vectors.values()) { for (DrillBuf buf : vector.getBuffers(false)) { buffers.add(buf); if (clear) { buf.retain(); } } if (clear) { vector.clear(); } } return buffers.toArray(new DrillBuf[buffers.size()]); }
@Override public boolean setSafe(int index, DrillBuf bytebuf, int start, int length) { if (index >= varCharVector.getValueCapacity()) { return false; } if (usingDictionary) { currDictValToWrite = pageReader.dictionaryValueReader.readBytes(); mutator.setSafe(index, currDictValToWrite.toByteBuffer(), 0, currDictValToWrite.length()); } else { mutator.setSafe(index, start, start + length, bytebuf); } return true; }
private boolean transfer(Accountor target, DrillBuf buf, long size, boolean release) { boolean withinLimit = target.forceAdditionalReservation(size); if(release){ release(buf, size); } if (ENABLE_ACCOUNTING) { target.buffers.put(buf, new DebugStackTrace(buf.capacity(), Thread.currentThread().getStackTrace())); } return withinLimit; }
public void releasePartial(DrillBuf buf, long size) { remainder.returnAllocation(size); if (ENABLE_ACCOUNTING) { if (buf != null) { DebugStackTrace dst = buffers.get(buf); if (dst == null) { throw new IllegalStateException("Partially releasing a buffer that has already been released. Buffer: " + buf); } dst.size -= size; if (dst.size < 0) { throw new IllegalStateException("Partially releasing a buffer that has already been released. Buffer: " + buf); } } } }
public void release(DrillBuf buf, long size) { remainder.returnAllocation(size); if (ENABLE_ACCOUNTING) { if (buf != null && buffers.remove(buf) == null) { throw new IllegalStateException("Releasing a buffer that has already been released. Buffer: " + buf); } } }
public static int compareSparseSamePrecScale(DrillBuf left, int lStart, byte[] right, int length) { // check the sign first boolean lSign = (left.getInt(lStart) & 0x80000000) != 0; boolean rSign = ByteFunctionHelpers.getSign(right); int cmp = 0; if (lSign != rSign) { return (lSign == false) ? 1 : -1; } // invert the comparison if we are comparing negative numbers int invert = (lSign == true) ? -1 : 1; // compare byte by byte int n = 0; int lPos = lStart; int rPos = 0; while (n < length/4) { int leftInt = Decimal38SparseHolder.getInteger(n, lStart, left); int rightInt = ByteFunctionHelpers.getInteger(right, n); if (leftInt != rightInt) { cmp = (leftInt - rightInt ) > 0 ? 1 : -1; break; } n++; } return cmp * invert; }
public void loadPage(DrillBuf target, int pageLength) throws IOException { target.clear(); ByteBuffer directBuffer = target.nioBuffer(0, pageLength); while (directBuffer.remaining() > 0) { CompatibilityUtil.getBuf(input, directBuffer, directBuffer.remaining()); } target.writerIndex(pageLength); }
public static int compareDenseBytes(DrillBuf left, int leftStart, boolean leftSign, DrillBuf right, int rightStart, boolean rightSign, int width) { int invert = 1; /* If signs are different then simply look at the * sign of the two inputs and determine which is greater */ if (leftSign != rightSign) { return((leftSign == true) ? -1 : 1); } else if(leftSign == true) { /* Both inputs are negative, at the end we will * have to invert the comparison */ invert = -1; } int cmp = 0; for (int i = 0; i < width; i++) { byte leftByte = left.getByte(leftStart + i); byte rightByte = right.getByte(rightStart + i); // Unsigned byte comparison if ((leftByte & 0xFF) > (rightByte & 0xFF)) { cmp = 1; break; } else if ((leftByte & 0xFF) < (rightByte & 0xFF)) { cmp = -1; break; } } cmp *= invert; // invert the comparison if both were negative values return cmp; }
public static int getIntegerFromSparseBuffer(DrillBuf buffer, int start, int index) { int value = buffer.getInt(start + (index * 4)); if (index == 0) { /* the first byte contains sign bit, return value without it */ value = (value & 0x7FFFFFFF); } return value; }
public QueryDataBatch(QueryData header, DrillBuf data) { // logger.debug("New Result Batch with header {} and data {}", header, data); this.header = header; this.data = data; if (this.data != null) { data.retain(1); } }
public static void initCap(int start, int end, DrillBuf inBuf, DrillBuf outBuf) { boolean capNext = true; int out = 0; for (int id = start; id < end; id++, out++) { byte currentByte = inBuf.getByte(id); // 'A - Z' : 0x41 - 0x5A // 'a - z' : 0x61 - 0x7A // '0-9' : 0x30 - 0x39 if (capNext) { // curCh is whitespace or first character of word. if (currentByte >= 0x30 && currentByte <= 0x39) { // 0-9 capNext = false; } else if (currentByte >= 0x41 && currentByte <= 0x5A) { // A-Z capNext = false; } else if (currentByte >= 0x61 && currentByte <= 0x7A) { // a-z capNext = false; currentByte -= 0x20; // Uppercase this character } // else {} whitespace } else { // Inside of a word or white space after end of word. if (currentByte >= 0x30 && currentByte <= 0x39) { // 0-9 // noop } else if (currentByte >= 0x41 && currentByte <= 0x5A) { // A-Z currentByte -= 0x20; // Lowercase this character } else if (currentByte >= 0x61 && currentByte <= 0x7A) { // a-z // noop } else { // whitespace capNext = true; } } outBuf.setByte(out, currentByte); } // end of for_loop }
@Override public void decompress(DrillBuf input, int compressedSize, DrillBuf output, int uncompressedSize) throws IOException { BytesInput uncompressed = decompress(new ByteBufBytesInput(input), uncompressedSize); output.clear(); output.setBytes(0, uncompressed.toByteArray()); output.writerIndex((int) uncompressed.size()); }
private long getBufferSize(VectorAccessible batch) { long size = 0; for (VectorWrapper<?> w : batch) { DrillBuf[] bufs = w.getValueVector().getBuffers(false); for (DrillBuf buf : bufs) { if (buf.isRootBuffer()) { size += buf.capacity(); } } } return size; }
@Override public void close() { if (closed) { logger.debug("Attempted to close Operator context for {}, but context is already closed", popConfig != null ? popConfig.getClass().getName() : null); return; } logger.debug("Closing context for {}", popConfig != null ? popConfig.getClass().getName() : null); // release managed buffers. Object[] buffers = ((LongObjectOpenHashMap<Object>)(Object)managedBuffers).values; for (int i =0; i < buffers.length; i++) { if (managedBuffers.allocated[i]) { ((DrillBuf)buffers[i]).release(); } } if (allocator != null) { allocator.close(); } if (fs != null) { try { fs.close(); } catch (IOException e) { throw new DrillRuntimeException(e); } } closed = true; }
@Override public void close() throws Exception { final Object[] mbuffers = ((LongObjectOpenHashMap<Object>) (Object) managedBuffers).values; for (int i = 0; i < mbuffers.length; i++) { if (managedBuffers.allocated[i]) { ((DrillBuf) mbuffers[i]).release(1); } } managedBuffers.clear(); }
public DrillBuf replace(DrillBuf old, int newSize) { if (managedBuffers.remove(old.memoryAddress()) == null) { throw new IllegalStateException("Tried to remove unmanaged buffer."); } old.release(1); return getManagedBuffer(newSize); }
public DrillBuf getManagedBuffer(int size) { DrillBuf newBuf = allocator.buffer(size); managedBuffers.put(newBuf.memoryAddress(), newBuf); newBuf.setFragmentContext(fragmentContext); newBuf.setBufferManager(this); return newBuf; }
public static VarCharHolder getVarCharHolder(DrillBuf buf, String s){ VarCharHolder vch = new VarCharHolder(); byte[] b = s.getBytes(Charsets.UTF_8); vch.start = 0; vch.end = b.length; vch.buffer = buf.reallocIfNeeded(b.length); vch.buffer.setBytes(0, b); return vch; }
public static Decimal28SparseHolder getDecimal28Holder(DrillBuf buf, String decimal) { Decimal28SparseHolder dch = new Decimal28SparseHolder(); BigDecimal bigDecimal = new BigDecimal(decimal); dch.scale = bigDecimal.scale(); dch.precision = bigDecimal.precision(); Decimal28SparseHolder.setSign(bigDecimal.signum() == -1, dch.start, dch.buffer); dch.start = 0; dch.buffer = buf.reallocIfNeeded(5 * DecimalUtility.integerSize); DecimalUtility.getSparseFromBigDecimal(bigDecimal, dch.buffer, dch.start, dch.scale, dch.precision, dch.nDecimalDigits); return dch; }
public static Decimal38SparseHolder getDecimal38Holder(DrillBuf buf, String decimal) { Decimal38SparseHolder dch = new Decimal38SparseHolder(); BigDecimal bigDecimal = new BigDecimal(decimal); dch.scale = bigDecimal.scale(); dch.precision = bigDecimal.precision(); Decimal38SparseHolder.setSign(bigDecimal.signum() == -1, dch.start, dch.buffer); dch.start = 0; dch.buffer = buf.reallocIfNeeded(dch.maxPrecision * DecimalUtility.integerSize); DecimalUtility.getSparseFromBigDecimal(bigDecimal, dch.buffer, dch.start, dch.scale, dch.precision, dch.nDecimalDigits); return dch; }
public static BigDecimal getBigDecimalFromDrillBuf(DrillBuf data, int startIndex, int nDecimalDigits, int scale, boolean truncateScale) { // For sparse decimal type we have padded zeroes at the end, strip them while converting to BigDecimal. int actualDigits; // Initialize the BigDecimal, first digit in the DrillBuf has the sign so mask it out BigInteger decimalDigits = BigInteger.valueOf((data.getInt(startIndex)) & 0x7FFFFFFF); BigInteger base = BigInteger.valueOf(DIGITS_BASE); for (int i = 1; i < nDecimalDigits; i++) { BigInteger temp = BigInteger.valueOf(data.getInt(startIndex + (i * integerSize))); decimalDigits = decimalDigits.multiply(base); decimalDigits = decimalDigits.add(temp); } // Truncate any additional padding we might have added if (truncateScale == true && scale > 0 && (actualDigits = scale % MAX_DIGITS) != 0) { BigInteger truncate = BigInteger.valueOf((int)Math.pow(10, (MAX_DIGITS - actualDigits))); decimalDigits = decimalDigits.divide(truncate); } // set the sign if ((data.getInt(startIndex) & 0x80000000) != 0) { decimalDigits = decimalDigits.negate(); } BigDecimal decimal = new BigDecimal(decimalDigits, scale); return decimal; }
@Override public void load(SerializedField metadata, DrillBuf buffer) { Preconditions.checkArgument(this.field.matches(metadata), "The field %s doesn't match the provided metadata %s.", this.field, metadata); final int valueCount = metadata.getValueCount(); final int expectedLength = getSizeFromCount(valueCount); final int actualLength = metadata.getBufferLength(); assert expectedLength == actualLength: "expected and actual buffer sizes do not match"; clear(); data = buffer.slice(0, actualLength); data.retain(); this.valueCount = valueCount; }
public static void getSparseFromBigDecimal(BigDecimal input, DrillBuf data, int startIndex, int scale, int precision, int nDecimalDigits) { // Initialize the buffer for (int i = 0; i < nDecimalDigits; i++) { data.setInt(startIndex + (i * integerSize), 0); } boolean sign = false; if (input.signum() == -1) { // negative input sign = true; input = input.abs(); } // Truncate the input as per the scale provided input = input.setScale(scale, BigDecimal.ROUND_HALF_UP); // Separate out the integer part BigDecimal integerPart = input.setScale(0, BigDecimal.ROUND_DOWN); int destIndex = nDecimalDigits - roundUp(scale) - 1; // we use base 1 billion integer digits for out integernal representation BigDecimal base = new BigDecimal(DIGITS_BASE); while (integerPart.compareTo(BigDecimal.ZERO) == 1) { // store the modulo as the integer value data.setInt(startIndex + (destIndex * integerSize), (integerPart.remainder(base)).intValue()); destIndex--; // Divide by base 1 billion integerPart = (integerPart.divide(base)).setScale(0, BigDecimal.ROUND_DOWN); } /* Sparse representation contains padding of additional zeroes * so each digit contains MAX_DIGITS for ease of arithmetic */ int actualDigits; if ((actualDigits = (scale % MAX_DIGITS)) != 0) { // Pad additional zeroes scale = scale + (MAX_DIGITS - actualDigits); input = input.setScale(scale, BigDecimal.ROUND_DOWN); } //separate out the fractional part BigDecimal fractionalPart = input.remainder(BigDecimal.ONE).movePointRight(scale); destIndex = nDecimalDigits - 1; while (scale > 0) { // Get next set of MAX_DIGITS (9) store it in the DrillBuf fractionalPart = fractionalPart.movePointLeft(MAX_DIGITS); BigDecimal temp = fractionalPart.remainder(BigDecimal.ONE); data.setInt(startIndex + (destIndex * integerSize), (temp.unscaledValue().intValue())); destIndex--; fractionalPart = fractionalPart.setScale(0, BigDecimal.ROUND_DOWN); scale -= MAX_DIGITS; } // Set the negative sign if (sign == true) { data.setInt(startIndex, data.getInt(startIndex) | 0x80000000); } }
@Override public DrillBuf[] getBuffers(boolean clear) { final DrillBuf[] buffers = ObjectArrays.concat(offsets.getBuffers(false), vector.getBuffers(false), DrillBuf.class); if (clear) { for (DrillBuf buffer:buffers) { buffer.retain(); } clear(); } return buffers; }
@Override public void load(UserBitShared.SerializedField metadata, DrillBuf buffer) { final UserBitShared.SerializedField offsetMetadata = metadata.getChild(0); offsets.load(offsetMetadata, buffer); final UserBitShared.SerializedField vectorMetadata = metadata.getChild(1); if (getDataVector() == DEFAULT_DATA_VECTOR) { addOrGetVector(VectorDescriptor.create(vectorMetadata.getMajorType())); } final int offsetLength = offsetMetadata.getBufferLength(); final int vectorLength = vectorMetadata.getBufferLength(); vector.load(vectorMetadata, buffer.slice(offsetLength, vectorLength)); }
public JsonReader(DrillBuf managedBuf, List<SchemaPath> columns, boolean allTextMode, boolean skipOuterList, boolean readNumbersAsDouble) { super(managedBuf); assert Preconditions.checkNotNull(columns).size() > 0 : "json record reader requires at least a column"; this.selection = FieldSelection.getFieldSelection(columns); this.workingBuffer = new WorkingBuffer(managedBuf); this.skipOuterList = skipOuterList; this.allTextMode = allTextMode; this.columns = columns; this.mapOutput = new MapVectorOutput(workingBuffer); this.listOutput = new ListVectorOutput(workingBuffer); this.currentFieldName="<none>"; this.readNumbersAsDouble = readNumbersAsDouble; }
@Override public boolean setSafe(int index, DrillBuf value, int start, int length) { if (index >= varBinaryVector.getValueCapacity()) { return false; } if (usingDictionary) { currDictValToWrite = pageReader.dictionaryValueReader.readBytes(); mutator.setSafe(index, currDictValToWrite.toByteBuffer(), 0, currDictValToWrite.length()); } else { mutator.setSafe(index, start, start + length, value); } return true; }
@Override public boolean setSafe(int index, DrillBuf bytebuf, int start, int length) { int width = Decimal28SparseHolder.WIDTH; BigDecimal intermediate = DecimalUtility.getBigDecimalFromDrillBuf(bytebuf, start, length, schemaElement.getScale()); if (index >= decimal28Vector.getValueCapacity()) { return false; } DecimalUtility.getSparseFromBigDecimal(intermediate, decimal28Vector.getBuffer(), index * width, schemaElement.getScale(), schemaElement.getPrecision(), Decimal28SparseHolder.nDecimalDigits); return true; }
public static void trim(ByteBuf data, int idx) { data.writerIndex(idx); if (data instanceof DrillBuf) { // data.capacity(idx); data.writerIndex(idx); } }
@Override public void dataArrived(QueryDataBatch result, ConnectionThrottle throttle) { final QueryData header = result.getHeader(); final DrillBuf data = result.getData(); if (data != null) { count.addAndGet(header.getRowCount()); try { loader.load(header.getDef(), data); // TODO: Clean: DRILL-2933: That load(...) no longer throws // SchemaChangeException, so check/clean catch clause below. } catch (SchemaChangeException e) { submissionFailed(UserException.systemError(e).build(logger)); } switch(format) { case TABLE: VectorUtil.showVectorAccessibleContent(loader, columnWidth); break; case TSV: VectorUtil.showVectorAccessibleContent(loader, "\t"); break; case CSV: VectorUtil.showVectorAccessibleContent(loader, ","); break; } loader.clear(); } result.release(); }
@Test public void testHadooopVInt() throws Exception { final int _0 = 0; final int _9 = 9; final DrillBuf buffer = getAllocator().buffer(_9); long longVal = 0; buffer.clear(); HadoopWritables.writeVLong(buffer, _0, _9, 0); longVal = HadoopWritables.readVLong(buffer, _0, _9); assertEquals(longVal, 0); buffer.clear(); HadoopWritables.writeVLong(buffer, _0, _9, Long.MAX_VALUE); longVal = HadoopWritables.readVLong(buffer, _0, _9); assertEquals(longVal, Long.MAX_VALUE); buffer.clear(); HadoopWritables.writeVLong(buffer, _0, _9, Long.MIN_VALUE); longVal = HadoopWritables.readVLong(buffer, _0, _9); assertEquals(longVal, Long.MIN_VALUE); int intVal = 0; buffer.clear(); HadoopWritables.writeVInt(buffer, _0, _9, 0); intVal = HadoopWritables.readVInt(buffer, _0, _9); assertEquals(intVal, 0); buffer.clear(); HadoopWritables.writeVInt(buffer, _0, _9, Integer.MAX_VALUE); intVal = HadoopWritables.readVInt(buffer, _0, _9); assertEquals(intVal, Integer.MAX_VALUE); buffer.clear(); HadoopWritables.writeVInt(buffer, _0, _9, Integer.MIN_VALUE); intVal = HadoopWritables.readVInt(buffer, _0, _9); assertEquals(intVal, Integer.MIN_VALUE); buffer.release(); }
@Override public void close() { if (!svAllocatorUsed) { final DrillBuf drillBuf = svAllocator.getAllocation(); if (drillBuf != null) { drillBuf.release(); } } }
/** * Verifies that the the space provided in the buffer is of specified size. * @throws IllegalArgumentException if the specified boundaries do not describe the expected size. */ public static void checkBufferLength(DrillBuf buffer, int start, int end, int requiredLen) { int actualLen = (end - start); if (actualLen != requiredLen) { throw new IllegalArgumentException(String.format("Wrong length %d(%d-%d) in the buffer '%s', expected %d.", actualLen, end, start, DrillStringUtils.toBinaryString(buffer, start, end), requiredLen)); } }
public void setBuffer(DrillBuf bufferHandle) { /* clear the existing buffer */ clear(); this.buffer = bufferHandle; buffer.retain(1); }
@Override public boolean setSafe(int index, DrillBuf bytebuf, int start, int length) { int width = Decimal38SparseHolder.WIDTH; BigDecimal intermediate = DecimalUtility.getBigDecimalFromDrillBuf(bytebuf, start, length, schemaElement.getScale()); if (index >= nullableDecimal38Vector.getValueCapacity()) { return false; } DecimalUtility.getSparseFromBigDecimal(intermediate, nullableDecimal38Vector.getBuffer(), index * width, schemaElement.getScale(), schemaElement.getPrecision(), Decimal38SparseHolder.nDecimalDigits); nullableDecimal38Vector.getMutator().setIndexDefined(index); return true; }
@Override public void generate() throws SchemaChangeException { Stopwatch watch = new Stopwatch(); watch.start(); final DrillBuf drillBuf = allocator.buffer(4 * queueSize); finalSv4 = new SelectionVector4(drillBuf, queueSize, 4000); for (int i = queueSize - 1; i >= 0; i--) { finalSv4.set(i, pop()); } logger.debug("Took {} us to generate output of {}", watch.elapsed(TimeUnit.MICROSECONDS), finalSv4.getTotalCount()); }