Skip to content

Commit 969b4f3

Browse files
committed
In LateralJoinIter, provide parent rows to the secondary child rowIter.
1 parent 0147ad3 commit 969b4f3

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

sql/rowexec/join_iters.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ type lateralJoinIterator struct {
743743
foundMatch bool
744744
}
745745

746-
func newLateralJoinIter(ctx *sql.Context, b sql.NodeExecBuilder, j *plan.JoinNode, row sql.Row) (sql.RowIter, error) {
746+
func newLateralJoinIter(ctx *sql.Context, b sql.NodeExecBuilder, j *plan.JoinNode, parentRow sql.Row) (sql.RowIter, error) {
747747
var left, right string
748748
if leftTable, ok := j.Left().(sql.Nameable); ok {
749749
left = leftTable.Name()
@@ -761,31 +761,37 @@ func newLateralJoinIter(ctx *sql.Context, b sql.NodeExecBuilder, j *plan.JoinNod
761761
attribute.String("right", right),
762762
))
763763

764-
l, err := b.Build(ctx, j.Left(), row)
764+
l, err := b.Build(ctx, j.Left(), parentRow)
765765
if err != nil {
766766
span.End()
767767
return nil, err
768768
}
769769

770+
parentLen := len(parentRow)
771+
772+
primaryRow := make(sql.Row, parentLen+len(j.Left().Schema()))
773+
copy(primaryRow, parentRow)
774+
770775
return sql.NewSpanIter(span, &lateralJoinIterator{
771-
parentRow: row,
776+
primaryRow: primaryRow,
777+
parentRow: parentRow,
772778
primary: l,
773779
secondaryNode: j.Right(),
774780
cond: j.Filter,
775781
jType: j.Op,
776-
rowSize: len(row) + len(j.Left().Schema()) + len(j.Right().Schema()),
782+
rowSize: len(parentRow) + len(j.Left().Schema()) + len(j.Right().Schema()),
777783
scopeLen: j.ScopeLen,
778784
b: b,
779785
}), nil
780786
}
781787

782788
func (i *lateralJoinIterator) loadPrimary(ctx *sql.Context) error {
783-
if i.primaryRow == nil {
789+
if i.secondaryRow == nil {
784790
lRow, err := i.primary.Next(ctx)
785791
if err != nil {
786792
return err
787793
}
788-
i.primaryRow = lRow
794+
copy(i.primaryRow[len(i.parentRow):], lRow)
789795
i.foundMatch = false
790796
}
791797
return nil
@@ -807,21 +813,18 @@ func (i *lateralJoinIterator) buildSecondary(ctx *sql.Context) error {
807813
}
808814

809815
func (i *lateralJoinIterator) loadSecondary(ctx *sql.Context) error {
810-
if i.secondaryRow == nil {
811-
sRow, err := i.secondary.Next(ctx)
812-
if err != nil {
813-
return err
814-
}
815-
i.secondaryRow = sRow[len(i.primaryRow):]
816+
sRow, err := i.secondary.Next(ctx)
817+
if err != nil {
818+
return err
816819
}
820+
i.secondaryRow = sRow[len(i.primaryRow):]
817821
return nil
818822
}
819823

820824
func (i *lateralJoinIterator) buildRow(primaryRow, secondaryRow sql.Row) sql.Row {
821825
row := make(sql.Row, i.rowSize)
822-
copy(row, i.parentRow)
823-
copy(row[len(i.parentRow):], primaryRow)
824-
copy(row[len(i.parentRow)+len(primaryRow):], secondaryRow)
826+
copy(row, primaryRow)
827+
copy(row[len(primaryRow):], secondaryRow)
825828
return row
826829
}
827830

@@ -836,7 +839,6 @@ func (i *lateralJoinIterator) reset(ctx *sql.Context) (err error) {
836839
err = i.secondary.Close(ctx)
837840
i.secondary = nil
838841
}
839-
i.primaryRow = nil
840842
i.secondaryRow = nil
841843
return
842844
}
@@ -865,9 +867,7 @@ func (i *lateralJoinIterator) Next(ctx *sql.Context) (sql.Row, error) {
865867
}
866868
return nil, err
867869
}
868-
869870
row := i.buildRow(i.primaryRow, i.secondaryRow)
870-
i.secondaryRow = nil
871871
if i.cond != nil {
872872
if res, err := sql.EvaluateCondition(ctx, i.cond, row); err != nil {
873873
return nil, err

0 commit comments

Comments
 (0)