Permalink
Browse files

Linear Regression: Fix a wrong check caused by new linregr

Jira: MADLIB-637

A bug in merge function:
Wrong check was triggered if one of the segment is merged as a "stateRight"
does not have tuples.
  • Loading branch information...
1 parent 2d3d581 commit 72b11c16b292c15e45fc54080366fd0591069eed Xixuan Feng committed with haradh1 Aug 20, 2012
Showing with 14 additions and 10 deletions.
  1. +6 −10 src/modules/regress/LinearRegression_impl.hpp
  2. +8 −0 src/modules/regress/linear.cpp
View
16 src/modules/regress/LinearRegression_impl.hpp
@@ -80,6 +80,12 @@ LinearRegressionAccumulator<Container>::operator<<(const tuple_type& inTuple) {
this->resize();
}
+ // dimension check
+ if (widthOfX != static_cast<uint16_t>(x.size())) {
+ throw std::runtime_error("Inconsistent numbers of independent "
+ "variables.");
+ }
+
numRows++;
y_sum += y;
y_square_sum += y * y;
@@ -101,16 +107,6 @@ LinearRegressionAccumulator<Container>&
LinearRegressionAccumulator<Container>::operator<<(
const LinearRegressionAccumulator<OtherContainer>& inOther) {
- // Initialize if necessary
- if (numRows == 0) {
- *this = inOther;
- return *this;
- }
-
- if (widthOfX != inOther.widthOfX)
- throw std::runtime_error("Inconsistent numbers of independent "
- "variables.");
-
numRows += inOther.numRows;
y_sum += inOther.y_sum;
y_square_sum += inOther.y_square_sum;
View
8 src/modules/regress/linear.cpp
@@ -36,6 +36,14 @@ linregr_merge_states::run(AnyType& args) {
MutableLinRegrState stateLeft = args[0].getAs<MutableByteString>();
LinRegrState stateRight = args[1].getAs<ByteString>();
+ // We first handle the trivial case where this function is called with one
+ // of the states being the initial state
+ if (stateLeft.numRows == 0) {
+ return stateRight.storage();
+ } else if (stateRight.numRows == 0) {
+ return stateLeft.storage();
+ }
+
stateLeft << stateRight;
return stateLeft.storage();
}

0 comments on commit 72b11c1

Please sign in to comment.