github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/causetstore/petri/acyclic/causet/embedded/resolve_indices.go (about) 1 // Copyright 2020 WHTCORPS INC, Inc. 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 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package embedded 15 16 import ( 17 "github.com/whtcorpsinc/milevadb/memex" 18 "github.com/whtcorpsinc/milevadb/soliton/disjointset" 19 ) 20 21 // ResolveIndices implements Causet interface. 22 func (p *PhysicalProjection) ResolveIndices() (err error) { 23 err = p.physicalSchemaProducer.ResolveIndices() 24 if err != nil { 25 return err 26 } 27 for i, expr := range p.Exprs { 28 p.Exprs[i], err = expr.ResolveIndices(p.children[0].Schema()) 29 if err != nil { 30 return err 31 } 32 } 33 childProj, isProj := p.children[0].(*PhysicalProjection) 34 if !isProj { 35 return 36 } 37 refine4NeighbourProj(p, childProj) 38 return 39 } 40 41 // refine4NeighbourProj refines the index for p.Exprs whose type is *DeferredCauset when 42 // there is two neighbouring Projections. 43 // This function is introduced because that different childProj.Expr may refer 44 // to the same index of childProj.Schema, so we need to keep this relation 45 // between the specified memexs in the parent Projection. 46 func refine4NeighbourProj(p, childProj *PhysicalProjection) { 47 inputIdx2OutputIdxes := make(map[int][]int) 48 for i, expr := range childProj.Exprs { 49 col, isDefCaus := expr.(*memex.DeferredCauset) 50 if !isDefCaus { 51 continue 52 } 53 inputIdx2OutputIdxes[col.Index] = append(inputIdx2OutputIdxes[col.Index], i) 54 } 55 childSchemaUnionSet := disjointset.NewIntSet(childProj.schemaReplicant.Len()) 56 for _, outputIdxes := range inputIdx2OutputIdxes { 57 if len(outputIdxes) <= 1 { 58 continue 59 } 60 for i := 1; i < len(outputIdxes); i++ { 61 childSchemaUnionSet.Union(outputIdxes[0], outputIdxes[i]) 62 } 63 } 64 65 for _, expr := range p.Exprs { 66 col, isDefCaus := expr.(*memex.DeferredCauset) 67 if !isDefCaus { 68 continue 69 } 70 col.Index = childSchemaUnionSet.FindRoot(col.Index) 71 } 72 } 73 74 // ResolveIndices implements Causet interface. 75 func (p *PhysicalHashJoin) ResolveIndices() (err error) { 76 err = p.physicalSchemaProducer.ResolveIndices() 77 if err != nil { 78 return err 79 } 80 lSchema := p.children[0].Schema() 81 rSchema := p.children[1].Schema() 82 for i, fun := range p.EqualConditions { 83 lArg, err := fun.GetArgs()[0].ResolveIndices(lSchema) 84 if err != nil { 85 return err 86 } 87 p.LeftJoinKeys[i] = lArg.(*memex.DeferredCauset) 88 rArg, err := fun.GetArgs()[1].ResolveIndices(rSchema) 89 if err != nil { 90 return err 91 } 92 p.RightJoinKeys[i] = rArg.(*memex.DeferredCauset) 93 p.EqualConditions[i] = memex.NewFunctionInternal(fun.GetCtx(), fun.FuncName.L, fun.GetType(), lArg, rArg).(*memex.ScalarFunction) 94 } 95 for i, expr := range p.LeftConditions { 96 p.LeftConditions[i], err = expr.ResolveIndices(lSchema) 97 if err != nil { 98 return err 99 } 100 } 101 for i, expr := range p.RightConditions { 102 p.RightConditions[i], err = expr.ResolveIndices(rSchema) 103 if err != nil { 104 return err 105 } 106 } 107 for i, expr := range p.OtherConditions { 108 p.OtherConditions[i], err = expr.ResolveIndices(memex.MergeSchema(lSchema, rSchema)) 109 if err != nil { 110 return err 111 } 112 } 113 return 114 } 115 116 // ResolveIndices implements Causet interface. 117 func (p *PhysicalBroadCastJoin) ResolveIndices() (err error) { 118 err = p.physicalSchemaProducer.ResolveIndices() 119 if err != nil { 120 return err 121 } 122 lSchema := p.children[0].Schema() 123 rSchema := p.children[1].Schema() 124 for i, col := range p.LeftJoinKeys { 125 newKey, err := col.ResolveIndices(lSchema) 126 if err != nil { 127 return err 128 } 129 p.LeftJoinKeys[i] = newKey.(*memex.DeferredCauset) 130 } 131 for i, col := range p.RightJoinKeys { 132 newKey, err := col.ResolveIndices(rSchema) 133 if err != nil { 134 return err 135 } 136 p.RightJoinKeys[i] = newKey.(*memex.DeferredCauset) 137 } 138 for i, expr := range p.LeftConditions { 139 p.LeftConditions[i], err = expr.ResolveIndices(lSchema) 140 if err != nil { 141 return err 142 } 143 } 144 for i, expr := range p.RightConditions { 145 p.RightConditions[i], err = expr.ResolveIndices(rSchema) 146 if err != nil { 147 return err 148 } 149 } 150 for i, expr := range p.OtherConditions { 151 p.OtherConditions[i], err = expr.ResolveIndices(memex.MergeSchema(lSchema, rSchema)) 152 if err != nil { 153 return err 154 } 155 } 156 return 157 } 158 159 // ResolveIndices implements Causet interface. 160 func (p *PhysicalMergeJoin) ResolveIndices() (err error) { 161 err = p.physicalSchemaProducer.ResolveIndices() 162 if err != nil { 163 return err 164 } 165 lSchema := p.children[0].Schema() 166 rSchema := p.children[1].Schema() 167 for i, col := range p.LeftJoinKeys { 168 newKey, err := col.ResolveIndices(lSchema) 169 if err != nil { 170 return err 171 } 172 p.LeftJoinKeys[i] = newKey.(*memex.DeferredCauset) 173 } 174 for i, col := range p.RightJoinKeys { 175 newKey, err := col.ResolveIndices(rSchema) 176 if err != nil { 177 return err 178 } 179 p.RightJoinKeys[i] = newKey.(*memex.DeferredCauset) 180 } 181 for i, expr := range p.LeftConditions { 182 p.LeftConditions[i], err = expr.ResolveIndices(lSchema) 183 if err != nil { 184 return err 185 } 186 } 187 for i, expr := range p.RightConditions { 188 p.RightConditions[i], err = expr.ResolveIndices(rSchema) 189 if err != nil { 190 return err 191 } 192 } 193 for i, expr := range p.OtherConditions { 194 p.OtherConditions[i], err = expr.ResolveIndices(memex.MergeSchema(lSchema, rSchema)) 195 if err != nil { 196 return err 197 } 198 } 199 return 200 } 201 202 // ResolveIndices implements Causet interface. 203 func (p *PhysicalIndexJoin) ResolveIndices() (err error) { 204 err = p.physicalSchemaProducer.ResolveIndices() 205 if err != nil { 206 return err 207 } 208 lSchema := p.children[0].Schema() 209 rSchema := p.children[1].Schema() 210 for i := range p.InnerJoinKeys { 211 newOuterKey, err := p.OuterJoinKeys[i].ResolveIndices(p.children[1-p.InnerChildIdx].Schema()) 212 if err != nil { 213 return err 214 } 215 p.OuterJoinKeys[i] = newOuterKey.(*memex.DeferredCauset) 216 newInnerKey, err := p.InnerJoinKeys[i].ResolveIndices(p.children[p.InnerChildIdx].Schema()) 217 if err != nil { 218 return err 219 } 220 p.InnerJoinKeys[i] = newInnerKey.(*memex.DeferredCauset) 221 } 222 for i, expr := range p.LeftConditions { 223 p.LeftConditions[i], err = expr.ResolveIndices(lSchema) 224 if err != nil { 225 return err 226 } 227 } 228 for i, expr := range p.RightConditions { 229 p.RightConditions[i], err = expr.ResolveIndices(rSchema) 230 if err != nil { 231 return err 232 } 233 } 234 mergedSchema := memex.MergeSchema(lSchema, rSchema) 235 for i, expr := range p.OtherConditions { 236 p.OtherConditions[i], err = expr.ResolveIndices(mergedSchema) 237 if err != nil { 238 return err 239 } 240 } 241 if p.CompareFilters != nil { 242 err = p.CompareFilters.resolveIndices(p.children[1-p.InnerChildIdx].Schema()) 243 if err != nil { 244 return err 245 } 246 for i := range p.CompareFilters.affectedDefCausSchema.DeferredCausets { 247 resolvedDefCaus, err1 := p.CompareFilters.affectedDefCausSchema.DeferredCausets[i].ResolveIndices(p.children[1-p.InnerChildIdx].Schema()) 248 if err1 != nil { 249 return err1 250 } 251 p.CompareFilters.affectedDefCausSchema.DeferredCausets[i] = resolvedDefCaus.(*memex.DeferredCauset) 252 } 253 } 254 return 255 } 256 257 // ResolveIndices implements Causet interface. 258 func (p *PhysicalUnionScan) ResolveIndices() (err error) { 259 err = p.basePhysicalCauset.ResolveIndices() 260 if err != nil { 261 return err 262 } 263 for i, expr := range p.Conditions { 264 p.Conditions[i], err = expr.ResolveIndices(p.children[0].Schema()) 265 if err != nil { 266 return err 267 } 268 } 269 resolvedHandleDefCaus, err := p.HandleDefCauss.ResolveIndices(p.children[0].Schema()) 270 if err != nil { 271 return err 272 } 273 p.HandleDefCauss = resolvedHandleDefCaus 274 return 275 } 276 277 // resolveIndicesForVirtualDeferredCauset resolves dependent columns's indices for virtual columns. 278 func resolveIndicesForVirtualDeferredCauset(result []*memex.DeferredCauset, schemaReplicant *memex.Schema) error { 279 for _, col := range result { 280 if col.VirtualExpr != nil { 281 newExpr, err := col.VirtualExpr.ResolveIndices(schemaReplicant) 282 if err != nil { 283 return err 284 } 285 col.VirtualExpr = newExpr 286 } 287 } 288 return nil 289 } 290 291 // ResolveIndices implements Causet interface. 292 func (p *PhysicalBlockReader) ResolveIndices() error { 293 err := resolveIndicesForVirtualDeferredCauset(p.schemaReplicant.DeferredCausets, p.schemaReplicant) 294 if err != nil { 295 return err 296 } 297 return p.blockCauset.ResolveIndices() 298 } 299 300 // ResolveIndices implements Causet interface. 301 func (p *PhysicalIndexReader) ResolveIndices() (err error) { 302 err = p.physicalSchemaProducer.ResolveIndices() 303 if err != nil { 304 return err 305 } 306 err = p.indexCauset.ResolveIndices() 307 if err != nil { 308 return err 309 } 310 for i, col := range p.OutputDeferredCausets { 311 newDefCaus, err := col.ResolveIndices(p.indexCauset.Schema()) 312 if err != nil { 313 return err 314 } 315 p.OutputDeferredCausets[i] = newDefCaus.(*memex.DeferredCauset) 316 } 317 return 318 } 319 320 // ResolveIndices implements Causet interface. 321 func (p *PhysicalIndexLookUpReader) ResolveIndices() (err error) { 322 err = resolveIndicesForVirtualDeferredCauset(p.blockCauset.Schema().DeferredCausets, p.schemaReplicant) 323 if err != nil { 324 return err 325 } 326 err = p.blockCauset.ResolveIndices() 327 if err != nil { 328 return err 329 } 330 err = p.indexCauset.ResolveIndices() 331 if err != nil { 332 return err 333 } 334 if p.ExtraHandleDefCaus != nil { 335 newDefCaus, err := p.ExtraHandleDefCaus.ResolveIndices(p.blockCauset.Schema()) 336 if err != nil { 337 return err 338 } 339 p.ExtraHandleDefCaus = newDefCaus.(*memex.DeferredCauset) 340 } 341 for i, commonHandleDefCaus := range p.CommonHandleDefCauss { 342 newDefCaus, err := commonHandleDefCaus.ResolveIndices(p.BlockCausets[0].Schema()) 343 if err != nil { 344 return err 345 } 346 p.CommonHandleDefCauss[i] = newDefCaus.(*memex.DeferredCauset) 347 } 348 return 349 } 350 351 // ResolveIndices implements Causet interface. 352 func (p *PhysicalIndexMergeReader) ResolveIndices() (err error) { 353 err = resolveIndicesForVirtualDeferredCauset(p.blockCauset.Schema().DeferredCausets, p.schemaReplicant) 354 if err != nil { 355 return err 356 } 357 if p.blockCauset != nil { 358 err = p.blockCauset.ResolveIndices() 359 if err != nil { 360 return err 361 } 362 } 363 for i := 0; i < len(p.partialCausets); i++ { 364 err = p.partialCausets[i].ResolveIndices() 365 if err != nil { 366 return err 367 } 368 } 369 return nil 370 } 371 372 // ResolveIndices implements Causet interface. 373 func (p *PhysicalSelection) ResolveIndices() (err error) { 374 err = p.basePhysicalCauset.ResolveIndices() 375 if err != nil { 376 return err 377 } 378 for i, expr := range p.Conditions { 379 p.Conditions[i], err = expr.ResolveIndices(p.children[0].Schema()) 380 if err != nil { 381 return err 382 } 383 } 384 return 385 } 386 387 // ResolveIndices implements Causet interface. 388 func (p *basePhysicalAgg) ResolveIndices() (err error) { 389 err = p.physicalSchemaProducer.ResolveIndices() 390 if err != nil { 391 return err 392 } 393 for _, aggFun := range p.AggFuncs { 394 for i, arg := range aggFun.Args { 395 aggFun.Args[i], err = arg.ResolveIndices(p.children[0].Schema()) 396 if err != nil { 397 return err 398 } 399 } 400 for _, byItem := range aggFun.OrderByItems { 401 byItem.Expr, err = byItem.Expr.ResolveIndices(p.children[0].Schema()) 402 if err != nil { 403 return err 404 } 405 } 406 } 407 for i, item := range p.GroupByItems { 408 p.GroupByItems[i], err = item.ResolveIndices(p.children[0].Schema()) 409 if err != nil { 410 return err 411 } 412 } 413 return 414 } 415 416 // ResolveIndices implements Causet interface. 417 func (p *PhysicalSort) ResolveIndices() (err error) { 418 err = p.basePhysicalCauset.ResolveIndices() 419 if err != nil { 420 return err 421 } 422 for _, item := range p.ByItems { 423 item.Expr, err = item.Expr.ResolveIndices(p.children[0].Schema()) 424 if err != nil { 425 return err 426 } 427 } 428 return err 429 } 430 431 // ResolveIndices implements Causet interface. 432 func (p *PhysicalWindow) ResolveIndices() (err error) { 433 err = p.physicalSchemaProducer.ResolveIndices() 434 if err != nil { 435 return err 436 } 437 for i := 0; i < len(p.Schema().DeferredCausets)-len(p.WindowFuncDescs); i++ { 438 col := p.Schema().DeferredCausets[i] 439 newDefCaus, err := col.ResolveIndices(p.children[0].Schema()) 440 if err != nil { 441 return err 442 } 443 p.Schema().DeferredCausets[i] = newDefCaus.(*memex.DeferredCauset) 444 } 445 for i, item := range p.PartitionBy { 446 newDefCaus, err := item.DefCaus.ResolveIndices(p.children[0].Schema()) 447 if err != nil { 448 return err 449 } 450 p.PartitionBy[i].DefCaus = newDefCaus.(*memex.DeferredCauset) 451 } 452 for i, item := range p.OrderBy { 453 newDefCaus, err := item.DefCaus.ResolveIndices(p.children[0].Schema()) 454 if err != nil { 455 return err 456 } 457 p.OrderBy[i].DefCaus = newDefCaus.(*memex.DeferredCauset) 458 } 459 for _, desc := range p.WindowFuncDescs { 460 for i, arg := range desc.Args { 461 desc.Args[i], err = arg.ResolveIndices(p.children[0].Schema()) 462 if err != nil { 463 return err 464 } 465 } 466 } 467 if p.Frame != nil { 468 for i := range p.Frame.Start.CalcFuncs { 469 p.Frame.Start.CalcFuncs[i], err = p.Frame.Start.CalcFuncs[i].ResolveIndices(p.children[0].Schema()) 470 if err != nil { 471 return err 472 } 473 } 474 for i := range p.Frame.End.CalcFuncs { 475 p.Frame.End.CalcFuncs[i], err = p.Frame.End.CalcFuncs[i].ResolveIndices(p.children[0].Schema()) 476 if err != nil { 477 return err 478 } 479 } 480 } 481 return nil 482 } 483 484 // ResolveIndices implements Causet interface. 485 func (p *PhysicalShuffle) ResolveIndices() (err error) { 486 err = p.basePhysicalCauset.ResolveIndices() 487 if err != nil { 488 return err 489 } 490 for i := range p.HashByItems { 491 // "Shuffle" get value of items from `DataSource`, other than children[0]. 492 p.HashByItems[i], err = p.HashByItems[i].ResolveIndices(p.DataSource.Schema()) 493 if err != nil { 494 return err 495 } 496 } 497 return err 498 } 499 500 // ResolveIndices implements Causet interface. 501 func (p *PhysicalTopN) ResolveIndices() (err error) { 502 err = p.basePhysicalCauset.ResolveIndices() 503 if err != nil { 504 return err 505 } 506 for _, item := range p.ByItems { 507 item.Expr, err = item.Expr.ResolveIndices(p.children[0].Schema()) 508 if err != nil { 509 return err 510 } 511 } 512 return 513 } 514 515 // ResolveIndices implements Causet interface. 516 func (p *PhysicalApply) ResolveIndices() (err error) { 517 err = p.PhysicalHashJoin.ResolveIndices() 518 if err != nil { 519 return err 520 } 521 // p.OuterSchema may have duplicated CorrelatedDeferredCausets, 522 // we deduplicate it here. 523 dedupDefCauss := make(map[int64]*memex.CorrelatedDeferredCauset, len(p.OuterSchema)) 524 for _, col := range p.OuterSchema { 525 dedupDefCauss[col.UniqueID] = col 526 } 527 p.OuterSchema = make([]*memex.CorrelatedDeferredCauset, 0, len(dedupDefCauss)) 528 for _, col := range dedupDefCauss { 529 newDefCaus, err := col.DeferredCauset.ResolveIndices(p.children[0].Schema()) 530 if err != nil { 531 return err 532 } 533 col.DeferredCauset = *newDefCaus.(*memex.DeferredCauset) 534 p.OuterSchema = append(p.OuterSchema, col) 535 } 536 // Resolve index for equal conditions again, because apply is different from 537 // hash join on the fact that equal conditions are evaluated against the join result, 538 // so columns from equal conditions come from merged schemaReplicant of children, instead of 539 // single child's schemaReplicant. 540 joinedSchema := memex.MergeSchema(p.children[0].Schema(), p.children[1].Schema()) 541 for i, cond := range p.PhysicalHashJoin.EqualConditions { 542 newSf, err := cond.ResolveIndices(joinedSchema) 543 if err != nil { 544 return err 545 } 546 p.PhysicalHashJoin.EqualConditions[i] = newSf.(*memex.ScalarFunction) 547 } 548 return 549 } 550 551 // ResolveIndices implements Causet interface. 552 func (p *UFIDelate) ResolveIndices() (err error) { 553 err = p.baseSchemaProducer.ResolveIndices() 554 if err != nil { 555 return err 556 } 557 schemaReplicant := p.SelectCauset.Schema() 558 for _, assign := range p.OrderedList { 559 newDefCaus, err := assign.DefCaus.ResolveIndices(schemaReplicant) 560 if err != nil { 561 return err 562 } 563 assign.DefCaus = newDefCaus.(*memex.DeferredCauset) 564 assign.Expr, err = assign.Expr.ResolveIndices(schemaReplicant) 565 if err != nil { 566 return err 567 } 568 } 569 return 570 } 571 572 // ResolveIndices implements Causet interface. 573 func (p *PhysicalLock) ResolveIndices() (err error) { 574 err = p.basePhysicalCauset.ResolveIndices() 575 if err != nil { 576 return err 577 } 578 for i, defcaus := range p.TblID2Handle { 579 for j, col := range defcaus { 580 resolvedDefCaus, err := col.ResolveIndices(p.children[0].Schema()) 581 if err != nil { 582 return err 583 } 584 p.TblID2Handle[i][j] = resolvedDefCaus 585 } 586 } 587 return nil 588 } 589 590 // ResolveIndices implements Causet interface. 591 func (p *Insert) ResolveIndices() (err error) { 592 err = p.baseSchemaProducer.ResolveIndices() 593 if err != nil { 594 return err 595 } 596 for _, asgn := range p.OnDuplicate { 597 newDefCaus, err := asgn.DefCaus.ResolveIndices(p.blockSchema) 598 if err != nil { 599 return err 600 } 601 asgn.DefCaus = newDefCaus.(*memex.DeferredCauset) 602 asgn.Expr, err = asgn.Expr.ResolveIndices(p.Schema4OnDuplicate) 603 if err != nil { 604 return err 605 } 606 } 607 for _, set := range p.SetList { 608 newDefCaus, err := set.DefCaus.ResolveIndices(p.blockSchema) 609 if err != nil { 610 return err 611 } 612 set.DefCaus = newDefCaus.(*memex.DeferredCauset) 613 set.Expr, err = set.Expr.ResolveIndices(p.blockSchema) 614 if err != nil { 615 return err 616 } 617 } 618 for i, expr := range p.GenDefCauss.Exprs { 619 p.GenDefCauss.Exprs[i], err = expr.ResolveIndices(p.blockSchema) 620 if err != nil { 621 return err 622 } 623 } 624 for _, asgn := range p.GenDefCauss.OnDuplicates { 625 newDefCaus, err := asgn.DefCaus.ResolveIndices(p.blockSchema) 626 if err != nil { 627 return err 628 } 629 asgn.DefCaus = newDefCaus.(*memex.DeferredCauset) 630 asgn.Expr, err = asgn.Expr.ResolveIndices(p.Schema4OnDuplicate) 631 if err != nil { 632 return err 633 } 634 } 635 return 636 } 637 638 func (p *physicalSchemaProducer) ResolveIndices() (err error) { 639 err = p.basePhysicalCauset.ResolveIndices() 640 return err 641 } 642 643 func (p *baseSchemaProducer) ResolveIndices() (err error) { 644 return 645 } 646 647 // ResolveIndices implements Causet interface. 648 func (p *basePhysicalCauset) ResolveIndices() (err error) { 649 for _, child := range p.children { 650 err = child.ResolveIndices() 651 if err != nil { 652 return err 653 } 654 } 655 return 656 }