From 4089e78c18037c2698ad3a929bfd5b8c7187f2f0 Mon Sep 17 00:00:00 2001
From: Paul Yang <github@ardorful.com>
Date: Mon, 11 Nov 2024 16:05:41 -0800
Subject: [PATCH] Add test for optimization.

---
 .../integration/test_mf_engine.py             | 25 +++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/tests_metricflow/integration/test_mf_engine.py b/tests_metricflow/integration/test_mf_engine.py
index 9aa1373701..b933835aa8 100644
--- a/tests_metricflow/integration/test_mf_engine.py
+++ b/tests_metricflow/integration/test_mf_engine.py
@@ -3,6 +3,8 @@
 from _pytest.fixtures import FixtureRequest
 from metricflow_semantics.test_helpers.config_helpers import MetricFlowTestConfiguration
 
+from metricflow.engine.metricflow_engine import MetricFlowExplainResult, MetricFlowQueryRequest
+from metricflow.sql.optimizer.optimization_levels import SqlQueryOptimizationLevel
 from tests_metricflow.integration.conftest import IntegrationTestHelpers
 from tests_metricflow.snapshot_utils import assert_object_snapshot_equal
 
@@ -16,3 +18,26 @@ def test_list_dimensions(  # noqa: D103
         obj_id="result0",
         obj=sorted([dim.qualified_name for dim in it_helpers.mf_engine.list_dimensions()]),
     )
+
+
+def test_sql_optimization_level(it_helpers: IntegrationTestHelpers) -> None:
+    """Check that different SQL optimization levels produce different SQL."""
+    assert (
+        SqlQueryOptimizationLevel.default_level() != SqlQueryOptimizationLevel.O0
+    ), "The default optimization level should be different from the lowest level."
+    explain_result_at_default_level: MetricFlowExplainResult = it_helpers.mf_engine.explain(
+        MetricFlowQueryRequest.create_with_random_request_id(
+            metric_names=("bookings",),
+            group_by_names=("metric_time",),
+            sql_optimization_level=SqlQueryOptimizationLevel.default_level(),
+        )
+    )
+    explain_result_at_level_0: MetricFlowExplainResult = it_helpers.mf_engine.explain(
+        MetricFlowQueryRequest.create_with_random_request_id(
+            metric_names=("bookings",),
+            group_by_names=("metric_time",),
+            sql_optimization_level=SqlQueryOptimizationLevel.O0,
+        )
+    )
+
+    assert explain_result_at_default_level.rendered_sql.sql_query != explain_result_at_level_0.rendered_sql.sql_query