private RelRoot optimize(Planner planner, RelRoot relRoot) {
    RelTraitSet relTraitSet = RelTraitSet.createEmpty();
    try {
      RelRoot optimizedRelRoot =
          RelRoot.of(planner.transform(0, relTraitSet, relRoot.project()), SqlKind.SELECT);
      LOG.info("query plan with optimization:\n"
          + RelOptUtil.toString(optimizedRelRoot.rel, SqlExplainLevel.EXPPLAN_ATTRIBUTES));
      return optimizedRelRoot;
    } catch (Exception e) {
      String errorMsg =
          "Error while optimizing query plan:\n" + RelOptUtil.toString(relRoot.rel, SqlExplainLevel.EXPPLAN_ATTRIBUTES);
      LOG.error(errorMsg, e);
      throw new SamzaException(errorMsg, e);
    }
  }
QueryPlanner that uses calcite engine to convert the sql query to relational graph.
samza-sql/src/main/java/org/apache/samza/sql/planner/QueryPlanner.java