code: def check_optimization(  # noqa: D
      request: FixtureRequest,
      mf_test_session_state: MetricFlowTestSessionState,
      dataflow_plan_builder: DataflowPlanBuilder,
      query_spec: MetricFlowQuerySpec,
      expected_num_sources_in_unoptimized: int,
      expected_num_sources_in_optimized: int,
  ) -> None:
      dataflow_plan = dataflow_plan_builder.build_plan(query_spec)
  
      assert_plan_snapshot_text_equal(
          request=request,
          mf_test_session_state=mf_test_session_state,
          plan=dataflow_plan,
          plan_snapshot_text=dataflow_plan_as_text(dataflow_plan),
      )
  
      display_graph_if_requested(
          request=request,
          mf_test_session_state=mf_test_session_state,
          dag_graph=dataflow_plan,
      )
  
      source_counter = ReadSqlSourceNodeCounter()
      assert (
          source_counter.count_source_nodes(dataflow_plan)
          == expected_num_sources_in_unoptimized
      )
  
      optimizer = SourceScanOptimizer()
      optimized_dataflow_plan = optimizer.optimize(dataflow_plan)
  
      assert_plan_snapshot_text_equal(
          request=request,
          mf_test_session_state=mf_test_session_state,
          plan=optimized_dataflow_plan,
          plan_snapshot_text=dataflow_plan_as_text(
              optimized_dataflow_plan
          ),
      )
  
      display_graph_if_requested(
          request=request,
          mf_test_session_state=mf_test_session_state,
          dag_graph=optimized_dataflow_plan,
      )
      assert (
          source_counter.count_source_nodes(optimized_dataflow_plan)
          == expected_num_sources_in_optimized
      )
  
imports: from __future__ import annotations
  import logging
  import pytest
  from _pytest.fixtures import FixtureRequest
  from dbt_semantic_interfaces.naming.keywords import METRIC_TIME_ELEMENT_NAME
  from dbt_semantic_interfaces.type_enums.time_granularity import TimeGranularity
  from metricflow.dataflow.builder.dataflow_plan_builder import DataflowPlanBuilder
  from metricflow.dataflow.dataflow_plan import AddGeneratedUuidColumnNode, AggregateMeasuresNode, CombineAggregatedOutputsNode, ComputeMetricsNode, ConstrainTimeRangeNode, DataflowPlan, DataflowPlanNode, DataflowPlanNodeVisitor, FilterElementsNode, JoinConversionEventsNode, JoinOverTimeRangeNode, JoinToBaseOutputNode, JoinToTimeSpineNode, MetricTimeDimensionTransformNode, MinMaxNode, OrderByLimitNode, ReadSqlSourceNode, SemiAdditiveJoinNode, WhereConstraintNode, WriteToResultDataframeNode, WriteToResultTableNode
  from metricflow.dataflow.dataflow_plan_to_text import dataflow_plan_as_text
  from metricflow.dataflow.optimizer.source_scan.source_scan_optimizer import SourceScanOptimizer
  from metricflow.dataset.dataset import DataSet
  from metricflow.query.query_parser import MetricFlowQueryParser
  from metricflow.specs.column_assoc import ColumnAssociationResolver
  from metricflow.specs.specs import DimensionSpec, EntityReference, MetricFlowQuerySpec, MetricSpec
  from metricflow.test.dataflow_plan_to_svg import display_graph_if_requested
  from metricflow.test.fixtures.setup_fixtures import MetricFlowTestSessionState
  from metricflow.test.snapshot_utils import assert_plan_snapshot_text_equal
label_desc: Use image node_id 1 for calling a global function with example usage: check_optimization(request, mf_test_session_state, dataflow_plan_builder, query_spec, expected_num_sources_in_unoptimized, expected_num_sources_in_optimized) without return types