| 1 | // Copyright 2022 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package checker |
| 16 | |
| 17 | import ( |
| 18 | "math" |
| 19 | "strings" |
| 20 | "testing" |
| 21 | |
| 22 | "github.com/google/cel-go/common" |
| 23 | "github.com/google/cel-go/common/containers" |
| 24 | "github.com/google/cel-go/common/decls" |
| 25 | "github.com/google/cel-go/common/overloads" |
| 26 | "github.com/google/cel-go/common/stdlib" |
| 27 | "github.com/google/cel-go/common/types" |
| 28 | "github.com/google/cel-go/parser" |
| 29 | |
| 30 | proto3pb "github.com/google/cel-go/test/proto3pb" |
| 31 | ) |
| 32 | |
| 33 | func TestCost(t *testing.T) { |
| 34 | allTypes := types.NewObjectType("google.expr.proto3.test.TestAllTypes") |
| 35 | allList := types.NewListType(allTypes) |
| 36 | intList := types.NewListType(types.IntType) |
| 37 | nestedList := types.NewListType(allList) |
| 38 | |
| 39 | allMap := types.NewMapType(types.StringType, allTypes) |
| 40 | nestedMap := types.NewMapType(types.StringType, allMap) |
| 41 | |
| 42 | zeroCost := CostEstimate{} |
| 43 | oneCost := FixedCostEstimate(1) |
| 44 | cases := []struct { |
| 45 | name string |
| 46 | expr string |
| 47 | vars []*decls.VariableDecl |
| 48 | hints map[string]uint64 |
| 49 | options []CostOption |
| 50 | wanted CostEstimate |
| 51 | }{ |
| 52 | { |
| 53 | name: "const", |
| 54 | expr: `"Hello World!"`, |
| 55 | wanted: zeroCost, |
| 56 | }, |
| 57 | { |
| 58 | name: "identity", |
| 59 | expr: `input`, |
| 60 | vars: []*decls.VariableDecl{decls.NewVariable("input", intList)}, |
| 61 | wanted: CostEstimate{Min: 1, Max: 1}, |
| 62 | }, |
| 63 | { |
| 64 | name: "select: map", |
| 65 | expr: `input['key']`, |
| 66 | vars: []*decls.VariableDecl{ |
| 67 | decls.NewVariable("input", types.NewMapType(types.StringType, types.StringType))}, |
| 68 | wanted: CostEstimate{Min: 2, Max: 2}, |
| 69 | }, |
| 70 | { |
| 71 | name: "select: field", |
| 72 | expr: `input.single_int32`, |
| 73 | vars: []*decls.VariableDecl{decls.NewVariable("input", allTypes)}, |
| 74 | wanted: CostEstimate{Min: 2, Max: 2}, |
| 75 | }, |
| 76 | { |
| 77 | name: "select: field test only no has() cost", |
| 78 | expr: `has(input.single_int32)`, |
| 79 | vars: []*decls.VariableDecl{decls.NewVariable("input", types.NewObjectType("google.expr.proto3.test.TestAllTypes"))}, |
| 80 | wanted: CostEstimate{Min: 1, Max: 1}, |
| 81 | options: []CostOption{PresenceTestHasCost(false)}, |
| 82 | }, |
| 83 | { |
| 84 | name: "select: field test only", |
| 85 | expr: `has(input.single_int32)`, |
| 86 | vars: []*decls.VariableDecl{decls.NewVariable("input", types.NewObjectType("google.expr.proto3.test.TestAllTypes"))}, |
| 87 | wanted: CostEstimate{Min: 2, Max: 2}, |
| 88 | }, |
| 89 | { |
| 90 | name: "select: non-proto field test has() cost", |
| 91 | expr: `has(input.testAttr.nestedAttr)`, |
| 92 | vars: []*decls.VariableDecl{decls.NewVariable("input", nestedMap)}, |
| 93 | wanted: CostEstimate{Min: 3, Max: 3}, |
| 94 | options: []CostOption{PresenceTestHasCost(true)}, |
| 95 | }, |
| 96 | { |
| 97 | name: "select: non-proto field test no has() cost", |
| 98 | expr: `has(input.testAttr.nestedAttr)`, |
| 99 | vars: []*decls.VariableDecl{decls.NewVariable("input", nestedMap)}, |
| 100 | wanted: CostEstimate{Min: 2, Max: 2}, |
| 101 | options: []CostOption{PresenceTestHasCost(false)}, |
| 102 | }, |
| 103 | { |
| 104 | name: "select: non-proto field test", |
| 105 | expr: `has(input.testAttr.nestedAttr)`, |
| 106 | vars: []*decls.VariableDecl{decls.NewVariable("input", nestedMap)}, |
| 107 | wanted: CostEstimate{Min: 3, Max: 3}, |
| 108 | }, |
| 109 | { |
| 110 | name: "estimated function call", |
| 111 | expr: `input.getFullYear()`, |
| 112 | vars: []*decls.VariableDecl{decls.NewVariable("input", types.TimestampType)}, |
| 113 | wanted: CostEstimate{Min: 8, Max: 8}, |
| 114 | }, |
| 115 | { |
| 116 | name: "create list", |
| 117 | expr: `[1, 2, 3]`, |
| 118 | wanted: CostEstimate{Min: 10, Max: 10}, |
| 119 | }, |
| 120 | { |
| 121 | name: "create struct", |
| 122 | expr: `google.expr.proto3.test.TestAllTypes{single_int32: 1, single_float: 3.14, single_string: 'str'}`, |
| 123 | wanted: CostEstimate{Min: 40, Max: 40}, |
| 124 | }, |
| 125 | { |
| 126 | name: "create map", |
| 127 | expr: `{"a": 1, "b": 2, "c": 3}`, |
| 128 | wanted: CostEstimate{Min: 30, Max: 30}, |
| 129 | }, |
| 130 | { |
| 131 | name: "all comprehension", |
| 132 | vars: []*decls.VariableDecl{decls.NewVariable("input", allList)}, |
| 133 | hints: map[string]uint64{"input": 100}, |
| 134 | expr: `input.all(x, true)`, |
| 135 | wanted: CostEstimate{Min: 2, Max: 302}, |
| 136 | }, |
| 137 | { |
| 138 | name: "nested all comprehension", |
| 139 | vars: []*decls.VariableDecl{decls.NewVariable("input", nestedList)}, |
| 140 | hints: map[string]uint64{"input": 50, "input.@items": 10}, |
| 141 | expr: `input.all(x, x.all(y, true))`, |
| 142 | wanted: CostEstimate{Min: 2, Max: 1752}, |
| 143 | }, |
| 144 | { |
| 145 | name: "all comprehension on literal", |
| 146 | expr: `[1, 2, 3].all(x, true)`, |
| 147 | wanted: CostEstimate{Min: 20, Max: 20}, |
| 148 | }, |
| 149 | { |
| 150 | name: "variable cost function", |
| 151 | vars: []*decls.VariableDecl{decls.NewVariable("input", types.StringType)}, |
| 152 | hints: map[string]uint64{"input": 500}, |
| 153 | expr: `input.matches('[0-9]')`, |
| 154 | wanted: CostEstimate{Min: 3, Max: 103}, |
| 155 | }, |
| 156 | { |
| 157 | name: "variable cost function with constant", |
| 158 | expr: `'123'.matches('[0-9]')`, |
| 159 | wanted: CostEstimate{Min: 2, Max: 2}, |
| 160 | }, |
| 161 | { |
| 162 | name: "or", |
| 163 | expr: `true || false`, |
| 164 | wanted: zeroCost, |
| 165 | }, |
| 166 | { |
| 167 | name: "or accumulated branch cost", |
| 168 | expr: `a || b || c || d`, |
| 169 | vars: []*decls.VariableDecl{ |
| 170 | decls.NewVariable("a", types.BoolType), |
| 171 | decls.NewVariable("b", types.BoolType), |
| 172 | decls.NewVariable("c", types.BoolType), |
| 173 | decls.NewVariable("d", types.BoolType), |
| 174 | }, |
| 175 | wanted: CostEstimate{Min: 1, Max: 4}, |
| 176 | }, |
| 177 | { |
| 178 | name: "and", |
| 179 | expr: `true && false`, |
| 180 | wanted: zeroCost, |
| 181 | }, |
| 182 | { |
| 183 | name: "and accumulated branch cost", |
| 184 | expr: `a && b && c && d`, |
| 185 | vars: []*decls.VariableDecl{ |
| 186 | decls.NewVariable("a", types.BoolType), |
| 187 | decls.NewVariable("b", types.BoolType), |
| 188 | decls.NewVariable("c", types.BoolType), |
| 189 | decls.NewVariable("d", types.BoolType), |
| 190 | }, |
| 191 | wanted: CostEstimate{Min: 1, Max: 4}, |
| 192 | }, |
| 193 | { |
| 194 | name: "lt", |
| 195 | expr: `1 < 2`, |
| 196 | wanted: oneCost, |
| 197 | }, |
| 198 | { |
| 199 | name: "lte", |
| 200 | expr: `1 <= 2`, |
| 201 | wanted: oneCost, |
| 202 | }, |
| 203 | { |
| 204 | name: "eq", |
| 205 | expr: `1 == 2`, |
| 206 | wanted: oneCost, |
| 207 | }, |
| 208 | { |
| 209 | name: "gt", |
| 210 | expr: `2 > 1`, |
| 211 | wanted: oneCost, |
| 212 | }, |
| 213 | { |
| 214 | name: "gte", |
| 215 | expr: `2 >= 1`, |
| 216 | wanted: oneCost, |
| 217 | }, |
| 218 | { |
| 219 | name: "in", |
| 220 | expr: `2 in [1, 2, 3]`, |
| 221 | wanted: CostEstimate{Min: 13, Max: 13}, |
| 222 | }, |
| 223 | { |
| 224 | name: "plus", |
| 225 | expr: `1 + 1`, |
| 226 | wanted: oneCost, |
| 227 | }, |
| 228 | { |
| 229 | name: "minus", |
| 230 | expr: `1 - 1`, |
| 231 | wanted: oneCost, |
| 232 | }, |
| 233 | { |
| 234 | name: "/", |
| 235 | expr: `1 / 1`, |
| 236 | wanted: oneCost, |
| 237 | }, |
| 238 | { |
| 239 | name: "/", |
| 240 | expr: `1 * 1`, |
| 241 | wanted: oneCost, |
| 242 | }, |
| 243 | { |
| 244 | name: "%", |
| 245 | expr: `1 % 1`, |
| 246 | wanted: oneCost, |
| 247 | }, |
| 248 | { |
| 249 | name: "ternary", |
| 250 | expr: `true ? 1 : 2`, |
| 251 | wanted: zeroCost, |
| 252 | }, |
| 253 | { |
| 254 | name: "string size", |
| 255 | expr: `size("123")`, |
| 256 | wanted: oneCost, |
| 257 | }, |
| 258 | { |
| 259 | name: "bytes size", |
| 260 | expr: `size(b"123")`, |
| 261 | wanted: oneCost, |
| 262 | }, |
| 263 | { |
| 264 | name: "bytes to string conversion", |
| 265 | vars: []*decls.VariableDecl{decls.NewVariable("input", types.BytesType)}, |
| 266 | hints: map[string]uint64{"input": 500}, |
| 267 | expr: `string(input)`, |
| 268 | wanted: CostEstimate{Min: 1, Max: 51}, |
| 269 | }, |
| 270 | { |
| 271 | name: "bytes to string conversion equality", |
| 272 | vars: []*decls.VariableDecl{decls.NewVariable("input", types.BytesType)}, |
| 273 | hints: map[string]uint64{"input": 500}, |
| 274 | // equality check ensures that the resultSize calculation is included in cost |
| 275 | expr: `string(input) == string(input)`, |
| 276 | wanted: CostEstimate{Min: 3, Max: 152}, |
| 277 | }, |
| 278 | { |
| 279 | name: "string to bytes conversion", |
| 280 | vars: []*decls.VariableDecl{decls.NewVariable("input", types.StringType)}, |
| 281 | hints: map[string]uint64{"input": 500}, |
| 282 | expr: `bytes(input)`, |
| 283 | wanted: CostEstimate{Min: 1, Max: 51}, |
| 284 | }, |
| 285 | { |
| 286 | name: "string to bytes conversion equality", |
| 287 | vars: []*decls.VariableDecl{decls.NewVariable("input", types.StringType)}, |
| 288 | hints: map[string]uint64{"input": 500}, |
| 289 | // equality check ensures that the resultSize calculation is included in cost |
| 290 | expr: `bytes(input) == bytes(input)`, |
| 291 | wanted: CostEstimate{Min: 3, Max: 302}, |
| 292 | }, |
| 293 | { |
| 294 | name: "int to string conversion", |
| 295 | expr: `string(1)`, |
| 296 | wanted: CostEstimate{Min: 1, Max: 1}, |
| 297 | }, |
| 298 | { |
| 299 | name: "contains", |
| 300 | expr: `input.contains(arg1)`, |
| 301 | vars: []*decls.VariableDecl{ |
| 302 | decls.NewVariable("input", types.StringType), |
| 303 | decls.NewVariable("arg1", types.StringType), |
| 304 | }, |
| 305 | hints: map[string]uint64{"input": 500, "arg1": 500}, |
| 306 | wanted: CostEstimate{Min: 2, Max: 2502}, |
| 307 | }, |
| 308 | { |
| 309 | name: "matches", |
| 310 | expr: `input.matches('\\d+a\\d+b')`, |
| 311 | vars: []*decls.VariableDecl{ |
| 312 | decls.NewVariable("input", types.StringType), |
| 313 | }, |
| 314 | hints: map[string]uint64{"input": 500}, |
| 315 | wanted: CostEstimate{Min: 3, Max: 103}, |
| 316 | }, |
| 317 | { |
| 318 | name: "startsWith", |
| 319 | expr: `input.startsWith(arg1)`, |
| 320 | vars: []*decls.VariableDecl{ |
| 321 | decls.NewVariable("input", types.StringType), |
| 322 | decls.NewVariable("arg1", types.StringType), |
| 323 | }, |
| 324 | hints: map[string]uint64{"arg1": 500}, |
| 325 | wanted: CostEstimate{Min: 2, Max: 52}, |
| 326 | }, |
| 327 | { |
| 328 | name: "endsWith", |
| 329 | expr: `input.endsWith(arg1)`, |
| 330 | vars: []*decls.VariableDecl{ |
| 331 | decls.NewVariable("input", types.StringType), |
| 332 | decls.NewVariable("arg1", types.StringType), |
| 333 | }, |
| 334 | hints: map[string]uint64{"arg1": 500}, |
| 335 | wanted: CostEstimate{Min: 2, Max: 52}, |
| 336 | }, |
| 337 | { |
| 338 | name: "size receiver", |
| 339 | expr: `input.size()`, |
| 340 | vars: []*decls.VariableDecl{ |
| 341 | decls.NewVariable("input", types.StringType), |
| 342 | }, |
| 343 | wanted: CostEstimate{Min: 2, Max: 2}, |
| 344 | }, |
| 345 | { |
| 346 | name: "size", |
| 347 | expr: `size(input)`, |
| 348 | vars: []*decls.VariableDecl{ |
| 349 | decls.NewVariable("input", types.StringType), |
| 350 | }, |
| 351 | wanted: CostEstimate{Min: 2, Max: 2}, |
| 352 | }, |
| 353 | { |
| 354 | name: "ternary eval", |
| 355 | expr: `(x > 2 ? input1 : input2).all(y, true)`, |
| 356 | vars: []*decls.VariableDecl{ |
| 357 | decls.NewVariable("x", types.IntType), |
| 358 | decls.NewVariable("input1", allList), |
| 359 | decls.NewVariable("input2", allList), |
| 360 | }, |
| 361 | hints: map[string]uint64{"input1": 1, "input2": 1}, |
| 362 | wanted: CostEstimate{Min: 4, Max: 7}, |
| 363 | }, |
| 364 | { |
| 365 | name: "comprehension over map", |
| 366 | expr: `input.all(k, input[k].single_int32 > 3)`, |
| 367 | vars: []*decls.VariableDecl{ |
| 368 | decls.NewVariable("input", allMap), |
| 369 | }, |
| 370 | hints: map[string]uint64{"input": 10}, |
| 371 | wanted: CostEstimate{Min: 2, Max: 82}, |
| 372 | }, |
| 373 | { |
| 374 | name: "comprehension over nested map of maps", |
| 375 | expr: `input.all(k, input[k].all(x, true))`, |
| 376 | vars: []*decls.VariableDecl{ |
| 377 | decls.NewVariable("input", nestedMap), |
| 378 | }, |
| 379 | hints: map[string]uint64{"input": 5, "input.@values": 10}, |
| 380 | wanted: CostEstimate{Min: 2, Max: 187}, |
| 381 | }, |
| 382 | { |
| 383 | name: "string size of map keys", |
| 384 | expr: `input.all(k, k.contains(k))`, |
| 385 | vars: []*decls.VariableDecl{ |
| 386 | decls.NewVariable("input", nestedMap), |
| 387 | }, |
| 388 | hints: map[string]uint64{"input": 5, "input.@keys": 10}, |
| 389 | wanted: CostEstimate{Min: 2, Max: 32}, |
| 390 | }, |
| 391 | { |
| 392 | name: "comprehension variable shadowing", |
| 393 | expr: `input.all(k, input[k].all(k, true) && k.contains(k))`, |
| 394 | vars: []*decls.VariableDecl{ |
| 395 | decls.NewVariable("input", nestedMap), |
| 396 | }, |
| 397 | hints: map[string]uint64{"input": 2, "input.@values": 2, "input.@keys": 5}, |
| 398 | wanted: CostEstimate{Min: 2, Max: 34}, |
| 399 | }, |
| 400 | { |
| 401 | name: "comprehension variable shadowing", |
| 402 | expr: `input.all(k, input[k].all(k, true) && k.contains(k))`, |
| 403 | vars: []*decls.VariableDecl{ |
| 404 | decls.NewVariable("input", nestedMap), |
| 405 | }, |
| 406 | hints: map[string]uint64{"input": 2, "input.@values": 2, "input.@keys": 5}, |
| 407 | wanted: CostEstimate{Min: 2, Max: 34}, |
| 408 | }, |
| 409 | { |
| 410 | name: "list concat", |
| 411 | expr: `(list1 + list2).all(x, true)`, |
| 412 | vars: []*decls.VariableDecl{ |
| 413 | decls.NewVariable("list1", types.NewListType(types.IntType)), |
| 414 | decls.NewVariable("list2", types.NewListType(types.IntType)), |
| 415 | }, |
| 416 | hints: map[string]uint64{"list1": 10, "list2": 10}, |
| 417 | wanted: CostEstimate{Min: 4, Max: 64}, |
| 418 | }, |
| 419 | { |
| 420 | name: "str concat", |
| 421 | expr: `"abcdefg".contains(str1 + str2)`, |
| 422 | vars: []*decls.VariableDecl{ |
| 423 | decls.NewVariable("str1", types.StringType), |
| 424 | decls.NewVariable("str2", types.StringType), |
| 425 | }, |
| 426 | hints: map[string]uint64{"str1": 10, "str2": 10}, |
| 427 | wanted: CostEstimate{Min: 2, Max: 6}, |
| 428 | }, |
| 429 | { |
| 430 | name: "str concat custom cost estimate", |
| 431 | expr: `"abcdefg".contains(str1 + str2)`, |
| 432 | vars: []*decls.VariableDecl{ |
| 433 | decls.NewVariable("str1", types.StringType), |
| 434 | decls.NewVariable("str2", types.StringType), |
| 435 | }, |
| 436 | hints: map[string]uint64{"str1": 10, "str2": 10}, |
| 437 | options: []CostOption{ |
| 438 | OverloadCostEstimate(overloads.ContainsString, |
| 439 | func(estimator CostEstimator, target *AstNode, args []AstNode) *CallEstimate { |
| 440 | if target != nil && len(args) == 1 { |
| 441 | strSize := estimateSize(estimator, *target).MultiplyByCostFactor(0.2) |
| 442 | subSize := estimateSize(estimator, args[0]).MultiplyByCostFactor(0.2) |
| 443 | return &CallEstimate{CostEstimate: strSize.Multiply(subSize)} |
| 444 | } |
| 445 | return nil |
| 446 | }), |
| 447 | }, |
| 448 | wanted: CostEstimate{Min: 2, Max: 12}, |
| 449 | }, |
| 450 | { |
| 451 | name: "list size comparison", |
| 452 | expr: `list1.size() == list2.size()`, |
| 453 | vars: []*decls.VariableDecl{ |
| 454 | decls.NewVariable("list1", types.NewListType(types.IntType)), |
| 455 | decls.NewVariable("list2", types.NewListType(types.IntType)), |
| 456 | }, |
| 457 | wanted: CostEstimate{Min: 5, Max: 5}, |
| 458 | }, |
| 459 | { |
| 460 | name: "list size from ternary", |
| 461 | expr: `x > y ? list1.size() : list2.size()`, |
| 462 | vars: []*decls.VariableDecl{ |
| 463 | decls.NewVariable("x", types.IntType), |
| 464 | decls.NewVariable("y", types.IntType), |
| 465 | decls.NewVariable("list1", types.NewListType(types.IntType)), |
| 466 | decls.NewVariable("list2", types.NewListType(types.IntType)), |
| 467 | }, |
| 468 | wanted: CostEstimate{Min: 5, Max: 5}, |
| 469 | }, |
| 470 | { |
| 471 | name: "list size from concat", |
| 472 | expr: `([x, y] + list1 + list2).size()`, |
| 473 | vars: []*decls.VariableDecl{ |
| 474 | decls.NewVariable("x", types.IntType), |
| 475 | decls.NewVariable("y", types.IntType), |
| 476 | decls.NewVariable("list1", types.NewListType(types.IntType)), |
| 477 | decls.NewVariable("list2", types.NewListType(types.IntType)), |
| 478 | }, |
| 479 | hints: map[string]uint64{ |
| 480 | "list1": 10, |
| 481 | "list2": 20, |
| 482 | }, |
| 483 | wanted: CostEstimate{Min: 17, Max: 17}, |
| 484 | }, |
| 485 | { |
| 486 | name: "list cost tracking through comprehension", |
| 487 | expr: `[list1, list2].exists(l, l.exists(v, v.startsWith('hi')))`, |
| 488 | vars: []*decls.VariableDecl{ |
| 489 | decls.NewVariable("list1", types.NewListType(types.StringType)), |
| 490 | decls.NewVariable("list2", types.NewListType(types.StringType)), |
| 491 | }, |
| 492 | hints: map[string]uint64{ |
| 493 | "list1": 10, |
| 494 | "list1.@items": 64, |
| 495 | "list2": 20, |
| 496 | "list2.@items": 128, |
| 497 | }, |
| 498 | wanted: CostEstimate{Min: 21, Max: 265}, |
| 499 | }, |
| 500 | { |
| 501 | name: "str endsWith equality", |
| 502 | expr: `str1.endsWith("abcdefghijklmnopqrstuvwxyz") == str2.endsWith("abcdefghijklmnopqrstuvwxyz")`, |
| 503 | vars: []*decls.VariableDecl{ |
| 504 | decls.NewVariable("str1", types.StringType), |
| 505 | decls.NewVariable("str2", types.StringType), |
| 506 | }, |
| 507 | wanted: CostEstimate{Min: 9, Max: 9}, |
| 508 | }, |
| 509 | { |
| 510 | name: "nested subexpression operators", |
| 511 | expr: `((5 != 6) == (1 == 2)) == ((3 <= 4) == (9 != 9))`, |
| 512 | wanted: CostEstimate{Min: 7, Max: 7}, |
| 513 | }, |
| 514 | { |
| 515 | name: "str size estimate", |
| 516 | expr: `string(timestamp1) == string(timestamp2)`, |
| 517 | vars: []*decls.VariableDecl{ |
| 518 | decls.NewVariable("timestamp1", types.TimestampType), |
| 519 | decls.NewVariable("timestamp2", types.TimestampType), |
| 520 | }, |
| 521 | wanted: CostEstimate{Min: 5, Max: 1844674407370955268}, |
| 522 | }, |
| 523 | { |
| 524 | name: "timestamp equality check", |
| 525 | expr: `timestamp1 == timestamp2`, |
| 526 | vars: []*decls.VariableDecl{ |
| 527 | decls.NewVariable("timestamp1", types.TimestampType), |
| 528 | decls.NewVariable("timestamp2", types.TimestampType), |
| 529 | }, |
| 530 | wanted: CostEstimate{Min: 3, Max: 3}, |
| 531 | }, |
| 532 | { |
| 533 | name: "duration inequality check", |
| 534 | expr: `duration1 != duration2`, |
| 535 | vars: []*decls.VariableDecl{ |
| 536 | decls.NewVariable("duration1", types.DurationType), |
| 537 | decls.NewVariable("duration2", types.DurationType), |
| 538 | }, |
| 539 | wanted: CostEstimate{Min: 3, Max: 3}, |
| 540 | }, |
| 541 | { |
| 542 | name: ".filter list literal", |
| 543 | expr: `[1,2,3,4,5].filter(x, x % 2 == 0)`, |
| 544 | wanted: CostEstimate{Min: 41, Max: 101}, |
| 545 | }, |
| 546 | { |
| 547 | name: ".map list literal", |
| 548 | expr: `[1,2,3,4,5].map(x, x)`, |
| 549 | wanted: CostEstimate{Min: 86, Max: 86}, |
| 550 | }, |
| 551 | { |
| 552 | name: ".map.filter list literal", |
| 553 | expr: `[1,2,3,4,5].map(x, x).filter(x, x % 2 == 0)`, |
| 554 | wanted: CostEstimate{Min: 117, Max: 177}, |
| 555 | }, |
| 556 | { |
| 557 | name: ".map.exists list literal", |
| 558 | expr: `[1,2,3,4,5].map(x, x).exists(x, x == 5) == true`, |
| 559 | wanted: CostEstimate{Min: 108, Max: 118}, |
| 560 | }, |
| 561 | { |
| 562 | name: ".map.map list literal", |
| 563 | expr: `[1,2,3,4,5].map(x, x).map(x, x)`, |
| 564 | wanted: CostEstimate{Min: 162, Max: 162}, |
| 565 | }, |
| 566 | { |
| 567 | name: ".map list literal selection", |
| 568 | expr: `[1,2,3,4,5].map(x, x)[4]`, |
| 569 | wanted: CostEstimate{Min: 87, Max: 87}, |
| 570 | }, |
| 571 | { |
| 572 | name: "nested array selection", |
| 573 | expr: `[[1,2],[1,2],[1,2],[1,2],[1,2]][4]`, |
| 574 | wanted: CostEstimate{Min: 61, Max: 61}, |
| 575 | }, |
| 576 | { |
| 577 | name: "nested map selection", |
| 578 | expr: `{'a': [1,2], 'b': [1,2], 'c': [1,2], 'd': [1,2], 'e': [1,2]}.b`, |
| 579 | wanted: CostEstimate{Min: 81, Max: 81}, |
| 580 | }, |
| 581 | { |
| 582 | name: "comprehension on nested list", |
| 583 | expr: `[[1, 1], [2, 2], [3, 3], [4, 4], [5, 5]].all(y, y.all(y, y == 1))`, |
| 584 | wanted: CostEstimate{Min: 76, Max: 136}, |
| 585 | }, |
| 586 | { |
| 587 | name: "comprehension on transformed nested list", |
| 588 | expr: `[1,2,3,4,5].map(x, [x, x]).all(y, y.all(y, y == 1))`, |
| 589 | wanted: CostEstimate{Min: 157, Max: 217}, |
| 590 | }, |
| 591 | { |
| 592 | name: "comprehension on nested literal list", |
| 593 | expr: `["a", "ab", "abc", "abcd", "abcde"].map(x, [x, x]).all(y, y.all(y, y.startsWith('a')))`, |
| 594 | wanted: CostEstimate{Min: 157, Max: 217}, |
| 595 | }, |
| 596 | { |
| 597 | name: "comprehension on nested variable list", |
| 598 | expr: `input.map(x, [x, x]).all(y, y.all(y, y.startsWith('a')))`, |
| 599 | vars: []*decls.VariableDecl{decls.NewVariable("input", types.NewListType(types.StringType))}, |
| 600 | hints: map[string]uint64{ |
| 601 | "input": 5, |
| 602 | "input.@items": 10, |
| 603 | }, |
| 604 | wanted: CostEstimate{Min: 13, Max: 208}, |
| 605 | }, |
| 606 | { |
| 607 | name: "comprehension chaining with concat", |
| 608 | expr: `[1,2,3,4,5].map(x, x).map(x, x) + [1]`, |
| 609 | wanted: CostEstimate{Min: 173, Max: 173}, |
| 610 | }, |
| 611 | { |
| 612 | name: "nested comprehension", |
| 613 | expr: `[1,2,3].all(i, i in [1,2,3].map(j, j + j))`, |
| 614 | wanted: CostEstimate{Min: 20, Max: 230}, |
| 615 | }, |
| 616 | { |
| 617 | name: "nested dyn comprehension", |
| 618 | expr: `dyn([1,2,3]).all(i, i in dyn([1,2,3]).map(j, j + j))`, |
| 619 | wanted: CostEstimate{Min: 21, Max: 234}, |
| 620 | }, |
| 621 | { |
| 622 | name: "literal map access", |
| 623 | expr: `{'hello': 'hi'}['hello'] != {'hello': 'bye'}['hello']`, |
| 624 | wanted: CostEstimate{Min: 63, Max: 63}, |
| 625 | }, |
| 626 | { |
| 627 | name: "literal list access", |
| 628 | expr: `['hello', 'hi'][0] != ['hello', 'bye'][1]`, |
| 629 | wanted: CostEstimate{Min: 23, Max: 23}, |
| 630 | }, |
| 631 | { |
| 632 | name: "type call", |
| 633 | expr: `type(1)`, |
| 634 | wanted: CostEstimate{Min: 1, Max: 1}, |
| 635 | }, |
| 636 | { |
| 637 | name: "type call variable", |
| 638 | expr: `type(self.val1)`, |
| 639 | vars: []*decls.VariableDecl{ |
| 640 | decls.NewVariable("self", types.NewMapType(types.StringType, types.IntType)), |
| 641 | }, |
| 642 | wanted: CostEstimate{Min: 3, Max: 3}, |
| 643 | }, |
| 644 | { |
| 645 | name: "type call variable equality", |
| 646 | expr: `type(self.val1) == int`, |
| 647 | vars: []*decls.VariableDecl{ |
| 648 | decls.NewVariable("self", types.NewMapType(types.StringType, types.IntType)), |
| 649 | }, |
| 650 | wanted: CostEstimate{Min: 5, Max: 1844674407370955268}, |
| 651 | }, |
| 652 | { |
| 653 | name: "type literal equality cost", |
| 654 | expr: `type(1) == int`, |
| 655 | wanted: CostEstimate{Min: 3, Max: 1844674407370955266}, |
| 656 | }, |
| 657 | { |
| 658 | name: "type variable equality cost", |
| 659 | expr: `type(1) == int`, |
| 660 | wanted: CostEstimate{Min: 3, Max: 1844674407370955266}, |
| 661 | }, |
| 662 | { |
| 663 | name: "namespace variable equality", |
| 664 | expr: `self.val1 == 1.0`, |
| 665 | vars: []*decls.VariableDecl{ |
| 666 | decls.NewVariable("self.val1", types.DoubleType), |
| 667 | }, |
| 668 | wanted: CostEstimate{Min: 2, Max: 2}, |
| 669 | }, |
| 670 | { |
| 671 | name: "simple map variable equality", |
| 672 | expr: `self.val1 == 1.0`, |
| 673 | vars: []*decls.VariableDecl{ |
| 674 | decls.NewVariable("self", types.NewMapType(types.StringType, types.DoubleType)), |
| 675 | }, |
| 676 | wanted: CostEstimate{Min: 3, Max: 3}, |
| 677 | }, |
| 678 | { |
| 679 | name: "date-time math", |
| 680 | vars: []*decls.VariableDecl{ |
| 681 | decls.NewVariable("self", types.NewMapType(types.StringType, types.TimestampType)), |
| 682 | }, |
| 683 | expr: `self.val1 == timestamp('2011-08-18T00:00:00.000+01:00') + duration('19h3m37s10ms')`, |
| 684 | wanted: FixedCostEstimate(6), |
| 685 | }, |
| 686 | { |
| 687 | name: "date-time math self-conversion", |
| 688 | vars: []*decls.VariableDecl{ |
| 689 | decls.NewVariable("self", types.NewMapType(types.StringType, types.TimestampType)), |
| 690 | }, |
| 691 | expr: `timestamp(self.val1) == timestamp('2011-08-18T00:00:00.000+01:00') + duration('19h3m37s10ms')`, |
| 692 | wanted: FixedCostEstimate(7), |
| 693 | }, |
| 694 | { |
| 695 | name: "boolean vars equal", |
| 696 | vars: []*decls.VariableDecl{ |
| 697 | decls.NewVariable("self", types.NewMapType(types.StringType, types.BoolType)), |
| 698 | }, |
| 699 | expr: `self.val1 != self.val2`, |
| 700 | wanted: FixedCostEstimate(5), |
| 701 | }, |
| 702 | { |
| 703 | name: "boolean var equals literal", |
| 704 | vars: []*decls.VariableDecl{ |
| 705 | decls.NewVariable("self", types.NewMapType(types.StringType, types.BoolType)), |
| 706 | }, |
| 707 | expr: `self.val1 != true`, |
| 708 | wanted: FixedCostEstimate(3), |
| 709 | }, |
| 710 | { |
| 711 | name: "double var equals literal", |
| 712 | vars: []*decls.VariableDecl{ |
| 713 | decls.NewVariable("self", types.NewMapType(types.StringType, types.DoubleType)), |
| 714 | }, |
| 715 | expr: `self.val1 == 1.0`, |
| 716 | wanted: FixedCostEstimate(3), |
| 717 | }, |
| 718 | { |
| 719 | name: "bytes list max", |
| 720 | expr: "[bytes('012345678901'), bytes('012345678901'), bytes('012345678901'), bytes('012345678901'), bytes('012345678901')].max()", |
| 721 | options: []CostOption{ |
| 722 | OverloadCostEstimate("list_bytes_max", |
| 723 | func(estimator CostEstimator, target *AstNode, args []AstNode) *CallEstimate { |
| 724 | if target != nil { |
| 725 | // Charge 1 cost for comparing each element in the list |
| 726 | elCost := CostEstimate{Min: 1, Max: 1} |
| 727 | // If the list contains strings or bytes, add the cost of traversing all the strings/bytes as a way |
| 728 | // of estimating the additional comparison cost. |
| 729 | if elNode := listElementNode(*target); elNode != nil { |
| 730 | k := elNode.Type().Kind() |
| 731 | if k == types.StringKind || k == types.BytesKind { |
| 732 | sz := sizeEstimate(estimator, elNode) |
| 733 | elCost = elCost.Add(sz.MultiplyByCostFactor(common.StringTraversalCostFactor)) |
| 734 | } |
| 735 | return &CallEstimate{CostEstimate: sizeEstimate(estimator, *target).MultiplyByCost(elCost)} |
| 736 | } |
| 737 | } |
| 738 | return nil |
| 739 | }), |
| 740 | }, |
| 741 | wanted: CostEstimate{Min: 25, Max: 35}, |
| 742 | }, |
| 743 | } |
| 744 | |
| 745 | for _, tst := range cases { |
| 746 | tc := tst |
| 747 | t.Run(tc.name, func(t *testing.T) { |
| 748 | if tc.hints == nil { |
| 749 | tc.hints = map[string]uint64{} |
| 750 | } |
| 751 | p, err := parser.NewParser(parser.Macros(parser.AllMacros...)) |
| 752 | if err != nil { |
| 753 | t.Fatalf("parser.NewParser() failed: %v", err) |
| 754 | } |
| 755 | src := common.NewStringSource(tc.expr, "<input>") |
| 756 | pe, errs := p.Parse(src) |
| 757 | if len(errs.GetErrors()) != 0 { |
| 758 | t.Fatalf("parser.Parse(%v) failed: %v", tc.expr, errs.ToDisplayString()) |
| 759 | } |
| 760 | reg, err := types.NewRegistry(&proto3pb.TestAllTypes{}) |
| 761 | if err != nil { |
| 762 | t.Fatalf("types.NewRegistry(...) failed: %v", err) |
| 763 | } |
| 764 | |
| 765 | e, err := NewEnv(containers.DefaultContainer, reg) |
| 766 | if err != nil { |
| 767 | t.Fatalf("NewEnv() failed: %v", err) |
| 768 | } |
| 769 | err = e.AddFunctions(stdlib.Functions()...) |
| 770 | if err != nil { |
| 771 | t.Fatalf("environment creation error: %v", err) |
| 772 | } |
| 773 | maxFunc, _ := decls.NewFunction("max", |
| 774 | decls.MemberOverload("list_bytes_max", |
| 775 | []*types.Type{types.NewListType(types.BytesType)}, |
| 776 | types.BytesType)) |
| 777 | err = e.AddFunctions(maxFunc) |
| 778 | if err != nil { |
| 779 | t.Fatalf("environment creation error: %v", err) |
| 780 | } |
| 781 | err = e.AddIdents(tc.vars...) |
| 782 | if err != nil { |
| 783 | t.Fatalf("environment creation error: %s\n", err) |
| 784 | } |
| 785 | checked, errs := Check(pe, src, e) |
| 786 | if len(errs.GetErrors()) != 0 { |
| 787 | t.Fatalf("Check(%s) failed: %v", tc.expr, errs.ToDisplayString()) |
| 788 | } |
| 789 | est, err := Cost(checked, testCostEstimator{hints: tc.hints}, tc.options...) |
| 790 | if err != nil { |
| 791 | t.Fatalf("Cost() failed: %v", err) |
| 792 | } |
| 793 | if est.Min != tc.wanted.Min || est.Max != tc.wanted.Max { |
| 794 | t.Fatalf("Got cost interval [%v, %v], wanted [%v, %v]", |
| 795 | est.Min, est.Max, tc.wanted.Min, tc.wanted.Max) |
| 796 | } |
| 797 | }) |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | type testCostEstimator struct { |
| 802 | hints map[string]uint64 |
| 803 | } |
| 804 | |
| 805 | func (tc testCostEstimator) EstimateSize(element AstNode) *SizeEstimate { |
| 806 | if l, ok := tc.hints[strings.Join(element.Path(), ".")]; ok { |
| 807 | return &SizeEstimate{Min: 0, Max: l} |
| 808 | } |
| 809 | if element.Type() == types.BytesType { |
| 810 | return &SizeEstimate{Min: 0, Max: 12} |
| 811 | } |
| 812 | return nil |
| 813 | } |
| 814 | |
| 815 | func (tc testCostEstimator) EstimateCallCost(function, overloadID string, target *AstNode, args []AstNode) *CallEstimate { |
| 816 | switch overloadID { |
| 817 | case overloads.TimestampToYear: |
| 818 | return &CallEstimate{CostEstimate: CostEstimate{Min: 7, Max: 7}} |
| 819 | } |
| 820 | return nil |
| 821 | } |
| 822 | |
| 823 | func estimateSize(estimator CostEstimator, node AstNode) SizeEstimate { |
| 824 | if l := node.ComputedSize(); l != nil { |
| 825 | return *l |
| 826 | } |
| 827 | if l := estimator.EstimateSize(node); l != nil { |
| 828 | return *l |
| 829 | } |
| 830 | return SizeEstimate{Min: 0, Max: math.MaxUint64} |
| 831 | } |
| 832 | |
| 833 | func listElementNode(list AstNode) AstNode { |
| 834 | if params := list.Type().Parameters(); len(params) > 0 { |
| 835 | lt := params[0] |
| 836 | nodePath := list.Path() |
| 837 | if nodePath != nil { |
| 838 | // Provide path if we have it so that a OpenAPIv3 maxLength validation can be looked up, if it exists |
| 839 | // for this node. |
| 840 | path := make([]string, len(nodePath)+1) |
| 841 | copy(path, nodePath) |
| 842 | path[len(nodePath)] = "@items" |
| 843 | return &astNode{path: path, t: lt, expr: nil} |
| 844 | } else { |
| 845 | // Provide just the type if no path is available so that worst case size can be looked up based on type. |
| 846 | return &astNode{t: lt, expr: nil} |
| 847 | } |
| 848 | } |
| 849 | return nil |
| 850 | } |
| 851 | |
| 852 | func sizeEstimate(estimator CostEstimator, t AstNode) SizeEstimate { |
| 853 | if sz := t.ComputedSize(); sz != nil { |
| 854 | return *sz |
| 855 | } |
| 856 | if sz := estimator.EstimateSize(t); sz != nil { |
| 857 | return *sz |
| 858 | } |
| 859 | return SizeEstimate{Min: 0, Max: math.MaxUint64} |
| 860 | } |