/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.drill.exec.vector;

import java.nio.ByteOrder;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.SwappedByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.buffer.UnpooledByteBufAllocator;

import org.apache.drill.common.util.DecimalUtility;
import org.apache.drill.exec.expr.holders.VarCharHolder;
import org.apache.drill.exec.expr.holders.IntervalDayHolder;
import org.apache.drill.exec.expr.holders.Decimal9Holder;
import org.apache.drill.exec.expr.holders.Decimal18Holder;
import org.apache.drill.exec.expr.holders.Decimal28SparseHolder;
import org.apache.drill.exec.expr.holders.Decimal38SparseHolder;

import com.google.common.base.Charsets;

import java.math.BigDecimal;


public class ValueHolderHelper {
  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(ValueHolderHelper.class);
  
  public static VarCharHolder getVarCharHolder(String s){
    VarCharHolder vch = new VarCharHolder();
    
    byte[] b = s.getBytes(Charsets.UTF_8);
    vch.start = 0;
    vch.end = b.length;
    vch.buffer = UnpooledByteBufAllocator.DEFAULT.buffer(s.length()).order(ByteOrder.LITTLE_ENDIAN); // use the length of input string to allocate buffer. 
    vch.buffer.setBytes(0, b);
    return vch;
  }

  public static IntervalDayHolder getIntervalDayHolder(int days, int millis) {
      IntervalDayHolder dch = new IntervalDayHolder();

      dch.days = days;
      dch.milliSeconds = millis;
      return dch;
  }

  public static Decimal9Holder getDecimal9Holder(int decimal, int scale, int precision) {
    Decimal9Holder dch = new Decimal9Holder();

    dch.scale = scale;
    dch.precision = precision;
    dch.value = decimal;

    return dch;
  }

  public static Decimal18Holder getDecimal18Holder(long decimal, int scale, int precision) {
    Decimal18Holder dch = new Decimal18Holder();

    dch.scale = scale;
    dch.precision = precision;
    dch.value = decimal;

    return dch;
  }

  public static Decimal28SparseHolder getDecimal28Holder(String decimal) {

    Decimal28SparseHolder dch = new Decimal28SparseHolder();

    BigDecimal bigDecimal = new BigDecimal(decimal);

    dch.scale = bigDecimal.scale();
    dch.precision = bigDecimal.precision();
    dch.sign = (bigDecimal.signum() == -1);
    dch.start = 0;

    dch.buffer = Unpooled.wrappedBuffer(new byte[5 * DecimalUtility.integerSize]);
    dch.buffer = new SwappedByteBuf(dch.buffer);
    DecimalUtility.getSparseFromBigDecimal(bigDecimal, dch.buffer, dch.start, dch.scale, dch.precision, dch.nDecimalDigits);

    return dch;
  }

  public static Decimal38SparseHolder getDecimal38Holder(String decimal) {


      Decimal38SparseHolder dch = new Decimal38SparseHolder();

      BigDecimal bigDecimal = new BigDecimal(decimal);

      dch.scale = bigDecimal.scale();
      dch.precision = bigDecimal.precision();
      dch.sign = (bigDecimal.signum() == -1);
      dch.start = 0;


      dch.buffer = Unpooled.wrappedBuffer(new byte[dch.maxPrecision * DecimalUtility.integerSize]);
      dch.buffer = new SwappedByteBuf(dch.buffer);
      DecimalUtility.getSparseFromBigDecimal(bigDecimal, dch.buffer, dch.start, dch.scale, dch.precision, dch.nDecimalDigits);

      return dch;
  }
}

@@ -839,67 +839,8 @@
 r;%0A%0A
-import java.nio.ByteOrder;%0Aimport io.netty.buffer.ByteBuf;%0A
 impo
@@ -968,110 +968,58 @@
 ort 
-org.apache.drill.common.util.DecimalUtility;%0Aimport org.apache.drill.exec.expr.holders.VarCharHol
+java.math.BigDecimal;%0Aimport java.nio.ByteOr
 der;%0A
+%0A
 impo
@@ -1042,94 +1042,27 @@
 ill.
-exec.expr.holders.IntervalDayHolder;%0Aimport org.apache.drill.exec.expr.holders
+common.util
 .Decimal
 9Hol
@@ -1057,23 +1057,23 @@
 .Decimal
-9Holder
+Utility
 ;%0Aimport
@@ -1250,24 +1250,200 @@
 parseHolder;
+%0Aimport org.apache.drill.exec.expr.holders.Decimal9Holder;%0Aimport org.apache.drill.exec.expr.holders.IntervalDayHolder;%0Aimport org.apache.drill.exec.expr.holders.VarCharHolder;
 %0A%0Aimport com
@@ -1477,38 +1477,8 @@
 s;%0A%0A
-import java.math.BigDecimal;%0A%0A
 %0Apub
@@ -1608,18 +1608,16 @@
 class);%0A
-  
 %0A  publi
@@ -1712,20 +1712,16 @@
 lder();%0A
-    
 %0A    byt
@@ -1860,18 +1860,16 @@
 fer(
-s
+b
 .length
-()
 ).or

Fix ByteBuf allocation in o.a.d.exec.vector.ValueHolderHelper.getVarCharHolder()