use of org.apache.calcite.rel.type.RelDataType in project hive by apache.
the class HiveTableScan method buildColIndxsFrmReloptHT.
private static Pair<ImmutableList<Integer>, ImmutableSet<Integer>> buildColIndxsFrmReloptHT(RelOptHiveTable relOptHTable, RelDataType scanRowType) {
RelDataType relOptHtRowtype = relOptHTable.getRowType();
ImmutableList<Integer> neededColIndxsFrmReloptHT;
Builder<Integer> neededColIndxsFrmReloptHTBldr = new ImmutableList.Builder<Integer>();
ImmutableSet<Integer> viurtualOrPartColIndxsInTS;
ImmutableSet.Builder<Integer> viurtualOrPartColIndxsInTSBldr = new ImmutableSet.Builder<Integer>();
Map<String, Integer> colNameToPosInReloptHT = HiveCalciteUtil.getRowColNameIndxMap(relOptHtRowtype.getFieldList());
List<String> colNamesInScanRowType = scanRowType.getFieldNames();
int partOrVirtualColStartPosInrelOptHtRowtype = relOptHTable.getNonPartColumns().size();
int tmp;
for (int i = 0; i < colNamesInScanRowType.size(); i++) {
tmp = colNameToPosInReloptHT.get(colNamesInScanRowType.get(i));
neededColIndxsFrmReloptHTBldr.add(tmp);
if (tmp >= partOrVirtualColStartPosInrelOptHtRowtype) {
viurtualOrPartColIndxsInTSBldr.add(i);
}
}
neededColIndxsFrmReloptHT = neededColIndxsFrmReloptHTBldr.build();
viurtualOrPartColIndxsInTS = viurtualOrPartColIndxsInTSBldr.build();
return new Pair<ImmutableList<Integer>, ImmutableSet<Integer>>(neededColIndxsFrmReloptHT, viurtualOrPartColIndxsInTS);
}
use of org.apache.calcite.rel.type.RelDataType in project hive by apache.
the class HiveSubQRemoveRelBuilder method fields.
/** Returns references to the fields of a given input. */
public ImmutableList<RexNode> fields(int inputCount, int inputOrdinal) {
final RelNode input = peek(inputCount, inputOrdinal);
final RelDataType rowType = input.getRowType();
final ImmutableList.Builder<RexNode> nodes = ImmutableList.builder();
for (int fieldOrdinal : Util.range(rowType.getFieldCount())) {
nodes.add(field(inputCount, inputOrdinal, fieldOrdinal));
}
return nodes.build();
}
use of org.apache.calcite.rel.type.RelDataType in project hive by apache.
the class HiveSubQRemoveRelBuilder method sortLimit.
/** Creates a {@link Sort} by a list of expressions, with limit and offset.
*
* @param offset Number of rows to skip; non-positive means don't skip any
* @param fetch Maximum number of rows to fetch; negative means no limit
* @param nodes Sort expressions
*/
public HiveSubQRemoveRelBuilder sortLimit(int offset, int fetch, Iterable<? extends RexNode> nodes) {
final List<RelFieldCollation> fieldCollations = new ArrayList<>();
final RelDataType inputRowType = peek().getRowType();
final List<RexNode> extraNodes = projects(inputRowType);
final List<RexNode> originalExtraNodes = ImmutableList.copyOf(extraNodes);
for (RexNode node : nodes) {
fieldCollations.add(collation(node, RelFieldCollation.Direction.ASCENDING, null, extraNodes));
}
final RexNode offsetNode = offset <= 0 ? null : literal(offset);
final RexNode fetchNode = fetch < 0 ? null : literal(fetch);
if (offsetNode == null && fetch == 0) {
return empty();
}
if (offsetNode == null && fetchNode == null && fieldCollations.isEmpty()) {
// sort is trivial
return this;
}
final boolean addedFields = extraNodes.size() > originalExtraNodes.size();
if (fieldCollations.isEmpty()) {
assert !addedFields;
RelNode top = peek();
if (top instanceof Sort) {
final Sort sort2 = (Sort) top;
if (sort2.offset == null && sort2.fetch == null) {
stack.pop();
push(sort2.getInput());
final RelNode sort = sortFactory.createSort(build(), sort2.collation, offsetNode, fetchNode);
push(sort);
return this;
}
}
if (top instanceof Project) {
final Project project = (Project) top;
if (project.getInput() instanceof Sort) {
final Sort sort2 = (Sort) project.getInput();
if (sort2.offset == null && sort2.fetch == null) {
stack.pop();
push(sort2.getInput());
final RelNode sort = sortFactory.createSort(build(), sort2.collation, offsetNode, fetchNode);
push(sort);
project(project.getProjects());
return this;
}
}
}
}
if (addedFields) {
project(extraNodes);
}
final RelNode sort = sortFactory.createSort(build(), RelCollations.of(fieldCollations), offsetNode, fetchNode);
push(sort);
if (addedFields) {
project(originalExtraNodes);
}
return this;
}
use of org.apache.calcite.rel.type.RelDataType in project hive by apache.
the class HiveSubQRemoveRelBuilder method aggregate.
/** Creates an {@link org.apache.calcite.rel.core.Aggregate} with a list of
* calls. */
public HiveSubQRemoveRelBuilder aggregate(GroupKey groupKey, Iterable<AggCall> aggCalls) {
final RelDataType inputRowType = peek().getRowType();
final List<RexNode> extraNodes = projects(inputRowType);
final GroupKeyImpl groupKey_ = (GroupKeyImpl) groupKey;
final ImmutableBitSet groupSet = ImmutableBitSet.of(registerExpressions(extraNodes, groupKey_.nodes));
final ImmutableList<ImmutableBitSet> groupSets;
if (groupKey_.nodeLists != null) {
final int sizeBefore = extraNodes.size();
final SortedSet<ImmutableBitSet> groupSetSet = new TreeSet<>(ImmutableBitSet.ORDERING);
for (ImmutableList<RexNode> nodeList : groupKey_.nodeLists) {
final ImmutableBitSet groupSet2 = ImmutableBitSet.of(registerExpressions(extraNodes, nodeList));
if (!groupSet.contains(groupSet2)) {
throw new IllegalArgumentException("group set element " + nodeList + " must be a subset of group key");
}
groupSetSet.add(groupSet2);
}
groupSets = ImmutableList.copyOf(groupSetSet);
if (extraNodes.size() > sizeBefore) {
throw new IllegalArgumentException("group sets contained expressions not in group key: " + extraNodes.subList(sizeBefore, extraNodes.size()));
}
} else {
groupSets = ImmutableList.of(groupSet);
}
for (AggCall aggCall : aggCalls) {
if (aggCall instanceof AggCallImpl) {
final AggCallImpl aggCall1 = (AggCallImpl) aggCall;
registerExpressions(extraNodes, aggCall1.operands);
if (aggCall1.filter != null) {
registerExpression(extraNodes, aggCall1.filter);
}
}
}
if (extraNodes.size() > inputRowType.getFieldCount()) {
project(extraNodes);
}
final RelNode r = build();
final List<AggregateCall> aggregateCalls = new ArrayList<>();
for (AggCall aggCall : aggCalls) {
final AggregateCall aggregateCall;
if (aggCall instanceof AggCallImpl) {
final AggCallImpl aggCall1 = (AggCallImpl) aggCall;
final List<Integer> args = registerExpressions(extraNodes, aggCall1.operands);
final int filterArg = aggCall1.filter == null ? -1 : registerExpression(extraNodes, aggCall1.filter);
aggregateCall = AggregateCall.create(aggCall1.aggFunction, aggCall1.distinct, args, filterArg, groupSet.cardinality(), r, null, aggCall1.alias);
} else {
aggregateCall = ((AggCallImpl2) aggCall).aggregateCall;
}
aggregateCalls.add(aggregateCall);
}
assert ImmutableBitSet.ORDERING.isStrictlyOrdered(groupSets) : groupSets;
for (ImmutableBitSet set : groupSets) {
assert groupSet.contains(set);
}
RelNode aggregate = aggregateFactory.createAggregate(r, groupKey_.indicator, groupSet, groupSets, aggregateCalls);
push(aggregate);
return this;
}
use of org.apache.calcite.rel.type.RelDataType in project hive by apache.
the class HiveCalciteUtil method createUDTFForSetOp.
public static HiveTableFunctionScan createUDTFForSetOp(RelOptCluster cluster, RelNode input) throws SemanticException {
RelTraitSet traitSet = TraitsUtil.getDefaultTraitSet(cluster);
List<RexNode> originalInputRefs = Lists.transform(input.getRowType().getFieldList(), new Function<RelDataTypeField, RexNode>() {
@Override
public RexNode apply(RelDataTypeField input) {
return new RexInputRef(input.getIndex(), input.getType());
}
});
ImmutableList.Builder<RelDataType> argTypeBldr = ImmutableList.<RelDataType>builder();
for (int i = 0; i < originalInputRefs.size(); i++) {
argTypeBldr.add(originalInputRefs.get(i).getType());
}
RelDataType retType = input.getRowType();
String funcName = "replicate_rows";
FunctionInfo fi = FunctionRegistry.getFunctionInfo(funcName);
SqlOperator calciteOp = SqlFunctionConverter.getCalciteOperator(funcName, fi.getGenericUDTF(), argTypeBldr.build(), retType);
// Hive UDTF only has a single input
List<RelNode> list = new ArrayList<>();
list.add(input);
RexNode rexNode = cluster.getRexBuilder().makeCall(calciteOp, originalInputRefs);
return HiveTableFunctionScan.create(cluster, traitSet, list, rexNode, null, retType, null);
}
Aggregations