Skip to content

Commit

Permalink
Fixed simple materialized views. We weren't initializing the fields w…
Browse files Browse the repository at this point in the history
…hen we first start. I have no idea how this was working before #153
  • Loading branch information
apavlo committed Apr 18, 2014
1 parent ae4f384 commit 7346860
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 11 deletions.
34 changes: 24 additions & 10 deletions src/ee/storage/MaterializedViewMetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "common/debuglog.h"
#include "common/types.h"
#include "common/FatalException.hpp"
#include "common/ValueFactory.hpp"
#include "catalog/catalog.h"
#include "catalog/columnref.h"
#include "catalog/column.h"
Expand Down Expand Up @@ -69,17 +70,16 @@ MaterializedViewMetadata::MaterializedViewMetadata(
int destIndex = destCol->index();

const catalog::Column *srcCol = destCol->matviewsource();


m_outputColumnAggTypes[destIndex] = static_cast<ExpressionType>(destCol->aggregatetype());
if (srcCol) {
m_outputColumnSrcTableIndexes[destIndex] = srcCol->index();
m_outputColumnAggTypes[destIndex] = static_cast<ExpressionType>(destCol->aggregatetype());
VOLT_DEBUG("Setting column %d to exp type %d/%d", destIndex, destCol->aggregatetype(), m_outputColumnAggTypes[destIndex]);
}
else {
m_outputColumnSrcTableIndexes[destIndex] = -1;
m_outputColumnAggTypes[destIndex] = EXPRESSION_TYPE_INVALID;
VOLT_DEBUG("Setting column %d to exp type %d", destIndex, EXPRESSION_TYPE_INVALID);
}
VOLT_DEBUG("%s - Setting column %d to exp type %d/%d",
m_name.c_str(), destIndex, destCol->aggregatetype(), m_outputColumnAggTypes[destIndex]);
}

m_index = m_target->primaryKeyIndex();
Expand Down Expand Up @@ -136,12 +136,22 @@ void MaterializedViewMetadata::processTupleInsert(TableTuple &newTuple) {
&& (m_filterPredicate->eval(&newTuple, NULL).isFalse()))
return;

VOLT_DEBUG("Attempting to insert a new tuple into materialized view %s", m_name.c_str());
VOLT_DEBUG("%s - Attempting to insert a new tuple into materialized view", m_name.c_str());

bool exists = findExistingTuple(newTuple);
if (!exists) {
// create a blank tuple
m_existingTuple.move(m_emptyTupleBackingStore);
VOLT_DEBUG("%s - Tuple does not already exist. Creating a blank entry", m_name.c_str());

// PAVLO: 2014-04-18
// We need to make sure that we initialize the aggregate columns to zero!
// We assume that they are always the last columns in the output list
NValue zero = ValueFactory::getIntegerValue(0);
for (int colindex = m_groupByColumnCount; colindex < m_outputColumnCount; colindex++) {
// HACK: We assume the aggregate columns are integers
m_existingTuple.setNValue(colindex, zero);
} // FOR
}

// clear the tuple that will be built to insert or overwrite
Expand All @@ -155,20 +165,22 @@ void MaterializedViewMetadata::processTupleInsert(TableTuple &newTuple) {
}

// set up the next column, which is a count
m_updatedTuple.setNValue(colindex,
m_existingTuple.getNValue(colindex).op_increment());
colindex++;
// m_updatedTuple.setNValue(colindex,
// m_existingTuple.getNValue(colindex).op_increment());
// colindex++;

// set values for the other columns
for (int i = colindex; i < m_outputColumnCount; i++) {
NValue newValue = newTuple.getNValue(m_outputColumnSrcTableIndexes[i]);
NValue newValue = newTuple.getNValue(i); // m_outputColumnSrcTableIndexes[i]);
NValue existingValue = m_existingTuple.getNValue(i);

if (m_outputColumnAggTypes[i] == EXPRESSION_TYPE_AGGREGATE_SUM) {
m_updatedTuple.setNValue(i, newValue.op_add(existingValue));
}
else if (m_outputColumnAggTypes[i] == EXPRESSION_TYPE_AGGREGATE_COUNT) {
m_updatedTuple.setNValue(i, existingValue.op_increment());
VOLT_DEBUG("%s - Updating COUNT column #%d -> %s",
m_name.c_str(), i, m_updatedTuple.getNValue(i).debug().c_str());
}
else {
char message[128];
Expand All @@ -184,9 +196,11 @@ void MaterializedViewMetadata::processTupleInsert(TableTuple &newTuple) {
// shouldn't need to update indexes as this shouldn't ever change the
// key
m_target->updateTuple(m_updatedTuple, m_existingTuple, false);
VOLT_DEBUG("Updating entry => %s", m_updatedTuple.debug(m_name).c_str());
}
else {
m_target->insertTuple(m_updatedTuple);
VOLT_DEBUG("Inserting new entry => %s", m_updatedTuple.debug(m_name).c_str());
}
}

Expand Down
52 changes: 51 additions & 1 deletion tests/frontend/org/voltdb/regressionsuites/TestVoterSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.voltdb.VoltTable;
import org.voltdb.client.Client;
import org.voltdb.client.ClientResponse;
import org.voltdb.utils.VoltTableUtil;

import edu.brown.benchmark.voter.VoterConstants;
import edu.brown.benchmark.voter.VoterProjectBuilder;
Expand Down Expand Up @@ -121,7 +122,56 @@ public void testVoteLimit() throws Exception {
//assertEquals(expected, results[0].asScalarLong());
} // FOR
}


// /**
// * testViews
// */
// public void testVotesByContestantView() throws Exception {
// Client client = this.getClient();
// this.initializeDatabase(client);
//
// int num_contestants = VoterConstants.NUM_CONTESTANTS;
//
// // Insert the same number of votes for each contestant as their id number
// long phoneNumber = TestVoterSuite.phoneNumber;
// for (int contestant = 0 ; contestant < num_contestants; contestant++) {
// for (int i = 0; i < contestant; i++) {
// ClientResponse cresponse = client.callProcedure(Vote.class.getSimpleName(),
// voteId++,
// phoneNumber++,
// contestant,
// maxVotesPerPhoneNumber);
// assertEquals(Status.OK, cresponse.getStatus());
// VoltTable results[] = cresponse.getResults();
// assertEquals(1, results.length);
// assertEquals(VoterConstants.VOTE_SUCCESSFUL, results[0].asScalarLong());
// } // FOR
// } // FOR
//
//
// // Now check that the view is correct
// String sql = "SELECT * FROM v_votes_by_contestant_number";
// ClientResponse cresponse = RegressionSuiteUtil.sql(client, sql);
// assertEquals(Status.OK, cresponse.getStatus());
// VoltTable results[] = cresponse.getResults();
// assertEquals(1, results.length);
// System.out.println(VoltTableUtil.format(results[0]));
//
//// sql = "SELECT * FROM votes";
//// cresponse = RegressionSuiteUtil.sql(client, sql);
//// assertEquals(Status.OK, cresponse.getStatus());
//// results = cresponse.getResults();
//// assertEquals(1, results.length);
//// System.out.println(VoltTableUtil.format(results[0]));
//
// while (results[0].advanceRow()) {
// int contestant = (int)results[0].getLong(0);
// assert(contestant < VoterConstants.NUM_CONTESTANTS) : contestant;
// int num_votes = (int)results[0].getLong(1);
//
// assertEquals(contestant, num_votes);
// } // WHILE
// }

public static Test suite() {
// the suite made here will all be using the tests from this class
Expand Down

0 comments on commit 7346860

Please sign in to comment.