|
| 1 | +package metricsview |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + runtimev1 "github.com/rilldata/rill/proto/gen/rill/runtime/v1" |
| 7 | + "github.com/rilldata/rill/runtime/drivers" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +func TestArrayContainsCondition(t *testing.T) { |
| 12 | + mv := &runtimev1.MetricsViewSpec{ |
| 13 | + Table: "test_table", |
| 14 | + Database: "", |
| 15 | + Connector: "", |
| 16 | + Dimensions: []*runtimev1.MetricsViewSpec_Dimension{ |
| 17 | + {Name: "id", Column: "id"}, |
| 18 | + {Name: "tags", Column: "tags", Unnest: true}, |
| 19 | + {Name: "city", Column: "city"}, |
| 20 | + }, |
| 21 | + Measures: []*runtimev1.MetricsViewSpec_Measure{ |
| 22 | + {Name: "count", Expression: "count(*)", Type: runtimev1.MetricsViewSpec_MEASURE_TYPE_SIMPLE}, |
| 23 | + }, |
| 24 | + } |
| 25 | + sec := skipMetricsViewSecurity{} |
| 26 | + |
| 27 | + tests := []struct { |
| 28 | + name string |
| 29 | + dialect drivers.Dialect |
| 30 | + dims []Dimension |
| 31 | + where *Expression |
| 32 | + wantSQL string |
| 33 | + wantArgs []any |
| 34 | + }{ |
| 35 | + { |
| 36 | + name: "duckdb: in on unnest dim uses list_has_any", |
| 37 | + dialect: drivers.DialectDuckDB, |
| 38 | + where: &Expression{Condition: &Condition{ |
| 39 | + Operator: OperatorIn, |
| 40 | + Expressions: []*Expression{ |
| 41 | + {Name: "tags"}, |
| 42 | + {Value: []any{"a", "b", "c"}}, |
| 43 | + }, |
| 44 | + }}, |
| 45 | + wantSQL: `(list_has_any(("tags"), [?,?,?]))`, |
| 46 | + wantArgs: []any{"a", "b", "c"}, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "duckdb: nin on unnest dim uses NOT list_has_any", |
| 50 | + dialect: drivers.DialectDuckDB, |
| 51 | + where: &Expression{Condition: &Condition{ |
| 52 | + Operator: OperatorNin, |
| 53 | + Expressions: []*Expression{ |
| 54 | + {Name: "tags"}, |
| 55 | + {Value: []any{"a", "b"}}, |
| 56 | + }, |
| 57 | + }}, |
| 58 | + wantSQL: `(NOT list_has_any(("tags"), [?,?]))`, |
| 59 | + wantArgs: []any{"a", "b"}, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "duckdb: in on unnest dim with empty list", |
| 63 | + dialect: drivers.DialectDuckDB, |
| 64 | + where: &Expression{Condition: &Condition{ |
| 65 | + Operator: OperatorIn, |
| 66 | + Expressions: []*Expression{ |
| 67 | + {Name: "tags"}, |
| 68 | + {Value: []any{}}, |
| 69 | + }, |
| 70 | + }}, |
| 71 | + wantSQL: `FALSE`, |
| 72 | + wantArgs: nil, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "duckdb: nin on unnest dim with empty list", |
| 76 | + dialect: drivers.DialectDuckDB, |
| 77 | + where: &Expression{Condition: &Condition{ |
| 78 | + Operator: OperatorNin, |
| 79 | + Expressions: []*Expression{ |
| 80 | + {Name: "tags"}, |
| 81 | + {Value: []any{}}, |
| 82 | + }, |
| 83 | + }}, |
| 84 | + wantSQL: `TRUE`, |
| 85 | + wantArgs: nil, |
| 86 | + }, |
| 87 | + { |
| 88 | + name: "duckdb: in on unnest dim with null value in list", |
| 89 | + dialect: drivers.DialectDuckDB, |
| 90 | + where: &Expression{Condition: &Condition{ |
| 91 | + Operator: OperatorIn, |
| 92 | + Expressions: []*Expression{ |
| 93 | + {Name: "tags"}, |
| 94 | + {Value: []any{"a", nil, "b"}}, |
| 95 | + }, |
| 96 | + }}, |
| 97 | + wantSQL: `(list_has_any(("tags"), [?,?,?]))`, |
| 98 | + wantArgs: []any{"a", nil, "b"}, |
| 99 | + }, |
| 100 | + { |
| 101 | + name: "clickhouse: in on unnest dim uses hasAny", |
| 102 | + dialect: drivers.DialectClickHouse, |
| 103 | + where: &Expression{Condition: &Condition{ |
| 104 | + Operator: OperatorIn, |
| 105 | + Expressions: []*Expression{ |
| 106 | + {Name: "tags"}, |
| 107 | + {Value: []any{"a", "b"}}, |
| 108 | + }, |
| 109 | + }}, |
| 110 | + wantSQL: `(hasAny(("tags"), [?,?]))`, |
| 111 | + wantArgs: []any{"a", "b"}, |
| 112 | + }, |
| 113 | + { |
| 114 | + name: "clickhouse: nin on unnest dim uses NOT hasAny", |
| 115 | + dialect: drivers.DialectClickHouse, |
| 116 | + where: &Expression{Condition: &Condition{ |
| 117 | + Operator: OperatorNin, |
| 118 | + Expressions: []*Expression{ |
| 119 | + {Name: "tags"}, |
| 120 | + {Value: []any{"a", "b"}}, |
| 121 | + }, |
| 122 | + }}, |
| 123 | + wantSQL: `(NOT hasAny(("tags"), [?,?]))`, |
| 124 | + wantArgs: []any{"a", "b"}, |
| 125 | + }, |
| 126 | + { |
| 127 | + name: "clickhouse: in on unnest dim with null values", |
| 128 | + dialect: drivers.DialectClickHouse, |
| 129 | + where: &Expression{Condition: &Condition{ |
| 130 | + Operator: OperatorIn, |
| 131 | + Expressions: []*Expression{ |
| 132 | + {Name: "tags"}, |
| 133 | + {Value: []any{nil, "a"}}, |
| 134 | + }, |
| 135 | + }}, |
| 136 | + wantSQL: `(hasAny(("tags"), [?,?]))`, |
| 137 | + wantArgs: []any{nil, "a"}, |
| 138 | + }, |
| 139 | + { |
| 140 | + name: "duckdb: in on non-unnest dim uses normal IN", |
| 141 | + dialect: drivers.DialectDuckDB, |
| 142 | + where: &Expression{Condition: &Condition{ |
| 143 | + Operator: OperatorIn, |
| 144 | + Expressions: []*Expression{ |
| 145 | + {Name: "city"}, |
| 146 | + {Value: []any{"NYC", "LA"}}, |
| 147 | + }, |
| 148 | + }}, |
| 149 | + wantSQL: `(("city") IN (?,?))`, |
| 150 | + wantArgs: []any{"NYC", "LA"}, |
| 151 | + }, |
| 152 | + { |
| 153 | + name: "duckdb: nin on non-unnest dim uses normal NOT IN", |
| 154 | + dialect: drivers.DialectDuckDB, |
| 155 | + where: &Expression{Condition: &Condition{ |
| 156 | + Operator: OperatorNin, |
| 157 | + Expressions: []*Expression{ |
| 158 | + {Name: "city"}, |
| 159 | + {Value: []any{"NYC"}}, |
| 160 | + }, |
| 161 | + }}, |
| 162 | + wantSQL: `(("city") NOT IN (?) OR ("city") IS NULL)`, |
| 163 | + wantArgs: []any{"NYC"}, |
| 164 | + }, |
| 165 | + { |
| 166 | + name: "duckdb: in on unnest dim nested in AND", |
| 167 | + dialect: drivers.DialectDuckDB, |
| 168 | + where: &Expression{Condition: &Condition{ |
| 169 | + Operator: OperatorAnd, |
| 170 | + Expressions: []*Expression{ |
| 171 | + {Condition: &Condition{ |
| 172 | + Operator: OperatorIn, |
| 173 | + Expressions: []*Expression{ |
| 174 | + {Name: "tags"}, |
| 175 | + {Value: []any{"a", "b"}}, |
| 176 | + }, |
| 177 | + }}, |
| 178 | + {Condition: &Condition{ |
| 179 | + Operator: OperatorEq, |
| 180 | + Expressions: []*Expression{ |
| 181 | + {Name: "city"}, |
| 182 | + {Value: "NYC"}, |
| 183 | + }, |
| 184 | + }}, |
| 185 | + }, |
| 186 | + }}, |
| 187 | + wantSQL: `((list_has_any(("tags"), [?,?])) AND (("city") = ?))`, |
| 188 | + wantArgs: []any{"a", "b", "NYC"}, |
| 189 | + }, |
| 190 | + { |
| 191 | + name: "duckdb: in on unnest dim already in select falls back to normal IN", |
| 192 | + dialect: drivers.DialectDuckDB, |
| 193 | + dims: []Dimension{{Name: "tags"}}, |
| 194 | + where: &Expression{Condition: &Condition{ |
| 195 | + Operator: OperatorIn, |
| 196 | + Expressions: []*Expression{ |
| 197 | + {Name: "tags"}, |
| 198 | + {Value: []any{"a", "b"}}, |
| 199 | + }, |
| 200 | + }}, |
| 201 | + // When tags is in the query dimensions, it's already unnested via lateral join, so falls back to normal IN. |
| 202 | + // The AST qualifies the column with a table alias from the lateral unnest join. |
| 203 | + wantSQL: `(("t0"."tags") IN (?,?))`, |
| 204 | + wantArgs: []any{"a", "b"}, |
| 205 | + }, |
| 206 | + } |
| 207 | + |
| 208 | + for _, tt := range tests { |
| 209 | + t.Run(tt.name, func(t *testing.T) { |
| 210 | + qry := &Query{ |
| 211 | + MetricsView: "test", |
| 212 | + Dimensions: tt.dims, |
| 213 | + Measures: []Measure{{Name: "count"}}, |
| 214 | + Where: tt.where, |
| 215 | + } |
| 216 | + |
| 217 | + ast, err := NewAST(mv, sec, qry, tt.dialect) |
| 218 | + require.NoError(t, err) |
| 219 | + |
| 220 | + sql, args, err := ast.SQLForExpression(tt.where, nil, false, false) |
| 221 | + require.NoError(t, err) |
| 222 | + require.Equal(t, tt.wantSQL, sql) |
| 223 | + require.Equal(t, tt.wantArgs, args) |
| 224 | + }) |
| 225 | + } |
| 226 | +} |
0 commit comments