Java Code Examples for org.apache.calcite.util.Util#toString()

The following examples show how to use org.apache.calcite.util.Util#toString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MongoProject.java    From calcite with Apache License 2.0 6 votes vote down vote up
public void implement(Implementor implementor) {
  implementor.visitChild(0, getInput());

  final MongoRules.RexToMongoTranslator translator =
      new MongoRules.RexToMongoTranslator(
          (JavaTypeFactory) getCluster().getTypeFactory(),
          MongoRules.mongoFieldNames(getInput().getRowType()));
  final List<String> items = new ArrayList<>();
  for (Pair<RexNode, String> pair : getNamedProjects()) {
    final String name = pair.right;
    final String expr = pair.left.accept(translator);
    items.add(expr.equals("'$" + name + "'")
        ? MongoRules.maybeQuote(name) + ": 1"
        : MongoRules.maybeQuote(name) + ": " + expr);
  }
  final String findString = Util.toString(items, "{", ", ", "}");
  final String aggregateString = "{$project: " + findString + "}";
  final Pair<String, String> op = Pair.of(findString, aggregateString);
  implementor.add(op.left, op.right);
}
 
Example 2
Source File: MongoTable.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Executes an "aggregate" operation on the underlying collection.
 *
 * <p>For example:
 * <code>zipsTable.aggregate(
 * "{$filter: {state: 'OR'}",
 * "{$group: {_id: '$city', c: {$sum: 1}, p: {$sum: '$pop'}}}")
 * </code></p>
 *
 * @param mongoDb MongoDB connection
 * @param fields List of fields to project; or null to return map
 * @param operations One or more JSON strings
 * @return Enumerator of results
 */
private Enumerable<Object> aggregate(final MongoDatabase mongoDb,
    final List<Map.Entry<String, Class>> fields,
    final List<String> operations) {
  final List<Bson> list = new ArrayList<>();
  for (String operation : operations) {
    list.add(BsonDocument.parse(operation));
  }
  final Function1<Document, Object> getter =
      MongoEnumerator.getter(fields);
  return new AbstractEnumerable<Object>() {
    public Enumerator<Object> enumerator() {
      final Iterator<Document> resultIterator;
      try {
        resultIterator = mongoDb.getCollection(collectionName)
            .aggregate(list).iterator();
      } catch (Exception e) {
        throw new RuntimeException("While running MongoDB query "
            + Util.toString(operations, "[", ",\n", "]"), e);
      }
      return new MongoEnumerator(resultIterator, getter);
    }
  };
}
 
Example 3
Source File: GeodeFilter.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Translate a conjunctive predicate to a OQL string.
 *
 * @param condition A conjunctive predicate
 * @return OQL string for the predicate
 */
private String translateAnd(RexNode condition) {
  List<String> predicates = new ArrayList<>();
  for (RexNode node : RelOptUtil.conjunctions(condition)) {
    predicates.add(translateMatch2(node));
  }

  return Util.toString(predicates, "", " AND ", "");
}
 
Example 4
Source File: GeodeFilter.java    From calcite with Apache License 2.0 5 votes vote down vote up
private String translateOr(List<RexNode> disjunctions) {
  List<String> predicates = new ArrayList<>();

  List<String> leftFieldNameList = new ArrayList<>();
  List<String> inSetLeftFieldNameList = new ArrayList<>();

  for (RexNode node : disjunctions) {
    final String leftNodeFieldName = getLeftNodeFieldNameForNode(node);
    // If any one left node is processed with IN SET predicate
    // all the nodes are already handled
    if (inSetLeftFieldNameList.contains(leftNodeFieldName)) {
      continue;
    }

    List<RexNode> leftNodeDisjunctions = new ArrayList<>();
    boolean useInSetQueryClause = false;

    // In case the left field node name is already processed and not applicable
    // for IN SET query clause, we can skip the checking
    if (!leftFieldNameList.contains(leftNodeFieldName)) {
      leftNodeDisjunctions = getLeftNodeDisjunctions(node, disjunctions);
      useInSetQueryClause = useInSetQueryClause(leftNodeDisjunctions);
    }

    if (useInSetQueryClause) {
      predicates.add(translateInSet(leftNodeDisjunctions));
      inSetLeftFieldNameList.add(leftNodeFieldName);
    } else if (RelOptUtil.conjunctions(node).size() > 1) {
      predicates.add("(" + translateMatch(node) + ")");
    } else {
      predicates.add(translateMatch2(node));
    }
    leftFieldNameList.add(leftNodeFieldName);
  }

  return Util.toString(predicates, "", " OR ", "");
}
 
Example 5
Source File: GeodeAggregate.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public void implement(GeodeImplementContext geodeImplementContext) {
  geodeImplementContext.visitChild(getInput());

  List<String> inputFields = fieldNames(getInput().getRowType());

  List<String> groupByFields = new ArrayList<>();

  for (int group : groupSet) {
    groupByFields.add(inputFields.get(group));
  }

  geodeImplementContext.addGroupBy(groupByFields);

  // Find the aggregate functions (e.g. MAX, SUM ...)
  Builder<String, String> aggregateFunctionMap = ImmutableMap.builder();
  for (AggregateCall aggCall : aggCalls) {

    List<String> aggCallFieldNames = new ArrayList<>();
    for (int i : aggCall.getArgList()) {
      aggCallFieldNames.add(inputFields.get(i));
    }
    String functionName = aggCall.getAggregation().getName();

    // Workaround to handle count(*) case. Geode doesn't allow "AS" aliases on
    // 'count(*)' but allows it for count('any column name'). So we are
    // converting the count(*) into count (first input ColumnName).
    if ("COUNT".equalsIgnoreCase(functionName) && aggCallFieldNames.isEmpty()) {
      aggCallFieldNames.add(inputFields.get(0));
    }

    String oqlAggregateCall = Util.toString(aggCallFieldNames, functionName + "(", ", ",
        ")");

    aggregateFunctionMap.put(aggCall.getName(), oqlAggregateCall);
  }

  geodeImplementContext.addAggregateFunctions(aggregateFunctionMap.build());

}
 
Example 6
Source File: GeodeUtils.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static Object convert(Object o, Class clazz) {
  if (o == null) {
    return null;
  }
  Primitive primitive = Primitive.of(clazz);
  if (primitive != null) {
    clazz = primitive.boxClass;
  } else {
    primitive = Primitive.ofBox(clazz);
  }
  if (clazz == null) {
    return o.toString();
  }
  if (Map.class.isAssignableFrom(clazz)
          && o instanceof PdxInstance) {
    // This is in case of nested Objects!
    return Util.toString(
            ((PdxInstance) o).getFieldNames(), "PDX[", ",", "]");
  }
  if (clazz.isInstance(o)) {
    return o;
  }
  if (o instanceof Date && primitive != null) {
    o = ((Date) o).getTime() / DateTimeUtils.MILLIS_PER_DAY;
  }
  if (o instanceof Number && primitive != null) {
    return primitive.number((Number) o);
  }
  return o;
}
 
Example 7
Source File: CassandraFilter.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Translate a conjunctive predicate to a CQL string.
 *
 * @param condition A conjunctive predicate
 * @return CQL string for the predicate
 */
private String translateAnd(RexNode condition) {
  List<String> predicates = new ArrayList<>();
  for (RexNode node : RelOptUtil.conjunctions(condition)) {
    predicates.add(translateMatch2(node));
  }

  return Util.toString(predicates, "", " AND ", "");
}