Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

feat: introduce `FilterGroup` model for recursive table filters (#9119)

Kiran Gadhavecd403061d2fd
diff --git a/frontend/src/components/data-table/__tests__/filters.test.ts b/frontend/src/components/data-table/__tests__/filters.test.ts
new file mode 100644
index 0000000000..abc38d030b
--- /dev/null
+++ b/frontend/src/components/data-table/__tests__/filters.test.ts
@@ -0,0 +1,304 @@
+/* Copyright 2026 Marimo. All rights reserved. */
+import { describe, expect, it } from "vitest";
+import {
+ filterToFilterCondition,
+ filtersToFilterGroup,
+ Filter,
+} from "../filters";
+import {
+ FilterConditionSchema,
+ FilterGroupSchema,
+} from "@/plugins/impl/data-frames/schema";
+
+describe("filterToFilterCondition", () => {
+ it("returns empty array for undefined filter", () => {
+ expect(filterToFilterCondition("col", undefined)).toEqual([]);
+ });
+
+ it("handles is_null filter", () => {
+ const result = filterToFilterCondition(
+ "col",
+ Filter.number({ operator: "is_null" }),
+ );
+ expect(result).toEqual([
+ {
+ column_id: "col",
+ operator: "is_null",
+ value: undefined,
+ type: "condition",
+ negate: false,
+ },
+ ]);
+ });
+
+ it("handles is_not_null filter", () => {
+ const result = filterToFilterCondition(
+ "col",
+ Filter.number({ operator: "is_not_null" }),
+ );
+ expect(result).toEqual([
+ {
+ column_id: "col",
+ operator: "is_not_null",
+ value: undefined,
+ type: "condition",
+ negate: false,
+ },
+ ]);
+ });
+
+ it("handles number filter with min only", () => {
+ const result = filterToFilterCondition("age", Filter.number({ min: 18 }));
+ expect(result).toHaveLength(1);
+ expect(result[0]).toMatchObject({
+ column_id: "age",
+ operator: ">=",
+ value: 18,
+ type: "condition",
+ negate: false,
+ });
+ });
+
+ it("handles number filter with max only", () => {
+ const result = filterToFilterCondition("age", Filter.number({ max: 65 }));
+ expect(result).toHaveLength(1);
+ expect(result[0]).toMatchObject({
+ column_id: "age",
+ operator: "<=",
+ value: 65,
+ type: "condition",
+ negate: false,
+ });
+ });
+
+ it("handles number filter with min and max", () => {
+ const result = filterToFilterCondition(
+ "age",
+ Filter.number({ min: 18, max: 65 }),
+ );
+ expect(result).toHaveLength(2);
+ expect(result[0]).toMatchObject({ operator: ">=", value: 18 });
+ expect(result[1]).toMatchObject({ operator: "<=", value: 65 });
+ });
+
+ it("handles text filter", () => {
+ const result = filterToFilterCondition(
+ "name",
+ Filter.text({ text: "foo", operator: "contains" }),
+ );
+ expect(result).toEqual([
+ {
+ column_id: "name",
+ operator: "contains",
+ value: "foo",
+ type: "condition",
+ negate: false,
+ },
+ ]);
+ });
+
+ it("handles boolean true filter", () => {
+ const result = filterToFilterCondition(
+ "active",
+ Filter.boolean({ value: true }),
+ );
+ expect(result).toEqual([
+ {
+ column_id: "active",
+ operator: "is_true",
+ value: undefined,
+ type: "condition",
+ negate: false,
+ },
+ ]);
+ });
+
+ it("handles boolean false filter", () => {
+ const result = filterToFilterCondition(
+ "active",
+ Filter.boolean({ value: false }),
+ );
+ expect(result).toEqual([
+ {
+ column_id: "active",
+ operator: "is_false",
+ value: undefined,
+ type: "condition",
+ negate: false,
+ },
+ ]);
+ });
+
+ it("handles select in filter", () => {
+ const result = filterToFilterCondition(
+ "status",
+ Filter.select({ options: ["a", "b"], operator: "in" }),
+ );
+ expect(result).toEqual([
+ {
+ column_id: "status",
+ operator: "in",
+ value: ["a", "b"],
+ type: "condition",
+ negate: false,
+ },
+ ]);
+ });
+
+ it("handles date filter with min and max", () => {
+ const min = new Date("2024-01-01");
+ const max = new Date("2024-12-31");
+ const result = filterToFilterCondition(
+ "created",
+ Filter.date({ min, max }),
+ );
+ expect(result).toHaveLength(2);
+ expect(result[0]).toMatchObject({
+ operator: ">=",
+ value: min.toISOString(),
+ });
+ expect(result[1]).toMatchObject({
+ operator: "<=",
+ value: max.toISOString(),
+ });
+ });
+
+ it("every condition has type and negate fields", () => {
+ const result = filterToFilterCondition(
+ "col",
+ Filter.number({ min: 1, max: 10 }),
+ );
+ for (const condition of result) {
+ expect(condition).toHaveProperty("type", "condition");
+ expect(condition).toHaveProperty("negate", false);
+ }
+ });
+});
+
+describe("filtersToFilterGroup", () => {
+ it("returns empty AND group for no filters", () => {
+ const result = filtersToFilterGroup([]);
+ expect(result).toEqual({
+ type: "group",
+ operator: "and",
+ children: [],
+ negate: false,
+ });
+ });
+
+ it("wraps single filter in AND group", () => {
+ const result = filtersToFilterGroup([
+ { id: "age", value: Filter.number({ min: 18 }) },
+ ]);
+ expect(result.type).toBe("group");
+ expect(result.operator).toBe("and");
+ expect(result.negate).toBe(false);
+ expect(result.children).toHaveLength(1);
+ });
+
+ it("wraps multiple filters in AND group", () => {
+ const result = filtersToFilterGroup([
+ { id: "age", value: Filter.number({ min: 18 }) },
+ { id: "name", value: Filter.text({ text: "foo", operator: "contains" }) },
+ ]);
+ expect(result.children).toHaveLength(2);
+ expect(result.operator).toBe("and");
+ });
+
+ it("flattens multi-condition filters", () => {
+ const result = filtersToFilterGroup([
+ { id: "age", value: Filter.number({ min: 18, max: 65 }) },
+ ]);
+ // min + max = 2 conditions
+ expect(result.children).toHaveLength(2);
+ });
+});
+
+describe("schema validation", () => {
+ it("FilterConditionSchema accepts valid condition", () => {
+ const result = FilterConditionSchema.safeParse({
+ column_id: "age",
+ operator: ">=",
+ value: 18,
+ });
+ expect(result.success).toBe(true);
+ if (result.success) {
+ expect(result.data.type).toBe("condition");
+ expect(result.data.negate).toBe(false);
+ }
+ });
+
+ it("FilterConditionSchema defaults type and negate", () => {
+ const result = FilterConditionSchema.safeParse({
+ column_id: "age",
+ operator: "==",
+ value: 5,
+ });
+ expect(result.success).toBe(true);
+ if (result.success) {
+ expect(result.data.type).toBe("condition");
+ expect(result.data.negate).toBe(false);
+ }
+ });
+
+ it("FilterConditionSchema accepts negate=true", () => {
+ const result = FilterConditionSchema.safeParse({
+ column_id: "age",
+ operator: "==",
+ value: 5,
+ negate: true,
+ });
+ expect(result.success).toBe(true);
+ if (result.success) {
+ expect(result.data.negate).toBe(true);
+ }
+ });
+
+ it("FilterGroupSchema accepts valid group", () => {
+ const result = FilterGroupSchema.safeParse({
+ type: "group",
+ operator: "and",
+ children: [{ column_id: "age", operator: ">=", value: 18 }],
+ });
+ expect(result.success).toBe(true);
+ });
+
+ it("FilterGroupSchema accepts nested groups", () => {
+ const result = FilterGroupSchema.safeParse({
+ type: "group",
+ operator: "or",
+ children: [
+ {
+ type: "group",
+ operator: "and",
+ children: [
+ { column_id: "a", operator: "==", value: 1 },
+ { column_id: "b", operator: ">", value: 2 },
+ ],
+ },
+ { column_id: "c", operator: "==", value: 3 },
+ ],
+ });
+ expect(result.success).toBe(true);
+ });
+
+ it("FilterGroupSchema rejects invalid operator", () => {
+ const result = FilterGroupSchema.safeParse({
+ type: "group",
+ operator: "xor",
+ children: [],
+ });
+ expect(result.success).toBe(false);
+ });
+
+ it("FilterGroupSchema defaults fields", () => {
+ const result = FilterGroupSchema.safeParse({});
+ expect(result.success).toBe(true);
+ if (result.success) {
+ expect(result.data.type).toBe("group");
+ expect(result.data.operator).toBe("and");
+ expect(result.data.children).toEqual([]);
+ expect(result.data.negate).toBe(false);
+ }
+ });
+});
diff --git a/frontend/src/components/data-table/filters.ts b/frontend/src/components/data-table/filters.ts
index 3084000c7e..f5d757c253 100644
--- a/frontend/src/components/data-table/filters.ts
+++ b/frontend/src/components/data-table/filters.ts
@@ -1,9 +1,12 @@
/* Copyright 2026 Marimo. All rights reserved. */
"use no memo";
-import type { RowData } from "@tanstack/react-table";
+import type { ColumnFiltersState, RowData } from "@tanstack/react-table";
import type { DataType } from "@/core/kernel/messages";
-import type { ConditionType } from "@/plugins/impl/data-frames/schema";
+import type {
+ FilterConditionType,
+ FilterGroupType,
+} from "@/plugins/impl/data-frames/schema";
import type { ColumnId } from "@/plugins/impl/data-frames/types";
import type { OperatorType } from "@/plugins/impl/data-frames/utils/operators";
import { assertNever } from "@/utils/assertNever";
@@ -84,28 +87,34 @@ export type ColumnFilterForType<T extends FilterType> = T extends FilterType
export function filterToFilterCondition(
columnIdString: string,
filter: ColumnFilterValue | undefined,
-): ConditionType[] | ConditionType {
+): FilterConditionType[] {
if (!filter) {
return [];
}
const columnId = columnIdString as ColumnId;
if (filter.operator === "is_null" || filter.operator === "is_not_null") {
- return {
- column_id: columnId,
- operator: filter.operator,
- value: undefined,
- };
+ return [
+ {
+ column_id: columnId,
+ operator: filter.operator,
+ value: undefined,
+ type: "condition",
+ negate: false,
+ },
+ ];
}
switch (filter.type) {
case "number": {
- const conditions: ConditionType[] = [];
+ const conditions: FilterConditionType[] = [];
if (filter.min !== undefined) {
conditions.push({
column_id: columnId,
operator: ">=",
value: filter.min,
+ type: "condition",
+ negate: false,
});
}
if (filter.max !== undefined) {
@@ -113,23 +122,31 @@ export function filterToFilterCondition(
column_id: columnId,
operator: "<=",
value: filter.max,
+ type: "condition",
+ negate: false,
});
}
return conditions;
}
case "text":
- return {
- column_id: columnId,
- operator: filter.operator,
- value: filter.text,
- };
+ return [
+ {
+ column_id: columnId,
+ operator: filter.operator,
+ value: filter.text,
+ type: "condition",
+ negate: false,
+ },
+ ];
case "datetime": {
- const conditions: ConditionType[] = [];
+ const conditions: FilterConditionType[] = [];
if (filter.min !== undefined) {
conditions.push({
column_id: columnId,
operator: ">=",
value: filter.min.toISOString(),
+ type: "condition",
+ negate: false,
});
}
if (filter.max !== undefined) {
@@ -137,17 +154,21 @@ export function filterToFilterCondition(
column_id: columnId,
operator: "<=",
value: filter.max.toISOString(),
+ type: "condition",
+ negate: false,
});
}
return conditions;
}
case "date": {
- const conditions: ConditionType[] = [];
+ const conditions: FilterConditionType[] = [];
if (filter.min !== undefined) {
conditions.push({
column_id: columnId,
operator: ">=",
value: filter.min.toISOString(),
+ type: "condition",
+ negate: false,
});
}
if (filter.max !== undefined) {
@@ -155,17 +176,21 @@ export function filterToFilterCondition(
column_id: columnId,
operator: "<=",
value: filter.max.toISOString(),
+ type: "condition",
+ negate: false,
});
}
return conditions;
}
case "time": {
- const conditions: ConditionType[] = [];
+ const conditions: FilterConditionType[] = [];
if (filter.min !== undefined) {
conditions.push({
column_id: columnId,
operator: ">=",
value: filter.min.toISOString(),
+ type: "condition",
+ negate: false,
});
}
if (filter.max !== undefined) {
@@ -173,24 +198,34 @@ export function filterToFilterCondition(
column_id: columnId,
operator: "<=",
value: filter.max.toISOString(),
+ type: "condition",
+ negate: false,
});
}
return conditions;
}
case "boolean":
if (filter.value) {
- return {
- column_id: columnId,
- operator: "is_true",
- value: undefined,
- };
+ return [
+ {
+ column_id: columnId,
+ operator: "is_true",
+ value: undefined,
+ type: "condition",
+ negate: false,
+ },
+ ];
}
if (!filter.value) {
- return {
- column_id: columnId,
- operator: "is_false",
- value: undefined,
- };
+ return [
+ {
+ column_id: columnId,
+ operator: "is_false",
+ value: undefined,
+ type: "condition",
+ negate: false,
+ },
+ ];
}
return [];
@@ -200,16 +235,35 @@ export function filterToFilterCondition(
Logger.warn("Invalid operator for select filter", {
operator: filter.operator,
});
- operator = "in"; // default to in operator
+ operator = "in";
}
- return {
- column_id: columnId,
- operator,
- value: filter.options,
- };
+ return [
+ {
+ column_id: columnId,
+ operator,
+ value: filter.options,
+ type: "condition",
+ negate: false,
+ },
+ ];
}
default:
assertNever(filter);
}
}
+
+export function filtersToFilterGroup(
+ columnFilters: ColumnFiltersState,
+): FilterGroupType {
+ const conditions = columnFilters.flatMap((filter) =>
+ filterToFilterCondition(filter.id, filter.value as ColumnFilterValue),
+ );
+ // To maintain existing behavior "and" all the conditions
+ return {
+ type: "group",
+ operator: "and",
+ children: conditions,
+ negate: false,
+ };
+}
diff --git a/frontend/src/plugins/impl/DataTablePlugin.tsx b/frontend/src/plugins/impl/DataTablePlugin.tsx
index 016404b272..21f0a46492 100644
--- a/frontend/src/plugins/impl/DataTablePlugin.tsx
+++ b/frontend/src/plugins/impl/DataTablePlugin.tsx
@@ -28,10 +28,7 @@ import { TablePanel } from "@/components/data-table/charts/charts";
import { hasChart } from "@/components/data-table/charts/storage";
import { ColumnChartSpecModel } from "@/components/data-table/column-summary/chart-spec-model";
import { ColumnChartContext } from "@/components/data-table/column-summary/column-summary";
-import {
- type ColumnFilterValue,
- filterToFilterCondition,
-} from "@/components/data-table/filters";
+import { filtersToFilterGroup } from "@/components/data-table/filters";
import { usePanelOwnership } from "@/components/data-table/hooks/use-panel-ownership";
import { LoadingTable } from "@/components/data-table/loading-table";
import {
@@ -86,8 +83,8 @@ import { rpc } from "../core/rpc";
import { Banner } from "./common/error-banner";
import { Labeled } from "./common/labeled";
import {
- ConditionSchema,
- type ConditionType,
+ FilterGroupSchema,
+ type FilterGroupType,
columnToFieldTypesSchema,
} from "./data-frames/schema";
@@ -213,7 +210,7 @@ type DataTableFunctions = {
descending: boolean;
}[];
query?: string;
- filters?: ConditionType[];
+ filters?: FilterGroupType;
page_number: number;
page_size: number;
max_columns?: number | null;
@@ -312,7 +309,7 @@ export const DataTablePlugin = createPlugin<S>("marimo-table")
)
.optional(),
query: z.string().optional(),
- filters: z.array(ConditionSchema).optional(),
+ filters: FilterGroupSchema.optional(),
page_number: z.number(),
page_size: z.number(),
max_columns: z.number().nullable().optional(),
@@ -578,12 +575,7 @@ export const LoadingDataTableComponent = memo(
query: searchQuery,
page_number: paginationState.pageIndex,
page_size: paginationState.pageSize,
- filters: filters.flatMap((filter) => {
- return filterToFilterCondition(
- filter.id,
- filter.value as ColumnFilterValue,
- );
- }),
+ filters: filtersToFilterGroup(filters),
});
if (canShowInitialPage) {
@@ -641,12 +633,7 @@ export const LoadingDataTableComponent = memo(
page_size: 1,
sort: sortArgs,
query: searchQuery,
- filters: filters.flatMap((filter) => {
- return filterToFilterCondition(
- filter.id,
- filter.value as ColumnFilterValue,
- );
- }),
+ filters: filtersToFilterGroup(filters),
// Do not clamp number of columns since we are viewing a single row
max_columns: null,
});
diff --git a/frontend/src/plugins/impl/data-frames/DataFramePlugin.tsx b/frontend/src/plugins/impl/data-frames/DataFramePlugin.tsx
index 93a38c3242..890eeacfbb 100644
--- a/frontend/src/plugins/impl/data-frames/DataFramePlugin.tsx
+++ b/frontend/src/plugins/impl/data-frames/DataFramePlugin.tsx
@@ -29,8 +29,8 @@ import { LoadingDataTableComponent, TableProviders } from "../DataTablePlugin";
import type { DataType } from "../vega/vega-loader";
import { TransformPanel, type TransformPanelHandle } from "./panel";
import {
- ConditionSchema,
- type ConditionType,
+ FilterGroupSchema,
+ type FilterGroupType,
columnToFieldTypesSchema,
type Transformations,
} from "./schema";
@@ -75,7 +75,7 @@ type PluginFunctions = {
descending: boolean;
}[];
query?: string;
- filters?: ConditionType[];
+ filters?: FilterGroupType;
page_number: number;
page_size: number;
}) => Promise<{
@@ -138,7 +138,7 @@ export const DataFramePlugin = createPlugin<S>("marimo-dataframe")
)
.optional(),
query: z.string().optional(),
- filters: z.array(ConditionSchema).optional(),
+ filters: FilterGroupSchema.optional(),
page_number: z.number(),
page_size: z.number(),
}),
diff --git a/frontend/src/plugins/impl/data-frames/schema.ts b/frontend/src/plugins/impl/data-frames/schema.ts
index f9bd569e89..0115b722c2 100644
--- a/frontend/src/plugins/impl/data-frames/schema.ts
+++ b/frontend/src/plugins/impl/data-frames/schema.ts
@@ -73,16 +73,36 @@ const SortColumnTransformSchema = z.object({
.default("last"),
});
-export const ConditionSchema = z
+export const FilterConditionSchema = z
.object({
column_id: column_id,
operator: z
.enum(Object.keys(ALL_OPERATORS) as [OperatorType, ...OperatorType[]])
.describe(FieldOptions.of({ label: " " })),
+ type: z.literal("condition").default("condition"),
value: z.any().describe(FieldOptions.of({ label: "Value" })),
+ negate: z.boolean().default(false),
})
.describe(FieldOptions.of({ direction: "row", special: "column_filter" }));
-export type ConditionType = z.infer<typeof ConditionSchema>;
+export type FilterConditionType = z.infer<typeof FilterConditionSchema>;
+
+export interface FilterGroupType {
+ type: "group";
+ operator: "and" | "or";
+ children: (FilterConditionType | FilterGroupType)[];
+ negate: boolean;
+}
+
+export const FilterGroupSchema: z.ZodType<FilterGroupType> = z.lazy(() =>
+ z.object({
+ type: z.literal("group").default("group"),
+ operator: z.enum(["and", "or"]).default("and"),
+ children: z
+ .array(z.union([FilterConditionSchema, FilterGroupSchema]))
+ .default([]),
+ negate: z.boolean().default(false),
+ }),
+);
const FilterRowsTransformSchema = z.object({
type: z.literal("filter_rows"),
@@ -91,17 +111,29 @@ const FilterRowsTransformSchema = z.object({
.default("keep_rows")
.describe(FieldOptions.of({ special: "radio_group" })),
where: z
- .array(ConditionSchema)
+ .array(FilterConditionSchema)
.min(1)
.describe(FieldOptions.of({ label: "Value", minLength: 1 }))
- .transform((value) => {
- return value.filter((condition) => {
+ .default(() => [
+ {
+ column_id: "" as ColumnId,
+ operator: "==" as const,
+ value: "",
+ type: "condition" as const,
+ negate: false,
+ },
+ ])
+ .transform((value): FilterGroupType => {
+ const validConditions = value.filter((condition) => {
return isConditionValueValid(condition.operator, condition.value);
});
- })
- .default(() => [
- { column_id: "" as ColumnId, operator: "==" as const, value: "" },
- ]),
+ return {
+ type: "group",
+ operator: "and",
+ children: validConditions,
+ negate: false,
+ };
+ }),
});
const GroupByTransformSchema = z
diff --git a/frontend/src/plugins/impl/data-frames/utils/operators.ts b/frontend/src/plugins/impl/data-frames/utils/operators.ts
index e7ddd245bd..de49348503 100644
--- a/frontend/src/plugins/impl/data-frames/utils/operators.ts
+++ b/frontend/src/plugins/impl/data-frames/utils/operators.ts
@@ -41,6 +41,7 @@ const createComparisonOperators = (schema: z.ZodType) => ({
">=": [schema],
"<": [schema],
"<=": [schema],
+ between: [z.object({ min: schema, max: schema })],
is_null: [],
is_not_null: [],
});
@@ -59,6 +60,7 @@ export const STRING_OPERATORS = {
ends_with: [Schema.string],
in: [Schema.stringMultiColumnValues],
not_in: [Schema.stringMultiColumnValues],
+ is_empty: [],
is_null: [],
is_not_null: [],
};
diff --git a/marimo/_plugins/ui/_impl/dataframes/transforms/handlers.py b/marimo/_plugins/ui/_impl/dataframes/transforms/handlers.py
index 0c22fc227b..8685fef332 100644
--- a/marimo/_plugins/ui/_impl/dataframes/transforms/handlers.py
+++ b/marimo/_plugins/ui/_impl/dataframes/transforms/handlers.py
@@ -3,6 +3,7 @@ from __future__ import annotations
import datetime
import decimal
+from collections.abc import Callable
from functools import reduce
from typing import TYPE_CHECKING, Any
@@ -21,9 +22,12 @@ from marimo._plugins.ui._impl.dataframes.transforms.types import (
ColumnConversionTransform,
ExpandDictTransform,
ExplodeColumnsTransform,
+ FilterCondition,
+ FilterGroup,
FilterRowsTransform,
GroupByTransform,
PivotTransform,
+ RangeValue,
RenameColumnTransform,
SampleRowsTransform,
SelectColumnsTransform,
@@ -57,6 +61,20 @@ __all__ = [
DataFrame = nw.LazyFrame[IntoLazyFrame]
+def convert_value(v: Any, converter: Callable[[str], Any]) -> Any:
+ """
+ Convert a value whether it's a list or single value.
+ Ignore None as they usually raise errors when converted
+ """
+ if isinstance(v, (tuple, list)):
+ return [
+ converter(str(item)) if item is not None else None for item in v
+ ]
+ if v is None:
+ return None
+ return converter(str(v))
+
+
class NarwhalsTransformHandler(TransformHandler[DataFrame]):
@staticmethod
def handle_column_conversion(
@@ -123,171 +141,256 @@ class NarwhalsTransformHandler(TransformHandler[DataFrame]):
return result
@staticmethod
- def handle_filter_rows(
- df: DataFrame, transform: FilterRowsTransform
- ) -> DataFrame:
- if not transform.where:
- return df
-
- filter_expr: nw.Expr | None = None
-
- def convert_value(v: Any, converter: Callable[[str], Any]) -> Any:
- """
- Convert a value whether it's a list or single value.
- Ignore None as they usually raise errors when converted
- """
- if isinstance(v, (tuple, list)):
- return [
- converter(str(item)) if item is not None else None
- for item in v
- ]
- if v is None:
- return None
- return converter(str(v))
-
- for condition in transform.where:
- # Don't convert to string if already a string or int
- # Narwhals col() can handle both strings and integers
- column = col(condition.column_id)
- column_name = str(condition.column_id)
- value = condition.value
- dtype = df.collect_schema().get(column_name)
-
- # Convert string values to the appropriate type based on dtype
- if dtype == nw.Datetime:
- value = convert_value(value, datetime.datetime.fromisoformat)
- elif dtype == nw.Date:
- value = convert_value(value, datetime.date.fromisoformat)
- elif dtype == nw.Time:
- value = convert_value(value, datetime.time.fromisoformat)
- elif dtype == nw.Object:
- # Object dtype may contain date/datetime/Decimal values
- # (e.g., pandas stores these as Python objects)
- try:
- sample = (
- df.select(column_name)
- .filter(~col(column_name).is_null())
- .head(1)
- .collect()
- .get_column(column_name)
- .to_list()
- )
- if sample:
- if isinstance(sample[0], datetime.datetime):
- value = convert_value(
- value, datetime.datetime.fromisoformat
- )
- elif isinstance(sample[0], datetime.date):
- value = convert_value(
- value, datetime.date.fromisoformat
- )
- elif isinstance(sample[0], datetime.time):
- value = convert_value(
- value, datetime.time.fromisoformat
- )
- elif isinstance(sample[0], decimal.Decimal):
- # Cast to Float64 so Decimal values can be compared, minor precision loss
- value = convert_value(value, float)
- column = column.cast(nw.Float64)
- except Exception:
- pass
-
- # If the value includes NaNs or infs, we convert to floats so the filters apply correctly
- if (
- isinstance(value, tuple)
- and any(
- token in value
- for token in [NAN_VALUE, POSITIVE_INF, NEGATIVE_INF]
+ def _evaluate_condition(df: DataFrame, condition: FilterCondition) -> Expr:
+ column = col(condition.column_id)
+ column_name = str(condition.column_id)
+ value = condition.value
+ dtype = df.collect_schema().get(column_name)
+
+ # Convert string values to the appropriate type based on dtype
+ if dtype == nw.Datetime:
+ value = convert_value(value, datetime.datetime.fromisoformat)
+ elif dtype == nw.Date:
+ value = convert_value(value, datetime.date.fromisoformat)
+ elif dtype == nw.Time:
+ value = convert_value(value, datetime.time.fromisoformat)
+ elif dtype == nw.Object:
+ # Object dtype may contain date/datetime/Decimal values
+ # (e.g., pandas stores these as Python objects)
+ try:
+ sample = (
+ df.select(column_name)
+ .filter(~col(column_name).is_null())
+ .head(1)
+ .collect()
+ .get_column(column_name)
+ .to_list()
+ )
+ if sample:
+ if isinstance(sample[0], datetime.datetime):
+ value = convert_value(
+ value, datetime.datetime.fromisoformat
+ )
+ elif isinstance(sample[0], datetime.date):
+ value = convert_value(
+ value, datetime.date.fromisoformat
+ )
+ elif isinstance(sample[0], datetime.time):
+ value = convert_value(
+ value, datetime.time.fromisoformat
+ )
+ elif isinstance(sample[0], decimal.Decimal):
+ # Cast to Float64 so Decimal values can be compared, minor precision loss
+ value = convert_value(value, float)
+ column = column.cast(nw.Float64)
+ except Exception:
+ pass
+
+ # If the value includes NaNs or infs, we convert to floats so the filters apply correctly
+ if (
+ isinstance(value, tuple)
+ and any(
+ token in value
+ for token in [NAN_VALUE, POSITIVE_INF, NEGATIVE_INF]
+ )
+ and dtype is not None
+ and dtype.is_float() # Note: this doesn't cover Object types for pandas
+ ):
+ value = convert_value(value, float)
+ elif dtype is not None and dtype.is_decimal():
+ # Cast to Float64 so Decimal values can be compared, minor precision loss
+ value = convert_value(value, float)
+ column = column.cast(nw.Float64)
+
+ # Build the expression based on the operator
+ condition_expr: nw.Expr
+
+ is_negated = condition.negate
+ match condition.operator:
+ case "==":
+ condition_expr = (
+ column != value if is_negated else column == value
)
- and dtype is not None
- and dtype.is_float() # Note: this doesn't cover Object types for pandas
- ):
- value = convert_value(value, float)
- elif dtype is not None and dtype.is_decimal():
- # Cast to Float64 so Decimal values can be compared, minor precision loss
- value = convert_value(value, float)
- column = column.cast(nw.Float64)
-
- # Build the expression based on the operator
- condition_expr: nw.Expr
- if condition.operator == "==":
- condition_expr = column == value
- elif condition.operator == "!=":
- condition_expr = column != value
- elif condition.operator == ">":
- condition_expr = column > value
- elif condition.operator == "<":
- condition_expr = column < value
- elif condition.operator == ">=":
- condition_expr = column >= value
- elif condition.operator == "<=":
- condition_expr = column <= value
- elif condition.operator == "is_true":
- condition_expr = column == True # type: ignore[comparison-overlap] # noqa: E712
- elif condition.operator == "is_false":
- condition_expr = column == False # type: ignore[comparison-overlap] # noqa: E712
- elif condition.operator == "is_null":
- condition_expr = column.is_null()
- elif condition.operator == "is_not_null":
- condition_expr = ~column.is_null()
- elif condition.operator == "equals":
- condition_expr = column == value
- elif condition.operator == "does_not_equal":
- condition_expr = column != value
- elif condition.operator == "contains":
- # Fill null before string operation to avoid pandas issues
- condition_expr = column.fill_null("").str.contains(
+ case "!=":
+ condition_expr = (
+ column == value if is_negated else column != value
+ )
+ case ">":
+ condition_expr = (
+ column <= value if is_negated else column > value
+ )
+ case "<":
+ condition_expr = (
+ column >= value if is_negated else column < value
+ )
+ case ">=":
+ condition_expr = (
+ column < value if is_negated else column >= value
+ )
+ case "<=":
+ condition_expr = (
+ column > value if is_negated else column <= value
+ )
+ case "is_true":
+ condition_expr = (
+ column == False # type: ignore[comparison-overlap] # noqa: E712
+ if is_negated
+ else column == True # type: ignore[comparison-overlap] # noqa: E712
+ )
+ case "is_false":
+ condition_expr = (
+ column == True # type: ignore[comparison-overlap] # noqa: E712
+ if is_negated
+ else column == False # type: ignore[comparison-overlap] # noqa: E712
+ )
+ case "is_null":
+ condition_expr = (
+ ~column.is_null() if is_negated else column.is_null()
+ )
+ case "is_not_null":
+ condition_expr = (
+ column.is_null() if is_negated else ~column.is_null()
+ )
+ case "equals":
+ condition_expr = (
+ column != value if is_negated else column == value
+ )
+ case "does_not_equal":
+ condition_expr = (
+ column == value if is_negated else column != value
+ )
+ case "contains":
+ base_expr = column.fill_null("").str.contains(
str(value), literal=True
)
- elif condition.operator == "regex":
- # Fill null before string operation to avoid pandas issues
- condition_expr = column.fill_null("").str.contains(
+ condition_expr = (
+ ~base_expr & ~column.is_null() if is_negated else base_expr
+ )
+ case "regex":
+ base_expr = column.fill_null("").str.contains(
str(value), literal=False
)
- elif condition.operator == "starts_with":
- # Fill null before string operation to avoid pandas issues
- condition_expr = column.fill_null("").str.starts_with(
- str(value)
+ condition_expr = (
+ ~base_expr & ~column.is_null() if is_negated else base_expr
)
- elif condition.operator == "ends_with":
- # Fill null before string operation to avoid pandas issues
- condition_expr = column.fill_null("").str.ends_with(str(value))
- elif condition.operator == "in":
- # is_in doesn't support None values, so we need to handle them separately
+ case "starts_with":
+ base_expr = column.fill_null("").str.starts_with(str(value))
+ condition_expr = (
+ ~base_expr & ~column.is_null() if is_negated else base_expr
+ )
+ case "ends_with":
+ base_expr = column.fill_null("").str.ends_with(str(value))
+ condition_expr = (
+ ~base_expr & ~column.is_null() if is_negated else base_expr
+ )
+ case "in":
if value is not None and None in value:
- condition_expr = column.is_in(value) | column.is_null()
+ condition_expr = (
+ ~column.is_in(value) & ~column.is_null()
+ if is_negated
+ else column.is_in(value) | column.is_null()
+ )
else:
- condition_expr = column.is_in(value or [])
- elif condition.operator == "not_in":
- # ~is_in returns null for null values, so we need to explicitly include/exclude nulls
+ condition_expr = (
+ ~column.is_in(value or []) | column.is_null()
+ if is_negated
+ else column.is_in(value or [])
+ )
+ case "not_in":
if value is not None and None in value:
- condition_expr = ~column.is_in(value) & ~column.is_null()
+ base_expr = ~column.is_in(value) & ~column.is_null()
+ condition_expr = (
+ column.is_in(value) | column.is_null()
+ if is_negated
+ else base_expr
+ )
else:
+ base_expr = ~column.is_in(value or []) | column.is_null()
condition_expr = (
- ~column.is_in(value or []) | column.is_null()
+ column.is_in(value or []) if is_negated else base_expr
)
- else:
+ case "between":
+ if isinstance(value, RangeValue):
+ if is_negated:
+ condition_expr = (column < value.min) | (
+ column > value.max
+ )
+ else:
+ condition_expr = (column >= value.min) & (
+ column <= value.max
+ )
+ else:
+ raise TypeError(
+ f"between operator requires RangeValue, got {type(value)}"
+ )
+ case "is_empty":
+ condition_expr = (
+ (column != "") | column.is_null()
+ if is_negated
+ else (column == "") & ~column.is_null()
+ )
+ case _:
assert_never(condition.operator)
- # Combine the condition expression with the filter expression
+ return condition_expr
+
+ @staticmethod
+ def _evaluate_filter_group(
+ df: DataFrame, group: FilterGroup
+ ) -> Expr | None:
+ operator = group.operator
+ conditions = group.children
+ filter_expr: Expr | None = None
+
+ for condition in conditions:
+ expr: Expr | None = None
+ if isinstance(condition, FilterCondition):
+ expr = NarwhalsTransformHandler._evaluate_condition(
+ df, condition
+ )
+ elif isinstance(condition, FilterGroup):
+ expr = NarwhalsTransformHandler._evaluate_filter_group(
+ df, condition
+ )
+ else:
+ assert_never(condition)
+
+ if expr is None:
+ continue
+
if filter_expr is None:
- filter_expr = condition_expr
+ filter_expr = expr
else:
- filter_expr = filter_expr & condition_expr
+ if operator == "and":
+ filter_expr = filter_expr & expr
+ elif operator == "or":
+ filter_expr = filter_expr | expr
+ else:
+ assert_never(operator)
+
+ if filter_expr is not None and group.negate:
+ filter_expr = ~filter_expr
+
+ return filter_expr
+
+ @staticmethod
+ def handle_filter_rows(
+ df: DataFrame, transform: FilterRowsTransform
+ ) -> DataFrame:
+ filter_expr = NarwhalsTransformHandler._evaluate_filter_group(
+ df, transform.where
+ )
if filter_expr is None:
return df
- # Handle the operation (keep_rows or remove_rows)
if transform.operation == "keep_rows":
- result = df.filter(filter_expr)
+ return df.filter(filter_expr)
elif transform.operation == "remove_rows":
- result = df.filter(~filter_expr) # type: ignore[operator]
+ return df.filter(~filter_expr) # type: ignore[operator]
else:
assert_never(transform.operation)
- return result
-
@staticmethod
def handle_group_by(
df: DataFrame, transform: GroupByTransform
diff --git a/marimo/_plugins/ui/_impl/dataframes/transforms/print_code.py b/marimo/_plugins/ui/_impl/dataframes/transforms/print_code.py
index b1feff2c5c..d27d1d0769 100644
--- a/marimo/_plugins/ui/_impl/dataframes/transforms/print_code.py
+++ b/marimo/_plugins/ui/_impl/dataframes/transforms/print_code.py
@@ -4,7 +4,8 @@ from __future__ import annotations
from typing import TYPE_CHECKING, Any
from marimo._plugins.ui._impl.dataframes.transforms.types import (
- Condition,
+ FilterCondition,
+ RangeValue,
Transform,
TransformType,
)
@@ -32,53 +33,57 @@ def python_print_transforms(
def python_print_pandas(
df_name: str, all_columns: list[str], transform: Transform
) -> str:
- def generate_where_clause(df_name: str, where: Condition) -> str:
+ def generate_where_clause(df_name: str, where: FilterCondition) -> str:
column_id, operator, value = (
where.column_id,
where.operator,
where.value,
)
+ col = f"{df_name}[{_as_literal(column_id)}]"
- if operator == "==":
- return (
- f"{df_name}[{_as_literal(column_id)}] == {_as_literal(value)}"
- )
- elif operator == "equals":
- return (
- f"{df_name}[{_as_literal(column_id)}].eq({_as_literal(value)})"
- )
- elif operator == "does_not_equal":
- return (
- f"{df_name}[{_as_literal(column_id)}].ne({_as_literal(value)})"
- )
+ result: str
+ if operator == "==" or operator == "equals":
+ result = f"{col} == {_as_literal(value)}"
+ elif operator == "!=" or operator == "does_not_equal":
+ result = f"{col} != {_as_literal(value)}"
+ elif operator in [">", ">=", "<", "<="]:
+ result = f"{col} {operator} {_as_literal(value)}"
elif operator == "contains":
- return f"{df_name}[{_as_literal(column_id)}].str.contains({_as_literal(value)})"
+ result = f"{col}.str.contains({_as_literal(value)})"
elif operator == "regex":
- return f"{df_name}[{_as_literal(column_id)}].str.contains({_as_literal(value)}, regex=True)"
+ result = f"{col}.str.contains({_as_literal(value)}, regex=True)"
elif operator == "starts_with":
- return f"{df_name}[{_as_literal(column_id)}].str.startswith({_as_literal(value)})"
+ result = f"{col}.str.startswith({_as_literal(value)})"
elif operator == "ends_with":
- return f"{df_name}[{_as_literal(column_id)}].str.endswith({_as_literal(value)})"
+ result = f"{col}.str.endswith({_as_literal(value)})"
elif operator == "in" or operator == "not_in":
- result = f"{df_name}[{_as_literal(column_id)}].isin({_list_of_strings(value)})"
- return result if operator == "in" else f"~{result}"
- elif operator == "!=":
- return (
- f"{df_name}[{_as_literal(column_id)}].ne({_as_literal(value)})"
- )
- elif operator in [">", ">=", "<", "<="]:
- return f"{df_name}[{_as_literal(column_id)}] {operator} {_as_literal(value)}"
+ expr = f"{col}.isin({_list_of_strings(value)})"
+ result = expr if operator == "in" else f"~{expr}"
elif operator == "is_null":
- return f"{df_name}[{_as_literal(column_id)}].isna()"
+ result = f"{col}.isna()"
elif operator == "is_not_null":
- return f"{df_name}[{_as_literal(column_id)}].notna()"
+ result = f"{col}.notna()"
elif operator == "is_true":
- return f"{df_name}[{_as_literal(column_id)}].eq(True)"
+ result = f"{col}.eq(True)"
elif operator == "is_false":
- return f"{df_name}[{_as_literal(column_id)}].eq(False)"
+ result = f"{col}.eq(False)"
+ elif operator == "between":
+ if isinstance(value, RangeValue):
+ result = f"{col}.between({_as_literal(value.min)}, {_as_literal(value.max)})"
+ else:
+ raise ValueError(
+ f"between requires RangeValue, got {type(value)}"
+ )
+ elif operator == "is_empty":
+ result = f"({col} == '')"
else:
raise ValueError(f"Unknown operator: {operator}")
+ if where.negate:
+ result = f"~({result})"
+
+ return result
+
if transform.type == TransformType.COLUMN_CONVERSION:
column_id, data_type, errors = (
transform.column_id,
@@ -108,11 +113,15 @@ def python_print_pandas(
return f"{df_name}.sort_values({args})"
elif transform.type == TransformType.FILTER_ROWS:
- operation, where = transform.operation, transform.where
- if not where:
+ operation, group = transform.operation, transform.where
+ if not group.children:
return df_name
where_clauses = [
- generate_where_clause(df_name, condition) for condition in where
+ generate_where_clause(df_name, condition)
+ for condition in group.children
+ if isinstance(
+ condition, FilterCondition
+ ) # TODO: handle nested FilterGroup (OR, negate)
]
if operation == "keep_rows" and len(where_clauses) == 1:
return f"{df_name}[{where_clauses[0]}]"
@@ -288,41 +297,57 @@ def python_print_pandas(
def python_print_polars(
df_name: str, all_columns: list[str], transform: Transform
) -> str:
- def generate_where_clause_polars(where: Condition) -> str:
+ def generate_where_clause_polars(where: FilterCondition) -> str:
column_id, operator, value = (
where.column_id,
where.operator,
where.value,
)
+ col = f"pl.col({_as_literal(column_id)})"
+ result: str
if operator == "==" or operator == "equals":
- return f"pl.col({_as_literal(column_id)}) == {_as_literal(value)}"
- elif operator == "does_not_equal" or operator == "!=":
- return f"pl.col({_as_literal(column_id)}) != {_as_literal(value)}"
+ result = f"{col} == {_as_literal(value)}"
+ elif operator == "!=" or operator == "does_not_equal":
+ result = f"{col} != {_as_literal(value)}"
+ elif operator in [">", ">=", "<", "<="]:
+ result = f"{col} {operator} {_as_literal(value)}"
elif operator == "contains":
- return f"pl.col({_as_literal(column_id)}).str.contains({_as_literal(value)})"
+ result = f"{col}.str.contains({_as_literal(value)})"
elif operator == "regex":
- return f"pl.col({_as_literal(column_id)}).str.contains({_as_literal(value)}, literal=False)"
+ result = f"{col}.str.contains({_as_literal(value)}, literal=False)"
elif operator == "starts_with":
- return f"pl.col({_as_literal(column_id)}).str.starts_with({_as_literal(value)})"
+ result = f"{col}.str.starts_with({_as_literal(value)})"
elif operator == "ends_with":
- return f"pl.col({_as_literal(column_id)}).str.ends_with({_as_literal(value)})"
+ result = f"{col}.str.ends_with({_as_literal(value)})"
elif operator == "in" or operator == "not_in":
- result = f"pl.col({_as_literal(column_id)}).is_in({_list_of_strings(value)})"
- return result if operator == "in" else f"~{result}"
- elif operator in [">", ">=", "<", "<="]:
- return f"pl.col({_as_literal(column_id)}) {operator} {_as_literal(value)}"
+ expr = f"{col}.is_in({_list_of_strings(value)})"
+ result = expr if operator == "in" else f"~{expr}"
elif operator == "is_null":
- return f"pl.col({_as_literal(column_id)}).is_null()"
+ result = f"{col}.is_null()"
elif operator == "is_not_null":
- return f"pl.col({_as_literal(column_id)}).is_not_null()"
+ result = f"{col}.is_not_null()"
elif operator == "is_true":
- return f"pl.col({_as_literal(column_id)}) == True"
+ result = f"{col} == True"
elif operator == "is_false":
- return f"pl.col({_as_literal(column_id)}) == False"
+ result = f"{col} == False"
+ elif operator == "between":
+ if isinstance(value, RangeValue):
+ result = f"{col}.is_between({_as_literal(value.min)}, {_as_literal(value.max)})"
+ else:
+ raise ValueError(
+ f"between requires RangeValue, got {type(value)}"
+ )
+ elif operator == "is_empty":
+ result = f'({col} == "")'
else:
raise ValueError(f"Unknown operator: {operator}")
+ if where.negate:
+ result = f"~({result})"
+
+ return result
+
if transform.type == TransformType.COLUMN_CONVERSION:
column_id, data_type = transform.column_id, transform.data_type
try:
@@ -354,11 +379,15 @@ def python_print_polars(
return f"{df_name}.sort({_as_literal(column_id)}, descending={not ascending}, nulls_last={na_position == 'last'})"
elif transform.type == TransformType.FILTER_ROWS:
- operation, where = transform.operation, transform.where
- if not where:
+ operation, group = transform.operation, transform.where
+ if not group.children:
return df_name
where_clauses = [
- generate_where_clause_polars(condition) for condition in where
+ generate_where_clause_polars(condition)
+ for condition in group.children
+ if isinstance(
+ condition, FilterCondition
+ ) # TODO: handle nested FilterGroup (OR, negate)
]
if operation == "keep_rows" and len(where_clauses) == 1:
return f"{df_name}.filter({where_clauses[0]})"
@@ -505,41 +534,57 @@ def python_print_polars(
def python_print_ibis(
df_name: str, all_columns: list[str], transform: Transform
) -> str:
- def generate_where_clause(df_name: str, where: Condition) -> str:
+ def generate_where_clause(df_name: str, where: FilterCondition) -> str:
column_id, operator, value = (
where.column_id,
where.operator,
where.value,
)
+ col = f"{df_name}[{_as_literal(column_id)}]"
+ result: str
if operator == "==" or operator == "equals":
- return f"({df_name}[{_as_literal(column_id)}] == {_as_literal(value)})"
- elif operator == "does_not_equal" or operator == "!=":
- return f"({df_name}[{_as_literal(column_id)}] != {_as_literal(value)}))"
+ result = f"({col} == {_as_literal(value)})"
+ elif operator == "!=" or operator == "does_not_equal":
+ result = f"({col} != {_as_literal(value)})"
+ elif operator in [">", ">=", "<", "<="]:
+ result = f"({col} {operator} {_as_literal(value)})"
elif operator == "contains":
- return f"({df_name}[{_as_literal(column_id)}].contains({_as_literal(value)}))"
+ result = f"({col}.contains({_as_literal(value)}))"
elif operator == "regex":
- return f"({df_name}[{_as_literal(column_id)}].re_search({_as_literal(value)}))"
+ result = f"({col}.re_search({_as_literal(value)}))"
elif operator == "starts_with":
- return f"({df_name}[{_as_literal(column_id)}].startswith({_as_literal(value)}))"
+ result = f"({col}.startswith({_as_literal(value)}))"
elif operator == "ends_with":
- return f"({df_name}[{_as_literal(column_id)}].endswith({_as_literal(value)}))"
+ result = f"({col}.endswith({_as_literal(value)}))"
elif operator == "in" or operator == "not_in":
- result = f"({df_name}[{_as_literal(column_id)}].isin({_list_of_strings(value)}))"
- return result if operator == "in" else f"~{result}"
- elif operator in [">", ">=", "<", "<="]:
- return f"({df_name}[{_as_literal(column_id)}] {operator} {_as_literal(value)})"
+ expr = f"({col}.isin({_list_of_strings(value)}))"
+ result = expr if operator == "in" else f"~{expr}"
elif operator == "is_null":
- return f"({df_name}[{_as_literal(column_id)}].isnull())"
+ result = f"({col}.isnull())"
elif operator == "is_not_null":
- return f"({df_name}[{_as_literal(column_id)}].notnull())"
+ result = f"({col}.notnull())"
elif operator == "is_true":
- return f"({df_name}[{_as_literal(column_id)}] == True)"
+ result = f"({col} == True)"
elif operator == "is_false":
- return f"({df_name}[{_as_literal(column_id)}] == False)"
+ result = f"({col} == False)"
+ elif operator == "between":
+ if isinstance(value, RangeValue):
+ result = f"({col}.between({_as_literal(value.min)}, {_as_literal(value.max)}))"
+ else:
+ raise ValueError(
+ f"between requires RangeValue, got {type(value)}"
+ )
+ elif operator == "is_empty":
+ result = f'({col} == "")'
else:
raise ValueError(f"Unknown operator: {operator}")
+ if where.negate:
+ result = f"~({result})"
+
+ return result
+
if transform.type == TransformType.COLUMN_CONVERSION:
column_id, data_type, errors = (
transform.column_id,
@@ -572,10 +617,13 @@ def python_print_ibis(
return f"{df_name}.order_by([{df_name}[{_as_literal(column_id)}].{'asc' if ascending else 'desc'}()])"
elif transform.type == TransformType.FILTER_ROWS:
- conditions, operation = transform.where, transform.operation
+ group, operation = transform.where, transform.operation
expressions = [
generate_where_clause(df_name, condition)
- for condition in conditions
+ for condition in group.children
+ if isinstance(
+ condition, FilterCondition
+ ) # TODO: handle nested FilterGroup (OR, negate)
]
expression = " & ".join(expressions)
return (
diff --git a/marimo/_plugins/ui/_impl/dataframes/transforms/types.py b/marimo/_plugins/ui/_impl/dataframes/transforms/types.py
index 82f059a916..b8335fdf8f 100644
--- a/marimo/_plugins/ui/_impl/dataframes/transforms/types.py
+++ b/marimo/_plugins/ui/_impl/dataframes/transforms/types.py
@@ -33,6 +33,8 @@ Operator = Literal[
"ends_with",
"in",
"not_in",
+ "between",
+ "is_empty",
]
Aggregation = Literal[
"count",
@@ -62,15 +64,39 @@ class TransformType(Enum):
@dataclass(frozen=True)
-class Condition:
+class RangeValue:
+ min: int | float | str
+ max: int | float | str
+
+
+@dataclass(frozen=True)
+class FilterCondition:
column_id: ColumnId
operator: Operator
+ type: Literal["condition"] = "condition"
value: Any | None = None
+ negate: bool = False
def __hash__(self) -> int:
- return hash((self.column_id, self.operator, self.value))
+ return hash(
+ (self.type, self.column_id, self.operator, self.value, self.negate)
+ )
def __post_init__(self) -> None:
+ if self.operator == "between" and isinstance(self.value, dict):
+ if "min" not in self.value or "max" not in self.value:
+ raise ValueError(
+ "value must be a dict with 'min' and 'max' keys for 'between' operator"
+ )
+
+ object.__setattr__(
+ self,
+ "value",
+ RangeValue(
+ min=self.value["min"],
+ max=self.value["max"],
+ ),
+ )
if self.operator == "in" or self.operator == "not_in":
if isinstance(self.value, list):
# Hack to convert to tuple for frozen dataclass
@@ -84,6 +110,75 @@ class Condition:
)
+@dataclass(frozen=True)
+class FilterGroup:
+ children: tuple[FilterCondition | FilterGroup, ...]
+ type: Literal["group"] = "group"
+ operator: Literal["and", "or"] = "and"
+ negate: bool = False
+
+ def __post_init__(self) -> None:
+ if isinstance(self.children, list):
+ object.__setattr__(self, "children", tuple(self.children))
+
+
+DTYPE_OPERATORS: dict[str, frozenset[Operator]] = {
+ "number": frozenset(
+ {
+ "==",
+ "!=",
+ "<",
+ ">",
+ "<=",
+ ">=",
+ "is_null",
+ "is_not_null",
+ "in",
+ "not_in",
+ "between",
+ }
+ ),
+ "boolean": frozenset({"is_true", "is_false", "is_null", "is_not_null"}),
+ "str": frozenset(
+ {
+ "equals",
+ "does_not_equal",
+ "contains",
+ "regex",
+ "starts_with",
+ "ends_with",
+ "is_null",
+ "is_not_null",
+ "in",
+ "not_in",
+ "is_empty",
+ }
+ ),
+ "temporal": frozenset(
+ {"==", "!=", "<", ">", "<=", ">=", "is_null", "is_not_null", "between"}
+ ),
+}
+
+
+def conditions_to_filter_group(
+ conditions: list[FilterCondition],
+) -> FilterGroup:
+ return FilterGroup(
+ type="group",
+ operator="and",
+ children=tuple(conditions),
+ negate=False,
+ )
+
+
+def validate_operator_for_dtype(operator: Operator, dtype: str) -> bool:
+ allowed = DTYPE_OPERATORS.get(dtype, frozenset())
+ if not allowed:
+ # unknown data type, allow all operators
+ return True
+ return operator in allowed
+
+
@dataclass
class ColumnConversionTransform:
type: Literal[TransformType.COLUMN_CONVERSION]
@@ -111,7 +206,7 @@ class SortColumnTransform:
class FilterRowsTransform:
type: Literal[TransformType.FILTER_ROWS]
operation: Literal["keep_rows", "remove_rows"]
- where: list[Condition]
+ where: FilterGroup
@dataclass
diff --git a/marimo/_plugins/ui/_impl/table.py b/marimo/_plugins/ui/_impl/table.py
index 049a0e6566..af1720bd97 100644
--- a/marimo/_plugins/ui/_impl/table.py
+++ b/marimo/_plugins/ui/_impl/table.py
@@ -2,6 +2,7 @@
from __future__ import annotations
import functools
+from collections.abc import Callable
from dataclasses import dataclass
from typing import (
TYPE_CHECKING,
@@ -11,6 +12,9 @@ from typing import (
cast,
)
+if TYPE_CHECKING:
+ from collections.abc import Mapping
+
from narwhals.typing import IntoDataFrame
import marimo._output.data.data as mo_data
@@ -30,9 +34,11 @@ from marimo._plugins.ui._impl.dataframes.transforms.apply import (
apply_transforms_to_df,
)
from marimo._plugins.ui._impl.dataframes.transforms.types import (
- Condition,
+ FilterCondition,
+ FilterGroup,
FilterRowsTransform,
TransformType,
+ validate_operator_for_dtype,
)
from marimo._plugins.ui._impl.tables.selection import (
INDEX_COLUMN_NAME,
@@ -135,7 +141,7 @@ class SearchTableArgs:
page_number: int
query: str | None = None
sort: list[SortArgs] | None = None
- filters: list[Condition] | None = None
+ filters: FilterGroup | None = None
limit: int | None = None
max_columns: int | MaxColumnsNotProvided | None = MAX_COLUMNS_NOT_PROVIDED
@@ -204,6 +210,52 @@ def get_default_table_max_columns() -> int:
return ctx.marimo_config["display"]["default_table_max_columns"]
+_DATATYPE_TO_CATEGORY: dict[str, str] = {
+ "string": "str",
+ "boolean": "boolean",
+ "integer": "number",
+ "number": "number",
+ "date": "temporal",
+ "datetime": "temporal",
+ "time": "temporal",
+}
+
+
+def _filter_valid_columns(
+ group: FilterGroup,
+ column_dtypes: Mapping[str, str],
+) -> FilterGroup:
+ """Recursively remove conditions on non-existent columns
+ or with invalid operators for the column dtype."""
+ valid_children: list[FilterCondition | FilterGroup] = []
+ for child in group.children:
+ if isinstance(child, FilterCondition):
+ if child.column_id not in column_dtypes:
+ continue
+ category = _DATATYPE_TO_CATEGORY.get(
+ column_dtypes[child.column_id], ""
+ )
+ if not validate_operator_for_dtype(child.operator, category):
+ LOGGER.warning(
+ "Invalid operator '%s' for dtype '%s' on column '%s'",
+ child.operator,
+ column_dtypes[child.column_id],
+ child.column_id,
+ )
+ continue
+ valid_children.append(child)
+ elif isinstance(child, FilterGroup):
+ filtered = _filter_valid_columns(child, column_dtypes)
+ if filtered.children:
+ valid_children.append(filtered)
+ return FilterGroup(
+ type="group",
+ operator=group.operator,
+ children=tuple(valid_children),
+ negate=group.negate,
+ )
+
+
@mddoc
class table(
UIElement[
@@ -1143,40 +1195,37 @@ class table(
@functools.lru_cache(maxsize=1) # noqa: B019
def _apply_filters_query_sort_cached(
self,
- filters: tuple[Condition, ...] | None,
+ filters: FilterGroup | None,
query: str | None,
sort: tuple[SortArgs, ...] | None,
) -> TableManager[Any]:
"""Cached version that expects hashable arguments."""
return self._apply_filters_query_sort(
- list(filters) if filters else None,
+ filters,
query,
list(sort) if sort else None,
)
def _apply_filters_query_sort(
self,
- filters: list[Condition] | None,
+ filters: FilterGroup | None,
query: str | None,
sort: list[SortArgs] | None,
) -> TableManager[Any]:
result = self._manager
- if filters:
- # Filter out conditions for columns that don't exist
- existing_columns = set(result.get_column_names())
- valid_filters = [
- condition
- for condition in filters
- if condition.column_id in existing_columns
- ]
+ if filters and filters.children:
+ column_dtypes = {
+ name: dtype for name, (dtype, _) in result.get_field_types()
+ }
+ valid_group = _filter_valid_columns(filters, column_dtypes)
- if valid_filters:
+ if valid_group.children:
data = apply_transforms_to_df(
result.data,
FilterRowsTransform(
type=TransformType.FILTER_ROWS,
- where=valid_filters,
+ where=valid_group,
operation="keep_rows",
),
)
@@ -1470,7 +1519,7 @@ class table(
else self._apply_filters_query_sort
)
result = filter_function(
- tuple(args.filters) if args.filters else None, # type: ignore
+ args.filters,
args.query,
tuple(args.sort) if args.sort else None, # type: ignore
)
diff --git a/tests/_plugins/ui/_impl/dataframes/test_filter_model.py b/tests/_plugins/ui/_impl/dataframes/test_filter_model.py
new file mode 100644
index 0000000000..dfdb5f6113
--- /dev/null
+++ b/tests/_plugins/ui/_impl/dataframes/test_filter_model.py
@@ -0,0 +1,466 @@
+# Copyright 2026 Marimo. All rights reserved.
+from __future__ import annotations
+
+import pytest
+
+from marimo._plugins.ui._impl.dataframes.transforms.types import (
+ DTYPE_OPERATORS,
+ FilterCondition,
+ FilterGroup,
+ RangeValue,
+ conditions_to_filter_group,
+ validate_operator_for_dtype,
+)
+
+# --- RangeValue ---
+
+
+def test_range_value_int() -> None:
+ rv = RangeValue(min=1, max=10)
+ assert rv.min == 1
+ assert rv.max == 10
+
+
+def test_range_value_float() -> None:
+ rv = RangeValue(min=1.5, max=10.5)
+ assert rv.min == 1.5
+ assert rv.max == 10.5
+
+
+def test_range_value_str() -> None:
+ rv = RangeValue(min="2024-01-01", max="2024-12-31")
+ assert rv.min == "2024-01-01"
+ assert rv.max == "2024-12-31"
+
+
+def test_range_value_frozen() -> None:
+ rv = RangeValue(min=1, max=10)
+ with pytest.raises(AttributeError):
+ rv.min = 2 # type: ignore[misc]
+
+
+def test_range_value_hashable() -> None:
+ rv = RangeValue(min=1, max=10)
+ assert hash(rv) is not None
+ s = {rv}
+ assert rv in s
+
+
+def test_range_value_equality() -> None:
+ assert RangeValue(min=1, max=10) == RangeValue(min=1, max=10)
+
+
+def test_range_value_inequality() -> None:
+ assert RangeValue(min=1, max=10) != RangeValue(min=1, max=20)
+ assert RangeValue(min=1, max=10) != RangeValue(min=2, max=10)
+
+
+# --- FilterCondition ---
+
+
+def test_condition_basic() -> None:
+ fc = FilterCondition(
+ type="condition", column_id="x", operator="==", value=5
+ )
+ assert fc.type == "condition"
+ assert fc.column_id == "x"
+ assert fc.operator == "=="
+ assert fc.value == 5
+ assert fc.negate is False
+
+
+def test_condition_negate_default() -> None:
+ fc = FilterCondition(
+ type="condition", column_id="x", operator="==", value=5
+ )
+ assert fc.negate is False
+
+
+def test_condition_negate_true() -> None:
+ fc = FilterCondition(
+ type="condition", column_id="x", operator="==", value=5, negate=True
+ )
+ assert fc.negate is True
+
+
+def test_condition_negate_changes_hash() -> None:
+ fc1 = FilterCondition(
+ type="condition", column_id="x", operator="==", value=5
+ )
+ fc2 = FilterCondition(
+ type="condition", column_id="x", operator="==", value=5, negate=True
+ )
+ assert hash(fc1) != hash(fc2)
+
+
+def test_condition_frozen() -> None:
+ fc = FilterCondition(
+ type="condition", column_id="x", operator="==", value=5
+ )
+ with pytest.raises(AttributeError):
+ fc.value = 10 # type: ignore[misc]
+
+
+def test_condition_hashable() -> None:
+ fc = FilterCondition(
+ type="condition", column_id="x", operator="==", value=5
+ )
+ assert hash(fc) is not None
+ s = {fc}
+ assert fc in s
+
+
+def test_condition_in_list_to_tuple() -> None:
+ fc = FilterCondition(
+ type="condition", column_id="x", operator="in", value=[1, 2, 3]
+ )
+ assert isinstance(fc.value, tuple)
+ assert fc.value == (1, 2, 3)
+
+
+def test_condition_in_tuple_passthrough() -> None:
+ fc = FilterCondition(
+ type="condition", column_id="x", operator="in", value=(1, 2, 3)
+ )
+ assert fc.value == (1, 2, 3)
+
+
+def test_condition_in_invalid_value() -> None:
+ with pytest.raises(ValueError, match="list or tuple"):
+ FilterCondition(
+ type="condition", column_id="x", operator="in", value="bad"
+ )
+
+
+def test_condition_not_in_list_to_tuple() -> None:
+ fc = FilterCondition(
+ type="condition", column_id="x", operator="not_in", value=[1, 2]
+ )
+ assert isinstance(fc.value, tuple)
+
+
+def test_condition_between_dict_to_range_value() -> None:
+ fc = FilterCondition(
+ type="condition",
+ column_id="x",
+ operator="between",
+ value={"min": 1, "max": 10},
+ )
+ assert isinstance(fc.value, RangeValue)
+ assert fc.value.min == 1
+ assert fc.value.max == 10
+
+
+def test_condition_between_range_value_passthrough() -> None:
+ rv = RangeValue(min=1, max=10)
+ fc = FilterCondition(
+ type="condition", column_id="x", operator="between", value=rv
+ )
+ assert fc.value is rv
+
+
+def test_condition_between_missing_min() -> None:
+ with pytest.raises(ValueError, match="min.*max"):
+ FilterCondition(
+ type="condition",
+ column_id="x",
+ operator="between",
+ value={"max": 10},
+ )
+
+
+def test_condition_between_missing_max() -> None:
+ with pytest.raises(ValueError, match="min.*max"):
+ FilterCondition(
+ type="condition",
+ column_id="x",
+ operator="between",
+ value={"min": 1},
+ )
+
+
+def test_condition_value_none_for_nullcheck() -> None:
+ fc = FilterCondition(type="condition", column_id="x", operator="is_null")
+ assert fc.value is None
+
+
+def test_condition_equality() -> None:
+ fc1 = FilterCondition(
+ type="condition", column_id="x", operator="==", value=5
+ )
+ fc2 = FilterCondition(
+ type="condition", column_id="x", operator="==", value=5
+ )
+ assert fc1 == fc2
+
+
+def test_condition_inequality() -> None:
+ fc1 = FilterCondition(
+ type="condition", column_id="x", operator="==", value=5
+ )
+ fc2 = FilterCondition(
+ type="condition", column_id="x", operator="!=", value=5
+ )
+ assert fc1 != fc2
+
+
+# --- FilterGroup ---
+
+
+def test_group_defaults() -> None:
+ fg = FilterGroup(type="group", operator="and", children=())
+ assert fg.type == "group"
+ assert fg.operator == "and"
+ assert fg.children == ()
+ assert fg.negate is False
+
+
+def test_group_single_child() -> None:
+ fc = FilterCondition(
+ type="condition", column_id="x", operator="==", value=5
+ )
+ fg = FilterGroup(type="group", operator="and", children=(fc,))
+ assert len(fg.children) == 1
+ assert fg.children[0] == fc
+
+
+def test_group_multiple_children() -> None:
+ fc1 = FilterCondition(
+ type="condition", column_id="x", operator="==", value=5
+ )
+ fc2 = FilterCondition(
+ type="condition", column_id="y", operator=">", value=10
+ )
+ fg = FilterGroup(type="group", operator="and", children=(fc1, fc2))
+ assert len(fg.children) == 2
+
+
+def test_group_nested() -> None:
+ fc1 = FilterCondition(
+ type="condition", column_id="x", operator="==", value=5
+ )
+ fc2 = FilterCondition(
+ type="condition", column_id="y", operator=">", value=10
+ )
+ inner = FilterGroup(type="group", operator="or", children=(fc1, fc2))
+ outer = FilterGroup(type="group", operator="and", children=(inner,))
+ assert len(outer.children) == 1
+ assert isinstance(outer.children[0], FilterGroup)
+
+
+def test_group_deeply_nested() -> None:
+ fc = FilterCondition(
+ type="condition", column_id="x", operator="==", value=1
+ )
+ level1 = FilterGroup(type="group", operator="and", children=(fc,))
+ level2 = FilterGroup(type="group", operator="or", children=(level1,))
+ level3 = FilterGroup(type="group", operator="and", children=(level2,))
+ assert isinstance(level3.children[0], FilterGroup)
+ inner = level3.children[0]
+ assert isinstance(inner, FilterGroup)
+ assert isinstance(inner.children[0], FilterGroup)
+
+
+def test_group_list_to_tuple() -> None:
+ fc = FilterCondition(
+ type="condition", column_id="x", operator="==", value=5
+ )
+ fg = FilterGroup(type="group", operator="and", children=[fc]) # type: ignore[arg-type]
+ assert isinstance(fg.children, tuple)
+
+
+def test_group_hashable() -> None:
+ fc = FilterCondition(
+ type="condition", column_id="x", operator="==", value=5
+ )
+ fg = FilterGroup(type="group", operator="and", children=(fc,))
+ assert hash(fg) is not None
+ s = {fg}
+ assert fg in s
+
+
+def test_group_hashable_nested() -> None:
+ fc = FilterCondition(
+ type="condition", column_id="x", operator="==", value=1
+ )
+ inner = FilterGroup(type="group", operator="and", children=(fc,))
+ outer = FilterGroup(type="group", operator="or", children=(inner, fc))
+ assert hash(outer) is not None
+
+
+def test_group_or() -> None:
+ fg = FilterGroup(type="group", operator="or", children=())
+ assert fg.operator == "or"
+
+
+def test_group_negate() -> None:
+ fg = FilterGroup(type="group", operator="and", children=(), negate=True)
+ assert fg.negate is True
+
+
+def test_group_mixed_children() -> None:
+ fc = FilterCondition(
+ type="condition", column_id="x", operator="==", value=5
+ )
+ inner = FilterGroup(type="group", operator="or", children=(fc,))
+ outer = FilterGroup(type="group", operator="and", children=(fc, inner))
+ assert isinstance(outer.children[0], FilterCondition)
+ assert isinstance(outer.children[1], FilterGroup)
+
+
+def test_group_frozen() -> None:
+ fg = FilterGroup(type="group", operator="and", children=())
+ with pytest.raises(AttributeError):
+ fg.operator = "or" # type: ignore[misc]
+
+
+# --- conditions_to_filter_group ---
+
+
+def test_conditions_to_group_empty() -> None:
+ fg = conditions_to_filter_group([])
+ assert fg.type == "group"
+ assert fg.operator == "and"
+ assert fg.children == ()
+ assert fg.negate is False
+
+
+def test_conditions_to_group_single() -> None:
+ fc = FilterCondition(
+ type="condition", column_id="x", operator="==", value=5
+ )
+ fg = conditions_to_filter_group([fc])
+ assert len(fg.children) == 1
+ assert fg.children[0] == fc
+
+
+def test_conditions_to_group_multiple() -> None:
+ fc1 = FilterCondition(
+ type="condition", column_id="x", operator="==", value=5
+ )
+ fc2 = FilterCondition(
+ type="condition", column_id="y", operator=">", value=10
+ )
+ fg = conditions_to_filter_group([fc1, fc2])
+ assert len(fg.children) == 2
+ assert fg.children[0] == fc1
+ assert fg.children[1] == fc2
+ assert fg.operator == "and"
+
+
+def test_conditions_to_group_preserves_values() -> None:
+ fc = FilterCondition(
+ type="condition",
+ column_id="x",
+ operator="in",
+ value=[1, 2, 3],
+ negate=True,
+ )
+ fg = conditions_to_filter_group([fc])
+ child = fg.children[0]
+ assert isinstance(child, FilterCondition)
+ assert child.column_id == "x"
+ assert child.operator == "in"
+ assert child.value == (1, 2, 3)
+ assert child.negate is True
+
+
+# --- validate_operator_for_dtype ---
+
+
+ ("dtype", "operator"),
+ [
+ ("number", "=="),
+ ("number", "!="),
+ ("number", "<"),
+ ("number", ">"),
+ ("number", "<="),
+ ("number", ">="),
+ ("number", "between"),
+ ("number", "in"),
+ ("number", "not_in"),
+ ("number", "is_null"),
+ ("number", "is_not_null"),
+ ("boolean", "is_true"),
+ ("boolean", "is_false"),
+ ("boolean", "is_null"),
+ ("boolean", "is_not_null"),
+ ("str", "equals"),
+ ("str", "does_not_equal"),
+ ("str", "contains"),
+ ("str", "regex"),
+ ("str", "starts_with"),
+ ("str", "ends_with"),
+ ("str", "in"),
+ ("str", "not_in"),
+ ("str", "is_null"),
+ ("str", "is_not_null"),
+ ("str", "is_empty"),
+ ("temporal", "=="),
+ ("temporal", "!="),
+ ("temporal", "<"),
+ ("temporal", ">"),
+ ("temporal", "<="),
+ ("temporal", ">="),
+ ("temporal", "between"),
+ ("temporal", "is_null"),
+ ("temporal", "is_not_null"),
+ ],
+)
+def test_validate_operator_valid(dtype: str, operator: str) -> None:
+ assert validate_operator_for_dtype(operator, dtype) is True
+
+
+ ("dtype", "operator"),
+ [
+ ("number", "contains"),
+ ("number", "starts_with"),
+ ("number", "is_empty"),
+ ("number", "is_true"),
+ ("boolean", "=="),
+ ("boolean", "between"),
+ ("boolean", "contains"),
+ ("boolean", "in"),
+ ("boolean", "is_empty"),
+ ("str", "between"),
+ ("str", "<"),
+ ("str", ">="),
+ ("str", "is_true"),
+ ("temporal", "contains"),
+ ("temporal", "is_empty"),
+ ("temporal", "is_true"),
+ ("temporal", "in"),
+ ],
+)
+def test_validate_operator_invalid(dtype: str, operator: str) -> None:
+ assert validate_operator_for_dtype(operator, dtype) is False
+
+
+def test_validate_operator_unknown_dtype() -> None:
+ assert validate_operator_for_dtype("==", "unknown_type") is True
+ assert validate_operator_for_dtype("contains", "foo") is True
+
+
+def test_validate_between_valid_dtypes() -> None:
+ assert validate_operator_for_dtype("between", "number") is True
+ assert validate_operator_for_dtype("between", "temporal") is True
+ assert validate_operator_for_dtype("between", "str") is False
+ assert validate_operator_for_dtype("between", "boolean") is False
+
+
+def test_validate_is_empty_str_only() -> None:
+ assert validate_operator_for_dtype("is_empty", "str") is True
+ assert validate_operator_for_dtype("is_empty", "number") is False
+ assert validate_operator_for_dtype("is_empty", "boolean") is False
+ assert validate_operator_for_dtype("is_empty", "temporal") is False
+
+
+def test_all_dtypes_covered() -> None:
+ assert set(DTYPE_OPERATORS.keys()) == {
+ "number",
+ "boolean",
+ "str",
+ "temporal",
+ }
diff --git a/tests/_plugins/ui/_impl/dataframes/test_handlers.py b/tests/_plugins/ui/_impl/dataframes/test_handlers.py
index f4af2f79c2..58735a24ab 100644
--- a/tests/_plugins/ui/_impl/dataframes/test_handlers.py
+++ b/tests/_plugins/ui/_impl/dataframes/test_handlers.py
@@ -18,13 +18,15 @@ from marimo._plugins.ui._impl.dataframes.transforms.handlers import (
from marimo._plugins.ui._impl.dataframes.transforms.types import (
AggregateTransform,
ColumnConversionTransform,
- Condition,
DataFrameType,
ExpandDictTransform,
ExplodeColumnsTransform,
+ FilterCondition,
+ FilterGroup,
FilterRowsTransform,
GroupByTransform,
PivotTransform,
+ RangeValue,
RenameColumnTransform,
SampleRowsTransform,
SelectColumnsTransform,
@@ -296,7 +298,15 @@ class TestTransformHandler:
transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[Condition(column_id="A", operator=">=", value=2)],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition", column_id="A", operator=">=", value=2
+ )
+ ],
+ ),
)
result = apply(df, transform)
assert_frame_equal(result, expected)
@@ -308,13 +318,18 @@ class TestTransformHandler:
transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[
- Condition(
- column_id="A",
- operator=cast(Any, operator),
- value="foo",
- )
- ],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="A",
+ operator=cast(Any, operator),
+ value="foo",
+ )
+ ],
+ ),
)
result = apply(df, transform)
assert_frame_equal(result, pd.DataFrame({"A": ["foo"]}))
@@ -336,7 +351,15 @@ class TestTransformHandler:
transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="remove_rows",
- where=[Condition(column_id="B", operator="!=", value=5)],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition", column_id="B", operator="!=", value=5
+ )
+ ],
+ ),
)
result = apply(df, transform)
assert_frame_equal(result, expected)
@@ -358,7 +381,15 @@ class TestTransformHandler:
transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[Condition(column_id="A", operator="<", value=4)],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition", column_id="A", operator="<", value=4
+ )
+ ],
+ ),
)
result = apply(df, transform)
assert_frame_equal(result, expected)
@@ -380,7 +411,15 @@ class TestTransformHandler:
transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="remove_rows",
- where=[Condition(column_id="A", operator="==", value=2)],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition", column_id="A", operator="==", value=2
+ )
+ ],
+ ),
)
result = apply(df, transform)
assert_frame_equal(result, expected)
@@ -402,7 +441,15 @@ class TestTransformHandler:
transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[Condition(column_id="B", operator=">=", value=5)],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition", column_id="B", operator=">=", value=5
+ )
+ ],
+ ),
)
result = apply(df, transform)
assert_frame_equal(result, expected)
@@ -424,7 +471,15 @@ class TestTransformHandler:
transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="remove_rows",
- where=[Condition(column_id="B", operator="<", value=6)],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition", column_id="B", operator="<", value=6
+ )
+ ],
+ ),
)
result = apply(df, transform)
assert_frame_equal(result, expected)
@@ -451,11 +506,18 @@ class TestTransformHandler:
transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[
- Condition(
- column_id="date", operator="==", value=date(2001, 1, 1)
- )
- ],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="date",
+ operator="==",
+ value=date(2001, 1, 1),
+ )
+ ],
+ ),
)
result = apply(df, transform)
assert_frame_equal(result, expected)
@@ -477,7 +539,18 @@ class TestTransformHandler:
transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[Condition(column_id="A", operator="in", value=[1, 2])],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="A",
+ operator="in",
+ value=[1, 2],
+ )
+ ],
+ ),
)
result = apply(df, transform)
assert_frame_equal(result, expected)
@@ -504,13 +577,18 @@ class TestTransformHandler:
transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[
- Condition(
- column_id="date",
- operator="in",
- value=["2001-01-01"], # Backend will receive as string
- ),
- ],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="date",
+ operator="in",
+ value=["2001-01-01"], # Backend will receive as string
+ ),
+ ],
+ ),
)
result = apply(df, transform)
assert_frame_equal(result, expected)
@@ -538,13 +616,18 @@ class TestTransformHandler:
transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[
- Condition(
- column_id="A",
- operator="in",
- value=["1.99", "3.25"],
- ),
- ],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="A",
+ operator="in",
+ value=["1.99", "3.25"],
+ ),
+ ],
+ ),
)
result = apply(df, transform)
assert_frame_equal(result, expected)
@@ -579,13 +662,18 @@ class TestTransformHandler:
transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[
- Condition(
- column_id="A",
- operator="in",
- value=["0.10"],
- ),
- ],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="A",
+ operator="in",
+ value=["0.10"],
+ ),
+ ],
+ ),
)
result = apply(df, transform)
assert_frame_equal(result, expected)
@@ -612,13 +700,18 @@ class TestTransformHandler:
transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[
- Condition(
- column_id="A",
- operator="in",
- value=["1.12345678901234567"],
- ),
- ],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="A",
+ operator="in",
+ value=["1.12345678901234567"],
+ ),
+ ],
+ ),
)
result = apply(df, transform)
assert df_size(result) == 1
@@ -644,34 +737,72 @@ class TestTransformHandler:
eq_transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[Condition(column_id="A", operator="==", value="0.50")],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="A",
+ operator="==",
+ value="0.50",
+ )
+ ],
+ ),
)
assert df_size(apply(df, eq_transform)) == 1
gte_transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[Condition(column_id="A", operator=">=", value="1.99")],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="A",
+ operator=">=",
+ value="1.99",
+ )
+ ],
+ ),
)
assert df_size(apply(df, gte_transform)) == 2
lt_transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[Condition(column_id="A", operator="<", value="1.00")],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="A",
+ operator="<",
+ value="1.00",
+ )
+ ],
+ ),
)
assert df_size(apply(df, lt_transform)) == 1
not_in_transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[
- Condition(
- column_id="A",
- operator="not_in",
- value=["0.50"],
- )
- ],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="A",
+ operator="not_in",
+ value=["0.50"],
+ )
+ ],
+ ),
)
assert df_size(apply(df, not_in_transform)) == 2
@@ -687,9 +818,18 @@ class TestTransformHandler:
in_transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[
- Condition(column_id="A", operator="in", value=["1.99", "3.25"])
- ],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="A",
+ operator="in",
+ value=["1.99", "3.25"],
+ )
+ ],
+ ),
)
result = apply(df, in_transform)
assert df_size(result) == 2
@@ -697,14 +837,36 @@ class TestTransformHandler:
eq_transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[Condition(column_id="A", operator="==", value="0.50")],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="A",
+ operator="==",
+ value="0.50",
+ )
+ ],
+ ),
)
assert df_size(apply(df, eq_transform)) == 1
gte_transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[Condition(column_id="A", operator=">=", value="1.99")],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="A",
+ operator=">=",
+ value="1.99",
+ )
+ ],
+ ),
)
assert df_size(apply(df, gte_transform)) == 2
@@ -724,22 +886,36 @@ class TestTransformHandler:
eq_transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[
- Condition(column_id="d", operator="==", value="2024-06-15")
- ],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="d",
+ operator="==",
+ value="2024-06-15",
+ )
+ ],
+ ),
)
assert df_size(apply(df, eq_transform)) == 1
in_transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[
- Condition(
- column_id="d",
- operator="in",
- value=["2024-01-01", "2024-12-31"],
- )
- ],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="d",
+ operator="in",
+ value=["2024-01-01", "2024-12-31"],
+ )
+ ],
+ ),
)
assert df_size(apply(df, in_transform)) == 2
@@ -754,9 +930,18 @@ class TestTransformHandler:
eq_transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[
- Condition(column_id="d", operator="==", value="2024-06-15")
- ],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="d",
+ operator="==",
+ value="2024-06-15",
+ )
+ ],
+ ),
)
assert df_size(apply(df, eq_transform)) == 1
@@ -779,7 +964,18 @@ class TestTransformHandler:
transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[Condition(column_id="A", operator="in", value=[[1, 2]])],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="A",
+ operator="in",
+ value=[[1, 2]],
+ )
+ ],
+ ),
)
result = apply(df, transform)
assert_frame_equal(result, expected)
@@ -806,11 +1002,18 @@ class TestTransformHandler:
transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[
- Condition(
- column_id="A", operator="in", value=[{"a": 1, "b": 2}]
- )
- ],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="A",
+ operator="in",
+ value=[{"a": 1, "b": 2}],
+ )
+ ],
+ ),
)
result = apply(df, transform)
assert_frame_equal(result, expected)
@@ -839,11 +1042,18 @@ class TestTransformHandler:
transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[
- Condition(
- column_id="A", operator="in", value=[{"a": 1, "b": None}]
- )
- ],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="A",
+ operator="in",
+ value=[{"a": 1, "b": None}],
+ )
+ ],
+ ),
)
result = apply(df, transform)
assert_frame_equal(result, expected)
@@ -869,7 +1079,18 @@ class TestTransformHandler:
transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[Condition(column_id="A", operator="in", value=[None])],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="A",
+ operator="in",
+ value=[None],
+ )
+ ],
+ ),
)
result = apply(df, transform)
assert_frame_equal(result, expected)
@@ -891,7 +1112,18 @@ class TestTransformHandler:
transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[Condition(column_id="A", operator="not_in", value=[1, 2])],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="A",
+ operator="not_in",
+ value=[1, 2],
+ )
+ ],
+ ),
)
result = apply(df, transform)
assert_frame_equal(result, expected)
@@ -915,11 +1147,18 @@ class TestTransformHandler:
transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[
- Condition(
- column_id="A", operator="not_in", value=["foo", "bar"]
- )
- ],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="A",
+ operator="not_in",
+ value=["foo", "bar"],
+ )
+ ],
+ ),
)
result = apply(df, transform)
assert_frame_equal(result, expected)
@@ -945,9 +1184,18 @@ class TestTransformHandler:
transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[
- Condition(column_id="A", operator="not_in", value=[1, 2, None])
- ],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="A",
+ operator="not_in",
+ value=[1, 2, None],
+ )
+ ],
+ ),
)
result = apply(df, transform)
assert_frame_equal(result, expected)
@@ -975,7 +1223,18 @@ class TestTransformHandler:
transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[Condition(column_id="A", operator="not_in", value=[1, 2])],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="A",
+ operator="not_in",
+ value=[1, 2],
+ )
+ ],
+ ),
)
result = apply(df, transform)
if nw.dependencies.is_pandas_dataframe(result):
@@ -1002,10 +1261,18 @@ class TestTransformHandler:
transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[
- Condition(column_id="A", operator=">=", value=3),
- Condition(column_id="B", operator="<=", value=3),
- ],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition", column_id="A", operator=">=", value=3
+ ),
+ FilterCondition(
+ type="condition", column_id="B", operator="<=", value=3
+ ),
+ ],
+ ),
)
result = apply(df, transform)
assert_frame_equal(result, expected)
@@ -1029,10 +1296,18 @@ class TestTransformHandler:
transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="remove_rows",
- where=[
- Condition(column_id="A", operator="==", value=2),
- Condition(column_id="B", operator="==", value=4),
- ],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition", column_id="A", operator="==", value=2
+ ),
+ FilterCondition(
+ type="condition", column_id="B", operator="==", value=4
+ ),
+ ],
+ ),
)
result = apply(df, transform)
assert_frame_equal(result, expected)
@@ -1054,7 +1329,15 @@ class TestTransformHandler:
transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[Condition(column_id="A", operator="is_true")],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition", column_id="A", operator="is_true"
+ )
+ ],
+ ),
)
result = apply(df, transform)
assert_frame_equal(result, expected)
@@ -1062,7 +1345,15 @@ class TestTransformHandler:
transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="remove_rows",
- where=[Condition(column_id="A", operator="is_false")],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition", column_id="A", operator="is_false"
+ )
+ ],
+ ),
)
result = apply(df, transform)
assert_frame_equal(result, expected)
@@ -1084,7 +1375,15 @@ class TestTransformHandler:
transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[Condition(column_id="B", operator=">=", value=2)],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition", column_id="B", operator=">=", value=2
+ )
+ ],
+ ),
)
with pytest.raises(expected):
apply(df, transform)
@@ -1110,7 +1409,15 @@ class TestTransformHandler:
transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[Condition(column_id=1, operator=">=", value=2)],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition", column_id=1, operator=">=", value=2
+ )
+ ],
+ ),
)
result = apply(df, transform)
assert_frame_equal(result, expected)
@@ -1124,13 +1431,18 @@ class TestTransformHandler:
transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[
- Condition(
- column_id="column_a",
- operator="equals",
- value="alpha",
- )
- ],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="column_a",
+ operator="equals",
+ value="alpha",
+ )
+ ],
+ ),
)
result = apply(df, transform)
assert df_size(result) == 1
@@ -1138,13 +1450,18 @@ class TestTransformHandler:
transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[
- Condition(
- column_id="column_a",
- operator="does_not_equal",
- value="alpha",
- )
- ],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="column_a",
+ operator="does_not_equal",
+ value="alpha",
+ )
+ ],
+ ),
)
result = apply(df, transform)
assert df_size(result) == 2
@@ -1152,11 +1469,18 @@ class TestTransformHandler:
ends_with_transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[
- Condition(
- column_id="column_a", operator="ends_with", value="mma"
- )
- ],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="column_a",
+ operator="ends_with",
+ value="mma",
+ )
+ ],
+ ),
)
result = apply(df, ends_with_transform)
assert df_size(result) == 1
@@ -1164,11 +1488,18 @@ class TestTransformHandler:
contains_transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[
- Condition(
- column_id="column_a", operator="contains", value="mma"
- )
- ],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="column_a",
+ operator="contains",
+ value="mma",
+ )
+ ],
+ ),
)
result = apply(df, contains_transform)
assert df_size(result) == 1
@@ -1176,13 +1507,18 @@ class TestTransformHandler:
does_not_contain_transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="remove_rows",
- where=[
- Condition(
- column_id="column_a",
- operator="contains",
- value="mma",
- )
- ],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="column_a",
+ operator="contains",
+ value="mma",
+ )
+ ],
+ ),
)
result = apply(df, does_not_contain_transform)
assert df_size(result) == 2
@@ -1190,11 +1526,18 @@ class TestTransformHandler:
starts_with_transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[
- Condition(
- column_id="column_a", operator="starts_with", value="alp"
- )
- ],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="column_a",
+ operator="starts_with",
+ value="alp",
+ )
+ ],
+ ),
)
result = apply(df, starts_with_transform)
assert df_size(result) == 1
@@ -1756,7 +2099,15 @@ class TestTransformHandler:
filter_transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[Condition(column_id="A", operator=">=", value=2)],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition", column_id="A", operator=">=", value=2
+ )
+ ],
+ ),
)
transformations = Transformations([sort_transform, filter_transform])
# Verify the next transformation
@@ -1785,7 +2136,15 @@ class TestTransformHandler:
filter_again_transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="remove_rows",
- where=[Condition(column_id="B", operator="==", value=5)],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition", column_id="B", operator="==", value=5
+ )
+ ],
+ ),
)
transformations = Transformations(
[sort_transform, filter_transform, filter_again_transform]
@@ -1910,13 +2269,18 @@ class TestTransformHandler:
eq_transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[
- Condition(
- column_id="date",
- operator="==",
- value="2001-01-01",
- )
- ],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="date",
+ operator="==",
+ value="2001-01-01",
+ )
+ ],
+ ),
)
result = apply(df, eq_transform)
assert_frame_equal(result, expected)
@@ -1924,13 +2288,18 @@ class TestTransformHandler:
in_transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[
- Condition(
- column_id="date",
- operator="in",
- value=["2001-01-01"],
- )
- ],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="date",
+ operator="in",
+ value=["2001-01-01"],
+ )
+ ],
+ ),
)
result = apply(df, in_transform)
assert_frame_equal(result, expected)
@@ -1956,13 +2325,18 @@ class TestTransformHandler:
in_transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[
- Condition(
- column_id="nulls",
- operator="in",
- value=[NAN_VALUE],
- )
- ],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="nulls",
+ operator="in",
+ value=[NAN_VALUE],
+ )
+ ],
+ ),
)
result = apply(df, in_transform)
assert_frame_equal_with_nans(result, expected)
@@ -1986,13 +2360,18 @@ class TestTransformHandler:
in_transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[
- Condition(
- column_id="nulls",
- operator="in",
- value=[None],
- )
- ],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="nulls",
+ operator="in",
+ value=[None],
+ )
+ ],
+ ),
)
result = apply(df, in_transform)
assert_frame_equal_with_nans(result, expected)
@@ -2020,13 +2399,18 @@ class TestTransformHandler:
in_transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[
- Condition(
- column_id="nulls",
- operator="in",
- value=[NAN_VALUE],
- )
- ],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="nulls",
+ operator="in",
+ value=[NAN_VALUE],
+ )
+ ],
+ ),
)
result = apply(df, in_transform)
assert_frame_equal_with_nans(result, expected)
@@ -2051,13 +2435,18 @@ class TestTransformHandler:
in_transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[
- Condition(
- column_id="nulls",
- operator="in",
- value=[POSITIVE_INF],
- )
- ],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="nulls",
+ operator="in",
+ value=[POSITIVE_INF],
+ )
+ ],
+ ),
)
result = apply(df, in_transform)
assert_frame_equal_with_nans(result, expected)
@@ -2082,13 +2471,18 @@ class TestTransformHandler:
in_transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[
- Condition(
- column_id="nulls",
- operator="in",
- value=[NEGATIVE_INF],
- )
- ],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="nulls",
+ operator="in",
+ value=[NEGATIVE_INF],
+ )
+ ],
+ ),
)
result = apply(df, in_transform)
assert_frame_equal_with_nans(result, expected)
@@ -2124,13 +2518,18 @@ class TestTransformHandler:
in_transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[
- Condition(
- column_id="nulls",
- operator="in",
- value=[NAN_VALUE, POSITIVE_INF, None],
- )
- ],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="nulls",
+ operator="in",
+ value=[NAN_VALUE, POSITIVE_INF, None],
+ )
+ ],
+ ),
)
result = apply(df, in_transform)
assert_frame_equal_with_nans(result, expected)
@@ -2167,13 +2566,615 @@ class TestTransformHandler:
in_transform = FilterRowsTransform(
type=TransformType.FILTER_ROWS,
operation="keep_rows",
- where=[
- Condition(
- column_id="nulls",
- operator="in",
- value=[NAN_VALUE, POSITIVE_INF, None],
- )
- ],
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="nulls",
+ operator="in",
+ value=[NAN_VALUE, POSITIVE_INF, None],
+ )
+ ],
+ ),
)
result = apply(df, in_transform)
assert_frame_equal_with_nans(result, expected)
+
+ # --- between operator ---
+
+ @staticmethod
+ @pytest.mark.parametrize(
+ ("df", "expected"),
+ list(
+ zip(
+ create_test_dataframes({"A": [1, 2, 3, 4, 5]}),
+ create_test_dataframes({"A": [2, 3, 4]}),
+ strict=True,
+ )
+ ),
+ )
+ def test_filter_between_int(
+ df: DataFrameType, expected: DataFrameType
+ ) -> None:
+ transform = FilterRowsTransform(
+ type=TransformType.FILTER_ROWS,
+ operation="keep_rows",
+ where=FilterGroup(
+ children=[
+ FilterCondition(
+ column_id="A",
+ operator="between",
+ value=RangeValue(min=2, max=4),
+ )
+ ]
+ ),
+ )
+ result = apply(df, transform)
+ assert_frame_equal(result, expected)
+
+ @staticmethod
+ @pytest.mark.parametrize(
+ ("df", "expected"),
+ list(
+ zip(
+ create_test_dataframes({"A": [1.0, 2.5, 3.0, 4.5, 5.0]}),
+ create_test_dataframes({"A": [2.5, 3.0]}),
+ strict=True,
+ )
+ ),
+ )
+ def test_filter_between_float(
+ df: DataFrameType, expected: DataFrameType
+ ) -> None:
+ transform = FilterRowsTransform(
+ type=TransformType.FILTER_ROWS,
+ operation="keep_rows",
+ where=FilterGroup(
+ children=[
+ FilterCondition(
+ column_id="A",
+ operator="between",
+ value=RangeValue(min=2.0, max=3.5),
+ )
+ ]
+ ),
+ )
+ result = apply(df, transform)
+ assert_frame_equal(result, expected)
+
+ @staticmethod
+ @pytest.mark.parametrize(
+ ("df", "expected"),
+ list(
+ zip(
+ create_test_dataframes(
+ {"A": [1, 2, None, 4, 5]}, strict=False
+ ),
+ create_test_dataframes({"A": [2, 4]}, strict=False),
+ strict=True,
+ )
+ ),
+ )
+ def test_filter_between_with_nulls(
+ df: DataFrameType, expected: DataFrameType
+ ) -> None:
+ transform = FilterRowsTransform(
+ type=TransformType.FILTER_ROWS,
+ operation="keep_rows",
+ where=FilterGroup(
+ children=[
+ FilterCondition(
+ column_id="A",
+ operator="between",
+ value=RangeValue(min=2, max=4),
+ )
+ ]
+ ),
+ )
+ result = apply(df, transform)
+ assert_frame_equal(result, expected)
+
+ @staticmethod
+ @pytest.mark.parametrize(
+ ("df", "expected"),
+ list(
+ zip(
+ create_test_dataframes({"A": [1, 2, 3, 4, 5]}),
+ create_test_dataframes({"A": [1, 5]}),
+ strict=True,
+ )
+ ),
+ )
+ def test_filter_between_negate(
+ df: DataFrameType, expected: DataFrameType
+ ) -> None:
+ transform = FilterRowsTransform(
+ type=TransformType.FILTER_ROWS,
+ operation="keep_rows",
+ where=FilterGroup(
+ children=[
+ FilterCondition(
+ column_id="A",
+ operator="between",
+ value=RangeValue(min=2, max=4),
+ negate=True,
+ )
+ ]
+ ),
+ )
+ result = apply(df, transform)
+ assert_frame_equal(result, expected)
+
+ @staticmethod
+ @pytest.mark.parametrize(
+ ("df", "expected"),
+ list(
+ zip(
+ create_test_dataframes({"A": [1, 2, 3, 4, 5]}),
+ create_test_dataframes({"A": [3]}),
+ strict=True,
+ )
+ ),
+ )
+ def test_filter_between_min_equals_max(
+ df: DataFrameType, expected: DataFrameType
+ ) -> None:
+ transform = FilterRowsTransform(
+ type=TransformType.FILTER_ROWS,
+ operation="keep_rows",
+ where=FilterGroup(
+ children=[
+ FilterCondition(
+ column_id="A",
+ operator="between",
+ value=RangeValue(min=3, max=3),
+ )
+ ]
+ ),
+ )
+ result = apply(df, transform)
+ assert_frame_equal(result, expected)
+
+ @staticmethod
+ @pytest.mark.parametrize(
+ "df",
+ create_test_dataframes({"A": [1, 2, 3, 4, 5]}),
+ )
+ def test_filter_between_min_gt_max_empty(df: DataFrameType) -> None:
+ transform = FilterRowsTransform(
+ type=TransformType.FILTER_ROWS,
+ operation="keep_rows",
+ where=FilterGroup(
+ children=[
+ FilterCondition(
+ column_id="A",
+ operator="between",
+ value=RangeValue(min=5, max=2),
+ )
+ ]
+ ),
+ )
+ result = apply(df, transform)
+ assert df_size(result) == 0
+
+ # --- is_empty operator ---
+
+ @staticmethod
+ def test_filter_is_empty() -> None:
+ df = pd.DataFrame({"A": ["foo", "", None, "bar", ""]})
+ transform = FilterRowsTransform(
+ type=TransformType.FILTER_ROWS,
+ operation="keep_rows",
+ where=FilterGroup(
+ children=[FilterCondition(column_id="A", operator="is_empty")]
+ ),
+ )
+ result = apply(df, transform)
+ assert_frame_equal(result, pd.DataFrame({"A": ["", ""]}))
+
+ @staticmethod
+ def test_filter_is_empty_no_nulls_matched() -> None:
+ df = pd.DataFrame({"A": [None, None, None]})
+ transform = FilterRowsTransform(
+ type=TransformType.FILTER_ROWS,
+ operation="keep_rows",
+ where=FilterGroup(
+ children=[FilterCondition(column_id="A", operator="is_empty")]
+ ),
+ )
+ result = apply(df, transform)
+ assert df_size(result) == 0
+
+ @staticmethod
+ def test_filter_is_empty_negate() -> None:
+ df = pd.DataFrame({"A": ["foo", "", None, "bar", ""]})
+ transform = FilterRowsTransform(
+ type=TransformType.FILTER_ROWS,
+ operation="keep_rows",
+ where=FilterGroup(
+ children=[
+ FilterCondition(
+ column_id="A", operator="is_empty", negate=True
+ )
+ ]
+ ),
+ )
+ result = apply(df, transform)
+ assert_frame_equal(result, pd.DataFrame({"A": ["foo", None, "bar"]}))
+
+ # --- FilterGroup with OR ---
+
+ @staticmethod
+ @pytest.mark.parametrize(
+ ("df", "expected"),
+ list(
+ zip(
+ create_test_dataframes({"A": [1, 2, 3, 4, 5]}),
+ create_test_dataframes({"A": [1, 4, 5]}),
+ strict=True,
+ )
+ ),
+ )
+ def test_filter_group_or(
+ df: DataFrameType, expected: DataFrameType
+ ) -> None:
+ transform = FilterRowsTransform(
+ type=TransformType.FILTER_ROWS,
+ operation="keep_rows",
+ where=FilterGroup(
+ operator="or",
+ children=[
+ FilterCondition(column_id="A", operator="==", value=1),
+ FilterCondition(column_id="A", operator=">=", value=4),
+ ],
+ ),
+ )
+ result = apply(df, transform)
+ assert_frame_equal(result, expected)
+
+ @staticmethod
+ @pytest.mark.parametrize(
+ ("df", "expected"),
+ list(
+ zip(
+ create_test_dataframes({"A": [1, 2, 3], "B": ["x", "y", "z"]}),
+ create_test_dataframes({"A": [1, 3], "B": ["x", "z"]}),
+ strict=True,
+ )
+ ),
+ )
+ def test_filter_group_or_different_columns(
+ df: DataFrameType, expected: DataFrameType
+ ) -> None:
+ transform = FilterRowsTransform(
+ type=TransformType.FILTER_ROWS,
+ operation="keep_rows",
+ where=FilterGroup(
+ operator="or",
+ children=[
+ FilterCondition(column_id="A", operator="==", value=1),
+ FilterCondition(
+ column_id="B", operator="equals", value="z"
+ ),
+ ],
+ ),
+ )
+ result = apply(df, transform)
+ assert_frame_equal(result, expected)
+
+ # --- Nested FilterGroups ---
+
+ @staticmethod
+ @pytest.mark.parametrize(
+ ("df", "expected"),
+ list(
+ zip(
+ create_test_dataframes(
+ {"A": [1, 2, 3, 4, 5], "B": [10, 20, 30, 40, 50]}
+ ),
+ create_test_dataframes({"A": [2, 3, 5], "B": [20, 30, 50]}),
+ strict=True,
+ )
+ ),
+ )
+ def test_filter_nested_and_or(
+ df: DataFrameType, expected: DataFrameType
+ ) -> None:
+ # (A >= 2 AND A <= 3) OR (B == 50)
+ transform = FilterRowsTransform(
+ type=TransformType.FILTER_ROWS,
+ operation="keep_rows",
+ where=FilterGroup(
+ operator="or",
+ children=[
+ FilterGroup(
+ operator="and",
+ children=[
+ FilterCondition(
+ column_id="A", operator=">=", value=2
+ ),
+ FilterCondition(
+ column_id="A", operator="<=", value=3
+ ),
+ ],
+ ),
+ FilterCondition(column_id="B", operator="==", value=50),
+ ],
+ ),
+ )
+ result = apply(df, transform)
+ assert_frame_equal(result, expected)
+
+ @staticmethod
+ @pytest.mark.parametrize(
+ ("df", "expected"),
+ list(
+ zip(
+ create_test_dataframes(
+ {"A": [1, 2, 3, 4, 5], "B": [10, 20, 30, 40, 50]}
+ ),
+ create_test_dataframes({"A": [2, 4], "B": [20, 40]}),
+ strict=True,
+ )
+ ),
+ )
+ def test_filter_nested_or_and(
+ df: DataFrameType, expected: DataFrameType
+ ) -> None:
+ # (A == 2 OR A == 4) AND (B >= 20 OR B <= 40)
+ transform = FilterRowsTransform(
+ type=TransformType.FILTER_ROWS,
+ operation="keep_rows",
+ where=FilterGroup(
+ operator="and",
+ children=[
+ FilterGroup(
+ operator="or",
+ children=[
+ FilterCondition(
+ column_id="A", operator="==", value=2
+ ),
+ FilterCondition(
+ column_id="A", operator="==", value=4
+ ),
+ ],
+ ),
+ FilterGroup(
+ operator="or",
+ children=[
+ FilterCondition(
+ column_id="B", operator=">=", value=20
+ ),
+ FilterCondition(
+ column_id="B", operator="<=", value=40
+ ),
+ ],
+ ),
+ ],
+ ),
+ )
+ result = apply(df, transform)
+ assert_frame_equal(result, expected)
+
+ # --- Negate on condition ---
+
+ @staticmethod
+ @pytest.mark.parametrize(
+ ("df", "expected"),
+ list(
+ zip(
+ create_test_dataframes({"A": [1, 2, 3]}),
+ create_test_dataframes({"A": [1, 3]}),
+ strict=True,
+ )
+ ),
+ )
+ def test_negate_equals(df: DataFrameType, expected: DataFrameType) -> None:
+ transform = FilterRowsTransform(
+ type=TransformType.FILTER_ROWS,
+ operation="keep_rows",
+ where=FilterGroup(
+ children=[
+ FilterCondition(
+ column_id="A", operator="==", value=2, negate=True
+ )
+ ]
+ ),
+ )
+ result = apply(df, transform)
+ assert_frame_equal(result, expected)
+
+ @staticmethod
+ @pytest.mark.parametrize(
+ ("df", "expected"),
+ list(
+ zip(
+ create_test_dataframes(
+ {"A": [1, 2, None, 4]},
+ strict=False,
+ exclude=["pyarrow", "ibis"],
+ ),
+ create_test_dataframes(
+ {"A": [1, 4]},
+ strict=False,
+ exclude=["pyarrow", "ibis"],
+ ),
+ strict=True,
+ )
+ ),
+ )
+ def test_negate_in_with_nulls(
+ df: DataFrameType, expected: DataFrameType
+ ) -> None:
+ transform = FilterRowsTransform(
+ type=TransformType.FILTER_ROWS,
+ operation="keep_rows",
+ where=FilterGroup(
+ children=[
+ FilterCondition(
+ column_id="A",
+ operator="in",
+ value=[2, None],
+ negate=True,
+ )
+ ]
+ ),
+ )
+ result = apply(df, transform)
+ assert_frame_equal(result, expected)
+
+ @staticmethod
+ def test_negate_contains() -> None:
+ df = pd.DataFrame({"A": ["foo", "bar", None, "foobar"]})
+ transform = FilterRowsTransform(
+ type=TransformType.FILTER_ROWS,
+ operation="keep_rows",
+ where=FilterGroup(
+ children=[
+ FilterCondition(
+ column_id="A",
+ operator="contains",
+ value="foo",
+ negate=True,
+ )
+ ]
+ ),
+ )
+ result = apply(df, transform)
+ assert_frame_equal(result, pd.DataFrame({"A": ["bar"]}))
+
+ @staticmethod
+ @pytest.mark.parametrize(
+ ("df", "expected"),
+ list(
+ zip(
+ create_test_dataframes(
+ {"A": [True, False, None]}, strict=False
+ ),
+ create_test_dataframes({"A": [False]}, strict=False),
+ strict=True,
+ )
+ ),
+ )
+ def test_negate_is_true(
+ df: DataFrameType, expected: DataFrameType
+ ) -> None:
+ transform = FilterRowsTransform(
+ type=TransformType.FILTER_ROWS,
+ operation="keep_rows",
+ where=FilterGroup(
+ children=[
+ FilterCondition(
+ column_id="A", operator="is_true", negate=True
+ )
+ ]
+ ),
+ )
+ result = apply(df, transform)
+ assert_frame_equal(result, expected)
+
+ @staticmethod
+ @pytest.mark.parametrize(
+ ("df", "expected"),
+ list(
+ zip(
+ create_test_dataframes({"A": [1, None, 3]}, strict=False),
+ create_test_dataframes({"A": [1, 3]}, strict=False),
+ strict=True,
+ )
+ ),
+ )
+ def test_negate_is_null(
+ df: DataFrameType, expected: DataFrameType
+ ) -> None:
+ transform = FilterRowsTransform(
+ type=TransformType.FILTER_ROWS,
+ operation="keep_rows",
+ where=FilterGroup(
+ children=[
+ FilterCondition(
+ column_id="A", operator="is_null", negate=True
+ )
+ ]
+ ),
+ )
+ result = apply(df, transform)
+ assert_frame_equal(result, expected)
+
+ # --- Group negate ---
+
+ @staticmethod
+ @pytest.mark.parametrize(
+ ("df", "expected"),
+ list(
+ zip(
+ create_test_dataframes({"A": [1, 2, 3, 4, 5]}),
+ create_test_dataframes({"A": [1, 4, 5]}),
+ strict=True,
+ )
+ ),
+ )
+ def test_group_negate(df: DataFrameType, expected: DataFrameType) -> None:
+ # NOT(A >= 2 AND A <= 3) -> A < 2 OR A > 3
+ transform = FilterRowsTransform(
+ type=TransformType.FILTER_ROWS,
+ operation="keep_rows",
+ where=FilterGroup(
+ operator="and",
+ negate=True,
+ children=[
+ FilterCondition(column_id="A", operator=">=", value=2),
+ FilterCondition(column_id="A", operator="<=", value=3),
+ ],
+ ),
+ )
+ result = apply(df, transform)
+ assert_frame_equal(result, expected)
+
+ @staticmethod
+ @pytest.mark.parametrize(
+ ("df", "expected"),
+ list(
+ zip(
+ create_test_dataframes({"A": [1, 2, 3, 4, 5]}),
+ create_test_dataframes({"A": [2, 3, 4]}),
+ strict=True,
+ )
+ ),
+ )
+ def test_group_negate_or(
+ df: DataFrameType, expected: DataFrameType
+ ) -> None:
+ # NOT(A <= 1 OR A >= 5) -> A > 1 AND A < 5
+ transform = FilterRowsTransform(
+ type=TransformType.FILTER_ROWS,
+ operation="keep_rows",
+ where=FilterGroup(
+ operator="or",
+ negate=True,
+ children=[
+ FilterCondition(column_id="A", operator="<=", value=1),
+ FilterCondition(column_id="A", operator=">=", value=5),
+ ],
+ ),
+ )
+ result = apply(df, transform)
+ assert_frame_equal(result, expected)
+
+ # --- Empty group ---
+
+ @staticmethod
+ @pytest.mark.parametrize(
+ "df",
+ create_test_dataframes({"A": [1, 2, 3]}),
+ )
+ def test_empty_group_returns_all(df: DataFrameType) -> None:
+ transform = FilterRowsTransform(
+ type=TransformType.FILTER_ROWS,
+ operation="keep_rows",
+ where=FilterGroup(children=()),
+ )
+ result = apply(df, transform)
+ assert df_size(result) == 3
diff --git a/tests/_plugins/ui/_impl/dataframes/test_print_code.py b/tests/_plugins/ui/_impl/dataframes/test_print_code.py
index 9362e1df74..0e1bf75808 100644
--- a/tests/_plugins/ui/_impl/dataframes/test_print_code.py
+++ b/tests/_plugins/ui/_impl/dataframes/test_print_code.py
@@ -25,9 +25,10 @@ from marimo._plugins.ui._impl.dataframes.transforms.print_code import (
from marimo._plugins.ui._impl.dataframes.transforms.types import (
AggregateTransform,
ColumnConversionTransform,
- Condition,
ExpandDictTransform,
ExplodeColumnsTransform,
+ FilterCondition,
+ FilterGroup,
FilterRowsTransform,
GroupByTransform,
PivotTransform,
@@ -102,7 +103,8 @@ def create_transform_strategy(
# Strategies for each condition type
comparison_condition_strategy = st.builds(
- Condition,
+ FilterCondition,
+ type=st.just("condition"),
column_id=comparison_column_id,
operator=st.sampled_from(["==", "!=", "<", ">", "<=", ">="]),
value=st.one_of(
@@ -111,14 +113,16 @@ def create_transform_strategy(
)
boolean_condition_strategy = st.builds(
- Condition,
+ FilterCondition,
+ type=st.just("condition"),
column_id=bool_column_id,
operator=st.sampled_from(["is_true", "is_false"]),
value=st.just(None),
)
string_condition_strategy = st.builds(
- Condition,
+ FilterCondition,
+ type=st.just("condition"),
column_id=string_column_id,
operator=st.sampled_from(
[
@@ -134,7 +138,8 @@ def create_transform_strategy(
)
list_condition_strategy = st.builds(
- Condition,
+ FilterCondition,
+ type=st.just("condition"),
column_id=list_column_id,
operator=st.sampled_from(["in", "not_in"]),
value=st.lists(st.one_of(st.text()), min_size=1),
@@ -170,11 +175,18 @@ def create_transform_strategy(
na_position=st.sampled_from(["first", "last"]),
)
+ filter_group_strategy = st.builds(
+ FilterGroup,
+ type=st.just("group"),
+ operator=st.just("and"),
+ children=st.lists(condition_strategy, min_size=1),
+ )
+
filter_rows_transform_strategy = st.builds(
FilterRowsTransform,
type=st.just(TransformType.FILTER_ROWS),
operation=st.sampled_from(["keep_rows", "remove_rows"]),
- where=st.lists(condition_strategy, min_size=1),
+ where=filter_group_strategy,
)
group_by_transform_strategy = st.builds(
@@ -375,7 +387,7 @@ def test_print_code_result_matches_actual_transform_pandas(
assume(
not any(
condition.column_id in {"dates", "times", "datetimes"}
- for condition in transform.where
+ for condition in transform.where.children
)
)
# Ignore groupby mean
@@ -558,7 +570,7 @@ def test_print_code_result_matches_actual_transform_polars(
assume(
not any(
condition.column_id in {"dates", "times", "datetimes"}
- for condition in transform.where
+ for condition in transform.where.children
)
)
# Only explode columns for lists
@@ -802,7 +814,7 @@ def test_print_code_result_matches_actual_transform_ibis(
assume(
not any(
condition.column_id in {"booleans"}
- for condition in transform.where
+ for condition in transform.where.children
)
)
# Skip column conversion with errors='ignore' - ibis coalesce has type precedence issues
diff --git a/tests/_plugins/ui/_impl/dataframes/test_transforms.py b/tests/_plugins/ui/_impl/dataframes/test_transforms.py
index 6e14158e47..1e310cb6e1 100644
--- a/tests/_plugins/ui/_impl/dataframes/test_transforms.py
+++ b/tests/_plugins/ui/_impl/dataframes/test_transforms.py
@@ -2,6 +2,7 @@
from __future__ import annotations
from marimo._plugins.ui._impl.dataframes.transforms.types import (
+ FilterCondition,
FilterRowsTransform,
Transformations,
)
@@ -14,22 +15,29 @@ def test_parse_transforms() -> None:
{
"type": "filter_rows",
"operation": "keep_rows",
- "where": [
- {
- "column_id": "student",
- "operator": "is_false",
- },
- {
- "column_id": "age",
- "operator": "<=",
- "value": 20,
- },
- {
- "column_id": "email",
- "operator": "ends_with",
- "value": "@gmail.com",
- },
- ],
+ "where": {
+ "type": "group",
+ "operator": "and",
+ "children": [
+ {
+ "type": "condition",
+ "column_id": "student",
+ "operator": "is_false",
+ },
+ {
+ "type": "condition",
+ "column_id": "age",
+ "operator": "<=",
+ "value": 20,
+ },
+ {
+ "type": "condition",
+ "column_id": "email",
+ "operator": "ends_with",
+ "value": "@gmail.com",
+ },
+ ],
+ },
},
]
}
@@ -44,13 +52,18 @@ def test_parse_transforms_with_in_operator() -> None:
{
"type": "filter_rows",
"operation": "keep_rows",
- "where": [
- {
- "column_id": "category",
- "operator": operator,
- "value": ["A", "B", "C"],
- },
- ],
+ "where": {
+ "type": "group",
+ "operator": "and",
+ "children": [
+ {
+ "type": "condition",
+ "column_id": "category",
+ "operator": operator,
+ "value": ["A", "B", "C"],
+ },
+ ],
+ },
},
]
}
@@ -59,7 +72,8 @@ def test_parse_transforms_with_in_operator() -> None:
value = create_transform(operator)
result = parse_raw(value, Transformations)
assert isinstance(result, Transformations)
- # Verify the value is converted to tuple for hashability
transform = result.transforms[0]
assert isinstance(transform, FilterRowsTransform)
- assert transform.where[0].value == ("A", "B", "C")
+ condition = transform.where.children[0]
+ assert isinstance(condition, FilterCondition)
+ assert condition.value == ("A", "B", "C")
diff --git a/tests/_plugins/ui/_impl/test_table.py b/tests/_plugins/ui/_impl/test_table.py
index 22ff32f6a0..81fcdb8c80 100644
--- a/tests/_plugins/ui/_impl/test_table.py
+++ b/tests/_plugins/ui/_impl/test_table.py
@@ -12,7 +12,10 @@ import pytest
from marimo._data.models import ValueCount
from marimo._dependencies.dependencies import DependencyManager
from marimo._plugins import ui
-from marimo._plugins.ui._impl.dataframes.transforms.types import Condition
+from marimo._plugins.ui._impl.dataframes.transforms.types import (
+ FilterCondition,
+ FilterGroup,
+)
from marimo._plugins.ui._impl.table import (
CHART_MAX_ROWS_STRING_VALUE_COUNTS,
DEFAULT_MAX_COLUMNS,
@@ -1117,7 +1120,18 @@ def test_table_with_filtered_columns(df: Any) -> None:
table = ui.table(df)
result = table._search(
SearchTableArgs(
- filters=[Condition(column_id="b", operator="contains", value="f")],
+ filters=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="b",
+ operator="contains",
+ value="f",
+ )
+ ],
+ ),
page_size=10,
page_number=0,
)
@@ -1641,7 +1655,15 @@ def test_selection_with_clamped_columns_and_filter(df: Any):
search_args = SearchTableArgs(
page_size=10,
page_number=0,
- filters=[Condition(column_id="age", operator=">=", value=30)],
+ filters=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition", column_id="age", operator=">=", value=30
+ )
+ ],
+ ),
)
response = table._search(search_args)
result_data = json.loads(response.data)
@@ -2357,7 +2379,15 @@ def test_max_columns_not_provided_with_filters(df: Any):
search_args = SearchTableArgs(
page_size=10,
page_number=0,
- filters=[Condition(column_id="col0", operator="==", value=1)],
+ filters=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition", column_id="col0", operator="==", value=1
+ )
+ ],
+ ),
max_columns=MAX_COLUMNS_NOT_PROVIDED,
)
response = table._search(search_args)
@@ -2369,7 +2399,15 @@ def test_max_columns_not_provided_with_filters(df: Any):
search_args = SearchTableArgs(
page_size=10,
page_number=0,
- filters=[Condition(column_id="col0", operator="==", value=1)],
+ filters=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition", column_id="col0", operator="==", value=1
+ )
+ ],
+ ),
max_columns=20,
)
response = table._search(search_args)
@@ -2381,7 +2419,15 @@ def test_max_columns_not_provided_with_filters(df: Any):
search_args = SearchTableArgs(
page_size=10,
page_number=0,
- filters=[Condition(column_id="col0", operator="==", value=1)],
+ filters=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition", column_id="col0", operator="==", value=1
+ )
+ ],
+ ),
max_columns=None,
)
response = table._search(search_args)
@@ -2405,16 +2451,30 @@ def test_filters_with_nonexistent_columns(df: Any):
search_args = SearchTableArgs(
page_size=10,
page_number=0,
- filters=[
- Condition(column_id="a", operator="==", value=1), # exists
- Condition(
- column_id="nonexistent", operator="==", value=10
- ), # doesn't exist
- Condition(column_id="b", operator=">=", value=4), # exists
- Condition(
- column_id="missing_col", operator="!=", value=0
- ), # doesn't exist
- ],
+ filters=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition", column_id="a", operator="==", value=1
+ ), # exists
+ FilterCondition(
+ type="condition",
+ column_id="nonexistent",
+ operator="==",
+ value=10,
+ ), # doesn't exist
+ FilterCondition(
+ type="condition", column_id="b", operator=">=", value=4
+ ), # exists
+ FilterCondition(
+ type="condition",
+ column_id="missing_col",
+ operator="!=",
+ value=0,
+ ), # doesn't exist
+ ],
+ ),
)
# Should not raise an error and should apply only the valid filters
@@ -2432,10 +2492,24 @@ def test_filters_with_nonexistent_columns(df: Any):
search_args_all_invalid = SearchTableArgs(
page_size=10,
page_number=0,
- filters=[
- Condition(column_id="nonexistent1", operator="==", value=1),
- Condition(column_id="nonexistent2", operator="!=", value=2),
- ],
+ filters=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="nonexistent1",
+ operator="==",
+ value=1,
+ ),
+ FilterCondition(
+ type="condition",
+ column_id="nonexistent2",
+ operator="!=",
+ value=2,
+ ),
+ ],
+ ),
)
response = table._search(search_args_all_invalid)