Beispiel #1
0
  public static FieldValue evaluate(
      DefineFunction defineFunction, List<FieldValue> values, EvaluationContext context) {
    List<ParameterField> parameterFields = defineFunction.getParameterFields();

    if (parameterFields.size() < 1) {
      throw new InvalidFeatureException(defineFunction);
    } // End if

    if (parameterFields.size() != values.size()) {
      throw new EvaluationException();
    }

    FunctionEvaluationContext functionContext = new FunctionEvaluationContext(context);

    for (int i = 0; i < parameterFields.size(); i++) {
      ParameterField parameterField = parameterFields.get(i);

      FieldValue value = FieldValueUtil.refine(parameterField, values.get(i));

      functionContext.declare(parameterField.getName(), value);
    }

    Expression expression = defineFunction.getExpression();
    if (expression == null) {
      throw new InvalidFeatureException(defineFunction);
    }

    FieldValue result = ExpressionUtil.evaluate(expression, functionContext);

    return FieldValueUtil.refine(defineFunction.getDataType(), defineFunction.getOptype(), result);
  }
 public Token getSuccessor(Token token) {
   List tokens = sortedTokens();
   int index = Collections.binarySearch(tokens, token);
   assert index >= 0
       : token + " not found in " + StringUtils.join(tokenToEndPointMap.keySet(), ", ");
   return (Token) ((index == (tokens.size() - 1)) ? tokens.get(0) : tokens.get(index + 1));
 }
Beispiel #3
0
 public String getString(int start, int end) {
   StringBuilder buffer = new StringBuilder(parsedArgs.get(start));
   for (int i = start + 1; i < end + 1; ++i) {
     buffer.append(" ").append(parsedArgs.get(i));
   }
   return buffer.toString();
 }
Beispiel #4
0
    @Override
    public FieldValue evaluate(List<FieldValue> values) {

      if (values.size() != 2) {
        throw new EvaluationException();
      }

      FieldValue left = values.get(0);
      FieldValue right = values.get(1);

      // "If one of the input fields of a simple arithmetic function is a missing value, the result
      // evaluates to missing value"
      if (left == null || right == null) {
        return null;
      }

      DataType dataType = TypeUtil.getResultDataType(left.getDataType(), right.getDataType());

      Number result;

      try {
        result = evaluate(left.asNumber(), right.asNumber());
      } catch (ArithmeticException ae) {
        throw new InvalidResultException(null);
      }

      return FieldValueUtil.create(cast(dataType, result));
    }
Beispiel #5
0
    @Override
    public FieldValue evaluate(List<FieldValue> values) {
      checkArguments(values, 2);

      FieldValue left = values.get(0);
      FieldValue right = values.get(1);

      Boolean result = evaluate((left).compareToValue(right));

      return FieldValueUtil.create(result);
    }
Beispiel #6
0
    @Override
    public FieldValue evaluate(List<FieldValue> values) {
      checkVariableArguments(values, 2);

      Boolean result = (values.get(0)).asBoolean();

      for (int i = 1; i < values.size(); i++) {
        result = evaluate(result, (values.get(i)).asBoolean());
      }

      return FieldValueUtil.create(result);
    }
Beispiel #7
0
  public static QuantileDigest merge(List<QuantileDigest> digests) {
    if (digests.isEmpty()) {
      throw new RuntimeException("Digests to be unioned should not be empty!");
    }

    QuantileDigest ret = digests.get(0);

    for (int i = 1; i < digests.size(); i++) {
      ret.merge(digests.get(i));
    }

    return ret;
  }
Beispiel #8
0
  private void processPrimaryConstructor(
      @NotNull TopDownAnalysisContext c,
      @NotNull MutableClassDescriptor classDescriptor,
      @NotNull JetClass klass) {
    // TODO : not all the parameters are real properties
    JetScope memberScope = classDescriptor.getScopeForClassHeaderResolution();
    ConstructorDescriptor constructorDescriptor =
        descriptorResolver.resolvePrimaryConstructorDescriptor(
            memberScope, classDescriptor, klass, trace);
    if (constructorDescriptor != null) {
      List<ValueParameterDescriptor> valueParameterDescriptors =
          constructorDescriptor.getValueParameters();
      List<JetParameter> primaryConstructorParameters = klass.getPrimaryConstructorParameters();
      assert valueParameterDescriptors.size() == primaryConstructorParameters.size();
      List<ValueParameterDescriptor> notProperties = new ArrayList<ValueParameterDescriptor>();
      for (ValueParameterDescriptor valueParameterDescriptor : valueParameterDescriptors) {
        JetParameter parameter =
            primaryConstructorParameters.get(valueParameterDescriptor.getIndex());
        if (parameter.getValOrVarNode() != null) {
          PropertyDescriptor propertyDescriptor =
              descriptorResolver.resolvePrimaryConstructorParameterToAProperty(
                  classDescriptor, valueParameterDescriptor, memberScope, parameter, trace);
          classDescriptor.getBuilder().addPropertyDescriptor(propertyDescriptor);
          c.getPrimaryConstructorParameterProperties().put(parameter, propertyDescriptor);
        } else {
          notProperties.add(valueParameterDescriptor);
        }
      }

      if (classDescriptor.getKind() != ClassKind.TRAIT) {
        classDescriptor.setPrimaryConstructor(constructorDescriptor);
        classDescriptor.addConstructorParametersToInitializersScope(notProperties);
      }
    }
  }
Beispiel #9
0
 private ImmutableList<ColumnIdent> getPartitionedBy() {
   ImmutableList.Builder<ColumnIdent> builder = ImmutableList.builder();
   for (List<String> partitionedByInfo : partitionedByList) {
     builder.add(ColumnIdent.fromPath(partitionedByInfo.get(0)));
   }
   return builder.build();
 }
Beispiel #10
0
 public String getJoinedStrings(int initialIndex) {
   initialIndex = originalArgIndices.get(initialIndex);
   StringBuilder buffer = new StringBuilder(originalArgs[initialIndex]);
   for (int i = initialIndex + 1; i < originalArgs.length; ++i) {
     buffer.append(" ").append(originalArgs[i]);
   }
   return buffer.toString();
 }
Beispiel #11
0
    @Override
    public FieldValue evaluate(List<FieldValue> values) {
      checkVariableArguments(values, 2);

      Boolean result = evaluate(values.get(0), values.subList(1, values.size()));

      return FieldValueUtil.create(result);
    }
Beispiel #12
0
 @Override
 public PColumn getPKColumn(String name) throws ColumnNotFoundException {
   List<PColumn> columns = columnsByName.get(name);
   int size = columns.size();
   if (size == 0) {
     throw new ColumnNotFoundException(name);
   }
   if (size > 1) {
     do {
       PColumn column = columns.get(--size);
       if (column.getFamilyName() == null) {
         return column;
       }
     } while (size > 0);
     throw new ColumnNotFoundException(name);
   }
   return columns.get(0);
 }
Beispiel #13
0
  public InetAddress getFirstEndpoint() {
    assert tokenToEndPointMap.size() > 0;

    lock.readLock().lock();
    try {
      return tokenToEndPointMap.get(sortedTokens.get(0));
    } finally {
      lock.readLock().unlock();
    }
  }
Beispiel #14
0
    @Override
    public FieldValue evaluate(List<FieldValue> values) {
      checkArguments(values, 1, true);

      FieldValue value = values.get(0);

      Boolean result = evaluate(value);

      return FieldValueUtil.create(result);
    }
Beispiel #15
0
    @Override
    public FieldValue evaluate(List<FieldValue> values) {
      checkArguments(values, 1);

      FieldValue value = values.get(0);

      Number result = cast(getResultType(value.getDataType()), evaluate(value.asNumber()));

      return FieldValueUtil.create(result);
    }
Beispiel #16
0
    @Override
    public FieldValue evaluate(List<FieldValue> values) {
      checkArguments(values, 1);

      FieldValue value = values.get(0);

      String result = evaluate(value.asString());

      return FieldValueUtil.create(result);
    }
Beispiel #17
0
 @Override
 public void write(DataOutput output) throws IOException {
   Bytes.writeByteArray(output, name.getBytes());
   WritableUtils.writeVInt(output, type.ordinal());
   WritableUtils.writeVLong(output, sequenceNumber);
   output.writeLong(timeStamp);
   Bytes.writeByteArray(
       output, pkName == null ? ByteUtil.EMPTY_BYTE_ARRAY : Bytes.toBytes(pkName));
   WritableUtils.writeVInt(output, allColumns.size());
   for (int i = 0; i < allColumns.size(); i++) {
     PColumn column = allColumns.get(i);
     column.write(output);
   }
   stats.write(output);
 }
Beispiel #18
0
 @Override
 public PColumn getColumn(String name) throws ColumnNotFoundException, AmbiguousColumnException {
   List<PColumn> columns = columnsByName.get(name);
   int size = columns.size();
   if (size == 0) {
     throw new ColumnNotFoundException(name);
   }
   if (size > 1) {
     for (PColumn column : columns) {
       if (QueryConstants.DEFAULT_COLUMN_FAMILY.equals(column.getFamilyName().getString())) {
         // Allow ambiguity with default column, since a user would not know how to prefix it.
         return column;
       }
     }
     throw new AmbiguousColumnException(name);
   }
   return columns.get(0);
 }
 /**
  * @param buckets list of buckets, sorted from newest to oldest, from which to return the newest
  *     bucket within thresholds.
  * @param minThreshold minimum number of sstables in a bucket to qualify.
  * @param maxThreshold maximum number of sstables to compact at once (the returned bucket will be
  *     trimmed down to this).
  * @return a bucket (list) of sstables to compact.
  */
 @VisibleForTesting
 static List<SSTableReader> newestBucket(
     List<List<SSTableReader>> buckets,
     int minThreshold,
     int maxThreshold,
     long now,
     long baseTime) {
   // If the "incoming window" has at least minThreshold SSTables, choose that one.
   // For any other bucket, at least 2 SSTables is enough.
   // In any case, limit to maxThreshold SSTables.
   Target incomingWindow = getInitialTarget(now, baseTime);
   for (List<SSTableReader> bucket : buckets) {
     if (bucket.size() >= minThreshold
         || (bucket.size() >= 2 && !incomingWindow.onTarget(bucket.get(0).getMinTimestamp())))
       return trimToThreshold(bucket, maxThreshold);
   }
   return Collections.emptyList();
 }
  // batch job (possibly real-time using streaming APIs)
  public static void createProductAssociativityGraphPerCategory() {
    Set<String> customers = purchasesCache.keySet();
    customers.forEach(
        customer -> {
          List<String> customerPurchases = Lists.newArrayList(purchasesCache.get(customer));
          for (int i = 0; i < customerPurchases.size(); i++) {
            for (int j = i + 1; j < customerPurchases.size(); j++) {
              Product product1 = productCache.get(customerPurchases.get(i));
              Product product2 = productCache.get(customerPurchases.get(j));

              if (product1.category.equals(product2.category)) {
                ProductAssociativityGraph graph =
                    productAssociativityGraphMap.getOrDefault(
                        product1.category, ProductAssociativityGraph.create());

                graph.addAssociation(Vertex.create(product1.id), Vertex.create(product2.id), 1);
                productAssociativityGraphMap.putIfAbsent(product1.category, graph);
              }
            }
          }
        });
  }
  @NotNull
  private List<TypeProjection> getTypeArgsOfType(
      @NotNull JetType autoType,
      @NotNull ClassifierDescriptor classifier,
      @NotNull List<TypeAndVariance> typesFromSuper) {
    List<TypeProjection> autoArguments = autoType.getArguments();

    if (!(classifier instanceof ClassDescriptor)) {
      assert autoArguments.isEmpty()
          : "Unexpected type arguments when type constructor is not ClassDescriptor, type = "
              + autoType;
      return autoArguments;
    }

    List<List<TypeProjectionAndVariance>> typeArgumentsFromSuper =
        calculateTypeArgumentsFromSuper((ClassDescriptor) classifier, typesFromSuper);

    // Modify type arguments using info from typesFromSuper
    List<TypeProjection> resultArguments = Lists.newArrayList();
    for (TypeParameterDescriptor parameter : classifier.getTypeConstructor().getParameters()) {
      TypeProjection argument = autoArguments.get(parameter.getIndex());

      JetType argumentType = argument.getType();
      List<TypeProjectionAndVariance> projectionsFromSuper =
          typeArgumentsFromSuper.get(parameter.getIndex());
      List<TypeAndVariance> argTypesFromSuper = getTypes(projectionsFromSuper);

      JetType type =
          modifyTypeAccordingToSuperMethods(argumentType, argTypesFromSuper, TYPE_ARGUMENT);
      Variance projectionKind =
          calculateArgumentProjectionKindFromSuper(argument, projectionsFromSuper);

      resultArguments.add(new TypeProjection(projectionKind, type));
    }
    return resultArguments;
  }
Beispiel #22
0
  /**
   * iterator over the Tokens in the given ring, starting with the token for the node owning start
   * (which does not have to be a Token in the ring)
   *
   * @param includeMin True if the minimum token should be returned in the ring even if it has no
   *     owner.
   */
  public static Iterator<Token> ringIterator(final List ring, Token start, boolean includeMin) {
    assert ring.size() > 0;
    // insert the minimum token (at index == -1) if we were asked to include it and it isn't a
    // member of the ring
    final boolean insertMin =
        (includeMin && !ring.get(0).equals(StorageService.getPartitioner().getMinimumToken()))
            ? true
            : false;

    int i = Collections.binarySearch(ring, start);
    if (i < 0) {
      i = (i + 1) * (-1);
      if (i >= ring.size()) i = insertMin ? -1 : 0;
    }

    final int startIndex = i;
    return new AbstractIterator<Token>() {
      int j = startIndex;

      protected Token computeNext() {
        if (j < -1) return endOfData();
        try {
          // return minimum for index == -1
          if (j == -1) return StorageService.getPartitioner().getMinimumToken();
          // return ring token for other indexes
          return (Token) ring.get(j);
        } finally {
          j++;
          if (j == ring.size()) j = insertMin ? -1 : 0;
          if (j == startIndex)
            // end iteration
            j = -2;
        }
      }
    };
  }
Beispiel #23
0
 @NotNull
 private Instruction getNextPosition(int currentPosition) {
   int targetPosition = currentPosition + 1;
   assert targetPosition < mutableInstructionList.size() : currentPosition;
   return mutableInstructionList.get(targetPosition);
 }
Beispiel #24
0
 @Override
 @NotNull
 public SubroutineEnterInstruction getEnterInstruction() {
   return (SubroutineEnterInstruction) mutableInstructionList.get(0);
 }
Beispiel #25
0
  public MutationState dropColumn(DropColumnStatement statement) throws SQLException {
    connection.rollback();
    boolean wasAutoCommit = connection.getAutoCommit();
    try {
      connection.setAutoCommit(false);
      TableName tableNameNode = statement.getTableName();
      String schemaName = tableNameNode.getSchemaName();
      String tableName = tableNameNode.getTableName();
      PTable table = getLatestTable(schemaName, tableName); // TODO: Do in resolver?
      boolean retried = false;
      while (true) {
        final ColumnResolver resolver = FromCompiler.getResolver(statement, connection);
        ColumnRef columnRef = null;
        try {
          columnRef = resolver.resolveColumn((ColumnParseNode) statement.getColumnRef());
        } catch (ColumnNotFoundException e) {
          if (statement.ifExists()) {
            return new MutationState(0, connection);
          }
          throw e;
        }
        TableRef tableRef = columnRef.getTableRef();
        PColumn columnToDrop = columnRef.getColumn();
        if (SchemaUtil.isPKColumn(columnToDrop)) {
          throw new SQLExceptionInfo.Builder(SQLExceptionCode.CANNOT_DROP_PK)
              .setColumnName(columnToDrop.getName().getString())
              .build()
              .buildException();
        }
        int columnCount = table.getColumns().size() - 1;
        String familyName = null;
        List<String> binds = Lists.newArrayListWithExpectedSize(4);
        StringBuilder buf =
            new StringBuilder(
                "DELETE FROM " + TYPE_SCHEMA + ".\"" + TYPE_TABLE + "\" WHERE " + TABLE_SCHEM_NAME);
        if (schemaName == null || schemaName.length() == 0) {
          buf.append(" IS NULL AND ");
        } else {
          buf.append(" = ? AND ");
          binds.add(schemaName);
        }
        buf.append(TABLE_NAME_NAME + " = ? AND " + COLUMN_NAME + " = ? AND " + TABLE_CAT_NAME);
        binds.add(tableName);
        binds.add(columnToDrop.getName().getString());
        if (columnToDrop.getFamilyName() == null) {
          buf.append(" IS NULL");
        } else {
          buf.append(" = ?");
          binds.add(familyName = columnToDrop.getFamilyName().getString());
        }

        PreparedStatement colDelete = connection.prepareStatement(buf.toString());
        for (int i = 0; i < binds.size(); i++) {
          colDelete.setString(i + 1, binds.get(i));
        }
        colDelete.execute();

        PreparedStatement colUpdate = connection.prepareStatement(UPDATE_COLUMN_POSITION);
        colUpdate.setString(1, schemaName);
        colUpdate.setString(2, tableName);
        for (int i = columnToDrop.getPosition() + 1; i < table.getColumns().size(); i++) {
          PColumn column = table.getColumns().get(i);
          colUpdate.setString(3, column.getName().getString());
          colUpdate.setString(
              4, column.getFamilyName() == null ? null : column.getFamilyName().getString());
          colUpdate.setInt(5, i);
          colUpdate.execute();
        }
        final long seqNum = table.getSequenceNumber() + 1;
        PreparedStatement tableUpsert = connection.prepareStatement(MUTATE_TABLE);
        tableUpsert.setString(1, schemaName);
        tableUpsert.setString(2, tableName);
        tableUpsert.setString(3, table.getType().getSerializedValue());
        tableUpsert.setLong(4, seqNum);
        tableUpsert.setInt(5, columnCount);
        tableUpsert.execute();

        final List<Mutation> tableMetaData = connection.getMutationState().toMutations();
        connection.rollback();
        // If we're dropping the last KV colum, we have to pass an indication along to the
        // dropColumn call
        // to populate a new empty KV column
        byte[] emptyCF = null;
        if (table.getType() != PTableType.VIEW
            && !SchemaUtil.isPKColumn(columnToDrop)
            && table.getColumnFamilies().get(0).getName().equals(columnToDrop.getFamilyName())
            && table.getColumnFamilies().get(0).getColumns().size() == 1) {
          emptyCF =
              SchemaUtil.getEmptyColumnFamily(
                  table.getColumnFamilies().subList(1, table.getColumnFamilies().size()));
        }
        MetaDataMutationResult result =
            connection
                .getQueryServices()
                .dropColumn(
                    tableMetaData,
                    emptyCF != null
                            && Bytes.compareTo(emptyCF, QueryConstants.EMPTY_COLUMN_BYTES) == 0
                        ? emptyCF
                        : null);
        try {
          MutationCode code = processMutationResult(schemaName, tableName, result);
          if (code == MutationCode.COLUMN_NOT_FOUND) {
            connection.addTable(schemaName, result.getTable());
            if (!statement.ifExists()) {
              throw new ColumnNotFoundException(
                  schemaName, tableName, familyName, columnToDrop.getName().getString());
            }
            return new MutationState(0, connection);
          }
          connection.removeColumn(
              schemaName,
              tableName,
              familyName,
              columnToDrop.getName().getString(),
              seqNum,
              result.getMutationTime());
          // If we have a VIEW, then only delete the metadata, and leave the table data alone
          if (table.getType() != PTableType.VIEW) {
            connection.setAutoCommit(true);
            Long scn = connection.getSCN();
            // Delete everything in the column. You'll still be able to do queries at earlier
            // timestamps
            long ts = (scn == null ? result.getMutationTime() : scn);
            MutationPlan plan =
                new PostDDLCompiler(connection)
                    .compile(tableRef, emptyCF, Collections.singletonList(columnToDrop), ts);
            return connection.getQueryServices().updateData(plan);
          }
          return new MutationState(0, connection);
        } catch (ConcurrentTableMutationException e) {
          if (retried) {
            throw e;
          }
          table = connection.getPMetaData().getSchema(schemaName).getTable(tableName);
          retried = true;
        }
      }
    } finally {
      connection.setAutoCommit(wasAutoCommit);
    }
  }
Beispiel #26
0
 private long getRedisTime(List<String> timeRes) {
   return Long.parseLong(timeRes.get(0));
 }
  private Map<FieldName, ? extends ClassificationMap<?>> evaluateRuleSet(
      ModelManagerEvaluationContext context) {
    RuleSetModel ruleSetModel = getModel();

    RuleSet ruleSet = ruleSetModel.getRuleSet();

    List<RuleSelectionMethod> ruleSelectionMethods = ruleSet.getRuleSelectionMethods();

    RuleSelectionMethod ruleSelectionMethod;

    // "If more than one method is included, the first method is used as the default method for
    // scoring"
    if (ruleSelectionMethods.size() > 0) {
      ruleSelectionMethod = ruleSelectionMethods.get(0);
    } else {
      throw new InvalidFeatureException(ruleSet);
    }

    // Both the ordering of keys and values is significant
    ListMultimap<String, SimpleRule> firedRules = LinkedListMultimap.create();

    List<Rule> rules = ruleSet.getRules();
    for (Rule rule : rules) {
      collectFiredRules(firedRules, rule, context);
    }

    RuleClassificationMap result = new RuleClassificationMap();

    RuleSelectionMethod.Criterion criterion = ruleSelectionMethod.getCriterion();

    Set<String> keys = firedRules.keySet();
    for (String key : keys) {
      List<SimpleRule> keyRules = firedRules.get(key);

      switch (criterion) {
        case FIRST_HIT:
          {
            SimpleRule winner = keyRules.get(0);

            // The first value of the first key
            if (result.getEntity() == null) {
              result.setEntity(winner);
            }

            result.put(key, winner.getConfidence());
          }
          break;
        case WEIGHTED_SUM:
          {
            SimpleRule winner = null;

            double totalWeight = 0;

            for (SimpleRule keyRule : keyRules) {

              if (winner == null || (winner.getWeight() < keyRule.getWeight())) {
                winner = keyRule;
              }

              totalWeight += keyRule.getWeight();
            }

            result.put(winner, key, totalWeight / firedRules.size());
          }
          break;
        case WEIGHTED_MAX:
          {
            SimpleRule winner = null;

            for (SimpleRule keyRule : keyRules) {

              if (winner == null || (winner.getWeight() < keyRule.getWeight())) {
                winner = keyRule;
              }
            }

            result.put(winner, key, winner.getConfidence());
          }
          break;
        default:
          throw new UnsupportedFeatureException(ruleSelectionMethod, criterion);
      }
    }

    return TargetUtil.evaluateClassification(result, context);
  }
Beispiel #28
0
  public MutationState addColumn(AddColumnStatement statement) throws SQLException {
    connection.rollback();
    boolean wasAutoCommit = connection.getAutoCommit();
    try {
      connection.setAutoCommit(false);
      TableName tableNameNode = statement.getTableName();
      String schemaName = tableNameNode.getSchemaName();
      String tableName = tableNameNode.getTableName();

      PTable table = getLatestTable(schemaName, tableName);
      PSchema schema = connection.getPMetaData().getSchema(schemaName);
      boolean retried = false;
      while (true) {
        int ordinalPosition = table.getColumns().size();

        List<PColumn> currentPKs = table.getPKColumns();
        PColumn lastPK = currentPKs.get(currentPKs.size() - 1);
        // Disallow adding columns if the last column is VARBIANRY.
        if (lastPK.getDataType() == PDataType.VARBINARY) {
          throw new SQLExceptionInfo.Builder(SQLExceptionCode.VARBINARY_LAST_PK)
              .setColumnName(lastPK.getName().getString())
              .build()
              .buildException();
        }
        // Disallow adding columns if last column is fixed width and nullable.
        if (lastPK.isNullable() && lastPK.getDataType().isFixedWidth()) {
          throw new SQLExceptionInfo.Builder(SQLExceptionCode.NULLABLE_FIXED_WIDTH_LAST_PK)
              .setColumnName(lastPK.getName().getString())
              .build()
              .buildException();
        }

        List<PColumn> columns = Lists.newArrayListWithExpectedSize(1);
        ColumnDef colDef = statement.getColumnDef();
        if (!colDef.isNull() && colDef.isPK()) {
          throw new SQLExceptionInfo.Builder(SQLExceptionCode.NOT_NULLABLE_COLUMN_IN_ROW_KEY)
              .setColumnName(colDef.getColumnDefName().getColumnName().getName())
              .build()
              .buildException();
        }

        PreparedStatement colUpsert = connection.prepareStatement(INSERT_COLUMN);
        Pair<byte[], Map<String, Object>> family = null;
        PColumn column = newColumn(ordinalPosition++, colDef, null);
        addColumnMutation(schemaName, tableName, column, colUpsert);
        columns.add(column);
        if (column.getFamilyName() != null) {
          family =
              new Pair<byte[], Map<String, Object>>(
                  column.getFamilyName().getBytes(), statement.getProps());
        }
        final long seqNum = table.getSequenceNumber() + 1;
        PreparedStatement tableUpsert = connection.prepareStatement(MUTATE_TABLE);
        tableUpsert.setString(1, schemaName);
        tableUpsert.setString(2, tableName);
        tableUpsert.setString(3, table.getType().getSerializedValue());
        tableUpsert.setLong(4, seqNum);
        tableUpsert.setInt(5, ordinalPosition);
        tableUpsert.execute();

        final List<Mutation> tableMetaData = connection.getMutationState().toMutations();
        connection.rollback();
        byte[] emptyCF = null;
        if (table.getType() != PTableType.VIEW
            && family != null
            && table.getColumnFamilies().isEmpty()) {
          emptyCF = family.getFirst();
        }
        MetaDataMutationResult result =
            connection
                .getQueryServices()
                .addColumn(tableMetaData, table.getType() == PTableType.VIEW, family);
        try {
          MutationCode code = processMutationResult(schemaName, tableName, result);
          if (code == MutationCode.COLUMN_ALREADY_EXISTS) {
            connection.addTable(schemaName, result.getTable());
            if (!statement.ifNotExists()) {
              throw new ColumnAlreadyExistsException(
                  schemaName, tableName, SchemaUtil.findExistingColumn(result.getTable(), columns));
            }
            return new MutationState(0, connection);
          }
          connection.addColumn(schemaName, tableName, columns, seqNum, result.getMutationTime());
          if (emptyCF != null) {
            Long scn = connection.getSCN();
            connection.setAutoCommit(true);
            // Delete everything in the column. You'll still be able to do queries at earlier
            // timestamps
            long ts = (scn == null ? result.getMutationTime() : scn);
            TableRef tableRef = new TableRef(null, table, schema, ts);
            MutationPlan plan =
                new PostDDLCompiler(connection).compile(tableRef, emptyCF, null, ts);
            return connection.getQueryServices().updateData(plan);
          }
          return new MutationState(0, connection);
        } catch (ConcurrentTableMutationException e) {
          if (retried) {
            throw e;
          }
          table = connection.getPMetaData().getSchema(schemaName).getTable(tableName);
          retried = true;
        }
      }
    } finally {
      connection.setAutoCommit(wasAutoCommit);
    }
  }
Beispiel #29
0
 @Override
 public int newKey(ImmutableBytesWritable key, byte[][] values) {
   int i = 0;
   TrustedByteArrayOutputStream os =
       new TrustedByteArrayOutputStream(SchemaUtil.estimateKeyLength(this));
   try {
     List<PColumn> columns = getPKColumns();
     int nColumns = columns.size();
     PColumn lastPKColumn = columns.get(nColumns - 1);
     while (i < values.length && i < nColumns) {
       PColumn column = columns.get(i);
       PDataType type = column.getDataType();
       // This will throw if the value is null and the type doesn't allow null
       byte[] byteValue = values[i++];
       if (byteValue == null) {
         byteValue = ByteUtil.EMPTY_BYTE_ARRAY;
       }
       // An empty byte array return value means null. Do this,
       // since a type may have muliple representations of null.
       // For example, VARCHAR treats both null and an empty string
       // as null. This way we don't need to leak that part of the
       // implementation outside of PDataType by checking the value
       // here.
       if (byteValue.length == 0 && !column.isNullable()) {
         throw new ConstraintViolationException(
             name.getString() + "." + column.getName().getString() + " may not be null");
       }
       Integer byteSize = column.getByteSize();
       if (type.isFixedWidth()) { // TODO: handle multi-byte characters
         if (byteValue.length != byteSize) {
           throw new ConstraintViolationException(
               name.getString()
                   + "."
                   + column.getName().getString()
                   + " must be "
                   + byteSize
                   + " bytes ("
                   + SchemaUtil.toString(type, byteValue)
                   + ")");
         }
       } else if (byteSize != null && byteValue.length > byteSize) {
         throw new ConstraintViolationException(
             name.getString()
                 + "."
                 + column.getName().getString()
                 + " may not exceed "
                 + byteSize
                 + " bytes ("
                 + SchemaUtil.toString(type, byteValue)
                 + ")");
       }
       os.write(byteValue, 0, byteValue.length);
       // Separate variable length column values in key with zero byte
       if (!type.isFixedWidth() && column != lastPKColumn) {
         os.write(SEPARATOR_BYTE);
       }
     }
     // If some non null pk values aren't set, then throw
     if (i < nColumns) {
       PColumn column = columns.get(i);
       PDataType type = column.getDataType();
       if (type.isFixedWidth() || !column.isNullable()) {
         throw new ConstraintViolationException(
             name.getString() + "." + column.getName().getString() + " may not be null");
       }
       // Separate variable length column values in key with zero byte
       if (column != lastPKColumn) {
         os.write(SEPARATOR_BYTE);
       }
     }
     key.set(os.getBuffer(), 0, os.size());
     return i;
   } finally {
     try {
       os.close();
     } catch (IOException e) {
       throw new RuntimeException(e); // Impossible
     }
   }
 }
Beispiel #30
0
  private void init(
      PName name,
      PTableType type,
      long timeStamp,
      long sequenceNumber,
      String pkName,
      List<PColumn> columns,
      PTableStats stats) {
    this.name = name;
    this.type = type;
    this.timeStamp = timeStamp;
    this.sequenceNumber = sequenceNumber;
    this.columnsByName = ArrayListMultimap.create(columns.size(), 1);
    this.pkName = pkName;
    List<PColumn> pkColumns = Lists.newArrayListWithExpectedSize(columns.size() - 1);
    PColumn[] allColumns = new PColumn[columns.size()];
    RowKeySchemaBuilder builder = new RowKeySchemaBuilder();
    for (int i = 0; i < allColumns.length; i++) {
      PColumn column = columns.get(i);
      allColumns[column.getPosition()] = column;
      PName familyName = column.getFamilyName();
      if (familyName == null) {
        pkColumns.add(column);
        builder.addField(column);
      }
      columnsByName.put(column.getName().getString(), column);
    }
    this.pkColumns = ImmutableList.copyOf(pkColumns);
    this.rowKeySchema = builder.setMinNullable(pkColumns.size()).build();
    this.allColumns = ImmutableList.copyOf(allColumns);

    // Two pass so that column order in column families matches overall column order
    // and to ensure that column family order is constant
    int maxExpectedSize = allColumns.length - pkColumns.size();
    // Maintain iteration order so that column families are ordered as they are listed
    Map<PName, List<PColumn>> familyMap = Maps.newLinkedHashMap();
    for (PColumn column : allColumns) {
      PName familyName = column.getFamilyName();
      if (familyName != null) {
        List<PColumn> columnsInFamily = familyMap.get(familyName);
        if (columnsInFamily == null) {
          columnsInFamily = Lists.newArrayListWithExpectedSize(maxExpectedSize);
          familyMap.put(familyName, columnsInFamily);
        }
        columnsInFamily.add(column);
      }
    }

    Iterator<Map.Entry<PName, List<PColumn>>> iterator = familyMap.entrySet().iterator();
    PColumnFamily[] families = new PColumnFamily[familyMap.size()];
    ImmutableMap.Builder<String, PColumnFamily> familyByString = ImmutableMap.builder();
    ImmutableSortedMap.Builder<byte[], PColumnFamily> familyByBytes =
        ImmutableSortedMap.orderedBy(Bytes.BYTES_COMPARATOR);
    for (int i = 0; i < families.length; i++) {
      Map.Entry<PName, List<PColumn>> entry = iterator.next();
      PColumnFamily family = new PColumnFamilyImpl(entry.getKey(), entry.getValue());
      families[i] = family;
      familyByString.put(family.getName().getString(), family);
      familyByBytes.put(family.getName().getBytes(), family);
    }
    this.families = ImmutableList.copyOf(families);
    this.familyByBytes = familyByBytes.build();
    this.familyByString = familyByString.build();
    this.stats = stats;
  }