Java Code Examples for org.apache.calcite.rel.RelFieldCollation#Direction

The following examples show how to use org.apache.calcite.rel.RelFieldCollation#Direction . 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: SqlDialect.java    From Bats with Apache License 2.0 6 votes vote down vote up
/** Returns whether NULL values are sorted first or last, in this dialect,
 * in an ORDER BY item of a given direction. */
public RelFieldCollation.NullDirection defaultNullDirection(
    RelFieldCollation.Direction direction) {
  switch (direction) {
  case ASCENDING:
  case STRICTLY_ASCENDING:
    return getNullCollation().last(false)
        ? RelFieldCollation.NullDirection.LAST
        : RelFieldCollation.NullDirection.FIRST;
  case DESCENDING:
  case STRICTLY_DESCENDING:
    return getNullCollation().last(true)
        ? RelFieldCollation.NullDirection.LAST
        : RelFieldCollation.NullDirection.FIRST;
  default:
    return RelFieldCollation.NullDirection.UNSPECIFIED;
  }
}
 
Example 2
Source File: RelBuilder.java    From calcite with Apache License 2.0 6 votes vote down vote up
private static RelFieldCollation collation(RexNode node,
    RelFieldCollation.Direction direction,
    RelFieldCollation.NullDirection nullDirection, List<RexNode> extraNodes) {
  switch (node.getKind()) {
  case INPUT_REF:
    return new RelFieldCollation(((RexInputRef) node).getIndex(), direction,
        Util.first(nullDirection, direction.defaultNullDirection()));
  case DESCENDING:
    return collation(((RexCall) node).getOperands().get(0),
        RelFieldCollation.Direction.DESCENDING,
        nullDirection, extraNodes);
  case NULLS_FIRST:
    return collation(((RexCall) node).getOperands().get(0), direction,
        RelFieldCollation.NullDirection.FIRST, extraNodes);
  case NULLS_LAST:
    return collation(((RexCall) node).getOperands().get(0), direction,
        RelFieldCollation.NullDirection.LAST, extraNodes);
  default:
    final int fieldIndex = extraNodes.size();
    extraNodes.add(node);
    return new RelFieldCollation(fieldIndex, direction,
        Util.first(nullDirection, direction.defaultNullDirection()));
  }
}
 
Example 3
Source File: CassandraFilter.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Infer the implicit correlation from the unrestricted clustering keys.
 *
 * @return The collation of the filtered results
 */
public RelCollation getImplicitCollation() {
  // No collation applies if we aren't restricted to a single partition
  if (!isSinglePartition()) {
    return RelCollations.EMPTY;
  }

  // Pull out the correct fields along with their original collations
  List<RelFieldCollation> fieldCollations = new ArrayList<>();
  for (int i = restrictedClusteringKeys; i < clusteringKeys.size(); i++) {
    int fieldIndex = fieldNames.indexOf(clusteringKeys.get(i));
    RelFieldCollation.Direction direction = implicitFieldCollations.get(i).getDirection();
    fieldCollations.add(new RelFieldCollation(fieldIndex, direction));
  }

  return RelCollations.of(fieldCollations);
}
 
Example 4
Source File: SqlToRelTestBase.java    From calcite with Apache License 2.0 6 votes vote down vote up
private List<RelCollation> deduceMonotonicity(SqlValidatorTable table) {
  final RelDataType rowType = table.getRowType();
  final List<RelCollation> collationList = new ArrayList<>();

  // Deduce which fields the table is sorted on.
  int i = -1;
  for (RelDataTypeField field : rowType.getFieldList()) {
    ++i;
    final SqlMonotonicity monotonicity =
        table.getMonotonicity(field.getName());
    if (monotonicity != SqlMonotonicity.NOT_MONOTONIC) {
      final RelFieldCollation.Direction direction =
          monotonicity.isDecreasing()
              ? RelFieldCollation.Direction.DESCENDING
              : RelFieldCollation.Direction.ASCENDING;
      collationList.add(
          RelCollations.of(new RelFieldCollation(i, direction)));
    }
  }
  return collationList;
}
 
Example 5
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 6
Source File: SqlDialect.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Returns whether NULL values are sorted first or last, in this dialect,
 * in an ORDER BY item of a given direction. */
public @Nonnull RelFieldCollation.NullDirection defaultNullDirection(
    RelFieldCollation.Direction direction) {
  switch (direction) {
  case ASCENDING:
  case STRICTLY_ASCENDING:
    return getNullCollation().last(false)
        ? RelFieldCollation.NullDirection.LAST
        : RelFieldCollation.NullDirection.FIRST;
  case DESCENDING:
  case STRICTLY_DESCENDING:
    return getNullCollation().last(true)
        ? RelFieldCollation.NullDirection.LAST
        : RelFieldCollation.NullDirection.FIRST;
  default:
    return RelFieldCollation.NullDirection.UNSPECIFIED;
  }
}
 
Example 7
Source File: RexNodeConverter.java    From flink with Apache License 2.0 5 votes vote down vote up
private RexNode createCollation(RexNode node, RelFieldCollation.Direction direction,
		RelFieldCollation.NullDirection nullDirection, Set<SqlKind> kinds) {
	switch (node.getKind()) {
		case DESCENDING:
			kinds.add(node.getKind());
			return createCollation(((RexCall) node).getOperands().get(0), RelFieldCollation.Direction.DESCENDING,
					nullDirection, kinds);
		case NULLS_FIRST:
			kinds.add(node.getKind());
			return createCollation(((RexCall) node).getOperands().get(0), direction,
					RelFieldCollation.NullDirection.FIRST, kinds);
		case NULLS_LAST:
			kinds.add(node.getKind());
			return createCollation(((RexCall) node).getOperands().get(0), direction,
					RelFieldCollation.NullDirection.LAST, kinds);
		default:
			if (nullDirection == null) {
				// Set the null direction if not specified.
				// Consistent with HIVE/SPARK/MYSQL/BLINK-RUNTIME.
				if (FlinkPlannerImpl.defaultNullCollation()
						.last(direction.equals(RelFieldCollation.Direction.DESCENDING))) {
					kinds.add(SqlKind.NULLS_LAST);
				} else {
					kinds.add(SqlKind.NULLS_FIRST);
				}
			}
			return node;
	}
}
 
Example 8
Source File: IndexPlanUtils.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Build collation property for the 'lower' project, the one closer to the Scan
 * @param projectRexs
 * @param input
 * @param indexInfo
 * @return the output RelCollation
 */
public static RelCollation buildCollationLowerProject(List<RexNode> projectRexs, RelNode input, FunctionalIndexInfo indexInfo) {
  // if leading fields of index are here, add them to RelCollation
  List<RelFieldCollation> newFields = Lists.newArrayList();
  if (!indexInfo.hasFunctional()) {
    Map<LogicalExpression, Integer> projectExprs = Maps.newLinkedHashMap();
    DrillParseContext parserContext = new DrillParseContext(PrelUtil.getPlannerSettings(input.getCluster()));
    int idx=0;
    for (RexNode rex : projectRexs) {
      projectExprs.put(DrillOptiq.toDrill(parserContext, input, rex), idx);
      idx++;
    }
    int idxFieldCount = 0;
    for (LogicalExpression expr : indexInfo.getIndexDesc().getIndexColumns()) {
      if (!projectExprs.containsKey(expr)) {
        break;
      }
      RelFieldCollation.Direction dir = indexInfo.getIndexDesc().getCollation().getFieldCollations().get(idxFieldCount).direction;
      if ( dir == null) {
        break;
      }
      newFields.add(new RelFieldCollation(projectExprs.get(expr), dir,
          RelFieldCollation.NullDirection.UNSPECIFIED));
    }
    idxFieldCount++;
  } else {
    // TODO: handle functional index
  }

  return RelCollations.of(newFields);
}
 
Example 9
Source File: RelJson.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelFieldCollation toFieldCollation(Map<String, Object> map) {
  final Integer field = (Integer) map.get("field");
  final RelFieldCollation.Direction direction =
      Util.enumVal(RelFieldCollation.Direction.class,
          (String) map.get("direction"));
  final RelFieldCollation.NullDirection nullDirection =
      Util.enumVal(RelFieldCollation.NullDirection.class,
          (String) map.get("nulls"));
  return new RelFieldCollation(field, direction, nullDirection);
}
 
Example 10
Source File: OverConvertRule.java    From flink with Apache License 2.0 5 votes vote down vote up
private RexNode createCollation(
		RexNode node,
		RelFieldCollation.Direction direction,
		RelFieldCollation.NullDirection nullDirection,
		Set<SqlKind> kinds) {
	switch (node.getKind()) {
		case DESCENDING:
			kinds.add(node.getKind());
			return createCollation(((RexCall) node).getOperands().get(0), RelFieldCollation.Direction.DESCENDING,
				nullDirection, kinds);
		case NULLS_FIRST:
			kinds.add(node.getKind());
			return createCollation(((RexCall) node).getOperands().get(0), direction,
				RelFieldCollation.NullDirection.FIRST, kinds);
		case NULLS_LAST:
			kinds.add(node.getKind());
			return createCollation(((RexCall) node).getOperands().get(0), direction,
				RelFieldCollation.NullDirection.LAST, kinds);
		default:
			if (nullDirection == null) {
				// Set the null direction if not specified.
				// Consistent with HIVE/SPARK/MYSQL/BLINK-RUNTIME.
				if (FlinkPlannerImpl.defaultNullCollation()
					.last(direction.equals(RelFieldCollation.Direction.DESCENDING))) {
					kinds.add(SqlKind.NULLS_LAST);
				} else {
					kinds.add(SqlKind.NULLS_FIRST);
				}
			}
			return node;
	}
}
 
Example 11
Source File: CassandraSchema.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Get the collation of all clustering key columns.
 *
 * @return A RelCollations representing the collation of all clustering keys
 */
public List<RelFieldCollation> getClusteringOrder(String columnFamily, boolean view) {
  AbstractTableMetadata table;
  if (view) {
    table = getKeyspace().getMaterializedView("\"" + columnFamily + "\"");
  } else {
    table = getKeyspace().getTable("\"" + columnFamily + "\"");
  }

  List<ClusteringOrder> clusteringOrder = table.getClusteringOrder();
  List<RelFieldCollation> keyCollations = new ArrayList<>();

  int i = 0;
  for (ClusteringOrder order : clusteringOrder) {
    RelFieldCollation.Direction direction;
    switch (order) {
    case DESC:
      direction = RelFieldCollation.Direction.DESCENDING;
      break;
    case ASC:
    default:
      direction = RelFieldCollation.Direction.ASCENDING;
      break;
    }
    keyCollations.add(new RelFieldCollation(i, direction));
    i++;
  }

  return keyCollations;
}
 
Example 12
Source File: RelJson.java    From Bats with Apache License 2.0 5 votes vote down vote up
public RelFieldCollation toFieldCollation(Map<String, Object> map) {
  final Integer field = (Integer) map.get("field");
  final RelFieldCollation.Direction direction =
      Util.enumVal(RelFieldCollation.Direction.class,
          (String) map.get("direction"));
  final RelFieldCollation.NullDirection nullDirection =
      Util.enumVal(RelFieldCollation.NullDirection.class,
          (String) map.get("nulls"));
  return new RelFieldCollation(field, direction, nullDirection);
}
 
Example 13
Source File: CassandraRules.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Find the reverse of a given collation direction.
 *
 * @return Reverse of the input direction
 */
private RelFieldCollation.Direction reverseDirection(RelFieldCollation.Direction direction) {
  switch (direction) {
  case ASCENDING:
  case STRICTLY_ASCENDING:
    return RelFieldCollation.Direction.DESCENDING;
  case DESCENDING:
  case STRICTLY_DESCENDING:
    return RelFieldCollation.Direction.ASCENDING;
  default:
    return null;
  }
}
 
Example 14
Source File: ElasticsearchTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Called via code-generation.
 * @param ops list of queries (as strings)
 * @param fields projection
 * @see ElasticsearchMethod#ELASTICSEARCH_QUERYABLE_FIND
 * @return result as enumerable
 */
@SuppressWarnings("UnusedDeclaration")
public Enumerable<Object> find(List<String> ops,
     List<Map.Entry<String, Class>> fields,
     List<Map.Entry<String, RelFieldCollation.Direction>> sort,
     List<String> groupBy,
     List<Map.Entry<String, String>> aggregations,
     Map<String, String> mappings,
     Long offset, Long fetch) {
  try {
    return getTable().find(ops, fields, sort, groupBy, aggregations, mappings, offset, fetch);
  } catch (IOException e) {
    throw new UncheckedIOException("Failed to query " + getTable().indexName, e);
  }
}
 
Example 15
Source File: RelNodeConvertor.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
private static String op(RelFieldCollation.Direction direction) {
    return direction == RelFieldCollation.Direction.DESCENDING ? "DESC" : "ASC";
}
 
Example 16
Source File: BaseTestOperator.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
protected Order.Ordering ordering(String expression, RelFieldCollation.Direction direction, RelFieldCollation.NullDirection nullDirection) {
  return new Order.Ordering(direction, parseExpr(expression), nullDirection);
}
 
Example 17
Source File: GeodeSort.java    From calcite with Apache License 2.0 4 votes vote down vote up
private String direction(RelFieldCollation.Direction relDirection) {
  if (relDirection == RelFieldCollation.Direction.DESCENDING) {
    return DESC;
  }
  return ASC;
}
 
Example 18
Source File: RexFieldCollation.java    From calcite with Apache License 2.0 4 votes vote down vote up
public RelFieldCollation.Direction getDirection() {
  return right.contains(SqlKind.DESCENDING)
      ? RelFieldCollation.Direction.DESCENDING
      : RelFieldCollation.Direction.ASCENDING;
}
 
Example 19
Source File: ElasticsearchRel.java    From calcite with Apache License 2.0 4 votes vote down vote up
void addSort(String field, RelFieldCollation.Direction direction) {
  Objects.requireNonNull(field, "field");
  sort.add(new Pair<>(field, direction));
}
 
Example 20
Source File: ElasticsearchTable.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * Executes a "find" operation on the underlying index.
 *
 * @param ops List of operations represented as Json strings.
 * @param fields List of fields to project; or null to return map
 * @param sort list of fields to sort and their direction (asc/desc)
 * @param aggregations aggregation functions
 * @return Enumerator of results
 */
private Enumerable<Object> find(List<String> ops,
    List<Map.Entry<String, Class>> fields,
    List<Map.Entry<String, RelFieldCollation.Direction>> sort,
    List<String> groupBy,
    List<Map.Entry<String, String>> aggregations,
    Map<String, String> mappings,
    Long offset, Long fetch) throws IOException {

  if (!aggregations.isEmpty() || !groupBy.isEmpty()) {
    // process aggregations separately
    return aggregate(ops, fields, sort, groupBy, aggregations, mappings, offset, fetch);
  }

  final ObjectNode query = mapper.createObjectNode();
  // manually parse from previously concatenated string
  for (String op: ops) {
    query.setAll((ObjectNode) mapper.readTree(op));
  }

  if (!sort.isEmpty()) {
    ArrayNode sortNode = query.withArray("sort");
    sort.forEach(e ->
        sortNode.add(
            mapper.createObjectNode().put(e.getKey(),
                e.getValue().isDescending() ? "desc" : "asc")));
  }

  if (offset != null) {
    query.put("from", offset);
  }

  if (fetch != null) {
    query.put("size", fetch);
  }

  final Function1<ElasticsearchJson.SearchHit, Object> getter =
      ElasticsearchEnumerators.getter(fields, ImmutableMap.copyOf(mappings));

  Iterable<ElasticsearchJson.SearchHit> iter;
  if (offset == null) {
    // apply scrolling when there is no offsets
    iter = () -> new Scrolling(transport).query(query);
  } else {
    final ElasticsearchJson.Result search = transport.search().apply(query);
    iter = () -> search.searchHits().hits().iterator();
  }

  return Linq4j.asEnumerable(iter).select(getter);
}