예제 #1
0
  @Override
  public Datum eval(Tuple params) {
    Datum datum = params.get(0);
    if (datum instanceof NullDatum) return NullDatum.get();

    return DatumFactory.createInt4(datum.asByteArray().length * 8);
  }
  /**
   * It computes the value cardinality of a tuple range.
   *
   * @param dataType
   * @param start
   * @param end
   * @return
   */
  public static BigDecimal computeCardinality(
      DataType dataType, Datum start, Datum end, boolean inclusive) {
    BigDecimal columnCard;

    switch (dataType.getType()) {
      case BOOLEAN:
        columnCard = new BigDecimal(2);
        break;
      case CHAR:
        columnCard = new BigDecimal(end.asChar() - start.asChar());
        break;
      case BIT:
        columnCard = new BigDecimal(end.asByte() - start.asByte());
        break;
      case INT2:
        columnCard = new BigDecimal(end.asInt2() - start.asInt2());
        break;
      case INT4:
        columnCard = new BigDecimal(end.asInt4() - start.asInt4());
        break;
      case INT8:
        columnCard = new BigDecimal(end.asInt8() - start.asInt8());
        break;
      case FLOAT4:
        columnCard = new BigDecimal(end.asInt4() - start.asInt4());
        break;
      case FLOAT8:
        columnCard = new BigDecimal(end.asInt8() - start.asInt8());
        break;
      case TEXT:
        columnCard = new BigDecimal(end.asChars().charAt(0) - start.asChars().charAt(0));
        break;
      case DATE:
        columnCard = new BigDecimal(end.asInt4() - start.asInt4());
        break;
      case TIME:
      case TIMESTAMP:
        columnCard = new BigDecimal(end.asInt8() - start.asInt8());
        break;
      default:
        throw new UnsupportedOperationException(dataType + " is not supported yet");
    }

    return inclusive ? columnCard.add(new BigDecimal(1)) : columnCard;
  }