-- Use metric engine to match real PromQL usage.
CREATE TABLE comparison_filter_or_physical (
    ts TIMESTAMP TIME INDEX,
    greptime_value DOUBLE
) ENGINE=metric WITH ("physical_metric_table" = "");

Affected Rows: 0

CREATE TABLE a (
    k STRING NULL,
    ts TIMESTAMP NOT NULL,
    greptime_value DOUBLE NULL,
    TIME INDEX (ts),
    PRIMARY KEY (k)
) ENGINE=metric WITH (on_physical_table = 'comparison_filter_or_physical');

Affected Rows: 0

CREATE TABLE b (
    k STRING NULL,
    ts TIMESTAMP NOT NULL,
    greptime_value DOUBLE NULL,
    TIME INDEX (ts),
    PRIMARY KEY (k)
) ENGINE=metric WITH (on_physical_table = 'comparison_filter_or_physical');

Affected Rows: 0

INSERT INTO a (ts, k, greptime_value)
VALUES
    (0, 'x', 3),
    (0, 'y', 1),
    (0, 'z', 5);

Affected Rows: 3

INSERT INTO b (ts, k, greptime_value)
VALUES
    (0, 'x', 1),
    (0, 'y', 2);

Affected Rows: 2

-- Regression: vector-vector comparison without `bool` is a filter that keeps LHS values.
-- It must not leak RHS columns (e.g. duplicated `ts`), otherwise set operators like `or`
-- can fail with ambiguous column references.
--
-- Also cover the common PromQL pattern `(A > B) or B or A`, which uses `or` to fill missing
-- series from RHS (e.g. B missing for some labels, then fallback to A).
-- SQLNESS SORT_RESULT 2 1
tql eval (0, 0, '1s') (a > b) or b or a;

+---------------------+----------------+---+
| ts                  | greptime_value | k |
+---------------------+----------------+---+
| 1970-01-01T00:00:00 | 2.0            | y |
| 1970-01-01T00:00:00 | 3.0            | x |
| 1970-01-01T00:00:00 | 5.0            | z |
+---------------------+----------------+---+

-- Regression: derived vector expressions may not have a concrete `ctx.table_name`.
-- Function planning (which filters empty values) must not require a table name when the
-- input plan is valid.
-- SQLNESS SORT_RESULT 2 1
tql eval (0, 0, '1s') abs(a > b);

+---------------------+---------------------+---+
| ts                  | abs(greptime_value) | k |
+---------------------+---------------------+---+
| 1970-01-01T00:00:00 | 3.0                 | x |
+---------------------+---------------------+---+

-- Regression: scalar-vector comparison (scalar on LHS) without `bool` is a filter that keeps
-- the vector side's samples/labels (not the scalar side).
CREATE TABLE m (
    k STRING NULL,
    ts TIMESTAMP NOT NULL,
    greptime_value DOUBLE NULL,
    TIME INDEX (ts),
    PRIMARY KEY (k)
) ENGINE=metric WITH (on_physical_table = 'comparison_filter_or_physical');

Affected Rows: 0

INSERT INTO m (ts, k, greptime_value)
VALUES
    (1000, 'x', 3),
    (1000, 'y', 0),
    (2000, 'x', 4),
    (2000, 'y', 0);

Affected Rows: 4

-- SQLNESS SORT_RESULT 3 1
tql eval (1, 2, '1s') time() < m;

+---------------------+----------------+---+
| ts                  | greptime_value | k |
+---------------------+----------------+---+
| 1970-01-01T00:00:01 | 3.0            | x |
| 1970-01-01T00:00:02 | 4.0            | x |
+---------------------+----------------+---+

drop table m;

Affected Rows: 0

drop table a;

Affected Rows: 0

drop table b;

Affected Rows: 0

drop table comparison_filter_or_physical;

Affected Rows: 0

