Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Prevent expression editor from forcing literal types (#67647)

metamben8239cbfa00f8
diff --git a/frontend/src/metabase/querying/expressions/compile-expression.unit.spec.ts b/frontend/src/metabase/querying/expressions/compile-expression.unit.spec.ts
index 36242c86baa..998f7c1b1ac 100644
--- a/frontend/src/metabase/querying/expressions/compile-expression.unit.spec.ts
+++ b/frontend/src/metabase/querying/expressions/compile-expression.unit.spec.ts
@@ -43,19 +43,22 @@ function aggregation(source: string) {
describe("old recursive-parser tests", () => {
it("should parse numeric literals", () => {
- expect(expr("0")).toEqual(value(0, "type/Integer"));
- expect(expr("42")).toEqual(value(42, "type/Integer"));
- expect(expr("1.0")).toEqual(value(1, "type/Integer"));
- expect(expr("0.123")).toEqual(value(0.123, "type/Float"));
+ // Numbers are returned as raw values (except bigints which need type info)
+ expect(expr("0")).toEqual(0);
+ expect(expr("42")).toEqual(42);
+ expect(expr("1.0")).toEqual(1);
+ expect(expr("0.123")).toEqual(0.123);
+ // Bigints are wrapped because they serialize as strings
expect(expr("9223372036854775807")).toEqual(
value("9223372036854775807", "type/BigInteger"),
);
});
it("should parse string literals", () => {
- expect(expr("'Universe'")).toEqual(value("Universe", "type/Text"));
- expect(expr('"answer"')).toEqual(value("answer", "type/Text"));
- expect(expr('"\\""')).toEqual(value('"', "type/Text"));
+ // Strings are returned as raw values
+ expect(expr("'Universe'")).toEqual("Universe");
+ expect(expr('"answer"')).toEqual("answer");
+ expect(expr('"\\""')).toEqual('"');
expect(expr("length('Universe')")).toEqual(op("length", "Universe"));
expect(expr('length("answer")')).toEqual(op("length", "answer"));
@@ -75,15 +78,15 @@ describe("old recursive-parser tests", () => {
});
it("should parse unary expressions", () => {
- expect(expr("+6")).toEqual(value(6, "type/Integer"));
- expect(expr("++7")).toEqual(value(7, "type/Integer"));
- expect(expr("-+8")).toEqual(value(-8, "type/Integer"));
+ expect(expr("+6")).toEqual(6);
+ expect(expr("++7")).toEqual(7);
+ expect(expr("-+8")).toEqual(-8);
});
it("should flatten unary expressions", () => {
- expect(expr("--5")).toEqual(value(5, "type/Integer"));
- expect(expr("- 6")).toEqual(value(-6, "type/Integer"));
- expect(expr("+-7")).toEqual(value(-7, "type/Integer"));
+ expect(expr("--5")).toEqual(5);
+ expect(expr("- 6")).toEqual(-6);
+ expect(expr("+-7")).toEqual(-7);
expect(expr("sqrt(-1)")).toEqual(op("sqrt", -1));
expect(expr("- [Total]")).toEqual(op("-", fields.orders.TOTAL));
expect(expr("-[Total]")).toEqual(op("-", fields.orders.TOTAL));
@@ -441,8 +444,9 @@ describe("Specific expressions", () => {
});
it("should support negated numbers", () => {
- expect(expr(`-10`)).toEqual(value(-10, "type/Integer"));
- expect(expr(`-3.1415`)).toEqual(value(-3.1415, "type/Float"));
+ expect(expr(`-10`)).toEqual(-10);
+ expect(expr(`-3.1415`)).toEqual(-3.1415);
+ // Bigints are still wrapped because they need to be serialized as strings
expect(expr(`-9223372036854775809`)).toEqual(
value("-9223372036854775809", "type/BigInteger"),
);
diff --git a/frontend/src/metabase/querying/expressions/pratt/compiler.ts b/frontend/src/metabase/querying/expressions/pratt/compiler.ts
index 4adb4f80787..5abcfee6518 100644
--- a/frontend/src/metabase/querying/expressions/pratt/compiler.ts
+++ b/frontend/src/metabase/querying/expressions/pratt/compiler.ts
@@ -5,13 +5,7 @@ import * as Lib from "metabase-lib";
import { getClauseDefinition, getMBQLName, isDefinedClause } from "../clause";
import { CompileError, isExpressionError } from "../errors";
-import {
- isBigIntLiteral,
- isBooleanLiteral,
- isFloatLiteral,
- isIntegerLiteral,
- isStringLiteral,
-} from "../literal";
+import { isBigIntLiteral, isStringLiteral } from "../literal";
import type { Resolver } from "../resolver";
import type { ExpressionType } from "../types";
import { assert, check } from "../utils";
@@ -91,6 +85,7 @@ function compileRoot(
ctx: Context,
):
| Lib.ExpressionParts
+ | Lib.ExpressionArg
| Lib.ColumnMetadata
| Lib.MeasureMetadata
| Lib.MetricMetadata
@@ -99,18 +94,16 @@ function compileRoot(
assert(node.children.length === 1, t`Root must have one child`);
const value = compileNode(node.children[0], ctx);
- if (isStringLiteral(value)) {
- return compileValue(value, "type/Text");
- } else if (isBooleanLiteral(value)) {
- return compileValue(value, "type/Boolean");
- } else if (isIntegerLiteral(value)) {
- return compileValue(value, "type/Integer");
- } else if (isFloatLiteral(value)) {
- return compileValue(value, "type/Float");
- } else if (isBigIntLiteral(value)) {
+
+ // Bigints need to be wrapped in :value because they must be serialized as strings
+ // and need type info to be parsed correctly
+ if (isBigIntLiteral(value)) {
return compileValue(value, "type/BigInteger");
}
+ // All other literals (strings, numbers, booleans) are returned as raw values.
+ // This matches how filter clauses are created and allows the wrap-value-literals
+ // QP middleware to add proper type info from the column being compared.
return value;
}
diff --git a/frontend/src/metabase/querying/expressions/pratt/compiler.unit.spec.ts b/frontend/src/metabase/querying/expressions/pratt/compiler.unit.spec.ts
index 9546e3fc05e..47b48a70ec6 100644
--- a/frontend/src/metabase/querying/expressions/pratt/compiler.unit.spec.ts
+++ b/frontend/src/metabase/querying/expressions/pratt/compiler.unit.spec.ts
@@ -7,14 +7,6 @@ import type { ExpressionType } from "../types";
import { compile, lexify, parse } from ".";
-function integer(x: number) {
- return value(x, "type/Integer");
-}
-
-function text(x: string) {
- return value(x, "type/Text");
-}
-
function bigint(x: string) {
return value(x, "type/BigInteger");
}
@@ -30,19 +22,24 @@ describe("pratt/compiler", () => {
}
describe("(for an expression)", () => {
- it("should compile literals", () => {
- expect(expr("42")).toEqual(integer(42));
- expect(expr("'Universe'")).toEqual(text("Universe"));
- expect(expr(`"Universe"`)).toEqual(text("Universe"));
- expect(expr(`"\\""`)).toEqual(text(`"`));
- expect(expr(`'\\''`)).toEqual(text(`'`));
- expect(expr(`"a\\"b"`)).toEqual(text(`a"b`));
- expect(expr(`'a\\'b'`)).toEqual(text(`a'b`));
- expect(expr(`"'"`)).toEqual(text(`'`));
- expect(expr(`'"'`)).toEqual(text(`"`));
+ it("should compile literals as raw values", () => {
+ // Top-level literals should be returned as raw values, not wrapped in :value clauses.
+ // This matches how filter clauses are created and allows wrap-value-literals QP middleware
+ // to add proper type info from the column being compared.
+ expect(expr("42")).toEqual(42);
+ expect(expr("'Universe'")).toEqual("Universe");
+ expect(expr(`"Universe"`)).toEqual("Universe");
+ expect(expr(`"\\""`)).toEqual(`"`);
+ expect(expr(`'\\''`)).toEqual(`'`);
+ expect(expr(`"a\\"b"`)).toEqual(`a"b`);
+ expect(expr(`'a\\'b'`)).toEqual(`a'b`);
+ expect(expr(`"'"`)).toEqual(`'`);
+ expect(expr(`'"'`)).toEqual(`"`);
});
- it("should compile bigints", () => {
+ it("should compile bigints wrapped in :value clause", () => {
+ // Bigints need to be wrapped because they must be serialized as strings
+ // and need type info to be parsed correctly
expect(expr("12309109320930192039")).toEqual(
bigint("12309109320930192039"),
);
@@ -102,7 +99,7 @@ describe("pratt/compiler", () => {
},
],
});
- expect(expr("-12")).toEqual(value(-12, "type/Integer"));
+ expect(expr("-12")).toEqual(-12);
});
it("should compile comparisons", () => {
@@ -261,12 +258,12 @@ describe("pratt/compiler", () => {
});
it("should handle parenthesized expression", () => {
- expect(expr("(42)")).toEqual(integer(42));
- expect(expr("-42")).toEqual(integer(-42));
- expect(expr("-(42)")).toEqual(integer(-42));
- expect(expr("((43))")).toEqual(integer(43));
- expect(expr("('Universe')")).toEqual(text("Universe"));
- expect(expr("(('Answer'))")).toEqual(text("Answer"));
+ expect(expr("(42)")).toEqual(42);
+ expect(expr("-42")).toEqual(-42);
+ expect(expr("-(42)")).toEqual(-42);
+ expect(expr("((43))")).toEqual(43);
+ expect(expr("('Universe')")).toEqual("Universe");
+ expect(expr("(('Answer'))")).toEqual("Answer");
expect(expr("(1+2)")).toEqual({
operator: "+",
options: {},
diff --git a/frontend/src/metabase/querying/expressions/test/shared.ts b/frontend/src/metabase/querying/expressions/test/shared.ts
index 23d76d6d6e2..a8688c3ece9 100644
--- a/frontend/src/metabase/querying/expressions/test/shared.ts
+++ b/frontend/src/metabase/querying/expressions/test/shared.ts
@@ -75,13 +75,17 @@ const metadata = createMockMetadata({
id: getNextId(),
name: "Bar Measure",
table_id: ORDERS_ID,
- definition: createMockStructuredDatasetQuery({
+ definition: {
+ "lib/type": "mbql/query",
database: SAMPLE_DB_ID,
- query: createMockStructuredQuery({
- "source-table": ORDERS_ID,
- aggregation: [["sum", ["field", ORDERS.TOTAL, {}]]],
- }),
- }),
+ stages: [
+ {
+ "lib/type": "mbql.stage/mbql",
+ "source-table": ORDERS_ID,
+ aggregation: [["sum", {}, ["field", {}, ORDERS.TOTAL]]],
+ },
+ ],
+ } as any,
}),
],
}),
diff --git a/modules/drivers/athena/test/metabase/driver/athena_test.clj b/modules/drivers/athena/test/metabase/driver/athena_test.clj
index ee1ef4c59da..ae1378678a4 100644
--- a/modules/drivers/athena/test/metabase/driver/athena_test.clj
+++ b/modules/drivers/athena/test/metabase/driver/athena_test.clj
@@ -423,8 +423,8 @@
{:database (mt/id)
:type :query
:query {:filter [:and
- [:= a-str [:field "a_dt_tz_text" {:base-type :type/DateTime}]]
- [:= b-str [:field "b_dt_tz_text" {:base-type :type/DateTime}]]]
+ [:= a-str [:field "a_dt_tz_text" {:base-type :type/Text}]]
+ [:= b-str [:field "b_dt_tz_text" {:base-type :type/Text}]]]
:expressions (into {}
(for [unit units]
[(name unit) [:datetime-diff
diff --git a/src/metabase/driver/sql/query_processor.clj b/src/metabase/driver/sql/query_processor.clj
index 28eb1dbb9c2..dfe4d34ea6e 100644
--- a/src/metabase/driver/sql/query_processor.clj
+++ b/src/metabase/driver/sql/query_processor.clj
@@ -702,8 +702,11 @@
[[_ value {base-type :base_type effective-type :effective_type} :as clause]]
(and (driver-api/is-clause? :value clause)
(string? value)
- (isa? (or effective-type base-type)
- :type/Text)))
+ ;; If no type info is provided (nil opts), assume it's text since it's a string.
+ ;; This handles cases like [:value "some string" nil] from expression definitions.
+ (or (nil? base-type)
+ (isa? (or effective-type base-type)
+ :type/Text))))
(defmethod ->honeysql [:sql :expression]
[driver [_ expression-name opts :as _clause]]
@@ -716,6 +719,15 @@
(literal-text-value? expression-definition)
[::expression-literal-text-value expression-definition]
+ ;; Handle raw string literals (not wrapped in :value) - needed for
+ ;; expression definitions that are just string literals, e.g. from
+ ;; custom columns like `"fixed literal string"`. Without this,
+ ;; the string becomes a parameter placeholder without type info,
+ ;; which some databases (like H2) can't handle.
+ (string? expression-definition)
+ [::expression-literal-text-value
+ [:value expression-definition {:base_type :type/Text}]]
+
:else
expression-definition))))
diff --git a/src/metabase/lib/expression.cljc b/src/metabase/lib/expression.cljc
index 2e347f70e8f..27639d5fcf9 100644
--- a/src/metabase/lib/expression.cljc
+++ b/src/metabase/lib/expression.cljc
@@ -412,7 +412,7 @@
(mu/defn value :- ::lib.schema.expression/expression
"Creates a `:value` clause for the `literal`. Converts bigint literals to strings for serialization purposes."
[literal :- [:or :string number? :boolean [:fn u.number/bigint?]]]
- (let [base-type (lib.schema.expression/type-of literal)]
+ (let [base-type (lib.schema.expression/type-of-resolved literal)]
(lib.options/ensure-uuid [:value
{:base-type base-type, :effective-type base-type}
(cond-> literal (u.number/bigint? literal) str)])))
@@ -545,7 +545,7 @@
(lib.options/update-options
(if (lib.util/clause? an-expression-clause)
an-expression-clause
- [:value {:effective-type (lib.schema.expression/type-of an-expression-clause)}
+ [:value {:effective-type (lib.schema.expression/type-of-resolved an-expression-clause)}
an-expression-clause])
(fn [opts]
(let [opts (assoc opts :lib/uuid (str (random-uuid)))]
diff --git a/src/metabase/lib/fe_util.cljc b/src/metabase/lib/fe_util.cljc
index 5f112fe3f98..e8fb3e28b77 100644
--- a/src/metabase/lib/fe_util.cljc
+++ b/src/metabase/lib/fe_util.cljc
@@ -742,9 +742,12 @@
_ nil))
(mu/defn join-condition-lhs-or-rhs-literal? :- :boolean
- "Whether this LHS or RHS expression is a `:value` clause."
+ "Whether this LHS or RHS expression is a literal value (either a `:value` clause or a raw literal)."
[lhs-or-rhs :- [:maybe ::lib.schema.expression/expression]]
- (lib.util/clause-of-type? lhs-or-rhs :value))
+ (or (lib.util/clause-of-type? lhs-or-rhs :value)
+ (number? lhs-or-rhs)
+ (string? lhs-or-rhs)
+ (boolean? lhs-or-rhs)))
(mu/defn join-condition-lhs-or-rhs-column? :- :boolean
"Whether this LHS or RHS expression is a `:field` or `:expression` reference."
diff --git a/src/metabase/lib/filter.cljc b/src/metabase/lib/filter.cljc
index c483ffd05e5..8aa09a9c6bc 100644
--- a/src/metabase/lib/filter.cljc
+++ b/src/metabase/lib/filter.cljc
@@ -7,6 +7,7 @@
[metabase.lib.common :as lib.common]
[metabase.lib.dispatch :as lib.dispatch]
[metabase.lib.equality :as lib.equality]
+ [metabase.lib.expression :as lib.expression]
[metabase.lib.hierarchy :as lib.hierarchy]
[metabase.lib.metadata.calculation :as lib.metadata.calculation]
[metabase.lib.options :as lib.options]
@@ -399,7 +400,8 @@
"Add a new filter clause to a `stage`, ignoring it if it is a duplicate clause (ignoring :lib/uuid)."
[stage :- ::lib.schema/stage
new-filter :- [:maybe ::lib.schema.expression/boolean]]
- (if-not new-filter
+ ;; Use nil? instead of if-not because `false` is a valid boolean filter literal
+ (if (nil? new-filter)
stage
(let [existing-filter? (some (fn [existing-filter]
(lib.equality/= existing-filter new-filter))
@@ -454,7 +456,12 @@
(if (clojure.core/= (lib.dispatch/dispatch-value boolean-expression) :metadata/segment)
(recur query stage-number (lib.ref/ref boolean-expression))
(let [stage-number (clojure.core/or stage-number -1)
- new-filter (lib.common/->op-arg boolean-expression)]
+ new-filter (let [op-arg (lib.common/->op-arg boolean-expression)]
+ ;; Raw booleans like `false` need wrapping in a :value clause so they have
+ ;; a :lib/uuid and can be properly identified for removal/replacement.
+ (if (boolean? op-arg)
+ (lib.expression/value op-arg)
+ op-arg))]
(lib.util/update-query-stage query stage-number add-filter-to-stage new-filter)))))
(mu/defn filters :- [:maybe [:ref ::lib.schema/filters]]
diff --git a/src/metabase/lib/filter/desugar.cljc b/src/metabase/lib/filter/desugar.cljc
index 5f8a8c3d319..d2b2be064a0 100644
--- a/src/metabase/lib/filter/desugar.cljc
+++ b/src/metabase/lib/filter/desugar.cljc
@@ -46,7 +46,7 @@
(defn- emptyable?
[expr]
- (isa? (lib.schema.expression/type-of expr) ::lib.schema.expression/emptyable))
+ (isa? (lib.schema.expression/type-of-resolved expr) ::lib.schema.expression/emptyable))
(mu/defn- desugar-is-empty-and-not-empty :- ::clause
"Rewrite `:is-empty` and `:not-empty` filter clauses as simpler `:=` and `:!=`, respectively.
diff --git a/src/metabase/lib/metadata/calculation.cljc b/src/metabase/lib/metadata/calculation.cljc
index b63a400e5ee..6c275e14ad8 100644
--- a/src/metabase/lib/metadata/calculation.cljc
+++ b/src/metabase/lib/metadata/calculation.cljc
@@ -215,11 +215,7 @@
(lib.computed/with-cache-ephemeral* query [:expression-types/by-clause stage-number x]
(fn []
(let [calculated-type (type-of-method query stage-number x)]
- ;; if calculated type is not a true type but a placeholder like `:metabase.lib.schema.expression/type.unknown`
- ;; or a union of types then fall back to `:type/*`, an actual type.
- (if (isa? calculated-type :type/*)
- calculated-type
- :type/*))))))))
+ (lib.schema.expression/resolve-type calculated-type))))))))
(defmethod type-of-method :default
[_query _stage-number expr]
diff --git a/src/metabase/lib/metric.cljc b/src/metabase/lib/metric.cljc
index f0760348744..019648553c4 100644
--- a/src/metabase/lib/metric.cljc
+++ b/src/metabase/lib/metric.cljc
@@ -39,7 +39,7 @@
[{:keys [id], join-alias ::lib.join/join-alias, :as metric-metadata}]
(let [effective-type (or ((some-fn :effective-type :base-type) metric-metadata)
(when-let [aggregation (first (:aggregation (metric-definition metric-metadata)))]
- (let [ag-effective-type (lib.schema.expression/type-of aggregation)]
+ (let [ag-effective-type (lib.schema.expression/type-of-resolved aggregation)]
(when (isa? ag-effective-type :type/*)
ag-effective-type))))
options (cond-> {:lib/uuid (str (random-uuid))}
diff --git a/src/metabase/lib/schema/expression.cljc b/src/metabase/lib/schema/expression.cljc
index 9ea3cadab52..3fa3dee5403 100644
--- a/src/metabase/lib/schema/expression.cljc
+++ b/src/metabase/lib/schema/expression.cljc
@@ -53,6 +53,28 @@
(:base-type (second expr))))
(type-of-method expr)))
+(defn resolve-type
+ "Resolve a possibly-ambiguous type to a single keyword type.
+
+ [[type-of]] can return a set of types for ambiguous expressions (e.g. `#{:type/Text :type/Date}` for date-like
+ strings). This function collapses such sets to a single type using [[metabase.types.core/most-specific-common-ancestor]].
+ Also handles `::type.unknown` and other non-type keywords by returning `:type/*`."
+ [expr-type]
+ (cond
+ (and (keyword? expr-type) (isa? expr-type :type/*))
+ expr-type
+
+ (and (set? expr-type) (seq expr-type))
+ (reduce types/most-specific-common-ancestor (first expr-type) (rest expr-type))
+
+ :else
+ :type/*))
+
+(defn type-of-resolved
+ "Like [[type-of]], but always returns a single keyword type. Resolves ambiguous set types via [[resolve-type]]."
+ [expr]
+ (resolve-type (type-of expr)))
+
(defmethod type-of-method :default
[expr]
(throw (ex-info (i18n/tru "{0}: Don''t know how to determine the type of {1}" `type-of (pr-str expr))
diff --git a/src/metabase/lib/util.cljc b/src/metabase/lib/util.cljc
index d32da633404..65fc5ac7375 100644
--- a/src/metabase/lib/util.cljc
+++ b/src/metabase/lib/util.cljc
@@ -87,7 +87,7 @@
(isa?
(or (and (clause? expression)
((some-fn :metabase.lib.field/original-effective-type :effective-type :base-type) (lib.options/options expression)))
- (lib.schema.expression/type-of expression))
+ (lib.schema.expression/type-of-resolved expression))
typ))
(defn expression-name
@@ -108,7 +108,7 @@
:else
[:value {:lib/uuid (str (random-uuid))
- :effective-type (lib.schema.expression/type-of clause)}
+ :effective-type (lib.schema.expression/type-of-resolved clause)}
clause])
(lib.options/update-options (fn [opts]
(-> opts
diff --git a/src/metabase/query_processor/middleware/wrap_value_literals.clj b/src/metabase/query_processor/middleware/wrap_value_literals.clj
index d53518d85cd..1d28f8cd9eb 100644
--- a/src/metabase/query_processor/middleware/wrap_value_literals.clj
+++ b/src/metabase/query_processor/middleware/wrap_value_literals.clj
@@ -231,8 +231,8 @@
(lib.util.match/match-lite clause
;; two literals
[(tag :guard #{:= :!= :< :> :<= :>=}) opts (x :guard raw-value?) (y :guard raw-value?)]
- (let [x-type (lib.schema.expression/type-of x)
- y-type (lib.schema.expression/type-of y)]
+ (let [x-type (lib.schema.expression/type-of-resolved x)
+ y-type (lib.schema.expression/type-of-resolved y)]
[tag opts
(add-type-info x {:base-type x-type :effective-type x-type})
(add-type-info y {:base-type y-type :effective-type y-type})])
@@ -241,6 +241,10 @@
[(tag :guard #{:= :!= :< :> :<= :>=}) opts field (x :guard raw-value?)]
[tag opts field (add-type-info x (*type-info* query path field))]
+ ;; literal and field (literal on LHS)
+ [(tag :guard #{:= :!= :< :> :<= :>=}) opts (x :guard raw-value?) field]
+ [tag opts (add-type-info x (*type-info* query path field)) field]
+
[:datetime-diff opts (x :guard string?) (y :guard string?) unit]
[:datetime-diff opts (add-type-info (u.date/parse x) nil) (add-type-info (u.date/parse y) nil) unit]
@@ -288,7 +292,7 @@
function."
{:deprecated "0.57.0"}
[clause]
- (let [expr-type (lib.schema.expression/type-of clause)]
+ (let [expr-type (lib.schema.expression/type-of-resolved clause)]
(merge
(when (and (lib.util/clause-of-type? clause :field)
(qp.store/initialized?))
diff --git a/test/metabase/lib/filter_test.cljc b/test/metabase/lib/filter_test.cljc
index 5107f85a429..d87755044fd 100644
--- a/test/metabase/lib/filter_test.cljc
+++ b/test/metabase/lib/filter_test.cljc
@@ -769,6 +769,22 @@
"CREATED_AT"]
"2023-01-01T00:00:00Z"])))))
+(deftest ^:parallel boolean-literal-filter-test
+ (testing "Boolean literals can be used as filters"
+ (doseq [bool [false true]]
+ (testing (str bool " literal is wrapped in :value clause with :lib/uuid for removal support")
+ (let [query (lib/query meta/metadata-provider (meta/table-metadata :orders))
+ query' (lib/filter query bool)]
+ (is (=? {:stages [{:filters [[:value {:lib/uuid string?
+ :effective-type :type/Boolean
+ :base-type :type/Boolean}
+ bool]]}]}
+ query'))
+ (testing "can be removed"
+ (let [filters (lib/filters query')
+ query'' (lib/remove-clause query' (first filters))]
+ (is (empty? (lib/filters query''))))))))))
+
(deftest ^:parallel describe-filter-operator-test
(testing "default variant"
(are [expected operator] (= expected (lib/describe-filter-operator operator))
diff --git a/test/metabase/lib/schema/expression_test.cljc b/test/metabase/lib/schema/expression_test.cljc
index ff533026092..c54fe6a1c6e 100644
--- a/test/metabase/lib/schema/expression_test.cljc
+++ b/test/metabase/lib/schema/expression_test.cljc
@@ -8,6 +8,35 @@
[metabase.lib.test-metadata :as meta]
[metabase.util.malli.registry :as mr]))
+(deftest ^:parallel resolve-type-test
+ (testing "single valid type keywords pass through"
+ (are [typ] (= typ (lib.schema.expression/resolve-type typ))
+ :type/Integer
+ :type/Text
+ :type/Date
+ :type/DateTime
+ :type/*))
+ (testing "::type.unknown resolves to :type/*"
+ (is (= :type/*
+ (lib.schema.expression/resolve-type ::lib.schema.expression/type.unknown))))
+ (testing "set with unrelated types resolves to their common ancestor (:type/*)"
+ (is (= :type/*
+ (lib.schema.expression/resolve-type #{:type/Text :type/Date})))
+ (is (= :type/*
+ (lib.schema.expression/resolve-type #{:type/Text :type/Time}))))
+ (testing "set with related types resolves to their common ancestor"
+ (is (isa? (lib.schema.expression/resolve-type #{:type/Date :type/DateTime}) :type/Temporal))))
+
+(deftest ^:parallel type-of-resolved-test
+ (testing "plain string returns :type/Text"
+ (is (= :type/Text (lib.schema.expression/type-of-resolved "hello"))))
+ (testing "date-like string resolves to :type/* (ambiguous without context)"
+ (is (= :type/* (lib.schema.expression/type-of-resolved "2024-01-01"))))
+ (testing "integer returns :type/Integer"
+ (is (= :type/Integer (lib.schema.expression/type-of-resolved 42))))
+ (testing "boolean returns :type/Boolean"
+ (is (= :type/Boolean (lib.schema.expression/type-of-resolved true)))))
+
(deftest ^:parallel comparable-expressions?-test
(let [abs-datetime [:absolute-datetime {:lib/uuid (str (random-uuid))}
"2015-06-01T00:00Z" :day]]
diff --git a/test/metabase/query_processor/middleware/wrap_value_literals_test.clj b/test/metabase/query_processor/middleware/wrap_value_literals_test.clj
index 8ce933fb482..f684fada457 100644
--- a/test/metabase/query_processor/middleware/wrap_value_literals_test.clj
+++ b/test/metabase/query_processor/middleware/wrap_value_literals_test.clj
@@ -44,6 +44,18 @@
(lib.tu.macros/mbql-query venues
{:filter [:> $id 50]})))))
+(deftest ^:parallel wrap-string-literal-on-lhs-test
+ (testing "string literal on LHS of comparison should get wrapped with type info from RHS field (#53598)"
+ (is (= (lib.tu.macros/mbql-query venues
+ {:filter [:=
+ [:value "foo" {:base_type :type/Text
+ :semantic_type :type/Name
+ :database_type "CHARACTER VARYING"}]
+ $name]})
+ (wrap-value-literals
+ (lib.tu.macros/mbql-query venues
+ {:filter [:= "foo" $name]}))))))
+
(deftest ^:parallel wrap-integers-test-2
(is (= (lib.tu.macros/mbql-query venues
{:filter [:and