|
7 | 7 | "github.com/google/go-cmp/cmp"
|
8 | 8 |
|
9 | 9 | "github.com/influxdata/flux/planner"
|
| 10 | + "github.com/influxdata/flux/planner/plantest" |
10 | 11 | "github.com/influxdata/flux/values"
|
11 | 12 | )
|
12 | 13 |
|
@@ -246,3 +247,263 @@ func TestBounds_IsEmpty(t *testing.T) {
|
246 | 247 | })
|
247 | 248 | }
|
248 | 249 | }
|
| 250 | + |
| 251 | +// A BoundsAwareProcedureSpec that intersects its bounds with its predecessors' bounds |
| 252 | +type mockBoundsIntersectProcedureSpec struct { |
| 253 | + planner.DefaultCost |
| 254 | + bounds *planner.Bounds |
| 255 | +} |
| 256 | + |
| 257 | +func (m *mockBoundsIntersectProcedureSpec) Kind() planner.ProcedureKind { |
| 258 | + return "mock-intersect-bounds" |
| 259 | +} |
| 260 | + |
| 261 | +func (m *mockBoundsIntersectProcedureSpec) Copy() planner.ProcedureSpec { |
| 262 | + return &mockBoundsIntersectProcedureSpec{} |
| 263 | +} |
| 264 | + |
| 265 | +func (m *mockBoundsIntersectProcedureSpec) TimeBounds(predecessorBounds *planner.Bounds) *planner.Bounds { |
| 266 | + if predecessorBounds != nil { |
| 267 | + return predecessorBounds.Intersect(m.bounds) |
| 268 | + } |
| 269 | + return m.bounds |
| 270 | +} |
| 271 | + |
| 272 | +// A BoundsAwareProcedureSpec that shifts its predecessors' bounds |
| 273 | +type mockBoundsShiftProcedureSpec struct { |
| 274 | + planner.DefaultCost |
| 275 | + by values.Duration |
| 276 | +} |
| 277 | + |
| 278 | +func (m *mockBoundsShiftProcedureSpec) Kind() planner.ProcedureKind { |
| 279 | + return "mock-shift-bounds" |
| 280 | +} |
| 281 | + |
| 282 | +func (m *mockBoundsShiftProcedureSpec) Copy() planner.ProcedureSpec { |
| 283 | + return &mockBoundsShiftProcedureSpec{} |
| 284 | +} |
| 285 | + |
| 286 | +func (m *mockBoundsShiftProcedureSpec) TimeBounds(predecessorBounds *planner.Bounds) *planner.Bounds { |
| 287 | + if predecessorBounds != nil { |
| 288 | + return predecessorBounds.Shift(m.by) |
| 289 | + } |
| 290 | + return nil |
| 291 | +} |
| 292 | + |
| 293 | +// Create a PlanNode with id and mockBoundsIntersectProcedureSpec |
| 294 | +func makeBoundsNode(id string, bounds *planner.Bounds) planner.PlanNode { |
| 295 | + return planner.CreatePhysicalNode(planner.NodeID(id), |
| 296 | + &mockBoundsIntersectProcedureSpec{ |
| 297 | + bounds: bounds, |
| 298 | + }) |
| 299 | +} |
| 300 | + |
| 301 | +// Create a PlanNode with id and mockBoundsShiftProcedureSpec |
| 302 | +func makeShiftNode(id string, duration values.Duration) planner.PlanNode { |
| 303 | + return planner.CreateLogicalNode(planner.NodeID(id), |
| 304 | + &mockBoundsShiftProcedureSpec{ |
| 305 | + by: duration, |
| 306 | + }) |
| 307 | +} |
| 308 | + |
| 309 | +func bounds(start, stop int) *planner.Bounds { |
| 310 | + return &planner.Bounds{ |
| 311 | + Start: values.Time(start), |
| 312 | + Stop: values.Time(stop), |
| 313 | + } |
| 314 | +} |
| 315 | + |
| 316 | +// Test that bounds are propagated up through the plan correctly |
| 317 | +func TestBounds_ComputePlanBounds(t *testing.T) { |
| 318 | + tests := []struct { |
| 319 | + // Name of test |
| 320 | + name string |
| 321 | + // Nodes and edges defining plan |
| 322 | + spec *plantest.PhysicalPlanSpec |
| 323 | + // Map from node ID to the expected bounds for that node |
| 324 | + want map[planner.NodeID]*planner.Bounds |
| 325 | + }{ |
| 326 | + { |
| 327 | + name: "no bounds", |
| 328 | + spec: &plantest.PhysicalPlanSpec{ |
| 329 | + Nodes: []planner.PlanNode{ |
| 330 | + makeNode("0"), |
| 331 | + }, |
| 332 | + }, |
| 333 | + want: map[planner.NodeID]*planner.Bounds{ |
| 334 | + "0": nil, |
| 335 | + }, |
| 336 | + }, |
| 337 | + { |
| 338 | + name: "single time bounds", |
| 339 | + // 0 -> 1 -> 2 -> 3 -> 4 |
| 340 | + spec: &plantest.PhysicalPlanSpec{ |
| 341 | + Nodes: []planner.PlanNode{ |
| 342 | + makeNode("0"), |
| 343 | + makeNode("1"), |
| 344 | + makeBoundsNode("2", bounds(5, 10)), |
| 345 | + makeNode("3"), |
| 346 | + makeNode("4"), |
| 347 | + }, |
| 348 | + Edges: [][2]int{ |
| 349 | + {0, 1}, |
| 350 | + {1, 2}, |
| 351 | + {2, 3}, |
| 352 | + {3, 4}, |
| 353 | + }, |
| 354 | + }, |
| 355 | + want: map[planner.NodeID]*planner.Bounds{ |
| 356 | + "0": nil, |
| 357 | + "1": nil, |
| 358 | + "2": bounds(5, 10), |
| 359 | + "3": bounds(5, 10), |
| 360 | + "4": bounds(5, 10)}, |
| 361 | + }, |
| 362 | + { |
| 363 | + name: "multiple intersect time bounds", |
| 364 | + // 0 -> 1 -> 2 -> 3 -> 4 |
| 365 | + spec: &plantest.PhysicalPlanSpec{ |
| 366 | + Nodes: []planner.PlanNode{ |
| 367 | + makeNode("0"), |
| 368 | + makeBoundsNode("1", bounds(5, 10)), |
| 369 | + makeNode("2"), |
| 370 | + makeBoundsNode("3", bounds(7, 11)), |
| 371 | + makeNode("4"), |
| 372 | + }, |
| 373 | + Edges: [][2]int{ |
| 374 | + {0, 1}, |
| 375 | + {1, 2}, |
| 376 | + {2, 3}, |
| 377 | + {3, 4}, |
| 378 | + }, |
| 379 | + }, |
| 380 | + want: map[planner.NodeID]*planner.Bounds{ |
| 381 | + "0": nil, |
| 382 | + "1": bounds(5, 10), |
| 383 | + "2": bounds(5, 10), |
| 384 | + "3": bounds(7, 10), |
| 385 | + "4": bounds(7, 10)}, |
| 386 | + }, |
| 387 | + { |
| 388 | + name: "shift nil time bounds", |
| 389 | + // 0 -> 1 -> 2 |
| 390 | + spec: &plantest.PhysicalPlanSpec{ |
| 391 | + Nodes: []planner.PlanNode{ |
| 392 | + makeNode("0"), |
| 393 | + makeShiftNode("1", values.Duration(5)), |
| 394 | + makeNode("2"), |
| 395 | + }, |
| 396 | + Edges: [][2]int{ |
| 397 | + {0, 1}, |
| 398 | + {1, 2}, |
| 399 | + }, |
| 400 | + }, |
| 401 | + want: map[planner.NodeID]*planner.Bounds{ |
| 402 | + "0": nil, |
| 403 | + "1": nil, |
| 404 | + "2": nil, |
| 405 | + }, |
| 406 | + }, |
| 407 | + { |
| 408 | + name: "shift bounds after intersecting bounds", |
| 409 | + // 0 -> 1 -> 2 -> 3 -> 4 |
| 410 | + spec: &plantest.PhysicalPlanSpec{ |
| 411 | + Nodes: []planner.PlanNode{ |
| 412 | + makeNode("0"), |
| 413 | + makeBoundsNode("1", bounds(5, 10)), |
| 414 | + makeNode("2"), |
| 415 | + makeShiftNode("3", values.Duration(5)), |
| 416 | + makeNode("4"), |
| 417 | + }, |
| 418 | + Edges: [][2]int{ |
| 419 | + {0, 1}, |
| 420 | + {1, 2}, |
| 421 | + {2, 3}, |
| 422 | + {3, 4}, |
| 423 | + }, |
| 424 | + }, |
| 425 | + want: map[planner.NodeID]*planner.Bounds{ |
| 426 | + "0": nil, |
| 427 | + "1": bounds(5, 10), |
| 428 | + "2": bounds(5, 10), |
| 429 | + "3": bounds(10, 15), |
| 430 | + "4": bounds(10, 15)}, |
| 431 | + }, |
| 432 | + { |
| 433 | + name: "join", |
| 434 | + // 2 |
| 435 | + // / \ |
| 436 | + // 0 1 |
| 437 | + spec: &plantest.PhysicalPlanSpec{ |
| 438 | + Nodes: []planner.PlanNode{ |
| 439 | + makeBoundsNode("0", bounds(5, 10)), |
| 440 | + makeBoundsNode("1", bounds(12, 20)), |
| 441 | + makeNode("2"), |
| 442 | + }, |
| 443 | + Edges: [][2]int{ |
| 444 | + {0, 2}, |
| 445 | + {1, 2}, |
| 446 | + }, |
| 447 | + }, |
| 448 | + want: map[planner.NodeID]*planner.Bounds{ |
| 449 | + "0": bounds(5, 10), |
| 450 | + "1": bounds(12, 20), |
| 451 | + "2": bounds(5, 20), |
| 452 | + }, |
| 453 | + }, |
| 454 | + { |
| 455 | + name: "yields", |
| 456 | + // 3 4 |
| 457 | + // \ / |
| 458 | + // 1 2 |
| 459 | + // \ / |
| 460 | + // 0 |
| 461 | + spec: &plantest.PhysicalPlanSpec{ |
| 462 | + Nodes: []planner.PlanNode{ |
| 463 | + makeNode("0"), |
| 464 | + makeBoundsNode("1", bounds(5, 10)), |
| 465 | + makeNode("2"), |
| 466 | + makeNode("3"), |
| 467 | + makeNode("4"), |
| 468 | + }, |
| 469 | + Edges: [][2]int{ |
| 470 | + {0, 1}, |
| 471 | + {0, 2}, |
| 472 | + {1, 3}, |
| 473 | + {1, 4}, |
| 474 | + }, |
| 475 | + }, |
| 476 | + want: map[planner.NodeID]*planner.Bounds{ |
| 477 | + "0": nil, |
| 478 | + "1": bounds(5, 10), |
| 479 | + "2": nil, |
| 480 | + "3": bounds(5, 10), |
| 481 | + "4": bounds(5, 10), |
| 482 | + }, |
| 483 | + }, |
| 484 | + } |
| 485 | + |
| 486 | + for _, tc := range tests { |
| 487 | + tc := tc |
| 488 | + t.Run(tc.name, func(t *testing.T) { |
| 489 | + // Create plan from spec |
| 490 | + thePlan := plantest.CreatePhysicalPlanSpec(tc.spec) |
| 491 | + |
| 492 | + // Method used to compute the bounds at each node |
| 493 | + if err := thePlan.BottomUpWalk(planner.ComputeBounds); err != nil { |
| 494 | + t.Fatal(err) |
| 495 | + } |
| 496 | + |
| 497 | + // Map NodeID -> Bounds |
| 498 | + got := make(map[planner.NodeID]*planner.Bounds) |
| 499 | + thePlan.BottomUpWalk(func(n planner.PlanNode) error { |
| 500 | + got[n.ID()] = n.Bounds() |
| 501 | + return nil |
| 502 | + }) |
| 503 | + |
| 504 | + if !cmp.Equal(tc.want, got) { |
| 505 | + t.Errorf("Did not get expected time bounds, -want/+got:\n%v", cmp.Diff(tc.want, got)) |
| 506 | + } |
| 507 | + }) |
| 508 | + } |
| 509 | +} |
0 commit comments