Java Code Examples for org.apache.calcite.rel.RelFieldCollation#getDirection()

The following examples show how to use org.apache.calcite.rel.RelFieldCollation#getDirection() . 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: SqlImplementor.java    From Bats with Apache License 2.0 6 votes vote down vote up
/** Converts a collation to an ORDER BY item. */
public SqlNode toSql(RelFieldCollation collation) {
    SqlNode node = field(collation.getFieldIndex());
    switch (collation.getDirection()) {
    case DESCENDING:
    case STRICTLY_DESCENDING:
        node = SqlStdOperatorTable.DESC.createCall(POS, node);
    }
    if (collation.nullDirection != dialect.defaultNullDirection(collation.direction)) {
        switch (collation.nullDirection) {
        case FIRST:
            node = SqlStdOperatorTable.NULLS_FIRST.createCall(POS, node);
            break;
        case LAST:
            node = SqlStdOperatorTable.NULLS_LAST.createCall(POS, node);
            break;
        }
    }
    return node;
}
 
Example 2
Source File: SqlImplementor.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Converts a collation to an ORDER BY item.
 */
public SqlNode toSql(RelFieldCollation collation) {
  SqlNode node = field(collation.getFieldIndex());
  switch (collation.getDirection()) {
    case DESCENDING:
    case STRICTLY_DESCENDING:
      node = SqlStdOperatorTable.DESC.createCall(POS, node);
  }
  if (collation.nullDirection != dialect.defaultNullDirection(collation.direction)) {
    switch (collation.nullDirection) {
      case FIRST:
        node = SqlStdOperatorTable.NULLS_FIRST.createCall(POS, node);
        break;
      case LAST:
        node = SqlStdOperatorTable.NULLS_LAST.createCall(POS, node);
        break;
    }
  }
  return node;
}
 
Example 3
Source File: CalcitePlanner.java    From herddb with Apache License 2.0 6 votes vote down vote up
private PlannerOp planSort(EnumerableSort op, RelDataType rowType) {
    PlannerOp input = convertRelNode(op.getInput(), rowType, false, false);
    RelCollation collation = op.getCollation();
    List<RelFieldCollation> fieldCollations = collation.getFieldCollations();
    boolean[] directions = new boolean[fieldCollations.size()];
    int[] fields = new int[fieldCollations.size()];
    int i = 0;
    for (RelFieldCollation col : fieldCollations) {
        RelFieldCollation.Direction direction = col.getDirection();
        int index = col.getFieldIndex();
        directions[i] = direction == RelFieldCollation.Direction.ASCENDING
                || direction == RelFieldCollation.Direction.STRICTLY_ASCENDING;
        fields[i++] = index;
    }
    return new SortOp(input, directions, fields);

}
 
Example 4
Source File: RelToSqlConverter.java    From quark with Apache License 2.0 6 votes vote down vote up
/**
     * Converts a collation to an ORDER BY item.
     */
    public SqlNode toSql(RelFieldCollation collation) {
      SqlNode node = field(collation.getFieldIndex(), true);
      switch (collation.getDirection()) {
        case DESCENDING:
        case STRICTLY_DESCENDING:
          node = SqlStdOperatorTable.DESC.createCall(POS, node);
      }
// ******* Disable Null Collation ******
//      switch (collation.nullDirection) {
//        case FIRST:
//          node = SqlStdOperatorTable.NULLS_FIRST.createCall(POS, node);
//          break;
//        case LAST:
//          node = SqlStdOperatorTable.NULLS_LAST.createCall(POS, node);
//          break;
//      }
      return node;
    }
 
Example 5
Source File: CassandraSort.java    From calcite with Apache License 2.0 6 votes vote down vote up
public void implement(Implementor implementor) {
  implementor.visitChild(0, getInput());

  List<RelFieldCollation> sortCollations = collation.getFieldCollations();
  List<String> fieldOrder = new ArrayList<>();
  if (!sortCollations.isEmpty()) {
    // Construct a series of order clauses from the desired collation
    final List<RelDataTypeField> fields = getRowType().getFieldList();
    for (RelFieldCollation fieldCollation : sortCollations) {
      final String name =
          fields.get(fieldCollation.getFieldIndex()).getName();
      final String direction;
      switch (fieldCollation.getDirection()) {
      case DESCENDING:
        direction = "DESC";
        break;
      default:
        direction = "ASC";
      }
      fieldOrder.add(name + " " + direction);
    }

    implementor.addOrder(fieldOrder);
  }
}
 
Example 6
Source File: SqlImplementor.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Converts a collation to an ORDER BY item. */
public SqlNode toSql(RelFieldCollation collation) {
  SqlNode node = orderField(collation.getFieldIndex());
  switch (collation.getDirection()) {
  case DESCENDING:
  case STRICTLY_DESCENDING:
    node = SqlStdOperatorTable.DESC.createCall(POS, node);
  }
  if (collation.nullDirection != dialect.defaultNullDirection(collation.direction)) {
    switch (collation.nullDirection) {
    case FIRST:
      node = SqlStdOperatorTable.NULLS_FIRST.createCall(POS, node);
      break;
    case LAST:
      node = SqlStdOperatorTable.NULLS_LAST.createCall(POS, node);
      break;
    }
  }
  return node;
}
 
Example 7
Source File: ElasticsearchSort.java    From dk-fitting with Apache License 2.0 5 votes vote down vote up
public void implement(Implementor implementor) {
    implementor.visitChild(0, getInput());
    ElasticsearchTable esTable = implementor.getElasticsearchTable();
    List<RelFieldCollation> fieldCollations = collation.getFieldCollations();
    if(fieldCollations != null)
    {
        List<RelDataTypeField> fieldList = esTable.getRowType().getFieldList();
        for(RelFieldCollation fieldCollation : fieldCollations) {
            SortOrder order = SortOrder.ASC;
            esTable.setIsSort(true);
            esTable.setIsAsc(true);
            switch (fieldCollation.getDirection())
            {
                case DESCENDING:
                case STRICTLY_DESCENDING:
                    order = SortOrder.DESC;
                    esTable.setIsAsc(false);
            }
            esTable.addSortBuilder(esTable.transFieldName(fieldList.get(fieldCollation.getFieldIndex()).getName().toLowerCase(),fieldList), order);
        }
    }

    if(offset != null && offset instanceof RexLiteral)
        esTable.setSearchOffset(Integer.parseInt(((RexLiteral) offset).getValue2().toString()));
    if(fetch != null && fetch instanceof RexLiteral)
        esTable.setSearchSize(Integer.parseInt(((RexLiteral) fetch).getValue2().toString()));
}
 
Example 8
Source File: ElasticsearchSort.java    From dk-fitting with Apache License 2.0 5 votes vote down vote up
public void implement(Implementor implementor) {
    implementor.visitChild(0, getInput());
    ElasticsearchTable esTable = implementor.getElasticsearchTable();
    List<RelFieldCollation> fieldCollations = collation.getFieldCollations();
    if(fieldCollations != null)
    {
        List<RelDataTypeField> fieldList = esTable.getRowType().getFieldList();
        for(RelFieldCollation fieldCollation : fieldCollations) {
            SortOrder order = SortOrder.ASC;
            esTable.setIsSort(true);
            esTable.setIsAsc(true);
            switch (fieldCollation.getDirection())
            {
                case DESCENDING:
                case STRICTLY_DESCENDING:
                    order = SortOrder.DESC;
                    esTable.setIsAsc(false);
            }
            esTable.addSortBuilder(esTable.transFieldName(fieldList.get(fieldCollation.getFieldIndex()).getName().toLowerCase(),fieldList), order);
        }
    }

    if(offset != null && offset instanceof RexLiteral)
        esTable.setSearchOffset(Integer.parseInt(((RexLiteral) offset).getValue2().toString()));
    if(fetch != null && fetch instanceof RexLiteral)
        esTable.setSearchSize(Integer.parseInt(((RexLiteral) fetch).getValue2().toString()));
}
 
Example 9
Source File: CassandraRules.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Check if it is possible to exploit native CQL sorting for a given collation.
 *
 * @return True if it is possible to achieve this sort in Cassandra
 */
private boolean collationsCompatible(RelCollation sortCollation,
    RelCollation implicitCollation) {
  List<RelFieldCollation> sortFieldCollations = sortCollation.getFieldCollations();
  List<RelFieldCollation> implicitFieldCollations = implicitCollation.getFieldCollations();

  if (sortFieldCollations.size() > implicitFieldCollations.size()) {
    return false;
  }
  if (sortFieldCollations.size() == 0) {
    return true;
  }

  // Check if we need to reverse the order of the implicit collation
  boolean reversed = reverseDirection(sortFieldCollations.get(0).getDirection())
      == implicitFieldCollations.get(0).getDirection();

  for (int i = 0; i < sortFieldCollations.size(); i++) {
    RelFieldCollation sorted = sortFieldCollations.get(i);
    RelFieldCollation implied = implicitFieldCollations.get(i);

    // Check that the fields being sorted match
    if (sorted.getFieldIndex() != implied.getFieldIndex()) {
      return false;
    }

    // Either all fields must be sorted in the same direction
    // or the opposite direction based on whether we decided
    // if the sort direction should be reversed above
    RelFieldCollation.Direction sortDirection = sorted.getDirection();
    RelFieldCollation.Direction implicitDirection = implied.getDirection();
    if ((!reversed && sortDirection != implicitDirection)
        || (reversed && reverseDirection(sortDirection) != implicitDirection)) {
      return false;
    }
  }

  return true;
}
 
Example 10
Source File: MongoSort.java    From calcite with Apache License 2.0 5 votes vote down vote up
private int direction(RelFieldCollation fieldCollation) {
  switch (fieldCollation.getDirection()) {
  case DESCENDING:
  case STRICTLY_DESCENDING:
    return -1;
  case ASCENDING:
  case STRICTLY_ASCENDING:
  default:
    return 1;
  }
}
 
Example 11
Source File: IndexPlanUtils.java    From Bats with Apache License 2.0 4 votes vote down vote up
/**
 * Build collation property for the 'upper' project, the one above the filter
 * @param projectRexs
 * @param inputCollation
 * @param indexInfo
 * @param collationFilterMap
 * @return the output RelCollation
 */
public static RelCollation buildCollationUpperProject(List<RexNode> projectRexs,
                                                      RelCollation inputCollation, FunctionalIndexInfo indexInfo,
                                                      Map<Integer, List<RexNode>> collationFilterMap) {
  List<RelFieldCollation> outputFieldCollations = Lists.newArrayList();

  if (inputCollation != null) {
    List<RelFieldCollation> inputFieldCollations = inputCollation.getFieldCollations();
    if (!indexInfo.hasFunctional()) {
      for (int projectExprIdx = 0; projectExprIdx < projectRexs.size(); projectExprIdx++) {
        RexNode n = projectRexs.get(projectExprIdx);
        if (n instanceof RexInputRef) {
          RexInputRef ref = (RexInputRef)n;
          boolean eligibleForCollation = true;
          int maxIndex = getIndexFromCollation(ref.getIndex(), inputFieldCollations);
          if (maxIndex < 0) {
            eligibleForCollation = false;
            continue;
          }
          // check if the prefix has equality conditions
          for (int i = 0; i < maxIndex; i++) {
            int fieldIdx = inputFieldCollations.get(i).getFieldIndex();
            List<RexNode> conditions = collationFilterMap != null ? collationFilterMap.get(fieldIdx) : null;
            if ((conditions == null || conditions.size() == 0) &&
                i < maxIndex-1) {
              // if an intermediate column has no filter condition, it would select all values
              // of that column, so a subsequent column cannot be eligible for collation
              eligibleForCollation = false;
              break;
            } else {
              for (RexNode r : conditions) {
                if (!(r.getKind() == SqlKind.EQUALS)) {
                  eligibleForCollation = false;
                  break;
                }
              }
            }
          }
          // for every projected expr, if it is eligible for collation, get the
          // corresponding field collation from the input
          if (eligibleForCollation) {
            for (RelFieldCollation c : inputFieldCollations) {
              if (ref.getIndex() == c.getFieldIndex()) {
                RelFieldCollation outFieldCollation = new RelFieldCollation(projectExprIdx, c.getDirection(), c.nullDirection);
                outputFieldCollations.add(outFieldCollation);
              }
            }
          }
        }
      }
    } else {
      // TODO: handle functional index
    }
  }
  return RelCollations.of(outputFieldCollations);
}
 
Example 12
Source File: PhysTypeImpl.java    From calcite with Apache License 2.0 4 votes vote down vote up
public Expression generateComparator(RelCollation collation) {
  // int c;
  // c = Utilities.compare(v0, v1);
  // if (c != 0) return c; // or -c if descending
  // ...
  // return 0;
  BlockBuilder body = new BlockBuilder();
  final Type javaRowClass = Primitive.box(this.javaRowClass);
  final ParameterExpression parameterV0 =
      Expressions.parameter(javaRowClass, "v0");
  final ParameterExpression parameterV1 =
      Expressions.parameter(javaRowClass, "v1");
  final ParameterExpression parameterC =
      Expressions.parameter(int.class, "c");
  final int mod =
      collation.getFieldCollations().size() == 1 ? Modifier.FINAL : 0;
  body.add(Expressions.declare(mod, parameterC, null));
  for (RelFieldCollation fieldCollation : collation.getFieldCollations()) {
    final int index = fieldCollation.getFieldIndex();
    final RelDataType fieldType = rowType.getFieldList().get(index).getType();
    final Expression fieldComparator = generateCollatorExpression(fieldType.getCollation());
    Expression arg0 = fieldReference(parameterV0, index);
    Expression arg1 = fieldReference(parameterV1, index);
    switch (Primitive.flavor(fieldClass(index))) {
    case OBJECT:
      arg0 = EnumUtils.convert(arg0, Comparable.class);
      arg1 = EnumUtils.convert(arg1, Comparable.class);
    }
    final boolean nullsFirst =
        fieldCollation.nullDirection
            == RelFieldCollation.NullDirection.FIRST;
    final boolean descending =
        fieldCollation.getDirection()
            == RelFieldCollation.Direction.DESCENDING;
    body.add(
        Expressions.statement(
            Expressions.assign(
                parameterC,
                Expressions.call(
                    Utilities.class,
                    fieldNullable(index)
                        ? (nullsFirst != descending
                        ? "compareNullsFirst"
                        : "compareNullsLast")
                        : "compare",
                    Expressions.list(
                        arg0,
                        arg1)
                        .appendIfNotNull(fieldComparator)))));
    body.add(
        Expressions.ifThen(
            Expressions.notEqual(
                parameterC, Expressions.constant(0)),
            Expressions.return_(
                null,
                descending
                    ? Expressions.negate(parameterC)
                    : parameterC)));
  }
  body.add(
      Expressions.return_(null, Expressions.constant(0)));

  final List<MemberDeclaration> memberDeclarations =
      Expressions.list(
          Expressions.methodDecl(
              Modifier.PUBLIC,
              int.class,
              "compare",
              ImmutableList.of(parameterV0, parameterV1),
              body.toBlock()));

  if (EnumerableRules.BRIDGE_METHODS) {
    final ParameterExpression parameterO0 =
        Expressions.parameter(Object.class, "o0");
    final ParameterExpression parameterO1 =
        Expressions.parameter(Object.class, "o1");
    BlockBuilder bridgeBody = new BlockBuilder();
    bridgeBody.add(
        Expressions.return_(
            null,
            Expressions.call(
                Expressions.parameter(
                    Comparable.class, "this"),
                BuiltInMethod.COMPARATOR_COMPARE.method,
                Expressions.convert_(
                    parameterO0,
                    javaRowClass),
                Expressions.convert_(
                    parameterO1,
                    javaRowClass))));
    memberDeclarations.add(
        overridingMethodDecl(
            BuiltInMethod.COMPARATOR_COMPARE.method,
            ImmutableList.of(parameterO0, parameterO1),
            bridgeBody.toBlock()));
  }
  return Expressions.new_(
      Comparator.class,
      ImmutableList.of(),
      memberDeclarations);
}