@@ -502,6 +502,71 @@ func TestCost(t *testing.T) {
502502 },
503503 wanted : CostEstimate {Min : 3 , Max : 3 },
504504 },
505+ {
506+ name : ".filter list literal" ,
507+ expr : `[1,2,3,4,5].filter(x, x % 2 == 0)` ,
508+ wanted : CostEstimate {Min : 41 , Max : 101 },
509+ },
510+ {
511+ name : ".map list literal" ,
512+ expr : `[1,2,3,4,5].map(x, x)` ,
513+ wanted : CostEstimate {Min : 86 , Max : 86 },
514+ },
515+ {
516+ name : ".map.filter list literal" ,
517+ expr : `[1,2,3,4,5].map(x, x).filter(x, x % 2 == 0)` ,
518+ wanted : CostEstimate {Min : 117 , Max : 177 },
519+ },
520+ {
521+ name : ".map.exists list literal" ,
522+ expr : `[1,2,3,4,5].map(x, x).exists(x, x == 5) == true` ,
523+ wanted : CostEstimate {Min : 108 , Max : 118 },
524+ },
525+ {
526+ name : ".map.map list literal" ,
527+ expr : `[1,2,3,4,5].map(x, x).map(x, x)` ,
528+ wanted : CostEstimate {Min : 162 , Max : 162 },
529+ },
530+ {
531+ name : ".map list literal selection" ,
532+ expr : `[1,2,3,4,5].map(x, x)[4]` ,
533+ wanted : CostEstimate {Min : 87 , Max : 87 },
534+ },
535+ {
536+ name : "nested array selection" ,
537+ expr : `[[1,2],[1,2],[1,2],[1,2],[1,2]][4]` ,
538+ wanted : CostEstimate {Min : 61 , Max : 61 },
539+ },
540+ {
541+ name : "nested array selection" ,
542+ expr : `{'a': [1,2], 'b': [1,2], 'c': [1,2], 'd': [1,2], 'e': [1,2]}.b` ,
543+ wanted : CostEstimate {Min : 81 , Max : 81 },
544+ },
545+ {
546+ // Estimated cost does not track the sizes of nested aggregate types
547+ // (lists, maps, ...) and so assumes a worst case cost when an
548+ // expression applies a comprehension to a nested aggregated type,
549+ // even if the size information is available.
550+ // TODO: This should be fixed.
551+ name : "comprehension on nested list" ,
552+ expr : `[1,2,3,4,5].map(x, [x, x]).all(y, y.all(y, y == 1))` ,
553+ wanted : CostEstimate {Min : 157 , Max : 18446744073709551615 },
554+ },
555+ {
556+ // Make sure we're accounting for not just the iteration range size,
557+ // but also the overall comprehension size. The chained map calls
558+ // will treat the result of one map as the iteration range of the other,
559+ // so they're planned in reverse; however, the `+` should verify that
560+ // the comprehension result has a size.
561+ name : "comprehension size" ,
562+ expr : `[1,2,3,4,5].map(x, x).map(x, x) + [1]` ,
563+ wanted : CostEstimate {Min : 173 , Max : 173 },
564+ },
565+ {
566+ name : "nested comprehension" ,
567+ expr : `[1,2,3].all(i, i in [1,2,3].map(j, j + j))` ,
568+ wanted : CostEstimate {Min : 20 , Max : 230 },
569+ },
505570 }
506571
507572 for _ , tc := range cases {
0 commit comments