@TypeParameter("T")
 @SqlType(StandardTypes.BIGINT)
 public static long arrayPosition(
     @TypeParameter("T") Type type,
     @OperatorDependency(
             operator = EQUAL,
             returnType = StandardTypes.BOOLEAN,
             argumentTypes = {"T", "T"})
         MethodHandle equalMethodHandle,
     @SqlType("array(T)") Block array,
     @SqlType("T") Slice element) {
   int size = array.getPositionCount();
   for (int i = 0; i < size; i++) {
     if (!array.isNull(i)) {
       Slice arrayValue = type.getSlice(array, i);
       try {
         if ((boolean) equalMethodHandle.invokeExact(arrayValue, element)) {
           return i + 1; // result is 1-based (instead of 0)
         }
       } catch (Throwable t) {
         Throwables.propagateIfInstanceOf(t, Error.class);
         Throwables.propagateIfInstanceOf(t, PrestoException.class);
         throw new PrestoException(INTERNAL_ERROR, t);
       }
     }
   }
   return 0;
 }
Example #2
0
  @TypeParameter("T")
  @SqlType(StandardTypes.BOOLEAN)
  @Nullable
  public static Boolean contains(
      @TypeParameter("T") Type elementType,
      @OperatorDependency(
              operator = EQUAL,
              returnType = StandardTypes.BOOLEAN,
              argumentTypes = {"T", "T"})
          MethodHandle equals,
      @SqlType("array(T)") Block arrayBlock,
      @SqlType("T") Slice value) {
    boolean foundNull = false;
    for (int i = 0; i < arrayBlock.getPositionCount(); i++) {
      if (arrayBlock.isNull(i)) {
        foundNull = true;
        continue;
      }
      try {
        if ((boolean) equals.invokeExact(elementType.getSlice(arrayBlock, i), value)) {
          return true;
        }
      } catch (Throwable t) {
        Throwables.propagateIfInstanceOf(t, Error.class);
        Throwables.propagateIfInstanceOf(t, PrestoException.class);

        throw new PrestoException(INTERNAL_ERROR, t);
      }
    }
    if (foundNull) {
      return null;
    }
    return false;
  }
  public static Slice sliceElementAt(Type elementType, Block array, long index) {
    int position = checkedIndexToBlockPosition(array, index);
    if (array.isNull(position)) {
      return null;
    }

    return elementType.getSlice(array, position);
  }
  @UsedByGeneratedCode
  public static Slice sliceSubscript(Type elementType, Block array, long index) {
    checkIndex(array, index);
    int position = Ints.checkedCast(index - 1);
    if (array.isNull(position)) {
      return null;
    }

    return elementType.getSlice(array, position);
  }
 private static void appendTo(Type type, SliceOutput output, Block block) {
   if (type.getJavaType() == long.class) {
     output.appendLong(type.getLong(block, 0));
   } else if (type.getJavaType() == double.class) {
     output.appendDouble(type.getDouble(block, 0));
   } else if (type.getJavaType() == Slice.class) {
     output.appendBytes(type.getSlice(block, 0));
   } else if (type.getJavaType() == boolean.class) {
     output.appendByte(type.getBoolean(block, 0) ? 1 : 0);
   } else {
     throw new IllegalArgumentException("Unsupported type: " + type.getJavaType().getSimpleName());
   }
 }
Example #6
0
File: Row.java Project: cdma/presto
 private static Object getNativeContainerValue(Type type, Block block, int position) {
   if (block.isNull(position)) {
     return null;
   } else if (type.getJavaType() == boolean.class) {
     return type.getBoolean(block, position);
   } else if (type.getJavaType() == long.class) {
     return type.getLong(block, position);
   } else if (type.getJavaType() == double.class) {
     return type.getDouble(block, position);
   } else if (type.getJavaType() == Slice.class) {
     return type.getSlice(block, position);
   } else if (type.getJavaType() == Block.class) {
     return type.getObject(block, position);
   } else {
     throw new AssertionError("Unimplemented type: " + type);
   }
 }
 public static void input(Type type, SliceState state, Block block, int position) {
   if (state.getSlice() != null) {
     return;
   }
   state.setSlice(type.getSlice(block, position));
 }