Java Code Examples for org.apache.calcite.util.Pair#left()

The following examples show how to use org.apache.calcite.util.Pair#left() . 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: DiffRepository.java    From calcite with Apache License 2.0 5 votes vote down vote up
private Node ref(String testCaseName, List<Pair<String, Element>> map) {
  if (map.isEmpty()) {
    return null;
  }
  // Compute the position that the new element should be if the map were
  // sorted.
  int i = 0;
  final List<String> names = Pair.left(map);
  for (String s : names) {
    if (s.compareToIgnoreCase(testCaseName) <= 0) {
      ++i;
    }
  }
  // Starting at a proportional position in the list,
  // move forwards through lesser names, then
  // move backwards through greater names.
  //
  // The intended effect is that if the list is already sorted, the new item
  // will end up in exactly the right position, and if the list is not sorted,
  // the new item will end up in approximately the right position.
  while (i < map.size()
      && names.get(i).compareToIgnoreCase(testCaseName) < 0) {
    ++i;
  }
  if (i >= map.size() - 1) {
    return null;
  }
  while (i >= 0 && names.get(i).compareToIgnoreCase(testCaseName) > 0) {
    --i;
  }
  return map.get(i + 1).right;
}
 
Example 2
Source File: RelBuilder.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a reference to a field of given input relational expression
 * by name.
 *
 * @param inputCount Number of inputs
 * @param inputOrdinal Input ordinal
 * @param fieldName Field name
 */
public RexInputRef field(int inputCount, int inputOrdinal, String fieldName) {
  final Frame frame = peek_(inputCount, inputOrdinal);
  final List<String> fieldNames = Pair.left(frame.fields());
  int i = fieldNames.indexOf(fieldName);
  if (i >= 0) {
    return field(inputCount, inputOrdinal, i);
  } else {
    throw new IllegalArgumentException("field [" + fieldName
        + "] not found; input fields are: " + fieldNames);
  }
}
 
Example 3
Source File: RelBuilder.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Creates a reference to a field of given input relational expression
 * by name.
 *
 * @param inputCount Number of inputs
 * @param inputOrdinal Input ordinal
 * @param fieldName Field name
 */
public RexInputRef field(int inputCount, int inputOrdinal, String fieldName) {
    final Frame frame = peek_(inputCount, inputOrdinal);
    final List<String> fieldNames = Pair.left(frame.fields());
    int i = fieldNames.indexOf(fieldName);
    if (i >= 0) {
        return field(inputCount, inputOrdinal, i);
    } else {
        throw new IllegalArgumentException("field [" + fieldName + "] not found; input fields are: " + fieldNames);
    }
}
 
Example 4
Source File: RelStructuredTypeFlattener.java    From calcite with Apache License 2.0 5 votes vote down vote up
private RelNode coverNewRelByFlatteningProjection(RelNode rel, RelNode newRel) {
  final List<Pair<RexNode, String>> flattenedExpList = new ArrayList<>();
  RexNode newRowRef = rexBuilder.makeRangeReference(newRel);
  List<RelDataTypeField> inputRowFields = rel.getRowType().getFieldList();
  flattenInputs(inputRowFields, newRowRef, flattenedExpList);
  // cover new scan with flattening projection
  List<RexNode> projects = Pair.left(flattenedExpList);
  List<String> fieldNames = Pair.right(flattenedExpList);
  newRel = relBuilder.push(newRel)
      .projectNamed(projects, fieldNames, true)
      .build();
  newRel = RelOptUtil.copyRelHints(rel, newRel);
  return newRel;
}
 
Example 5
Source File: RelStructuredTypeFlattener.java    From calcite with Apache License 2.0 5 votes vote down vote up
public void rewriteRel(LogicalProject rel) {
  RewriteRexShuttle shuttle = new RewriteRexShuttle();
  List<RexNode> oldProjects = rel.getProjects();
  List<String> oldNames = rel.getRowType().getFieldNames();
  List<Pair<RexNode, String>> flattenedExpList = new ArrayList<>();
  flattenProjections(shuttle, oldProjects, oldNames, "", flattenedExpList);
  RelNode newInput = getNewForOldRel(rel.getInput());
  List<RexNode> newProjects = Pair.left(flattenedExpList);
  List<String> newNames = Pair.right(flattenedExpList);
  final RelNode newRel = relBuilder.push(newInput)
      .projectNamed(newProjects, newNames, true)
      .hints(rel.getHints())
      .build();
  setNewForOldRel(rel, newRel);
}
 
Example 6
Source File: DiffRepository.java    From flink with Apache License 2.0 5 votes vote down vote up
private Node ref(String testCaseName, List<Pair<String, Element>> map) {
	if (map.isEmpty()) {
		return null;
	}
	// Compute the position that the new element should be if the map were
	// sorted.
	int i = 0;
	final List<String> names = Pair.left(map);
	for (String s : names) {
		if (s.compareToIgnoreCase(testCaseName) <= 0) {
			++i;
		}
	}
	// Starting at a proportional position in the list,
	// move forwards through lesser names, then
	// move backwards through greater names.
	//
	// The intended effect is that if the list is already sorted, the new item
	// will end up in exactly the right position, and if the list is not sorted,
	// the new item will end up in approximately the right position.
	while (i < map.size()
		&& names.get(i).compareToIgnoreCase(testCaseName) < 0) {
		++i;
	}
	if (i >= map.size() - 1) {
		return null;
	}
	while (i >= 0 && names.get(i).compareToIgnoreCase(testCaseName) > 0) {
		--i;
	}
	return map.get(i + 1).right;
}
 
Example 7
Source File: DiffRepository.java    From flink with Apache License 2.0 5 votes vote down vote up
private Node ref(String testCaseName, List<Pair<String, Element>> map) {
	if (map.isEmpty()) {
		return null;
	}
	// Compute the position that the new element should be if the map were
	// sorted.
	int i = 0;
	final List<String> names = Pair.left(map);
	for (String s : names) {
		if (s.compareToIgnoreCase(testCaseName) <= 0) {
			++i;
		}
	}
	// Starting at a proportional position in the list,
	// move forwards through lesser names, then
	// move backwards through greater names.
	//
	// The intended effect is that if the list is already sorted, the new item
	// will end up in exactly the right position, and if the list is not sorted,
	// the new item will end up in approximately the right position.
	while (i < map.size()
		&& names.get(i).compareToIgnoreCase(testCaseName) < 0) {
		++i;
	}
	if (i >= map.size() - 1) {
		return null;
	}
	while (i >= 0 && names.get(i).compareToIgnoreCase(testCaseName) > 0) {
		--i;
	}
	return map.get(i + 1).right;
}
 
Example 8
Source File: RelOptTableImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
public static RelOptTableImpl create(RelOptSchema schema, RelDataType rowType,
    Table table, Path path) {
  final SchemaPlus schemaPlus = MySchemaPlus.create(path);
  return new RelOptTableImpl(schema, rowType, Pair.left(path), table,
      getClassExpressionFunction(schemaPlus, Util.last(path).left, table),
      table.getStatistic().getRowCount());
}
 
Example 9
Source File: JoinToMultiJoinRule.java    From calcite with Apache License 2.0 4 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final Join origJoin = call.rel(0);
  final RelNode left = call.rel(1);
  final RelNode right = call.rel(2);

  // combine the children MultiJoin inputs into an array of inputs
  // for the new MultiJoin
  final List<ImmutableBitSet> projFieldsList = new ArrayList<>();
  final List<int[]> joinFieldRefCountsList = new ArrayList<>();
  final List<RelNode> newInputs =
      combineInputs(
          origJoin,
          left,
          right,
          projFieldsList,
          joinFieldRefCountsList);

  // combine the outer join information from the left and right
  // inputs, and include the outer join information from the current
  // join, if it's a left/right outer join
  final List<Pair<JoinRelType, RexNode>> joinSpecs = new ArrayList<>();
  combineOuterJoins(
      origJoin,
      newInputs,
      left,
      right,
      joinSpecs);

  // pull up the join filters from the children MultiJoinRels and
  // combine them with the join filter associated with this LogicalJoin to
  // form the join filter for the new MultiJoin
  List<RexNode> newJoinFilters = combineJoinFilters(origJoin, left, right);

  // add on the join field reference counts for the join condition
  // associated with this LogicalJoin
  final ImmutableMap<Integer, ImmutableIntList> newJoinFieldRefCountsMap =
      addOnJoinFieldRefCounts(newInputs,
          origJoin.getRowType().getFieldCount(),
          origJoin.getCondition(),
          joinFieldRefCountsList);

  List<RexNode> newPostJoinFilters =
      combinePostJoinFilters(origJoin, left, right);

  final RexBuilder rexBuilder = origJoin.getCluster().getRexBuilder();
  RelNode multiJoin =
      new MultiJoin(
          origJoin.getCluster(),
          newInputs,
          RexUtil.composeConjunction(rexBuilder, newJoinFilters),
          origJoin.getRowType(),
          origJoin.getJoinType() == JoinRelType.FULL,
          Pair.right(joinSpecs),
          Pair.left(joinSpecs),
          projFieldsList,
          newJoinFieldRefCountsMap,
          RexUtil.composeConjunction(rexBuilder, newPostJoinFilters, true));

  call.transformTo(multiJoin);
}
 
Example 10
Source File: CalcitePrepareImpl.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override protected PreparedResult implement(RelRoot root) {
  Hook.PLAN_BEFORE_IMPLEMENTATION.run(root);
  RelDataType resultType = root.rel.getRowType();
  boolean isDml = root.kind.belongsTo(SqlKind.DML);
  final Bindable bindable;
  if (resultConvention == BindableConvention.INSTANCE) {
    bindable = Interpreters.bindable(root.rel);
  } else {
    EnumerableRel enumerable = (EnumerableRel) root.rel;
    if (!root.isRefTrivial()) {
      final List<RexNode> projects = new ArrayList<>();
      final RexBuilder rexBuilder = enumerable.getCluster().getRexBuilder();
      for (int field : Pair.left(root.fields)) {
        projects.add(rexBuilder.makeInputRef(enumerable, field));
      }
      RexProgram program = RexProgram.create(enumerable.getRowType(),
          projects, null, root.validatedRowType, rexBuilder);
      enumerable = EnumerableCalc.create(enumerable, program);
    }

    try {
      CatalogReader.THREAD_LOCAL.set(catalogReader);
      final SqlConformance conformance = context.config().conformance();
      internalParameters.put("_conformance", conformance);
      bindable = EnumerableInterpretable.toBindable(internalParameters,
          context.spark(), enumerable, prefer);
    } finally {
      CatalogReader.THREAD_LOCAL.remove();
    }
  }

  if (timingTracer != null) {
    timingTracer.traceTime("end codegen");
  }

  if (timingTracer != null) {
    timingTracer.traceTime("end compilation");
  }

  return new PreparedResultImpl(
      resultType,
      parameterRowType,
      fieldOrigins,
      root.collation.getFieldCollations().isEmpty()
          ? ImmutableList.of()
          : ImmutableList.of(root.collation),
      root.rel,
      mapTableModOp(isDml, root.kind),
      isDml) {
    public String getCode() {
      throw new UnsupportedOperationException();
    }

    public Bindable getBindable(Meta.CursorFactory cursorFactory) {
      return bindable;
    }

    public Type getElementType() {
      return ((Typed) bindable).getElementType();
    }
  };
}
 
Example 11
Source File: RelDataTypeHolder.java    From calcite with Apache License 2.0 4 votes vote down vote up
public List<String> getFieldNames() {
  return Pair.left(fields);
}
 
Example 12
Source File: RelDataTypeImpl.java    From calcite with Apache License 2.0 4 votes vote down vote up
public List<String> getFieldNames() {
  return Pair.left(fieldList);
}
 
Example 13
Source File: JoinToMultiJoinRule.java    From Bats with Apache License 2.0 4 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final Join origJoin = call.rel(0);
  final RelNode left = call.rel(1);
  final RelNode right = call.rel(2);

  // combine the children MultiJoin inputs into an array of inputs
  // for the new MultiJoin
  final List<ImmutableBitSet> projFieldsList = new ArrayList<>();
  final List<int[]> joinFieldRefCountsList = new ArrayList<>();
  final List<RelNode> newInputs =
      combineInputs(
          origJoin,
          left,
          right,
          projFieldsList,
          joinFieldRefCountsList);

  // combine the outer join information from the left and right
  // inputs, and include the outer join information from the current
  // join, if it's a left/right outer join
  final List<Pair<JoinRelType, RexNode>> joinSpecs = new ArrayList<>();
  combineOuterJoins(
      origJoin,
      newInputs,
      left,
      right,
      joinSpecs);

  // pull up the join filters from the children MultiJoinRels and
  // combine them with the join filter associated with this LogicalJoin to
  // form the join filter for the new MultiJoin
  List<RexNode> newJoinFilters = combineJoinFilters(origJoin, left, right);

  // add on the join field reference counts for the join condition
  // associated with this LogicalJoin
  final ImmutableMap<Integer, ImmutableIntList> newJoinFieldRefCountsMap =
      addOnJoinFieldRefCounts(newInputs,
          origJoin.getRowType().getFieldCount(),
          origJoin.getCondition(),
          joinFieldRefCountsList);

  List<RexNode> newPostJoinFilters =
      combinePostJoinFilters(origJoin, left, right);

  final RexBuilder rexBuilder = origJoin.getCluster().getRexBuilder();
  RelNode multiJoin =
      new MultiJoin(
          origJoin.getCluster(),
          newInputs,
          RexUtil.composeConjunction(rexBuilder, newJoinFilters),
          origJoin.getRowType(),
          origJoin.getJoinType() == JoinRelType.FULL,
          Pair.right(joinSpecs),
          Pair.left(joinSpecs),
          projFieldsList,
          newJoinFieldRefCountsMap,
          RexUtil.composeConjunction(rexBuilder, newPostJoinFilters, true));

  call.transformTo(multiJoin);
}
 
Example 14
Source File: ReduceExpressionsRule.java    From calcite with Apache License 2.0 4 votes vote down vote up
protected static boolean reduceExpressionsInternal(RelNode rel,
    RexSimplify simplify, RexUnknownAs unknownAs, List<RexNode> expList,
    RelOptPredicateList predicates) {
  // Replace predicates on CASE to CASE on predicates.
  boolean changed = new CaseShuttle().mutate(expList);

  // Find reducible expressions.
  final List<RexNode> constExps = new ArrayList<>();
  List<Boolean> addCasts = new ArrayList<>();
  findReducibleExps(rel.getCluster().getTypeFactory(), expList,
      predicates.constantMap, constExps, addCasts);
  if (constExps.isEmpty()) {
    return changed;
  }

  final List<RexNode> constExps2 = Lists.newArrayList(constExps);
  if (!predicates.constantMap.isEmpty()) {
    //noinspection unchecked
    final List<Map.Entry<RexNode, RexNode>> pairs =
        Lists.newArrayList(predicates.constantMap.entrySet());
    RexReplacer replacer =
        new RexReplacer(simplify, unknownAs, Pair.left(pairs),
            Pair.right(pairs), Collections.nCopies(pairs.size(), false));
    replacer.mutate(constExps2);
  }

  // Compute the values they reduce to.
  RexExecutor executor = rel.getCluster().getPlanner().getExecutor();
  if (executor == null) {
    // Cannot reduce expressions: caller has not set an executor in their
    // environment. Caller should execute something like the following before
    // invoking the planner:
    //
    // final RexExecutorImpl executor =
    //   new RexExecutorImpl(Schemas.createDataContext(null));
    // rootRel.getCluster().getPlanner().setExecutor(executor);
    return changed;
  }

  final List<RexNode> reducedValues = new ArrayList<>();
  executor.reduce(simplify.rexBuilder, constExps2, reducedValues);

  // Use RexNode.digest to judge whether each newly generated RexNode
  // is equivalent to the original one.
  if (RexUtil.strings(constExps).equals(RexUtil.strings(reducedValues))) {
    return changed;
  }

  // For Project, we have to be sure to preserve the result
  // types, so always cast regardless of the expression type.
  // For other RelNodes like Filter, in general, this isn't necessary,
  // and the presence of casts could hinder other rules such as sarg
  // analysis, which require bare literals.  But there are special cases,
  // like when the expression is a UDR argument, that need to be
  // handled as special cases.
  if (rel instanceof Project) {
    addCasts = Collections.nCopies(reducedValues.size(), true);
  }

  new RexReplacer(simplify, unknownAs, constExps, reducedValues, addCasts)
      .mutate(expList);
  return true;
}
 
Example 15
Source File: FlinkJoinToMultiJoinRule.java    From flink with Apache License 2.0 4 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
	final Join origJoin = call.rel(0);
	final RelNode left = call.rel(1);
	final RelNode right = call.rel(2);

	// combine the children MultiJoin inputs into an array of inputs
	// for the new MultiJoin
	final List<ImmutableBitSet> projFieldsList = new ArrayList<>();
	final List<int[]> joinFieldRefCountsList = new ArrayList<>();
	final List<RelNode> newInputs =
			combineInputs(
					origJoin,
					left,
					right,
					projFieldsList,
					joinFieldRefCountsList);

	// combine the outer join information from the left and right
	// inputs, and include the outer join information from the current
	// join, if it's a left/right outer join
	final List<Pair<JoinRelType, RexNode>> joinSpecs = new ArrayList<>();
	combineOuterJoins(
			origJoin,
			newInputs,
			left,
			right,
			joinSpecs);

	// pull up the join filters from the children MultiJoinRels and
	// combine them with the join filter associated with this LogicalJoin to
	// form the join filter for the new MultiJoin
	List<RexNode> newJoinFilters = combineJoinFilters(origJoin, left, right);

	// add on the join field reference counts for the join condition
	// associated with this LogicalJoin
	final com.google.common.collect.ImmutableMap<Integer, ImmutableIntList> newJoinFieldRefCountsMap =
			addOnJoinFieldRefCounts(newInputs,
					origJoin.getRowType().getFieldCount(),
					origJoin.getCondition(),
					joinFieldRefCountsList);

	List<RexNode> newPostJoinFilters =
			combinePostJoinFilters(origJoin, left, right);

	final RexBuilder rexBuilder = origJoin.getCluster().getRexBuilder();
	RelNode multiJoin =
			new MultiJoin(
					origJoin.getCluster(),
					newInputs,
					RexUtil.composeConjunction(rexBuilder, newJoinFilters),
					origJoin.getRowType(),
					origJoin.getJoinType() == JoinRelType.FULL,
					Pair.right(joinSpecs),
					Pair.left(joinSpecs),
					projFieldsList,
					newJoinFieldRefCountsMap,
					RexUtil.composeConjunction(rexBuilder, newPostJoinFilters, true));

	call.transformTo(multiJoin);
}
 
Example 16
Source File: RelOptTableImpl.java    From Bats with Apache License 2.0 4 votes vote down vote up
public static RelOptTableImpl create(RelOptSchema schema, RelDataType rowType, Table table, Path path) {
    return new RelOptTableImpl(schema, rowType, Pair.left(path), table, table.getStatistic().getRowCount());
}
 
Example 17
Source File: RelDataTypeHolder.java    From Bats with Apache License 2.0 4 votes vote down vote up
public List<String> getFieldNames() {
  return Pair.left(fields);
}
 
Example 18
Source File: RelDataTypeImpl.java    From Bats with Apache License 2.0 4 votes vote down vote up
public List<String> getFieldNames() {
  return Pair.left(fieldList);
}