Ejemplo n.º 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);
 }
Ejemplo n.º 2
0
 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);
 }
Ejemplo n.º 3
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;
 }
Ejemplo n.º 4
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);
   */
 }
Ejemplo n.º 5
0
 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;
 }
Ejemplo n.º 6
0
 public static String toString(PDataType type, byte[] value) {
   boolean isString = type.isCoercibleTo(PVarchar.INSTANCE);
   return isString
       ? ("'" + type.toObject(value).toString() + "'")
       : type.toObject(value).toString();
 }
Ejemplo n.º 7
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));
  }