(op dag.Op, parents []order.SortKeys)
| 314 | } |
| 315 | |
| 316 | func (o *Optimizer) propagateSortKeyOp(op dag.Op, parents []order.SortKeys) ([]order.SortKeys, error) { |
| 317 | switch op.(type) { |
| 318 | case *dag.HashJoinOp, *dag.JoinOp: |
| 319 | return []order.SortKeys{nil}, nil |
| 320 | } |
| 321 | // If the op is not a join then condense sort order into a single parent, |
| 322 | // since all the ops only care about the sort order of multiple parents if |
| 323 | // the SortKey of all parents is unified. |
| 324 | var parent order.SortKeys |
| 325 | for k, p := range parents { |
| 326 | if k == 0 { |
| 327 | parent = p |
| 328 | } else if !parent.Equal(p) { |
| 329 | parent = nil |
| 330 | break |
| 331 | } |
| 332 | } |
| 333 | switch op := op.(type) { |
| 334 | case *dag.AggregateOp: |
| 335 | if parent.IsNil() { |
| 336 | return []order.SortKeys{nil}, nil |
| 337 | } |
| 338 | //XXX handle only primary sortKey for now |
| 339 | sortKey := parent.Primary() |
| 340 | for _, k := range op.Keys { |
| 341 | if groupingKey := fieldOf(k.LHS); groupingKey.Equal(sortKey.Key) { |
| 342 | rhsExpr := k.RHS |
| 343 | rhs := fieldOf(rhsExpr) |
| 344 | if rhs.Equal(sortKey.Key) || orderPreservingCall(rhsExpr, groupingKey) { |
| 345 | op.InputSortDir = int(sortKey.Order.Direction()) |
| 346 | // Currently, the aggregate operator will sort its |
| 347 | // output according to the primary key, but we |
| 348 | // should relax this and do an analysis here as |
| 349 | // to whether the sort is necessary for the |
| 350 | // downstream consumer. |
| 351 | return []order.SortKeys{parent}, nil |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | // We'll leave this as unknown for now in spite of the aggregate |
| 356 | // and not try to optimize downstream of the first aggregate |
| 357 | // unless there is an excplicit sort encountered. |
| 358 | return []order.SortKeys{nil}, nil |
| 359 | case *dag.ForkOp: |
| 360 | var keys []order.SortKeys |
| 361 | for _, seq := range op.Paths { |
| 362 | out, err := o.propagateSortKey(seq, []order.SortKeys{parent}) |
| 363 | if err != nil { |
| 364 | return nil, err |
| 365 | } |
| 366 | keys = append(keys, out...) |
| 367 | } |
| 368 | return keys, nil |
| 369 | case *dag.ScatterOp: |
| 370 | var keys []order.SortKeys |
| 371 | for _, seq := range op.Paths { |
| 372 | out, err := o.propagateSortKey(seq, []order.SortKeys{parent}) |
| 373 | if err != nil { |
no test coverage detected