@@ -62,28 +62,29 @@ case class SortMergeJoin(
6262 private [this ] var rightElement : Row = _
6363 private [this ] var leftKey : Row = _
6464 private [this ] var rightKey : Row = _
65- private [this ] var currentlMatches : CompactBuffer [Row ] = _
66- private [this ] var currentrMatches : CompactBuffer [Row ] = _
67- private [this ] var currentlPosition : Int = - 1
68- private [this ] var currentrPosition : Int = - 1
65+ private [this ] var leftMatches : CompactBuffer [Row ] = _
66+ private [this ] var rightMatches : CompactBuffer [Row ] = _
67+ private [this ] var leftPosition : Int = - 1
68+ private [this ] var rightPosition : Int = - 1
6969
70- override final def hasNext : Boolean = currentlPosition != - 1 || nextMatchingPair
70+ override final def hasNext : Boolean = leftPosition != - 1 || nextMatchingPair
7171
7272 override final def next (): Row = {
73- if (! hasNext) {
74- return null
75- }
76- val joinedRow =
77- joinRow(currentlMatches(currentlPosition), currentrMatches(currentrPosition))
78- currentrPosition += 1
79- if (currentrPosition >= currentrMatches.size) {
80- currentlPosition += 1
81- currentrPosition = 0
82- if (currentlPosition >= currentlMatches.size) {
83- currentlPosition = - 1
73+ if (hasNext) {
74+ val joinedRow = joinRow(leftMatches(leftPosition), rightMatches(rightPosition))
75+ rightPosition += 1
76+ if (rightPosition >= rightMatches.size) {
77+ leftPosition += 1
78+ rightPosition = 0
79+ if (leftPosition >= leftMatches.size) {
80+ leftPosition = - 1
81+ }
8482 }
83+ joinedRow
84+ } else {
85+ // according to Scala doc, this is undefined
86+ null
8587 }
86- joinedRow
8788 }
8889
8990 private def fetchLeft () = {
@@ -104,13 +105,12 @@ case class SortMergeJoin(
104105 }
105106 }
106107
107- private def fetchFirst () = {
108+ private def initialize () = {
108109 fetchLeft()
109110 fetchRight()
110- currentrPosition = 0
111111 }
112112 // initialize iterator
113- fetchFirst ()
113+ initialize ()
114114
115115 /**
116116 * Searches the left/right iterator for the next rows that matches.
@@ -119,50 +119,42 @@ case class SortMergeJoin(
119119 * of tuples.
120120 */
121121 private def nextMatchingPair (): Boolean = {
122- if (currentlPosition > - 1 ) {
123- true
124- } else {
125- currentlPosition = - 1
126- currentlMatches = null
122+ if (leftPosition == - 1 ) {
123+ leftMatches = null
127124 var stop : Boolean = false
128125 while (! stop && leftElement != null && rightElement != null ) {
129- if (ordering.compare(leftKey, rightKey) == 0 && ! leftKey.anyNull) {
130- stop = true
131- } else if (ordering.compare(leftKey, rightKey) > 0 || rightKey.anyNull) {
126+ stop = ordering.compare(leftKey, rightKey) == 0 && ! leftKey.anyNull
127+ if (ordering.compare(leftKey, rightKey) > 0 || rightKey.anyNull) {
132128 fetchRight()
133- } else { // if (ordering.compare(leftKey, rightKey) < 0 || leftKey.anyNull)
129+ } else if (ordering.compare(leftKey, rightKey) < 0 || leftKey.anyNull) {
134130 fetchLeft()
135131 }
136132 }
137- currentrMatches = new CompactBuffer [Row ]()
133+ rightMatches = new CompactBuffer [Row ]()
138134 while (stop && rightElement != null ) {
139- currentrMatches += rightElement
135+ rightMatches += rightElement
140136 fetchRight()
141- if (ordering.compare(leftKey, rightKey) != 0 ) {
142- stop = false
143- }
137+ // exit loop when run out of right matches
138+ stop = ordering.compare(leftKey, rightKey) == 0
144139 }
145- if (currentrMatches .size > 0 ) {
140+ if (rightMatches .size > 0 ) {
146141 stop = false
147- currentlMatches = new CompactBuffer [Row ]()
142+ leftMatches = new CompactBuffer [Row ]()
148143 val leftMatch = leftKey.copy()
149144 while (! stop && leftElement != null ) {
150- currentlMatches += leftElement
145+ leftMatches += leftElement
151146 fetchLeft()
152- if (ordering.compare(leftKey, leftMatch) != 0 ) {
153- stop = true
154- }
147+ // exit loop when run out of left matches
148+ stop = ordering.compare(leftKey, leftMatch) != 0
155149 }
156150 }
157151
158- if (currentlMatches == null ) {
159- false
160- } else {
161- currentlPosition = 0
162- currentrPosition = 0
163- true
152+ if (leftMatches != null ) {
153+ leftPosition = 0
154+ rightPosition = 0
164155 }
165156 }
157+ leftPosition > - 1
166158 }
167159 }
168160 }
0 commit comments