From 1fca70512d2212e535ff980a3f94e7dc69ed0f2f Mon Sep 17 00:00:00 2001
From: Alexander Polyankin <alexander.polyankin@metabase.com>
Date: Wed, 24 Aug 2022 15:42:07 +0300
Subject: [PATCH] Fix segments in case custom expressions (#24939)

---
 .../lib/expressions/recursive-parser.js       |  2 +-
 .../24922-cc-case-segment.cy.spec.js          | 44 +++++++++++++++++++
 2 files changed, 45 insertions(+), 1 deletion(-)
 create mode 100644 frontend/test/metabase/scenarios/custom-column/reproductions/24922-cc-case-segment.cy.spec.js

diff --git a/frontend/src/metabase/lib/expressions/recursive-parser.js b/frontend/src/metabase/lib/expressions/recursive-parser.js
index 1fef7c805ca..ca92a016a38 100644
--- a/frontend/src/metabase/lib/expressions/recursive-parser.js
+++ b/frontend/src/metabase/lib/expressions/recursive-parser.js
@@ -319,7 +319,7 @@ export const adjustBooleans = tree =>
             const [op, _id, opts] = operand;
             const isBooleanField =
               op === "field" && opts?.["base-type"] === "type/Boolean";
-            if (isBooleanField || op === "segment") {
+            if (isBooleanField) {
               return withAST([["=", operand, true], value], operand);
             }
             return [operand, value];
diff --git a/frontend/test/metabase/scenarios/custom-column/reproductions/24922-cc-case-segment.cy.spec.js b/frontend/test/metabase/scenarios/custom-column/reproductions/24922-cc-case-segment.cy.spec.js
new file mode 100644
index 00000000000..d042ce64ee3
--- /dev/null
+++ b/frontend/test/metabase/scenarios/custom-column/reproductions/24922-cc-case-segment.cy.spec.js
@@ -0,0 +1,44 @@
+import {
+  enterCustomColumnDetails,
+  openOrdersTable,
+  restore,
+  visualize,
+} from "__support__/e2e/helpers";
+import { SAMPLE_DATABASE } from "__support__/e2e/cypress_sample_database";
+
+const { ORDERS, ORDERS_ID } = SAMPLE_DATABASE;
+
+const segmentDetails = {
+  name: "OrdersSegment",
+  description: "All orders with a total under $100.",
+  table_id: ORDERS_ID,
+  definition: {
+    "source-table": ORDERS_ID,
+    aggregation: [["count"]],
+    filter: ["<", ["field", ORDERS.TOTAL, null], 100],
+  },
+};
+
+const customColumnDetails = {
+  name: "CustomColumn",
+  formula: 'case([OrdersSegment], "Segment", "Other")',
+};
+
+describe("issue 24922", () => {
+  beforeEach(() => {
+    restore();
+    cy.signInAsAdmin();
+    cy.request("POST", "/api/segment", segmentDetails);
+  });
+
+  it("should allow segments in case custom expressions (metabase#24922)", () => {
+    openOrdersTable({ mode: "notebook" });
+
+    cy.findByText("Custom column").click();
+    enterCustomColumnDetails(customColumnDetails);
+    cy.button("Done").click();
+
+    visualize();
+    cy.findByText("CustomColumn").should("be.visible");
+  });
+});
-- 
GitLab