Java Code Examples for org.apache.calcite.rex.RexLiteral#intValue()

The following examples show how to use org.apache.calcite.rex.RexLiteral#intValue() . 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: RelMdMaxRowCount.java    From Bats with Apache License 2.0 6 votes vote down vote up
public Double getMaxRowCount(Sort rel, RelMetadataQuery mq) {
  Double rowCount = mq.getMaxRowCount(rel.getInput());
  if (rowCount == null) {
    rowCount = Double.POSITIVE_INFINITY;
  }
  final int offset = rel.offset == null ? 0 : RexLiteral.intValue(rel.offset);
  rowCount = Math.max(rowCount - offset, 0D);

  if (rel.fetch != null) {
    final int limit = RexLiteral.intValue(rel.fetch);
    if (limit < rowCount) {
      return (double) limit;
    }
  }
  return rowCount;
}
 
Example 2
Source File: RelMdMaxRowCount.java    From calcite with Apache License 2.0 6 votes vote down vote up
public Double getMaxRowCount(Sort rel, RelMetadataQuery mq) {
  Double rowCount = mq.getMaxRowCount(rel.getInput());
  if (rowCount == null) {
    rowCount = Double.POSITIVE_INFINITY;
  }
  final int offset = rel.offset == null ? 0 : RexLiteral.intValue(rel.offset);
  rowCount = Math.max(rowCount - offset, 0D);

  if (rel.fetch != null) {
    final int limit = RexLiteral.intValue(rel.fetch);
    if (limit < rowCount) {
      return (double) limit;
    }
  }
  return rowCount;
}
 
Example 3
Source File: RelMdUtil.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Returns whether a relational expression is already sorted and has fewer
 * rows than the sum of offset and limit.
 *
 * <p>If this is the case, it is safe to push down a
 * {@link org.apache.calcite.rel.core.Sort} with limit and optional offset. */
public static boolean checkInputForCollationAndLimit(RelMetadataQuery mq,
    RelNode input, RelCollation collation, RexNode offset, RexNode fetch) {
  // Check if the input is already sorted
  boolean alreadySorted = collation.getFieldCollations().isEmpty();
  for (RelCollation inputCollation : mq.collations(input)) {
    if (inputCollation.satisfies(collation)) {
      alreadySorted = true;
      break;
    }
  }
  // Check if we are not reducing the number of tuples
  boolean alreadySmaller = true;
  final Double rowCount = mq.getMaxRowCount(input);
  if (rowCount != null && fetch != null) {
    final int offsetVal = offset == null ? 0 : RexLiteral.intValue(offset);
    final int limit = RexLiteral.intValue(fetch);
    if ((double) offsetVal + (double) limit < rowCount) {
      alreadySmaller = false;
    }
  }
  return alreadySorted && alreadySmaller;
}
 
Example 4
Source File: DruidRules.java    From calcite with Apache License 2.0 6 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final Sort sort = call.rel(0);
  final DruidQuery query = call.rel(1);
  if (!DruidQuery.isValidSignature(query.signature() + 'l')) {
    return;
  }
  // Either it is:
  // - a pure limit above a query of type scan
  // - a sort and limit on a dimension/metric part of the druid group by query
  if (sort.offset != null && RexLiteral.intValue(sort.offset) != 0) {
    // offset not supported by Druid
    return;
  }
  if (query.getQueryType() == QueryType.SCAN && !RelOptUtil.isPureLimit(sort)) {
    return;
  }

  final RelNode newSort = sort
      .copy(sort.getTraitSet(), ImmutableList.of(Util.last(query.rels)));
  call.transformTo(DruidQuery.extendQuery(query, newSort));
}
 
Example 5
Source File: ElasticLimitRule.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Override
public boolean matches(RelOptRuleCall call) {
  final LimitPrel limit = call.rel(0);
  final ElasticsearchIntermediatePrel intermediatePrel = call.rel(1);

  if (intermediatePrel.hasTerminalPrel()) {
    return false;
  }

  // TODO: this can probably be supported in many cases.
  if (limit.getOffset() != null && RexLiteral.intValue(limit.getOffset()) != 0) {
    return false;
  }

  final PlannerSettings plannerSettings = PrelUtil.getPlannerSettings(limit.getCluster().getPlanner());
  if (intermediatePrel.contains(ElasticsearchSample.class)
      && limit.getFetch() != null
      && RexLiteral.intValue(limit.getFetch()) >= SampleCrel.getSampleSizeAndSetMinSampleSize(plannerSettings, ElasticSampleRule.SAMPLE_SIZE_DENOMINATOR)) {
    return false;
  }

  return true;
}
 
Example 6
Source File: RelMdMinRowCount.java    From calcite with Apache License 2.0 6 votes vote down vote up
public Double getMinRowCount(EnumerableLimit rel, RelMetadataQuery mq) {
  Double rowCount = mq.getMinRowCount(rel.getInput());
  if (rowCount == null) {
    rowCount = 0D;
  }
  final int offset = rel.offset == null ? 0 : RexLiteral.intValue(rel.offset);
  rowCount = Math.max(rowCount - offset, 0D);

  if (rel.fetch != null) {
    final int limit = RexLiteral.intValue(rel.fetch);
    if (limit < rowCount) {
      return (double) limit;
    }
  }
  return rowCount;
}
 
Example 7
Source File: RelMdMinRowCount.java    From calcite with Apache License 2.0 6 votes vote down vote up
public Double getMinRowCount(Sort rel, RelMetadataQuery mq) {
  Double rowCount = mq.getMinRowCount(rel.getInput());
  if (rowCount == null) {
    rowCount = 0D;
  }
  final int offset = rel.offset == null ? 0 : RexLiteral.intValue(rel.offset);
  rowCount = Math.max(rowCount - offset, 0D);

  if (rel.fetch != null) {
    final int limit = RexLiteral.intValue(rel.fetch);
    if (limit < rowCount) {
      return (double) limit;
    }
  }
  return rowCount;
}
 
Example 8
Source File: RelMdMinRowCount.java    From Bats with Apache License 2.0 6 votes vote down vote up
public Double getMinRowCount(Sort rel, RelMetadataQuery mq) {
  Double rowCount = mq.getMinRowCount(rel.getInput());
  if (rowCount == null) {
    rowCount = 0D;
  }
  final int offset = rel.offset == null ? 0 : RexLiteral.intValue(rel.offset);
  rowCount = Math.max(rowCount - offset, 0D);

  if (rel.fetch != null) {
    final int limit = RexLiteral.intValue(rel.fetch);
    if (limit < rowCount) {
      return (double) limit;
    }
  }
  return rowCount;
}
 
Example 9
Source File: RelMdMaxRowCount.java    From calcite with Apache License 2.0 6 votes vote down vote up
public Double getMaxRowCount(EnumerableLimit rel, RelMetadataQuery mq) {
  Double rowCount = mq.getMaxRowCount(rel.getInput());
  if (rowCount == null) {
    rowCount = Double.POSITIVE_INFINITY;
  }
  final int offset = rel.offset == null ? 0 : RexLiteral.intValue(rel.offset);
  rowCount = Math.max(rowCount - offset, 0D);

  if (rel.fetch != null) {
    final int limit = RexLiteral.intValue(rel.fetch);
    if (limit < rowCount) {
      return (double) limit;
    }
  }
  return rowCount;
}
 
Example 10
Source File: DrillLimitRelBase.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public double estimateRowCount(RelMetadataQuery mq) {
  int off = offset != null? RexLiteral.intValue(offset): 0;

  if (fetch == null) {
    // If estimated rowcount is less than offset return 0
    return Math.max(0, getInput().estimateRowCount(mq) - off);
  } else {
    int f = RexLiteral.intValue(fetch);
    return off + f;
  }
}
 
Example 11
Source File: Utilities.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Converts literal into path segment based on its type.
 * For unsupported types, returns null.
 *
 * @param literal literal
 * @return new path segment, null otherwise
 */
public static PathSegment convertLiteral(RexLiteral literal) {
  switch (literal.getType().getSqlTypeName()) {
    case CHAR:
      return new PathSegment.NameSegment(RexLiteral.stringValue(literal));
    case INTEGER:
      return new PathSegment.ArraySegment(RexLiteral.intValue(literal));
    default:
      return null;
  }
}
 
Example 12
Source File: ElasticsearchLimit.java    From dk-fitting with Apache License 2.0 5 votes vote down vote up
public void implement(Implementor implementor) {
    RelNode input = getInput();
    implementor.visitChild(0, getInput());
    ElasticsearchTable esTable = implementor.getElasticsearchTable();
    if (offset != null) {
        implementor.offset = RexLiteral.intValue(offset);
        esTable.setSearchOffset(RexLiteral.intValue(offset));
    }

    if (fetch != null) {
        implementor.fetch = RexLiteral.intValue(fetch);
        esTable.setSearchSize(RexLiteral.intValue(fetch));
    }
}
 
Example 13
Source File: RelMdMinRowCount.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Double getMinRowCount(RelSubset rel, RelMetadataQuery mq) {
  // FIXME This is a short-term fix for [CALCITE-1018]. A complete
  // solution will come with [CALCITE-1048].
  Util.discard(Bug.CALCITE_1048_FIXED);
  for (RelNode node : rel.getRels()) {
    if (node instanceof Sort) {
      Sort sort = (Sort) node;
      if (sort.fetch != null) {
        return (double) RexLiteral.intValue(sort.fetch);
      }
    }
  }

  return 0D;
}
 
Example 14
Source File: LimitRelBase.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public double estimateRowCount(RelMetadataQuery mq) {
  int off = offset != null ? RexLiteral.intValue(offset) : 0 ;

  if (fetch == null) {
    return mq.getRowCount(getInput()) - off;
  } else {
    int f = RexLiteral.intValue(fetch);
    return off + f;
  }
}
 
Example 15
Source File: RelMdMinRowCount.java    From Bats with Apache License 2.0 5 votes vote down vote up
public Double getMinRowCount(RelSubset rel, RelMetadataQuery mq) {
  // FIXME This is a short-term fix for [CALCITE-1018]. A complete
  // solution will come with [CALCITE-1048].
  Util.discard(Bug.CALCITE_1048_FIXED);
  for (RelNode node : rel.getRels()) {
    if (node instanceof Sort) {
      Sort sort = (Sort) node;
      if (sort.fetch != null) {
        return (double) RexLiteral.intValue(sort.fetch);
      }
    }
  }

  return 0D;
}
 
Example 16
Source File: ElasticsearchLimit.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Merges this limit with an ElasticsearchSample operator.
 * @param sample
 * @param treeWithoutLimit
 * @return
 */
public ElasticsearchLimit merge(ElasticsearchSample sample, RelNode treeWithoutLimit){
  if(sample == null){
    return this;
  }

  long sampleSize = SampleCrel.getSampleSizeAndSetMinSampleSize(PrelUtil.getPlannerSettings(getCluster().getPlanner()), ElasticSampleRule.SAMPLE_SIZE_DENOMINATOR);
  int limitAmount = RexLiteral.intValue(getFetch());
  int finalLimit = Math.min((int) sampleSize,  limitAmount);
  RexNode offset = getCluster().getRexBuilder().makeExactLiteral(BigDecimal.valueOf(0), getCluster().getTypeFactory().createSqlType(SqlTypeName.INTEGER));
  RexNode fetch = getCluster().getRexBuilder().makeExactLiteral(BigDecimal.valueOf(finalLimit), getCluster().getTypeFactory().createSqlType(SqlTypeName.INTEGER));
  return new ElasticsearchLimit(getCluster(), treeWithoutLimit.getTraitSet(), treeWithoutLimit, offset, fetch, isPushDown(), getPluginId());
}
 
Example 17
Source File: CassandraLimit.java    From calcite with Apache License 2.0 5 votes vote down vote up
public void implement(Implementor implementor) {
  implementor.visitChild(0, getInput());
  if (offset != null) {
    implementor.offset = RexLiteral.intValue(offset);
  }
  if (fetch != null) {
    implementor.fetch = RexLiteral.intValue(fetch);
  }
}
 
Example 18
Source File: RelMdMaxRowCount.java    From Bats with Apache License 2.0 5 votes vote down vote up
public Double getMaxRowCount(RelSubset rel, RelMetadataQuery mq) {
  // FIXME This is a short-term fix for [CALCITE-1018]. A complete
  // solution will come with [CALCITE-1048].
  Util.discard(Bug.CALCITE_1048_FIXED);
  for (RelNode node : rel.getRels()) {
    if (node instanceof Sort) {
      Sort sort = (Sort) node;
      if (sort.fetch != null) {
        return (double) RexLiteral.intValue(sort.fetch);
      }
    }
  }

  return Double.POSITIVE_INFINITY;
}
 
Example 19
Source File: IndexPlanUtils.java    From Bats with Apache License 2.0 4 votes vote down vote up
public static boolean generateLimit(OrderedRel sort) {
  RexNode fetchNode = sort.getFetch();
  int fetchValue = (fetchNode == null) ? -1 : RexLiteral.intValue(fetchNode);
  return fetchValue >=0;
}
 
Example 20
Source File: DruidQuery.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public boolean isValid(Litmus litmus, Context context) {
  if (!super.isValid(litmus, context)) {
    return false;
  }
  final String signature = signature();
  if (!isValidSignature(signature)) {
    return litmus.fail("invalid signature [{}]", signature);
  }
  if (rels.isEmpty()) {
    return litmus.fail("must have at least one rel");
  }
  for (int i = 0; i < rels.size(); i++) {
    final RelNode r = rels.get(i);
    if (i == 0) {
      if (!(r instanceof TableScan)) {
        return litmus.fail("first rel must be TableScan, was ", r);
      }
      if (r.getTable() != table) {
        return litmus.fail("first rel must be based on table table");
      }
    } else {
      final List<RelNode> inputs = r.getInputs();
      if (inputs.size() != 1 || inputs.get(0) != rels.get(i - 1)) {
        return litmus.fail("each rel must have a single input");
      }
      if (r instanceof Aggregate) {
        final Aggregate aggregate = (Aggregate) r;
        if (aggregate.getGroupSets().size() != 1) {
          return litmus.fail("no grouping sets");
        }
      }
      if (r instanceof Filter) {
        final Filter filter = (Filter) r;
        final DruidJsonFilter druidJsonFilter = DruidJsonFilter
            .toDruidFilters(filter.getCondition(), filter.getInput().getRowType(), this);
        if (druidJsonFilter == null) {
          return litmus.fail("invalid filter [{}]", filter.getCondition());
        }
      }
      if (r instanceof Sort) {
        final Sort sort = (Sort) r;
        if (sort.offset != null && RexLiteral.intValue(sort.offset) != 0) {
          return litmus.fail("offset not supported");
        }
      }
    }
  }
  return true;
}