Java Code Examples for org.apache.calcite.rel.logical.LogicalProject#create()

The following examples show how to use org.apache.calcite.rel.logical.LogicalProject#create() . 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: MockCatalogReader.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public RelNode toRel(ToRelContext context) {
  RelNode rel = LogicalTableScan.create(context.getCluster(), fromTable,
      context.getTableHints());
  final RexBuilder rexBuilder = context.getCluster().getRexBuilder();
  rel = LogicalFilter.create(
      rel, getConstraint(rexBuilder, rel.getRowType()));
  final List<RelDataTypeField> fieldList =
      rel.getRowType().getFieldList();
  final List<Pair<RexNode, String>> projects =
      new AbstractList<Pair<RexNode, String>>() {
        @Override public Pair<RexNode, String> get(int index) {
          return RexInputRef.of2(mapping.get(index), fieldList);
        }

        @Override public int size() {
          return mapping.size();
        }
      };
  return LogicalProject.create(rel,
      ImmutableList.of(),
      Pair.left(projects),
      Pair.right(projects));
}
 
Example 2
Source File: StreamRules.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override public void onMatch(RelOptRuleCall call) {
  final Delta delta = call.rel(0);
  Util.discard(delta);
  final Project project = call.rel(1);
  final LogicalDelta newDelta = LogicalDelta.create(project.getInput());
  final LogicalProject newProject =
      LogicalProject.create(newDelta, project.getProjects(),
          project.getRowType().getFieldNames());
  call.transformTo(newProject);
}
 
Example 3
Source File: RelRoot.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Returns the root relational expression as a {@link LogicalProject}.
 *
 * @param force Create a Project even if all fields are used */
public RelNode project(boolean force) {
  if (isRefTrivial()
      && (SqlKind.DML.contains(kind)
          || !force
          || rel instanceof LogicalProject)) {
    return rel;
  }
  final List<RexNode> projects = new ArrayList<>();
  final RexBuilder rexBuilder = rel.getCluster().getRexBuilder();
  for (Pair<Integer, String> field : fields) {
    projects.add(rexBuilder.makeInputRef(rel, field.left));
  }
  return LogicalProject.create(rel, projects, Pair.right(fields));
}
 
Example 4
Source File: ShortenJdbcColumnAliases.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public RelNode visit(RelNode other) {
  if (other instanceof JdbcCrel) {
    final JdbcCrel logical = (JdbcCrel) other;

    // Eliminate subsets to enforce that all nodes in the Jdbc subtree are JdbcRelImpl nodes.
    final RelNode subsetRemoved = logical.getInput().accept(new MoreRelOptUtil.SubsetRemover());
    logical.replaceInput(0, subsetRemoved);

    if (logical.getPluginId() == null) {
      return logical;
    }

    final RelNode updatedJdbcRoot = super.visitChildren(logical);

    // Add a LogicalProject to map the original set of column aliases if necessary.
    if (logical.getRowType().equals(updatedJdbcRoot.getRowType())) {
      return updatedJdbcRoot;
    }

    final LogicalProject logicalProject = LogicalProject.create(updatedJdbcRoot,
      updatedJdbcRoot.getCluster().getRexBuilder().identityProjects(updatedJdbcRoot.getRowType()), other.getRowType());

    return logicalProject;

  } else if (other instanceof JdbcRelImpl) {
    final RelNode updatedNode = super.visitChildren(other);

    final JdbcRelImpl jdbcNode = (JdbcRelImpl) updatedNode;
    return jdbcNode.shortenAliases(SHORT_ALIAS_SUGGESTER, usedAliases);
  }
  return super.visit(other);
}
 
Example 5
Source File: SplunkPushDownRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static RelNode addProjectionRule(LogicalProject proj, RelNode rel) {
  if (proj == null) {
    return rel;
  }
  return LogicalProject.create(rel, proj.getHints(),
      proj.getProjects(), proj.getRowType());
}
 
Example 6
Source File: StreamRules.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public void onMatch(RelOptRuleCall call) {
  final Delta delta = call.rel(0);
  Util.discard(delta);
  final Project project = call.rel(1);
  final LogicalDelta newDelta = LogicalDelta.create(project.getInput());
  final LogicalProject newProject =
      LogicalProject.create(newDelta,
          project.getHints(),
          project.getProjects(),
          project.getRowType().getFieldNames());
  call.transformTo(newProject);
}
 
Example 7
Source File: RelRoot.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Returns the root relational expression as a {@link LogicalProject}.
 *
 * @param force Create a Project even if all fields are used */
public RelNode project(boolean force) {
  if (isRefTrivial()
      && (SqlKind.DML.contains(kind)
          || !force
          || rel instanceof LogicalProject)) {
    return rel;
  }
  final List<RexNode> projects = new ArrayList<>(fields.size());
  final RexBuilder rexBuilder = rel.getCluster().getRexBuilder();
  for (Pair<Integer, String> field : fields) {
    projects.add(rexBuilder.makeInputRef(rel, field.left));
  }
  return LogicalProject.create(rel, hints, projects, Pair.right(fields));
}
 
Example 8
Source File: RelFactories.java    From Bats with Apache License 2.0 4 votes vote down vote up
public RelNode createProject(RelNode input,
    List<? extends RexNode> childExprs, List<String> fieldNames) {
  return LogicalProject.create(input, childExprs, fieldNames);
}
 
Example 9
Source File: RelFactories.java    From calcite with Apache License 2.0 4 votes vote down vote up
public RelNode createProject(RelNode input, List<RelHint> hints,
    List<? extends RexNode> childExprs, List<String> fieldNames) {
  return LogicalProject.create(input, hints, childExprs, fieldNames);
}
 
Example 10
Source File: LixToRelTranslator.java    From calcite with Apache License 2.0 4 votes vote down vote up
public RelNode translate(Expression expression) {
  if (expression instanceof MethodCallExpression) {
    final MethodCallExpression call = (MethodCallExpression) expression;
    BuiltInMethod method = BuiltInMethod.MAP.get(call.method);
    if (method == null) {
      throw new UnsupportedOperationException(
          "unknown method " + call.method);
    }
    RelNode input;
    switch (method) {
    case SELECT:
      input = translate(call.targetExpression);
      return LogicalProject.create(input,
          ImmutableList.of(),
          toRex(input, (FunctionExpression) call.expressions.get(0)),
          (List<String>) null);

    case WHERE:
      input = translate(call.targetExpression);
      return LogicalFilter.create(input,
          toRex((FunctionExpression) call.expressions.get(0), input));

    case AS_QUERYABLE:
      return LogicalTableScan.create(cluster,
          RelOptTableImpl.create(null,
              typeFactory.createJavaType(
                  Types.toClass(
                      Types.getElementType(call.targetExpression.getType()))),
              ImmutableList.of(),
              call.targetExpression),
          ImmutableList.of());

    case SCHEMA_GET_TABLE:
      return LogicalTableScan.create(cluster,
          RelOptTableImpl.create(null,
              typeFactory.createJavaType((Class)
                  ((ConstantExpression) call.expressions.get(1)).value),
              ImmutableList.of(),
              call.targetExpression),
          ImmutableList.of());

    default:
      throw new UnsupportedOperationException(
          "unknown method " + call.method);
    }
  }
  throw new UnsupportedOperationException(
      "unknown expression type " + expression.getNodeType());
}