Пример #1
0
 // TODO: cache?
 public static LiteralExpression newConstant(
     Object value,
     PDataType type,
     Integer maxLength,
     Integer scale,
     SortOrder sortOrder,
     Determinism determinism,
     boolean rowKeyOrderOptimizable)
     throws SQLException {
   if (value == null) {
     return (type == null)
         ? getNullLiteralExpression(determinism)
         : getTypedNullLiteralExpression(type, determinism);
   } else if (value instanceof Boolean) {
     return getBooleanLiteralExpression((Boolean) value, determinism);
   }
   PDataType actualType = PDataType.fromLiteral(value);
   // For array we should check individual element in it?
   // It would be costly though!!!!!
   // UpsertStatement can try to cast varchar to date type but PVarchar can't CoercibleTo Date or
   // Timestamp
   // otherwise TO_NUMBER like functions will fail
   if (!actualType.isCoercibleTo(type, value)
       && (!actualType.equals(PVarchar.INSTANCE)
           || !(type.equals(PDate.INSTANCE)
               || type.equals(PTimestamp.INSTANCE)
               || type.equals(PTime.INSTANCE)))) {
     throw TypeMismatchException.newException(type, actualType, value.toString());
   }
   value = type.toObject(value, actualType);
   byte[] b =
       type.isArrayType()
           ? ((PArrayDataType) type)
               .toBytes(
                   value, PArrayDataType.arrayBaseType(type), sortOrder, rowKeyOrderOptimizable)
           : type.toBytes(value, sortOrder);
   if (type == PVarchar.INSTANCE || type == PChar.INSTANCE) {
     if (type == PChar.INSTANCE && maxLength != null && b.length < maxLength) {
       if (rowKeyOrderOptimizable) {
         b = type.pad(b, maxLength, sortOrder);
       } else {
         b = StringUtil.padChar(b, maxLength);
       }
     } else if (value != null) {
       maxLength = ((String) value).length();
     }
   } else if (type.isArrayType()) {
     maxLength = ((PhoenixArray) value).getMaxLength();
   }
   if (b.length == 0) {
     return getTypedNullLiteralExpression(type, determinism);
   }
   if (maxLength == null) {
     maxLength = type == null || !type.isFixedWidth() ? null : type.getMaxLength(value);
   }
   return new LiteralExpression(value, type, b, maxLength, scale, sortOrder, determinism);
 }
Пример #2
0
 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);
   */
 }
Пример #3
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);
 }
Пример #4
0
  private void assertIteration(String dataColumns, String pk, Object[] values, String dataProps)
      throws Exception {
    String schemaName = "";
    String tableName = "T";
    Connection conn = DriverManager.getConnection(getUrl());
    String fullTableName =
        SchemaUtil.getTableName(
            SchemaUtil.normalizeIdentifier(schemaName), SchemaUtil.normalizeIdentifier(tableName));
    conn.createStatement()
        .execute(
            "CREATE TABLE "
                + fullTableName
                + "("
                + dataColumns
                + " CONSTRAINT pk PRIMARY KEY ("
                + pk
                + "))  "
                + (dataProps.isEmpty() ? "" : dataProps));
    PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
    PTable table = pconn.getTable(new PTableKey(pconn.getTenantId(), fullTableName));
    conn.close();
    StringBuilder buf = new StringBuilder("UPSERT INTO " + fullTableName + " VALUES(");
    for (int i = 0; i < values.length; i++) {
      buf.append("?,");
    }
    buf.setCharAt(buf.length() - 1, ')');
    PreparedStatement stmt = conn.prepareStatement(buf.toString());
    for (int i = 0; i < values.length; i++) {
      stmt.setObject(i + 1, values[i]);
    }
    stmt.execute();
    Iterator<Pair<byte[], List<KeyValue>>> iterator =
        PhoenixRuntime.getUncommittedDataIterator(conn);
    List<KeyValue> dataKeyValues = iterator.next().getSecond();
    KeyValue keyValue = dataKeyValues.get(0);

    List<SortOrder> sortOrders = Lists.newArrayListWithExpectedSize(table.getPKColumns().size());
    for (PColumn col : table.getPKColumns()) {
      sortOrders.add(col.getSortOrder());
    }
    RowKeySchema schema = table.getRowKeySchema();
    int minOffset = keyValue.getRowOffset();
    ImmutableBytesWritable ptr = new ImmutableBytesWritable();
    int nExpectedValues = values.length;
    for (int i = values.length - 1; i >= 0; i--) {
      if (values[i] == null) {
        nExpectedValues--;
      } else {
        break;
      }
    }
    int i = 0;
    int maxOffset =
        schema.iterator(keyValue.getRowArray(), minOffset, keyValue.getRowLength(), ptr);
    for (i = 0; i < schema.getFieldCount(); i++) {
      Boolean hasValue = schema.next(ptr, i, maxOffset);
      if (hasValue == null) {
        break;
      }
      assertTrue(hasValue);
      PDataType type = PDataType.fromLiteral(values[i]);
      SortOrder sortOrder = sortOrders.get(i);
      Object value = type.toObject(ptr, schema.getField(i).getDataType(), sortOrder);
      assertEquals(values[i], value);
    }
    assertEquals(nExpectedValues, i);
    assertNull(schema.next(ptr, i, maxOffset));

    for (i--; i >= 0; i--) {
      Boolean hasValue = schema.previous(ptr, i, minOffset);
      if (hasValue == null) {
        break;
      }
      assertTrue(hasValue);
      PDataType type = PDataType.fromLiteral(values[i]);
      SortOrder sortOrder = sortOrders.get(i);
      Object value = type.toObject(ptr, schema.getField(i).getDataType(), sortOrder);
      assertEquals(values[i], value);
    }
    assertEquals(-1, i);
    assertNull(schema.previous(ptr, i, minOffset));
  }