@Override
 public Block build() {
   if (currentEntrySize > 0) {
     throw new IllegalStateException("Current entry must be closed before the block can be built");
   }
   return new VariableWidthBlock(positions, sliceOutput.slice(), offsets, valueIsNull);
 }
예제 #2
0
  @Override
  public void serialize(MaxOrMinByState state, BlockBuilder out) {
    SliceOutput sliceOutput = new DynamicSliceOutput((int) state.getEstimatedSize());

    int keyLength = 0;
    if (state.getKey() != null && !state.getKey().isNull(0)) {
      keyLength = state.getKey().getLength(0);
    }
    sliceOutput.writeInt(keyLength);

    int valueLength = 0;
    if (state.getValue() != null && !state.getValue().isNull(0)) {
      valueLength = state.getValue().getLength(0);
    }
    sliceOutput.writeInt(valueLength);

    if (state.getKey() != null && !state.getKey().isNull(0)) {
      appendTo(keyType, sliceOutput, state.getKey());
    }
    if (state.getValue() != null && !state.getValue().isNull(0)) {
      appendTo(valueType, sliceOutput, state.getValue());
    }
    Slice slice = sliceOutput.slice();
    out.writeBytes(slice, 0, slice.length());
    out.closeEntry();
  }
  @Override
  public Block getRegion(int positionOffset, int length) {
    int positionCount = getPositionCount();
    if (positionOffset < 0 || length < 0 || positionOffset + length > positionCount) {
      throw new IndexOutOfBoundsException(
          "Invalid position " + positionOffset + " in block with " + positionCount + " positions");
    }

    return new VariableWidthBlock(
        positionOffset, length, sliceOutput.slice(), offsets, valueIsNull);
  }
  @Override
  public Block copyRegion(int positionOffset, int length) {
    int positionCount = getPositionCount();
    if (positionOffset < 0 || length < 0 || positionOffset + length > positionCount) {
      throw new IndexOutOfBoundsException(
          "Invalid position " + positionOffset + " in block with " + positionCount + " positions");
    }

    int[] newOffsets = Arrays.copyOfRange(offsets, positionOffset, positionOffset + length + 1);
    boolean[] newValueIsNull =
        Arrays.copyOfRange(valueIsNull, positionOffset, positionOffset + length);
    return new VariableWidthBlock(length, sliceOutput.slice(), newOffsets, newValueIsNull);
  }
  @Override
  public Block copyPositions(List<Integer> positions) {
    int finalLength = positions.stream().mapToInt(this::getLength).sum();
    SliceOutput newSlice = Slices.allocate(finalLength).getOutput();
    int[] newOffsets = new int[positions.size() + 1];
    boolean[] newValueIsNull = new boolean[positions.size()];

    for (int i = 0; i < positions.size(); i++) {
      int position = positions.get(i);
      if (isEntryNull(position)) {
        newValueIsNull[i] = true;
      } else {
        newSlice.appendBytes(
            sliceOutput
                .getUnderlyingSlice()
                .getBytes(getPositionOffset(position), getLength(position)));
      }
      newOffsets[i + 1] = newSlice.size();
    }
    return new VariableWidthBlock(positions.size(), newSlice.slice(), newOffsets, newValueIsNull);
  }
예제 #6
0
 @Override
 public RandomAccessBlock build() {
   return new VariableWidthRandomAccessBlock(
       type, sliceOutput.slice(), Arrays.copyOf(offsets, positions));
 }
예제 #7
0
  public static Expression toExpression(Object object, Type type) {
    requireNonNull(type, "type is null");

    if (object instanceof Expression) {
      return (Expression) object;
    }

    if (object == null) {
      if (type.equals(UNKNOWN)) {
        return new NullLiteral();
      }
      return new Cast(new NullLiteral(), type.getTypeSignature().toString(), false, true);
    }

    checkArgument(
        Primitives.wrap(type.getJavaType()).isInstance(object),
        "object.getClass (%s) and type.getJavaType (%s) do not agree",
        object.getClass(),
        type.getJavaType());

    if (type.equals(BIGINT)) {
      return new LongLiteral(object.toString());
    }

    if (type.equals(DOUBLE)) {
      Double value = (Double) object;
      // WARNING: the ORC predicate code depends on NaN and infinity not appearing in a tuple
      // domain, so
      // if you remove this, you will need to update the TupleDomainOrcPredicate
      if (value.isNaN()) {
        return new FunctionCall(new QualifiedName("nan"), ImmutableList.<Expression>of());
      } else if (value.equals(Double.NEGATIVE_INFINITY)) {
        return ArithmeticUnaryExpression.negative(
            new FunctionCall(new QualifiedName("infinity"), ImmutableList.<Expression>of()));
      } else if (value.equals(Double.POSITIVE_INFINITY)) {
        return new FunctionCall(new QualifiedName("infinity"), ImmutableList.<Expression>of());
      } else {
        return new DoubleLiteral(object.toString());
      }
    }

    if (type instanceof VarcharType) {
      if (object instanceof String) {
        object = Slices.utf8Slice((String) object);
      }

      if (object instanceof Slice) {
        Slice value = (Slice) object;
        int length = SliceUtf8.countCodePoints(value);

        if (length == ((VarcharType) type).getLength()) {
          return new StringLiteral(value.toStringUtf8());
        }

        return new Cast(
            new StringLiteral(value.toStringUtf8()), type.getDisplayName(), false, true);
      }

      throw new IllegalArgumentException(
          "object must be instance of Slice or String when type is VARCHAR");
    }

    if (type.equals(BOOLEAN)) {
      return new BooleanLiteral(object.toString());
    }

    if (object instanceof Block) {
      SliceOutput output = new DynamicSliceOutput(((Block) object).getSizeInBytes());
      BlockSerdeUtil.writeBlock(output, (Block) object);
      object = output.slice();
      // This if condition will evaluate to true: object instanceof Slice && !type.equals(VARCHAR)
    }

    if (object instanceof Slice) {
      // HACK: we need to serialize VARBINARY in a format that can be embedded in an expression to
      // be
      // able to encode it in the plan that gets sent to workers.
      // We do this by transforming the in-memory varbinary into a call to
      // from_base64(<base64-encoded value>)
      FunctionCall fromBase64 =
          new FunctionCall(
              new QualifiedName("from_base64"),
              ImmutableList.of(
                  new StringLiteral(VarbinaryFunctions.toBase64((Slice) object).toStringUtf8())));
      Signature signature = FunctionRegistry.getMagicLiteralFunctionSignature(type);
      return new FunctionCall(new QualifiedName(signature.getName()), ImmutableList.of(fromBase64));
    }

    Signature signature = FunctionRegistry.getMagicLiteralFunctionSignature(type);
    Expression rawLiteral = toExpression(object, FunctionRegistry.typeForMagicLiteral(type));

    return new FunctionCall(new QualifiedName(signature.getName()), ImmutableList.of(rawLiteral));
  }
예제 #8
0
    public UncompressedBlock build() {
      checkState(!positionOffsets.isEmpty(), "Cannot build an empty block");

      return new UncompressedBlock(
          positionOffsets.size(), new TupleInfo(type), sliceOutput.slice());
    }
 private static Slice blockToSlice(Block block) {
   // This function is strictly for testing use only
   SliceOutput sliceOutput = new DynamicSliceOutput(1000);
   BlockSerdeUtil.writeBlock(sliceOutput, block.copyRegion(0, block.getPositionCount()));
   return sliceOutput.slice();
 }