@Test
  public void testArrayPrependFunction11() throws Exception {
    Object[] o =
        new Object[] {
          BigDecimal.valueOf(2345), BigDecimal.valueOf(-23.45), BigDecimal.valueOf(785)
        };
    Object[] o2 =
        new Object[] {
          BigDecimal.valueOf(-19),
          BigDecimal.valueOf(2345),
          BigDecimal.valueOf(-23.45),
          BigDecimal.valueOf(785)
        };
    Object element = BigDecimal.valueOf(-19);
    PDataType baseType = PDecimal.INSTANCE;

    PhoenixArray arr = new PhoenixArray(baseType, o);
    PhoenixArray expected = new PhoenixArray(baseType, o2);
    test(
        arr,
        element,
        PDataType.fromTypeId(baseType.getSqlType() + PDataType.ARRAY_TYPE_BASE),
        null,
        null,
        baseType,
        null,
        null,
        expected,
        SortOrder.ASC,
        SortOrder.ASC);
  }
  @Test
  public void testArrayPrependFunction6() throws Exception {
    Object[] o = new Object[] {new Float(2.3), new Float(7.9), new Float(-9.6), new Float(2.3)};
    Object[] o2 =
        new Object[] {
          new Float(8.9), new Float(2.3), new Float(7.9), new Float(-9.6), new Float(2.3)
        };
    Object element = 8.9;
    PDataType baseType = PFloat.INSTANCE;

    PhoenixArray arr = new PhoenixArray.PrimitiveFloatPhoenixArray(baseType, o);
    PhoenixArray expected = new PhoenixArray.PrimitiveFloatPhoenixArray(baseType, o2);
    test(
        arr,
        element,
        PDataType.fromTypeId(baseType.getSqlType() + PDataType.ARRAY_TYPE_BASE),
        null,
        null,
        baseType,
        null,
        null,
        expected,
        SortOrder.ASC,
        SortOrder.ASC);
  }
  @Test
  public void testArrayPrependFunction14() throws Exception {
    Calendar calendar = Calendar.getInstance();
    java.util.Date currentDate = calendar.getTime();
    java.sql.Timestamp timestamp = new java.sql.Timestamp(currentDate.getTime());

    Object[] o = new Object[] {timestamp, timestamp, timestamp};
    Object[] o2 = new Object[] {timestamp, timestamp, timestamp, timestamp};
    PDataType baseType = PTimestamp.INSTANCE;

    PhoenixArray arr = new PhoenixArray(baseType, o);
    PhoenixArray expected = new PhoenixArray(baseType, o2);
    test(
        arr,
        timestamp,
        PDataType.fromTypeId(baseType.getSqlType() + PDataType.ARRAY_TYPE_BASE),
        null,
        null,
        baseType,
        null,
        null,
        expected,
        SortOrder.ASC,
        SortOrder.ASC);
  }
  @Test
  public void testForNullsWith509NullsAtBeginning() throws Exception {
    // 509 nulls at the beginning
    Object[] o = new Object[511];
    for (int i = 0; i < o.length - 2; i++) o[i] = null;

    o[o.length - 2] = "1";
    o[o.length - 1] = "2";

    Object[] o2 = new Object[512];
    for (int i = 0; i < o2.length - 2; i++) o2[i] = null;

    o2[o2.length - 2] = "1";
    o2[o2.length - 1] = "2";

    Object element = null;
    PDataType baseType = PVarchar.INSTANCE;

    PhoenixArray arr = new PhoenixArray(baseType, o);
    PhoenixArray expected = new PhoenixArray(baseType, o2);
    test(
        arr,
        element,
        PDataType.fromTypeId(baseType.getSqlType() + PDataType.ARRAY_TYPE_BASE),
        null,
        null,
        baseType,
        1,
        null,
        expected,
        SortOrder.ASC,
        SortOrder.DESC);
  }
示例#5
0
 /**
  * Imperfect estimate of row size given a PTable TODO: keep row count in stats table and use total
  * size / row count instead
  *
  * @param table
  * @return estimate of size in bytes of a row
  */
 public static long estimateRowSize(PTable table) {
   int keyLength = estimateKeyLength(table);
   long rowSize = 0;
   for (PColumn column : table.getColumns()) {
     if (!SchemaUtil.isPKColumn(column)) {
       PDataType type = column.getDataType();
       Integer maxLength = column.getMaxLength();
       int valueLength =
           !type.isFixedWidth()
               ? VAR_KV_LENGTH_ESTIMATE
               : maxLength == null ? type.getByteSize() : maxLength;
       rowSize +=
           KeyValue.getKeyValueDataStructureSize(
               keyLength,
               column.getFamilyName().getBytes().length,
               column.getName().getBytes().length,
               valueLength);
     }
   }
   // Empty key value
   rowSize +=
       KeyValue.getKeyValueDataStructureSize(
           keyLength,
           getEmptyColumnFamily(table).length,
           QueryConstants.EMPTY_COLUMN_BYTES.length,
           0);
   return rowSize;
 }
  @Test
  public void testArrayPrependFunction4() throws Exception {
    // offset array int
    Object[] o = new Object[Short.MAX_VALUE + 7];
    for (int i = 0; i < o.length; i++) {
      o[i] = "a";
    }
    Object[] o2 = new Object[Short.MAX_VALUE + 8];
    for (int i = 1; i < o2.length; i++) {
      o2[i] = "a";
    }
    Object element = "b";
    o2[0] = element;
    PDataType baseType = PVarchar.INSTANCE;

    PhoenixArray arr = new PhoenixArray(baseType, o);
    PhoenixArray expected = new PhoenixArray(baseType, o2);
    test(
        arr,
        element,
        PDataType.fromTypeId(baseType.getSqlType() + PDataType.ARRAY_TYPE_BASE),
        null,
        null,
        baseType,
        null,
        null,
        expected,
        SortOrder.ASC,
        SortOrder.ASC);
  }
示例#7
0
 @Override
 public String toString() {
   if (value == null) {
     return "null";
   }
   // TODO: move into PDataType?
   if (type.isCoercibleTo(PTimestamp.INSTANCE)) {
     return type + " " + type.toStringLiteral(value, null);
   }
   return type.toStringLiteral(value, null);
 }
示例#8
0
 private LiteralExpression(
     Object value, PDataType type, byte[] byteValue, Determinism determinism) {
   this(
       value,
       type,
       byteValue,
       type == null || !type.isFixedWidth() ? null : type.getMaxLength(value),
       null,
       SortOrder.getDefault(),
       determinism);
 }
 public LiteralParseNode literal(String value, String sqlTypeName) throws SQLException {
   PDataType expectedType =
       sqlTypeName == null
           ? null
           : PDataType.fromSqlTypeName(SchemaUtil.normalizeIdentifier(sqlTypeName));
   if (expectedType == null || !expectedType.isCoercibleTo(PTimestamp.INSTANCE)) {
     throw TypeMismatchException.newException(expectedType, PTimestamp.INSTANCE);
   }
   Object typedValue = expectedType.toObject(value);
   return new LiteralParseNode(typedValue);
 }
示例#10
0
 @Override
 public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
   Expression expression = getExpression();
   if (!expression.evaluate(tuple, ptr)) {
     return false;
   }
   PDataType type = expression.getDataType();
   Object value = formatter.format(type.toObject(ptr, expression.getSortOrder()));
   byte[] b = getDataType().toBytes(value);
   ptr.set(b);
   return true;
 }
 public LiteralParseNode literal(Object value, PDataType expectedType) throws SQLException {
   PDataType actualType = PDataType.fromLiteral(value);
   if (actualType != null && actualType != expectedType) {
     checkTypeMatch(expectedType, actualType);
     value = expectedType.toObject(value, actualType);
   }
   return new LiteralParseNode(value);
   /*
   Object typedValue = expectedType.toObject(value.toString());
   return new LiteralParseNode(typedValue);
   */
 }
示例#12
0
 @Override
 public void readFields(DataInput input) throws IOException {
   int encodedByteLengthAndBool = WritableUtils.readVInt(input);
   int byteLength = Math.abs(encodedByteLengthAndBool) - 1;
   this.byteValue = new byte[byteLength];
   input.readFully(byteValue, 0, byteLength);
   int sortOrderAndDeterminism = WritableUtils.readVInt(input);
   if (sortOrderAndDeterminism <= 2) {
     // client is on an older version
     this.determinism = encodedByteLengthAndBool > 0 ? Determinism.ALWAYS : Determinism.PER_ROW;
     this.sortOrder = SortOrder.fromSystemValue(sortOrderAndDeterminism);
     ;
   } else {
     int determinismOrdinal = (sortOrderAndDeterminism >> 2) - 1;
     this.determinism = Determinism.values()[determinismOrdinal];
     int sortOrderValue =
         sortOrderAndDeterminism & ((1 << 2) - 1); // get the least 2 significant bits
     this.sortOrder = SortOrder.fromSystemValue(sortOrderValue);
   }
   int typeOrdinal = WritableUtils.readVInt(input);
   if (typeOrdinal < 0) {
     this.type = null;
   } else {
     this.type = PDataType.values()[typeOrdinal];
   }
   if (this.byteValue.length == 0) {
     this.value = null;
   } else {
     this.value = this.type.toObject(byteValue, 0, byteValue.length, this.type, sortOrder);
   }
 }
示例#13
0
 public static PName padTenantIdIfNecessary(
     RowKeySchema schema, boolean isSalted, PName tenantId) {
   int pkPos = isSalted ? 1 : 0;
   String tenantIdStr = tenantId.getString();
   Field field = schema.getField(pkPos);
   PDataType dataType = field.getDataType();
   boolean isFixedWidth = dataType.isFixedWidth();
   Integer maxLength = field.getMaxLength();
   if (isFixedWidth && maxLength != null) {
     if (tenantIdStr.length() < maxLength) {
       tenantIdStr = (String) dataType.pad(tenantIdStr, maxLength);
       return PNameFactory.newName(tenantIdStr);
     }
   }
   return tenantId;
 }
示例#14
0
 /**
  * Estimate the max key length in bytes of the PK for a given table
  *
  * @param table the table
  * @return the max PK length
  */
 public static int estimateKeyLength(PTable table) {
   int maxKeyLength = 0;
   // Calculate the max length of a key (each part must currently be of a fixed width)
   int i = 0;
   List<PColumn> columns = table.getPKColumns();
   while (i < columns.size()) {
     PColumn keyColumn = columns.get(i++);
     PDataType type = keyColumn.getDataType();
     Integer maxLength = keyColumn.getMaxLength();
     maxKeyLength +=
         !type.isFixedWidth()
             ? VAR_LENGTH_ESTIMATE
             : maxLength == null ? type.getByteSize() : maxLength;
   }
   return maxKeyLength;
 }
示例#15
0
 public static boolean isBooleanNull(Expression child) {
   if (child != null) {
     return child
         == TYPED_NULL_EXPRESSIONS[
             PBoolean.INSTANCE.ordinal()
                 + PDataType.values().length * child.getDeterminism().ordinal()];
   }
   return false;
 }
示例#16
0
 @Override
 public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
   Expression arg = getChildren().get(0);
   if (!arg.evaluate(tuple, ptr)) {
     return false;
   }
   if (ptr.getLength() == 0) {
     return true;
   }
   long dateTime = inputCodec.decodeLong(ptr, arg.getSortOrder());
   DateTime jodaDT = new DateTime(dateTime);
   int day = jodaDT.getDayOfYear();
   PDataType returnDataType = getDataType();
   byte[] byteValue = new byte[returnDataType.getByteSize()];
   returnDataType.getCodec().encodeInt(day, byteValue, 0);
   ptr.set(byteValue);
   return true;
 }
示例#17
0
 // TODO: cache?
 public static LiteralExpression newConstant(Object value, Determinism determinism) {
   if (value instanceof Boolean) {
     return getBooleanLiteralExpression((Boolean) value, determinism);
   } else if (value == null) {
     return getNullLiteralExpression(determinism);
   }
   PDataType type = PDataType.fromLiteral(value);
   byte[] b = type.toBytes(value);
   if (type.isNull(b)) {
     return getTypedNullLiteralExpression(type, determinism);
   }
   if (type == PVarchar.INSTANCE) {
     String s = (String) value;
     if (s.length() == b.length) { // single byte characters only
       type = PChar.INSTANCE;
     }
   }
   return new LiteralExpression(value, type, b, determinism);
 }
  @Test
  public void testForNullsAllNulls() throws Exception {
    Object element = null;
    PDataType baseType = PChar.INSTANCE;

    PhoenixArray arr = null;
    PhoenixArray expected = null;
    test(
        arr,
        element,
        PDataType.fromTypeId(baseType.getSqlType() + PDataType.ARRAY_TYPE_BASE),
        4,
        null,
        baseType,
        1,
        null,
        expected,
        SortOrder.ASC,
        SortOrder.DESC);
  }
示例#19
0
 static {
   for (Determinism determinism : Determinism.values()) {
     NULL_EXPRESSIONS[determinism.ordinal()] = new LiteralExpression(null, determinism);
     for (int i = 0; i < PDataType.values().length; i++) {
       TYPED_NULL_EXPRESSIONS[i + PDataType.values().length * determinism.ordinal()] =
           new LiteralExpression(PDataType.values()[i], determinism);
     }
     BOOLEAN_EXPRESSIONS[determinism.ordinal()] =
         new LiteralExpression(
             Boolean.FALSE,
             PBoolean.INSTANCE,
             PBoolean.INSTANCE.toBytes(Boolean.FALSE),
             determinism);
     BOOLEAN_EXPRESSIONS[Determinism.values().length + determinism.ordinal()] =
         new LiteralExpression(
             Boolean.TRUE,
             PBoolean.INSTANCE,
             PBoolean.INSTANCE.toBytes(Boolean.TRUE),
             determinism);
   }
 }
  @Test
  public void testArrayPrependFunction8() throws Exception {
    Object[] o = new Object[] {123l, 677l, 98789l, -78989l, 66787l};
    Object[] o2 = new Object[] {543l, 123l, 677l, 98789l, -78989l, 66787l};
    Object element = 543l;
    PDataType baseType = PLong.INSTANCE;

    PhoenixArray arr = new PhoenixArray.PrimitiveLongPhoenixArray(baseType, o);
    PhoenixArray expected = new PhoenixArray.PrimitiveLongPhoenixArray(baseType, o2);
    test(
        arr,
        element,
        PDataType.fromTypeId(baseType.getSqlType() + PDataType.ARRAY_TYPE_BASE),
        null,
        null,
        baseType,
        null,
        null,
        expected,
        SortOrder.ASC,
        SortOrder.ASC);
  }
  @Test
  public void testArrayPrependFunctionBoolean() throws Exception {
    Boolean[] o = new Boolean[] {true, false, false, true};
    Boolean[] o2 = new Boolean[] {false, true, false, false, true};
    Boolean element = false;
    PDataType baseType = PBoolean.INSTANCE;

    PhoenixArray arr = new PhoenixArray.PrimitiveBooleanPhoenixArray(baseType, o);
    PhoenixArray expected = new PhoenixArray.PrimitiveBooleanPhoenixArray(baseType, o2);
    test(
        arr,
        element,
        PDataType.fromTypeId(baseType.getSqlType() + PDataType.ARRAY_TYPE_BASE),
        null,
        null,
        baseType,
        null,
        null,
        expected,
        SortOrder.ASC,
        SortOrder.ASC);
  }
  @Test
  public void testForNullsWithNullsInMiddle() throws Exception {
    Object[] o = new Object[] {"1   ", "2   ", null, "3   ", "4   "};
    Object[] o2 = new Object[] {null, "1   ", "2   ", null, "3   ", "4   "};
    Object element = null;
    PDataType baseType = PVarchar.INSTANCE;

    PhoenixArray arr = new PhoenixArray(baseType, o);
    PhoenixArray expected = new PhoenixArray(baseType, o2);
    test(
        arr,
        element,
        PDataType.fromTypeId(baseType.getSqlType() + PDataType.ARRAY_TYPE_BASE),
        null,
        null,
        baseType,
        1,
        null,
        expected,
        SortOrder.ASC,
        SortOrder.DESC);
  }
  @Test
  public void testArrayPrependFunction10() throws Exception {
    Object[] o = new Object[] {(byte) 4, (byte) 8, (byte) 9};
    Object[] o2 = new Object[] {(byte) 6, (byte) 4, (byte) 8, (byte) 9};
    Object element = (byte) 6;
    PDataType baseType = PTinyint.INSTANCE;

    PhoenixArray arr = new PhoenixArray.PrimitiveBytePhoenixArray(baseType, o);
    PhoenixArray expected = new PhoenixArray.PrimitiveBytePhoenixArray(baseType, o2);
    test(
        arr,
        element,
        PDataType.fromTypeId(baseType.getSqlType() + PDataType.ARRAY_TYPE_BASE),
        null,
        null,
        baseType,
        null,
        null,
        expected,
        SortOrder.ASC,
        SortOrder.ASC);
  }
  @Test
  public void testArrayPrependFunction1() throws Exception {
    Object[] o = new Object[] {1, 2, -3, 4};
    Object[] o2 = new Object[] {5, 1, 2, -3, 4};
    Object element = 5;
    PDataType baseType = PInteger.INSTANCE;

    PhoenixArray arr = new PhoenixArray.PrimitiveIntPhoenixArray(baseType, o);
    PhoenixArray expected = new PhoenixArray.PrimitiveIntPhoenixArray(baseType, o2);
    test(
        arr,
        element,
        PDataType.fromTypeId(baseType.getSqlType() + PDataType.ARRAY_TYPE_BASE),
        null,
        null,
        baseType,
        null,
        null,
        expected,
        SortOrder.ASC,
        SortOrder.ASC);
  }
 public LiteralParseNode coerce(LiteralParseNode literalNode, PDataType expectedType)
     throws SQLException {
   PDataType actualType = literalNode.getType();
   if (actualType != null) {
     Object before = literalNode.getValue();
     checkTypeMatch(expectedType, actualType);
     Object after = expectedType.toObject(before, actualType);
     if (before != after) {
       literalNode = literal(after);
     }
   }
   return literalNode;
 }
  @Test
  public void testArrayPrependFunction21() throws Exception {
    Object[] o = new Object[] {4.78, 9.54, 2.34, -9.675, Double.MAX_VALUE};
    Object[] o2 = new Object[] {12.67, 4.78, 9.54, 2.34, -9.675, Double.MAX_VALUE};
    Object element = 12.67;
    PDataType baseType = PDouble.INSTANCE;

    PhoenixArray arr = new PhoenixArray.PrimitiveDoublePhoenixArray(baseType, o);
    PhoenixArray expected = new PhoenixArray.PrimitiveDoublePhoenixArray(baseType, o2);
    test(
        arr,
        element,
        PDataType.fromTypeId(baseType.getSqlType() + PDataType.ARRAY_TYPE_BASE),
        null,
        null,
        baseType,
        null,
        null,
        expected,
        SortOrder.ASC,
        SortOrder.DESC);
  }
  @Test
  public void testArrayPrependFunction20() throws Exception {
    Object[] o = new Object[] {"1   ", "2   ", "3   ", "4   "};
    Object[] o2 = new Object[] {"5", "1", "2", "3", "4"};
    Object element = "5";
    PDataType baseType = PChar.INSTANCE;

    PhoenixArray arr = new PhoenixArray(baseType, o);
    PhoenixArray expected = new PhoenixArray(baseType, o2);
    test(
        arr,
        element,
        PDataType.fromTypeId(baseType.getSqlType() + PDataType.ARRAY_TYPE_BASE),
        4,
        null,
        baseType,
        1,
        null,
        expected,
        SortOrder.DESC,
        SortOrder.DESC);
  }
  @Test
  public void testArrayPrependFunction9() throws Exception {
    Object[] o = new Object[] {(short) 34, (short) -23, (short) -89, (short) 999, (short) 34};
    Object[] o2 =
        new Object[] {(short) 7, (short) 34, (short) -23, (short) -89, (short) 999, (short) 34};
    Object element = (short) 7;
    PDataType baseType = PSmallint.INSTANCE;

    PhoenixArray arr = new PhoenixArray.PrimitiveShortPhoenixArray(baseType, o);
    PhoenixArray expected = new PhoenixArray.PrimitiveShortPhoenixArray(baseType, o2);
    test(
        arr,
        element,
        PDataType.fromTypeId(baseType.getSqlType() + PDataType.ARRAY_TYPE_BASE),
        null,
        null,
        baseType,
        null,
        null,
        expected,
        SortOrder.ASC,
        SortOrder.ASC);
  }
  @Test
  public void testArrayPrependFunction24() throws Exception {
    byte[][] o =
        new byte[][] {
          new byte[] {2, 0},
          new byte[] {13, 3},
          new byte[] {5, 3},
          new byte[] {6, 3},
          new byte[] {2, 5}
        };
    byte[][] o2 =
        new byte[][] {
          new byte[] {5, 6},
          new byte[] {2, 0},
          new byte[] {13, 3},
          new byte[] {5, 3},
          new byte[] {6, 3},
          new byte[] {2, 5}
        };
    byte[] element = new byte[] {5, 6};
    PDataType baseType = PBinary.INSTANCE;

    PhoenixArray arr = new PhoenixArray(baseType, o);
    PhoenixArray expected = new PhoenixArray(baseType, o2);
    test(
        arr,
        element,
        PDataType.fromTypeId(baseType.getSqlType() + PDataType.ARRAY_TYPE_BASE),
        3,
        null,
        baseType,
        1,
        null,
        expected,
        SortOrder.ASC,
        SortOrder.DESC);
  }
示例#30
0
  @Override
  public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    BigDecimal finalResult = BigDecimal.ZERO;

    for (int i = 0; i < children.size(); i++) {
      if (!children.get(i).evaluate(tuple, ptr)) {
        return false;
      }
      if (ptr.getLength() == 0) {
        return true;
      }
      BigDecimal value;
      PDataType type = children.get(i).getDataType();
      SortOrder sortOrder = children.get(i).getSortOrder();
      if (type == PTimestamp.INSTANCE || type == PUnsignedTimestamp.INSTANCE) {
        value = (BigDecimal) (PDecimal.INSTANCE.toObject(ptr, type, sortOrder));
      } else if (type.isCoercibleTo(PDecimal.INSTANCE)) {
        value =
            (((BigDecimal) PDecimal.INSTANCE.toObject(ptr, sortOrder))
                    .multiply(QueryConstants.BD_MILLIS_IN_DAY))
                .setScale(6, RoundingMode.HALF_UP);
      } else if (type.isCoercibleTo(PDouble.INSTANCE)) {
        value =
            ((BigDecimal.valueOf(type.getCodec().decodeDouble(ptr, sortOrder)))
                    .multiply(QueryConstants.BD_MILLIS_IN_DAY))
                .setScale(6, RoundingMode.HALF_UP);
      } else {
        value = BigDecimal.valueOf(type.getCodec().decodeLong(ptr, sortOrder));
      }
      finalResult = finalResult.add(value);
    }
    Timestamp ts = DateUtil.getTimestamp(finalResult);
    byte[] resultPtr = new byte[getDataType().getByteSize()];
    PTimestamp.INSTANCE.toBytes(ts, resultPtr, 0);
    ptr.set(resultPtr);
    return true;
  }