Java Code Examples for org.apache.calcite.rel.core.Project#getChildExps()

The following examples show how to use org.apache.calcite.rel.core.Project#getChildExps() . 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: RelToSqlConverter.java    From Bats with Apache License 2.0 6 votes vote down vote up
/** @see #dispatch */
public Result visit(Project e) {
  Result x = visitChild(0, e.getInput());
  parseCorrelTable(e, x);
  if (isStar(e.getChildExps(), e.getInput().getRowType(), e.getRowType())) {
    return x;
  }
  final Builder builder =
      x.builder(e, Clause.SELECT);
  final List<SqlNode> selectList = new ArrayList<>();
  for (RexNode ref : e.getChildExps()) {
    SqlNode sqlExpr = builder.context.toSql(null, ref);
    addSelect(selectList, sqlExpr, e.getRowType());
  }

  builder.setSelect(new SqlNodeList(selectList, POS));
  return builder.result();
}
 
Example 2
Source File: RelToSqlConverter.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
/**
 * @see #dispatch
 */
public Result visit(Project e) {
  Result x = visitChild(0, e.getInput());
  parseCorrelTable(e, x);
  if (isStar(e.getChildExps(), e.getInput().getRowType(), e.getRowType())) {
    return x;
  }
  final Builder builder =
    x.builder(e, Clause.SELECT);
  final List<SqlNode> selectList = new ArrayList<>();
  for (RexNode ref : e.getChildExps()) {
    SqlNode sqlExpr = builder.context.toSql(null,
      simplifyDatetimePlus(ref, e.getCluster().getRexBuilder()));
    addSelect(selectList, sqlExpr, e.getRowType());
  }

  builder.setSelect(new SqlNodeList(selectList, POS));
  return builder.result();
}
 
Example 3
Source File: RelNodeCompiler.java    From streamline with Apache License 2.0 6 votes vote down vote up
@Override
public Void visitProject(Project project, List<Void> inputStreams) throws Exception {
  beginStage(project);

  List<RexNode> childExps = project.getChildExps();
  RelDataType inputRowType = project.getInput(0).getRowType();
  int outputCount = project.getRowType().getFieldCount();

  pw.print("Context context = new StreamlineContext(Processor.dataContext);\n");
  pw.print("context.values = _data.toArray();\n");
  pw.print(String.format("Object[] outputValues = new Object[%d];\n", outputCount));

  pw.write(rexCompiler.compileToBlock(childExps, inputRowType).toString());

  pw.print("    ctx.emit(new CorrelatedValues(_data.getCorrelated(), outputValues));\n");
  endStage();
  return null;
}
 
Example 4
Source File: DrillPushProjectIntoScanRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public void onMatch(RelOptRuleCall call) {
  Project project = call.rel(0);
  TableScan scan = call.rel(1);

  try {
    if (scan.getRowType().getFieldList().isEmpty()) {
      return;
    }

    ProjectPushInfo projectPushInfo = DrillRelOptUtil.getFieldsInformation(scan.getRowType(), project.getProjects());
    if (!canPushProjectIntoScan(scan.getTable(), projectPushInfo)
        || skipScanConversion(projectPushInfo.createNewRowType(project.getCluster().getTypeFactory()), scan)) {
      // project above scan may be removed in ProjectRemoveRule for the case when it is trivial
      return;
    }

    DrillScanRelBase newScan = createScan(scan, projectPushInfo);

    List<RexNode> newProjects = new ArrayList<>();
    for (RexNode n : project.getChildExps()) {
      newProjects.add(n.accept(projectPushInfo.getInputReWriter()));
    }

    DrillProjectRelBase newProject =
        createProject(project, newScan, newProjects);

    if (ProjectRemoveRule.isTrivial(newProject)) {
      call.transformTo(newScan);
    } else {
      call.transformTo(newProject);
    }
  } catch (IOException e) {
    throw new DrillRuntimeException(e);
  }
}
 
Example 5
Source File: DrillMergeProjectRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
private boolean checkComplexOutput(Project project) {
  for (RexNode expr: project.getChildExps()) {
    if (expr instanceof RexCall) {
      if (functionRegistry.isFunctionComplexOutput(((RexCall) expr).getOperator().getName())) {
        return true;
      }
    }
  }
  return false;
}
 
Example 6
Source File: ProjectRule.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public boolean matches(RelOptRuleCall call) {
  // this cannot operate on a project that has a flatten in it.
  final Project project = call.rel(0);
  for (RexNode e : project.getChildExps()) {
    if (FlattenVisitors.hasFlatten(e)) {
      return false;
    }
  }
  return true;
}
 
Example 7
Source File: PushProjectIntoScanRule.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public void onMatch(RelOptRuleCall call) {
  final Project proj = call.rel(0);
  final ScanCrel scan = call.rel(1);

  ProjectPushInfo columnInfo = PrelUtil.getColumns(scan.getRowType(), proj.getProjects());

  // get TableBase, either wrapped in RelOptTable, or TranslatableTable. TableBase table = scan.getTable().unwrap(TableBase.class);
  if (columnInfo == null || columnInfo.isStarQuery()) {
    return;
  }

  ScanCrel newScan = scan.cloneWithProject(columnInfo.columns);

  List<RexNode> newProjects = Lists.newArrayList();
  for (RexNode n : proj.getChildExps()) {
    newProjects.add(n.accept(columnInfo.getInputRewriter()));
  }

  final RelBuilder relBuilder = relBuilderFactory.create(proj.getCluster(), null);
  relBuilder.push(newScan);
  relBuilder.project(newProjects, proj.getRowType().getFieldNames());
  final RelNode newProj = relBuilder.build();

  if (newProj instanceof Project
      && ProjectRemoveRule.isTrivial((Project) newProj)
      && newScan.getRowType().getFullTypeString().equals(newProj.getRowType().getFullTypeString())) {
      call.transformTo(newScan);
  } else {
    if(newScan.getProjectedColumns().equals(scan.getProjectedColumns())) {
      // no point in doing a pushdown that doesn't change anything.
      return;
    }

    call.transformTo(newProj);
  }
}
 
Example 8
Source File: FlattenVisitors.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public static boolean hasFlatten(Project project){
  for(RexNode n : project.getChildExps()){
    if(n.accept(new FlattenFinder())){
      return true;
    }
  }
  return false;
}
 
Example 9
Source File: RelToSqlConverter.java    From quark with Apache License 2.0 5 votes vote down vote up
public Result visitProject(Project e) {
  Result x = visitChild(0, e.getInput());
  if (isStar(e.getChildExps(), e.getInput().getRowType())) {
    return x;
  }
  final Builder builder =
      x.builder(e, Clause.SELECT);
  final List<SqlNode> selectList = new ArrayList<>();
  for (RexNode ref : e.getChildExps()) {
    SqlNode sqlExpr = builder.context.toSql(null, ref);
    addSelect(selectList, sqlExpr, e.getRowType());
  }
  builder.setSelect(new SqlNodeList(selectList, POS));
  return builder.result();
}
 
Example 10
Source File: FlattenVisitors.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
public void add(Project project){
  for(RexNode e : project.getChildExps()){
    e.accept(this);
  }
}
 
Example 11
Source File: DremioRelToSqlConverter.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Process the Result from visiting a Project node's child.
 *
 * @param project     The project node.
 * @param childResult The Result from calling visit() on the child of project.
 * @return The Result of visiting the Project node itself.
 */
protected DremioRelToSqlConverter.Result processProjectChild(Project project, DremioRelToSqlConverter.Result childResult) {
  // unlike the super impl #visit(Project), this expands star ("*") if additional push downs are enabled

  Pointer<Boolean> isPartial = new Pointer<>(false);
  project.accept(new StatelessRelShuttleImpl() {
    @Override
    public RelNode visit(TableScan scan) {
      if (scan instanceof ScanRelBase) {
        ScanRelBase tableScan = (ScanRelBase) scan;
        if (tableScan.getTableMetadata().getSchema().getFieldCount() != tableScan.getProjectedColumns().size()) {
          isPartial.value = true;
        }
      }
      return scan;
    }
  });

  PlannerSettings plannerSettings = PrelUtil.getPlannerSettings(project.getCluster());
  if (plannerSettings != null &&
    !plannerSettings.getOptions().getOption(PlannerSettings.JDBC_PUSH_DOWN_PLUS) &&
    !isPartial.value &&
    isStar(project.getChildExps(), project.getInput().getRowType(), project.getRowType())) {
    return childResult;
  }

  final DremioRelToSqlConverter.Builder builder =
    childResult.builder(project, SqlImplementor.Clause.SELECT);
  final List<SqlNode> selectList = new ArrayList<>();
  for (RexNode ref : project.getChildExps()) {
    SqlNode sqlExpr = builder.context.toSql(null, simplifyDatetimePlus(ref, project.getCluster().getRexBuilder()));
    if ((getDialect().shouldInjectNumericCastToProject() && isDecimal(ref.getType())) ||
        (getDialect().shouldInjectApproxNumericCastToProject() && isApproximateNumeric(ref.getType()))) {
      if (!((sqlExpr.getKind() == SqlKind.CAST) || (sqlExpr.getKind() == SqlKind.AS && ((SqlBasicCall) sqlExpr).operand(0).getKind() == SqlKind.CAST))) {
        // Add an explicit cast around this projection.
        sqlExpr = SqlStdOperatorTable.CAST.createCall(POS, sqlExpr, getDialect().getCastSpec(ref.getType()));
      }
    }

    addSelect(selectList, sqlExpr, project.getRowType());
  }

  builder.setSelect(new SqlNodeList(selectList, POS));
  return builder.result();
}