class_docstr: Visits the SQL query plan to simplify sub-queries. On each visit, return a simplified node.
  
  Unlike SqlSubQueryReducerVisitor, this will re-write expressions to realize more reductions.
code: def visit_select_statement_node(
      self, node: SqlSelectStatementNode
  ) -> SqlQueryPlanNode:  # noqa: D
      node_with_reduced_parents = self._reduce_parents(node)
  
      if len(node_with_reduced_parents.parent_nodes) > 1:
          return SqlRewritingSubQueryReducerVisitor._rewrite_node_with_join(
              node_with_reduced_parents
          )
  
      if not self._current_node_can_be_reduced(
          node_with_reduced_parents
      ):
          return node_with_reduced_parents
  
      # Note that just because this node can't be reduced doesn't mean the parent node can't. In this example, the
      # outer query can't be reduced because there is a join, but sub-query d can be.
      #
      # SELECT a.bookings, b.listing__country
      # FROM (
      #   SELECT d.bookings, d.listing_id
      #   FROM (
      #     SELECT e.bookings, e.listing_id
      #     FROM fct_bookings
      #   ) d
      #   LIMIT 10
      # ) b
      # JOIN dim_listings c
      # ON a.listing_id = b.listing_id
  
      assert len(node_with_reduced_parents.parent_nodes) == 1
      parent_node = node_with_reduced_parents.parent_nodes[0]
      parent_select_node = parent_node.as_select_node
      assert parent_select_node
  
      # At this point, the query should look similar to
      #
      #     SELECT b.foo, b.baz
      #     FROM (
      #       SELECT 1 AS foo, a.baz FROM bar a
      #       LIMIT 10
      #     ) b
      #     GROUP BY b.foo
      #     ORDER by b.baz
      #     LIMIT 1
  
      # The ORDER BY in the parent doesn't matter since the order by in this node will "overwrite" the order in the
      # parent as long as the parent has no limits.
      column_replacements = (
          SqlRewritingSubQueryReducerVisitor._get_column_replacements(
              parent_node=parent_select_node,
              parent_node_alias=node.from_source_alias,
          )
      )
      new_order_bys: List[SqlOrderByDescription] = []
      # Handle ORDER BY differently to avoid this hazard with expression rewriting:
      #
      # SELECT a.bookings AS bookings
      # FROM (
      #   SELECT SUM(b.bookings) AS bookings
      #   FROM fct_bookings b
      # ) a
      # ORDER BY a.bookings
      #
      # ->
      #
      # SELECT SUM(b.bookings) AS bookings
      # FROM fct_bookings b
      # ORDER BY SUM(b.bookings) -- Throws an error in some engines.
      if node_with_reduced_parents.order_bys:
          for order_by_item in node_with_reduced_parents.order_bys:
              order_by_item_expr = (
                  order_by_item.expr.as_column_reference_expression
              )
              assert order_by_item_expr
  
              matching_select_column = SqlRewritingSubQueryReducerVisitor._find_matching_select_column(
                  col_ref=order_by_item_expr.col_ref,
                  select_columns=node_with_reduced_parents.select_columns,
              )
              # This must be the case because of _should_reduce()
              assert matching_select_column
  
              new_order_bys.append(
                  SqlOrderByDescription(
                      expr=SqlColumnAliasReferenceExpression(
                          column_alias=matching_select_column.column_alias
                      ),
                      desc=order_by_item.desc,
                  )
              )
  
      # The limit should be the min of this SELECT limit and the parent SELECT limit.
      new_limit: Optional[int] = node_with_reduced_parents.limit
      if new_limit is None:
          new_limit = parent_select_node.limit
      elif parent_select_node.limit is not None:
          new_limit = min(new_limit, parent_select_node.limit)
  
      new_group_bys: Tuple[SqlSelectColumn, ...] = ()
      if node.group_bys and parent_select_node.group_bys:
          raise RuntimeError(
              "Attempting to reduce sub-queries when this and the parent have GROUP BYs. This should have been "
              "prevent by _should_reduce()"
          )
      elif node.group_bys:
          new_group_bys = SqlRewritingSubQueryReducerVisitor._rewrite_select_columns(
              old_select_columns=node.group_bys,
              column_replacements=column_replacements,
          )
      elif parent_select_node.group_bys:
          new_group_bys = parent_select_node.group_bys
  
      return SqlSelectStatementNode(
          description="\n".join(
              [
                  parent_select_node.description,
                  node_with_reduced_parents.description,
              ]
          ),
          select_columns=SqlRewritingSubQueryReducerVisitor._rewrite_select_columns(
              old_select_columns=node.select_columns,
              column_replacements=column_replacements,
          ),
          from_source=parent_select_node.from_source,
          from_source_alias=parent_select_node.from_source_alias,
          joins_descs=parent_select_node.join_descs,
          group_bys=new_group_bys,
          order_bys=tuple(new_order_bys),
          where=SqlRewritingSubQueryReducerVisitor._rewrite_where(
              column_replacements=column_replacements,
              node_where=node.where,
              parent_node_where=parent_select_node.where,
          ),
          limit=new_limit,
          distinct=parent_select_node.distinct,
      )
  
imports: from __future__ import annotations
  import logging
  from dataclasses import dataclass
  from typing import List, Optional, Sequence, Tuple
  from metricflow.sql.optimizer.sql_query_plan_optimizer import SqlQueryPlanOptimizer
  from metricflow.sql.sql_exprs import SqlColumnAliasReferenceExpression, SqlColumnReference, SqlColumnReplacements, SqlExpressionNode, SqlExpressionTreeLineage, SqlLogicalExpression, SqlLogicalOperator
  from metricflow.sql.sql_plan import SqlJoinDescription, SqlOrderByDescription, SqlQueryPlanNode, SqlQueryPlanNodeVisitor, SqlSelectColumn, SqlSelectQueryFromClauseNode, SqlSelectStatementNode, SqlTableFromClauseNode
label_desc: Use image node_id 14 for calling the SqlRewritingSubQueryReducerVisitor obj's underlying member method code with example usage: obj.visit_select_statement_node(node) and returns: SqlSelectStatementNode, SqlRewritingSubQueryReducerVisitor, node_with_reduced_parents