org.apache.calcite.rel.RelDistribution Java Examples

The following examples show how to use org.apache.calcite.rel.RelDistribution. 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: OLAPValuesRel.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
/** Creates an OLAPValuesRel. */
public static OLAPValuesRel create(RelOptCluster cluster, final RelDataType rowType,
        final ImmutableList<ImmutableList<RexLiteral>> tuples) {
    final RelMetadataQuery mq = cluster.getMetadataQuery();
    final RelTraitSet traitSet = cluster.traitSetOf(OLAPRel.CONVENTION)
            .replaceIfs(RelCollationTraitDef.INSTANCE, new Supplier<List<RelCollation>>() {
                public List<RelCollation> get() {
                    return RelMdCollation.values(mq, rowType, tuples);
                }
            }).replaceIf(RelDistributionTraitDef.INSTANCE, new Supplier<RelDistribution>() {
                public RelDistribution get() {
                    return RelMdDistribution.values(rowType, tuples);
                }
            });
    return new OLAPValuesRel(cluster, rowType, tuples, traitSet);
}
 
Example #2
Source File: OLAPValuesRel.java    From kylin with Apache License 2.0 6 votes vote down vote up
/** Creates an OLAPValuesRel. */
public static OLAPValuesRel create(RelOptCluster cluster, final RelDataType rowType,
        final ImmutableList<ImmutableList<RexLiteral>> tuples) {
    final RelMetadataQuery mq = cluster.getMetadataQuery();
    final RelTraitSet traitSet = cluster.traitSetOf(OLAPRel.CONVENTION)
            .replaceIfs(RelCollationTraitDef.INSTANCE, new Supplier<List<RelCollation>>() {
                public List<RelCollation> get() {
                    return RelMdCollation.values(mq, rowType, tuples);
                }
            }).replaceIf(RelDistributionTraitDef.INSTANCE, new Supplier<RelDistribution>() {
                public RelDistribution get() {
                    return RelMdDistribution.values(rowType, tuples);
                }
            });
    return new OLAPValuesRel(cluster, rowType, tuples, traitSet);
}
 
Example #3
Source File: RelJson.java    From calcite with Apache License 2.0 6 votes vote down vote up
public RelDistribution toDistribution(Map<String, Object> map) {
  final RelDistribution.Type type =
      Util.enumVal(RelDistribution.Type.class,
          (String) map.get("type"));

  ImmutableIntList list = EMPTY;
  if (map.containsKey("keys")) {
    List<Object> keysJson = (List<Object>) map.get("keys");
    ArrayList<Integer> keys = new ArrayList<>(keysJson.size());
    for (Object o : keysJson) {
      keys.add((Integer) o);
    }
    list = ImmutableIntList.copyOf(keys);
  }
  return RelDistributions.of(type, list);
}
 
Example #4
Source File: RelMetadataTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testNodeTypeCountExchange() {

    final RelNode rel = convertSql("select * from emp");
    final RelDistribution dist = RelDistributions.hash(ImmutableList.<Integer>of());
    final LogicalExchange exchange = LogicalExchange.create(rel, dist);

    final Map<Class<? extends RelNode>, Integer> expected = new HashMap<>();
    expected.put(TableScan.class, 1);
    expected.put(Exchange.class, 1);
    expected.put(Project.class, 1);

    final RelMetadataQuery mq = rel.getCluster().getMetadataQuery();
    final Multimap<Class<? extends RelNode>, RelNode> result = mq.getNodeTypes(exchange);
    assertThat(result, notNullValue());
    final Map<Class<? extends RelNode>, Integer> resultCount = new HashMap<>();
    for (Entry<Class<? extends RelNode>, Collection<RelNode>> e : result.asMap().entrySet()) {
      resultCount.put(e.getKey(), e.getValue().size());
    }
    assertThat(expected, equalTo(resultCount));
  }
 
Example #5
Source File: RelMetadataTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testDistributionHashEmpty() {
  final RelNode rel = convertSql("select * from emp");
  final RelDistribution dist = RelDistributions.hash(ImmutableList.<Integer>of());
  final LogicalExchange exchange = LogicalExchange.create(rel, dist);

  final RelMetadataQuery mq = rel.getCluster().getMetadataQuery();
  RelDistribution d = mq.getDistribution(exchange);
  assertThat(d, is(dist));
}
 
Example #6
Source File: RelMdDistribution.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Helper method to determine a {@link Project}'s collation. */
public static RelDistribution project(RelMetadataQuery mq, RelNode input,
    List<? extends RexNode> projects) {
  final RelDistribution inputDistribution = mq.distribution(input);
  final Mappings.TargetMapping mapping =
      Project.getPartialMapping(input.getRowType().getFieldCount(),
          projects);
  return inputDistribution.apply(mapping);
}
 
Example #7
Source File: RelBuilder.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a SortExchange by distribution and collation. */
public RelBuilder sortExchange(RelDistribution distribution,
    RelCollation collation) {
  RelNode exchange =
      struct.sortExchangeFactory.createSortExchange(peek(), distribution,
          collation);
  replaceTop(exchange);
  return this;
}
 
Example #8
Source File: RelMetadataQuery.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the
 * {@link BuiltInMetadata.Distribution#distribution()}
 * statistic.
 *
 * @param rel         the relational expression
 * @return List of sorted column combinations, or
 * null if not enough information is available to make that determination
 */
public RelDistribution distribution(RelNode rel) {
  for (;;) {
    try {
      return distributionHandler.distribution(rel, this);
    } catch (JaninoRelMetadataProvider.NoHandler e) {
      distributionHandler =
          revise(e.relClass, BuiltInMetadata.Distribution.DEF);
    }
  }
}
 
Example #9
Source File: Exchange.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an Exchange.
 *
 * @param cluster   Cluster this relational expression belongs to
 * @param traitSet  Trait set
 * @param input     Input relational expression
 * @param distribution Distribution specification
 */
protected Exchange(RelOptCluster cluster, RelTraitSet traitSet, RelNode input,
    RelDistribution distribution) {
  super(cluster, traitSet, input);
  this.distribution = Objects.requireNonNull(distribution);

  assert traitSet.containsIfApplicable(distribution)
      : "traits=" + traitSet + ", distribution" + distribution;
  assert distribution != RelDistributions.ANY;
}
 
Example #10
Source File: MockCatalogReader.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Statistic getStatistic() {
  return new Statistic() {
    public Double getRowCount() {
      return table.rowCount;
    }

    public boolean isKey(ImmutableBitSet columns) {
      return table.isKey(columns);
    }

    public List<ImmutableBitSet> getKeys() {
      return table.getKeys();
    }

    public List<RelReferentialConstraint> getReferentialConstraints() {
      return table.getReferentialConstraints();
    }

    public List<RelCollation> getCollations() {
      return table.collationList;
    }

    public RelDistribution getDistribution() {
      return table.getDistribution();
    }
  };
}
 
Example #11
Source File: SortExchange.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a SortExchange.
 *
 * @param cluster   Cluster this relational expression belongs to
 * @param traitSet  Trait set
 * @param input     Input relational expression
 * @param distribution Distribution specification
 */
protected SortExchange(RelOptCluster cluster, RelTraitSet traitSet,
    RelNode input, RelDistribution distribution, RelCollation collation) {
  super(cluster, traitSet, input, distribution);
  this.collation = Objects.requireNonNull(collation);

  assert traitSet.containsIfApplicable(collation)
      : "traits=" + traitSet + ", collation=" + collation;
}
 
Example #12
Source File: RelTraitSerializers.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public T read(final Kryo kryo, final Input input, final Class<T> type) {
  final boolean isKnown = kryo.readObject(input, Boolean.class);
  final T result;
  if (isKnown) {
    final RelDistribution.Type kind = kryo.readObject(input, RelDistribution.Type.class);
    result = (T)distributionMap.get(kind);
  } else {
    result = super.read(kryo, input, type);
  }

  final T normalized = (T) result.getTraitDef().canonize(result);
  kryo.reference(normalized);
  return normalized;
}
 
Example #13
Source File: LogicalSortExchange.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a LogicalSortExchange.
 *
 * @param input     Input relational expression
 * @param distribution Distribution specification
 * @param collation array of sort specifications
 */
public static LogicalSortExchange create(
    RelNode input,
    RelDistribution distribution,
    RelCollation collation) {
  RelOptCluster cluster = input.getCluster();
  collation = RelCollationTraitDef.INSTANCE.canonize(collation);
  distribution = RelDistributionTraitDef.INSTANCE.canonize(distribution);
  RelTraitSet traitSet =
      input.getTraitSet().replace(Convention.NONE).replace(distribution).replace(collation);
  return new LogicalSortExchange(cluster, traitSet, input, distribution,
      collation);
}
 
Example #14
Source File: LogicalSortExchange.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a LogicalSortExchange.
 *
 * @param input     Input relational expression
 * @param distribution Distribution specification
 * @param collation array of sort specifications
 */
public static LogicalSortExchange create(
    RelNode input,
    RelDistribution distribution,
    RelCollation collation) {
  RelOptCluster cluster = input.getCluster();
  collation = RelCollationTraitDef.INSTANCE.canonize(collation);
  distribution = RelDistributionTraitDef.INSTANCE.canonize(distribution);
  RelTraitSet traitSet =
      input.getTraitSet().replace(Convention.NONE).replace(distribution).replace(collation);
  return new LogicalSortExchange(cluster, traitSet, input, distribution,
      collation);
}
 
Example #15
Source File: MycatPhysicalTable.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
@NotNull
private Statistic createStatistic(Statistic parentStatistic) {
    return new Statistic() {
        @Override
        public Double getRowCount() {
            return StatisticCenter.INSTANCE.getPhysicsTableRow(backendTableInfo.getSchema(),
                    backendTableInfo.getTable(),
                    backendTableInfo.getTargetName());
        }

        @Override
        public boolean isKey(ImmutableBitSet columns) {
            return parentStatistic.isKey(columns);
        }

        @Override
        public List<ImmutableBitSet> getKeys() {
            return parentStatistic.getKeys();
        }

        @Override
        public List<RelReferentialConstraint> getReferentialConstraints() {
            return parentStatistic.getReferentialConstraints();
        }

        @Override
        public List<RelCollation> getCollations() {
            return parentStatistic.getCollations();
        }

        @Override
        public RelDistribution getDistribution() {
            return parentStatistic.getDistribution();
        }
    };
}
 
Example #16
Source File: Statistics.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Returns a statistic with a given row count, set of unique keys,
 * referential constraints, and collations. */
public static Statistic of(final Double rowCount,
    final List<ImmutableBitSet> keys,
    final List<RelReferentialConstraint> referentialConstraints,
    final List<RelCollation> collations) {
  return new Statistic() {
    public Double getRowCount() {
      return rowCount;
    }

    public boolean isKey(ImmutableBitSet columns) {
      for (ImmutableBitSet key : keys) {
        if (columns.contains(key)) {
          return true;
        }
      }
      return false;
    }

    public List<RelReferentialConstraint> getReferentialConstraints() {
      return referentialConstraints;
    }

    public List<RelCollation> getCollations() {
      return collations;
    }

    public RelDistribution getDistribution() {
      return RelDistributionTraitDef.INSTANCE.getDefault();
    }
  };
}
 
Example #17
Source File: Exchange.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Returns the distribution of the rows returned by this Exchange. */
public RelDistribution getDistribution() {
  return distribution;
}
 
Example #18
Source File: BuiltInMetadata.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Determines how the rows are distributed. */
RelDistribution distribution();
 
Example #19
Source File: RelMdDistribution.java    From calcite with Apache License 2.0 4 votes vote down vote up
public RelDistribution distribution(SetOp rel, RelMetadataQuery mq) {
  return mq.distribution(rel.getInputs().get(0));
}
 
Example #20
Source File: RelFieldTrimmer.java    From calcite with Apache License 2.0 4 votes vote down vote up
public TrimResult trimFields(
    SortExchange sortExchange,
    ImmutableBitSet fieldsUsed,
    Set<RelDataTypeField> extraFields) {
  final RelDataType rowType = sortExchange.getRowType();
  final int fieldCount = rowType.getFieldCount();
  final RelCollation collation = sortExchange.getCollation();
  final RelDistribution distribution = sortExchange.getDistribution();
  final RelNode input = sortExchange.getInput();

  // We use the fields used by the consumer, plus any fields used as sortExchange
  // keys.
  final ImmutableBitSet.Builder inputFieldsUsed = fieldsUsed.rebuild();
  for (RelFieldCollation field : collation.getFieldCollations()) {
    inputFieldsUsed.set(field.getFieldIndex());
  }
  for (int keyIndex : distribution.getKeys()) {
    inputFieldsUsed.set(keyIndex);
  }

  // Create input with trimmed columns.
  final Set<RelDataTypeField> inputExtraFields = Collections.emptySet();
  TrimResult trimResult =
      trimChild(sortExchange, input, inputFieldsUsed.build(), inputExtraFields);
  RelNode newInput = trimResult.left;
  final Mapping inputMapping = trimResult.right;

  // If the input is unchanged, and we need to project all columns,
  // there's nothing we can do.
  if (newInput == input
      && inputMapping.isIdentity()
      && fieldsUsed.cardinality() == fieldCount) {
    return result(sortExchange, Mappings.createIdentity(fieldCount));
  }

  relBuilder.push(newInput);
  RelCollation newCollation = RexUtil.apply(inputMapping, collation);
  RelDistribution newDistribution = distribution.apply(inputMapping);
  relBuilder.sortExchange(newDistribution, newCollation);

  return result(relBuilder.build(), inputMapping);
}
 
Example #21
Source File: RelMdDistribution.java    From calcite with Apache License 2.0 4 votes vote down vote up
public RelDistribution distribution(SingleRel rel, RelMetadataQuery mq) {
  return mq.distribution(rel.getInput());
}
 
Example #22
Source File: RelJson.java    From Bats with Apache License 2.0 4 votes vote down vote up
public RelDistribution toDistribution(Object o) {
  return RelDistributions.ANY; // TODO:
}
 
Example #23
Source File: RelOptNamespaceTable.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
 public RelDistribution getDistribution() {
   return RelDistributionTraitDef.INSTANCE.getDefault();
}
 
Example #24
Source File: NamespaceTable.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
public RelDistribution getDistribution() {
  return RelDistributionTraitDef.INSTANCE.getDefault();
}
 
Example #25
Source File: RelOptAbstractTable.java    From Bats with Apache License 2.0 4 votes vote down vote up
public RelDistribution getDistribution() {
  return RelDistributions.BROADCAST_DISTRIBUTED;
}
 
Example #26
Source File: RelMetadataTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Test void testDistributionSimple() {
  RelNode rel = convertSql("select * from emp where deptno = 10");
  final RelMetadataQuery mq = rel.getCluster().getMetadataQuery();
  RelDistribution d = mq.getDistribution(rel);
  assertThat(d, is(RelDistributions.BROADCAST_DISTRIBUTED));
}
 
Example #27
Source File: AbstractIndexStatistics.java    From Bats with Apache License 2.0 4 votes vote down vote up
public RelDistribution getDistribution() {
    throw new UnsupportedOperationException();
}
 
Example #28
Source File: Statistics.java    From calcite with Apache License 2.0 4 votes vote down vote up
public RelDistribution getDistribution() {
  return RelDistributionTraitDef.INSTANCE.getDefault();
}
 
Example #29
Source File: IndexStatistics.java    From Bats with Apache License 2.0 4 votes vote down vote up
/** Returns the distribution of the data in query result table. */
RelDistribution getDistribution();
 
Example #30
Source File: RelMdDistribution.java    From calcite with Apache License 2.0 4 votes vote down vote up
public RelDistribution distribution(Exchange exchange, RelMetadataQuery mq) {
  return exchange(exchange.distribution);
}