You've already forked mariadb-columnstore-engine
mirror of
https://github.com/mariadb-corporation/mariadb-columnstore-engine.git
synced 2025-07-27 21:01:50 +03:00
Reformat all code to coding standard
This commit is contained in:
349
utils/windowfunction/windowfunction.cpp
Executable file → Normal file
349
utils/windowfunction/windowfunction.cpp
Executable file → Normal file
@ -1,4 +1,4 @@
|
||||
/*
|
||||
/*
|
||||
Copyright (c) 2017, MariaDB
|
||||
Copyright (C) 2014 InfiniDB, Inc.
|
||||
|
||||
@ -56,7 +56,7 @@ WindowFunction::WindowFunction(boost::shared_ptr<WindowFunctionType>& f,
|
||||
boost::shared_ptr<WindowFrame>& w,
|
||||
const RowGroup& g,
|
||||
const Row& r) :
|
||||
fFunctionType(f), fPartitionBy(p), fOrderBy(o), fFrame(w), fRowGroup(g), fRow(r)
|
||||
fFunctionType(f), fPartitionBy(p), fOrderBy(o), fFrame(w), fRowGroup(g), fRow(r)
|
||||
{
|
||||
}
|
||||
|
||||
@ -68,194 +68,207 @@ WindowFunction::~WindowFunction()
|
||||
|
||||
void WindowFunction::operator()()
|
||||
{
|
||||
try
|
||||
{
|
||||
fRowData.reset(new vector<RowPosition>(fStep->getRowData()));
|
||||
try
|
||||
{
|
||||
fRowData.reset(new vector<RowPosition>(fStep->getRowData()));
|
||||
|
||||
if (fOrderBy->rule().fCompares.size() > 0)
|
||||
sort(fRowData->begin(), fRowData->size());
|
||||
if (fOrderBy->rule().fCompares.size() > 0)
|
||||
sort(fRowData->begin(), fRowData->size());
|
||||
|
||||
// get partitions
|
||||
if (fPartitionBy.get() != NULL && !fStep->cancelled())
|
||||
{
|
||||
int64_t i = 0;
|
||||
int64_t j = 1;
|
||||
int64_t rowCnt = fRowData->size();
|
||||
for (j = 1; j < rowCnt; j++)
|
||||
{
|
||||
if ((*(fPartitionBy.get()))
|
||||
(getPointer((*fRowData)[j-1]), getPointer((*fRowData)[j])))
|
||||
continue;
|
||||
// get partitions
|
||||
if (fPartitionBy.get() != NULL && !fStep->cancelled())
|
||||
{
|
||||
int64_t i = 0;
|
||||
int64_t j = 1;
|
||||
int64_t rowCnt = fRowData->size();
|
||||
|
||||
fPartition.push_back(make_pair(i, j-1));
|
||||
i = j;
|
||||
}
|
||||
fPartition.push_back(make_pair(i, j-1));
|
||||
}
|
||||
else
|
||||
{
|
||||
fPartition.push_back(make_pair(0, fRowData->size()));
|
||||
}
|
||||
for (j = 1; j < rowCnt; j++)
|
||||
{
|
||||
if ((*(fPartitionBy.get()))
|
||||
(getPointer((*fRowData)[j - 1]), getPointer((*fRowData)[j])))
|
||||
continue;
|
||||
|
||||
// compute partition by partition
|
||||
int64_t uft = fFrame->upper()->boundType();
|
||||
int64_t lft = fFrame->lower()->boundType();
|
||||
bool upperUbnd = (uft == WF__UNBOUNDED_PRECEDING || uft == WF__UNBOUNDED_FOLLOWING);
|
||||
bool lowerUbnd = (lft == WF__UNBOUNDED_PRECEDING || lft == WF__UNBOUNDED_FOLLOWING);
|
||||
bool upperCnrw = (uft == WF__CURRENT_ROW);
|
||||
bool lowerCnrw = (lft == WF__CURRENT_ROW);
|
||||
fFunctionType->setRowData(fRowData);
|
||||
fFunctionType->setRowMetaData(fRowGroup,fRow);
|
||||
fFrame->setRowData(fRowData);
|
||||
fFrame->setRowMetaData(fRowGroup, fRow);
|
||||
for (uint64_t k = 0; k < fPartition.size() && !fStep->cancelled(); k++)
|
||||
{
|
||||
fFunctionType->resetData();
|
||||
fFunctionType->partition(fPartition[k]);
|
||||
fPartition.push_back(make_pair(i, j - 1));
|
||||
i = j;
|
||||
}
|
||||
|
||||
int64_t begin = fPartition[k].first;
|
||||
int64_t end = fPartition[k].second;
|
||||
if (upperUbnd && lowerUbnd)
|
||||
{
|
||||
fFunctionType->operator()(begin, end, WF__BOUND_ALL);
|
||||
}
|
||||
else if (upperUbnd && lowerCnrw)
|
||||
{
|
||||
if (fFrame->unit() == WF__FRAME_ROWS)
|
||||
{
|
||||
for (int64_t i = begin; i <= end && !fStep->cancelled(); i++)
|
||||
{
|
||||
fFunctionType->operator()(begin, i, i);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int64_t i = begin; i <= end && !fStep->cancelled(); i++)
|
||||
{
|
||||
pair<int64_t, int64_t> w = fFrame->getWindow(begin, end, i);
|
||||
int64_t j = i;
|
||||
if (w.second > i)
|
||||
j = w.second;
|
||||
fFunctionType->operator()(begin, j, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (upperCnrw && lowerUbnd)
|
||||
{
|
||||
if (fFrame->unit() == WF__FRAME_ROWS)
|
||||
{
|
||||
for (int64_t i = end; i >= begin && !fStep->cancelled(); i--)
|
||||
{
|
||||
fFunctionType->operator()(i, end, i);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int64_t i = end; i >= begin && !fStep->cancelled(); i--)
|
||||
{
|
||||
pair<int64_t, int64_t> w = fFrame->getWindow(begin, end, i);
|
||||
int64_t j = i;
|
||||
if (w.first < i)
|
||||
j = w.first;
|
||||
fFunctionType->operator()(j, end, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
pair<int64_t, int64_t> w;
|
||||
pair<int64_t, int64_t> prevFrame;
|
||||
int64_t b, e;
|
||||
bool firstTime = true;
|
||||
for (int64_t i = begin; i <= end && !fStep->cancelled(); i++)
|
||||
{
|
||||
w = fFrame->getWindow(begin, end, i);
|
||||
b = w.first;
|
||||
e = w.second;
|
||||
if (firstTime)
|
||||
{
|
||||
prevFrame = w;
|
||||
}
|
||||
// UDAnF functions may have a dropValue function implemented.
|
||||
// If they do, we can optimize by calling dropValue() for those
|
||||
// values leaving the window and nextValue for those entering, rather
|
||||
// than a resetData() and then iterating over the entire window.
|
||||
// Built-in functions may have this functionality added in the future.
|
||||
if (fFunctionType->dropValues(prevFrame.first, w.first))
|
||||
{
|
||||
b = firstTime ? w.first : prevFrame.second+1;
|
||||
}
|
||||
else
|
||||
{
|
||||
fFunctionType->resetData();
|
||||
}
|
||||
fFunctionType->operator()(b, e, i);
|
||||
prevFrame = w;
|
||||
firstTime = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IDBExcept& iex)
|
||||
{
|
||||
fStep->handleException(iex.what(), iex.errorCode());
|
||||
}
|
||||
catch(const std::exception& ex)
|
||||
{
|
||||
fStep->handleException(ex.what(), logging::ERR_EXECUTE_WINDOW_FUNCTION);
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
fStep->handleException("unknow exception", logging::ERR_EXECUTE_WINDOW_FUNCTION);
|
||||
}
|
||||
fPartition.push_back(make_pair(i, j - 1));
|
||||
}
|
||||
else
|
||||
{
|
||||
fPartition.push_back(make_pair(0, fRowData->size()));
|
||||
}
|
||||
|
||||
// compute partition by partition
|
||||
int64_t uft = fFrame->upper()->boundType();
|
||||
int64_t lft = fFrame->lower()->boundType();
|
||||
bool upperUbnd = (uft == WF__UNBOUNDED_PRECEDING || uft == WF__UNBOUNDED_FOLLOWING);
|
||||
bool lowerUbnd = (lft == WF__UNBOUNDED_PRECEDING || lft == WF__UNBOUNDED_FOLLOWING);
|
||||
bool upperCnrw = (uft == WF__CURRENT_ROW);
|
||||
bool lowerCnrw = (lft == WF__CURRENT_ROW);
|
||||
fFunctionType->setRowData(fRowData);
|
||||
fFunctionType->setRowMetaData(fRowGroup, fRow);
|
||||
fFrame->setRowData(fRowData);
|
||||
fFrame->setRowMetaData(fRowGroup, fRow);
|
||||
|
||||
for (uint64_t k = 0; k < fPartition.size() && !fStep->cancelled(); k++)
|
||||
{
|
||||
fFunctionType->resetData();
|
||||
fFunctionType->partition(fPartition[k]);
|
||||
|
||||
int64_t begin = fPartition[k].first;
|
||||
int64_t end = fPartition[k].second;
|
||||
|
||||
if (upperUbnd && lowerUbnd)
|
||||
{
|
||||
fFunctionType->operator()(begin, end, WF__BOUND_ALL);
|
||||
}
|
||||
else if (upperUbnd && lowerCnrw)
|
||||
{
|
||||
if (fFrame->unit() == WF__FRAME_ROWS)
|
||||
{
|
||||
for (int64_t i = begin; i <= end && !fStep->cancelled(); i++)
|
||||
{
|
||||
fFunctionType->operator()(begin, i, i);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int64_t i = begin; i <= end && !fStep->cancelled(); i++)
|
||||
{
|
||||
pair<int64_t, int64_t> w = fFrame->getWindow(begin, end, i);
|
||||
int64_t j = i;
|
||||
|
||||
if (w.second > i)
|
||||
j = w.second;
|
||||
|
||||
fFunctionType->operator()(begin, j, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (upperCnrw && lowerUbnd)
|
||||
{
|
||||
if (fFrame->unit() == WF__FRAME_ROWS)
|
||||
{
|
||||
for (int64_t i = end; i >= begin && !fStep->cancelled(); i--)
|
||||
{
|
||||
fFunctionType->operator()(i, end, i);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int64_t i = end; i >= begin && !fStep->cancelled(); i--)
|
||||
{
|
||||
pair<int64_t, int64_t> w = fFrame->getWindow(begin, end, i);
|
||||
int64_t j = i;
|
||||
|
||||
if (w.first < i)
|
||||
j = w.first;
|
||||
|
||||
fFunctionType->operator()(j, end, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
pair<int64_t, int64_t> w;
|
||||
pair<int64_t, int64_t> prevFrame;
|
||||
int64_t b, e;
|
||||
bool firstTime = true;
|
||||
|
||||
for (int64_t i = begin; i <= end && !fStep->cancelled(); i++)
|
||||
{
|
||||
w = fFrame->getWindow(begin, end, i);
|
||||
b = w.first;
|
||||
e = w.second;
|
||||
|
||||
if (firstTime)
|
||||
{
|
||||
prevFrame = w;
|
||||
}
|
||||
|
||||
// UDAnF functions may have a dropValue function implemented.
|
||||
// If they do, we can optimize by calling dropValue() for those
|
||||
// values leaving the window and nextValue for those entering, rather
|
||||
// than a resetData() and then iterating over the entire window.
|
||||
// Built-in functions may have this functionality added in the future.
|
||||
if (fFunctionType->dropValues(prevFrame.first, w.first))
|
||||
{
|
||||
b = firstTime ? w.first : prevFrame.second + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
fFunctionType->resetData();
|
||||
}
|
||||
|
||||
fFunctionType->operator()(b, e, i);
|
||||
prevFrame = w;
|
||||
firstTime = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IDBExcept& iex)
|
||||
{
|
||||
fStep->handleException(iex.what(), iex.errorCode());
|
||||
}
|
||||
catch (const std::exception& ex)
|
||||
{
|
||||
fStep->handleException(ex.what(), logging::ERR_EXECUTE_WINDOW_FUNCTION);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
fStep->handleException("unknow exception", logging::ERR_EXECUTE_WINDOW_FUNCTION);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void WindowFunction::setCallback(joblist::WindowFunctionStep* step, int id)
|
||||
{
|
||||
fStep = step;
|
||||
fId = id;
|
||||
fFunctionType->setCallback(step);
|
||||
fFrame->setCallback(step);
|
||||
fStep = step;
|
||||
fId = id;
|
||||
fFunctionType->setCallback(step);
|
||||
fFrame->setCallback(step);
|
||||
}
|
||||
|
||||
|
||||
const Row& WindowFunction::getRow() const
|
||||
{
|
||||
return fRow;
|
||||
return fRow;
|
||||
}
|
||||
|
||||
|
||||
void WindowFunction::sort(std::vector<RowPosition>::iterator v, uint64_t n)
|
||||
{
|
||||
// recursive function termination condition.
|
||||
if (n < 2 || fStep->cancelled())
|
||||
return;
|
||||
// recursive function termination condition.
|
||||
if (n < 2 || fStep->cancelled())
|
||||
return;
|
||||
|
||||
RowPosition p = *(v + n/2); // pivot value
|
||||
vector<RowPosition>::iterator l = v; // low address
|
||||
vector<RowPosition>::iterator h = v + (n - 1); // high address
|
||||
while (l <= h && !(fStep->cancelled()))
|
||||
{
|
||||
// Can use while here, but need check boundary and cancel status.
|
||||
if (fOrderBy->operator()(getPointer(*l), getPointer(p)))
|
||||
{
|
||||
l++;
|
||||
}
|
||||
else if (fOrderBy->operator()(getPointer(p), getPointer(*h)))
|
||||
{
|
||||
h--;
|
||||
}
|
||||
else
|
||||
{
|
||||
RowPosition t = *l; // temp value for swap
|
||||
*l++ = *h;
|
||||
*h-- = t;
|
||||
}
|
||||
}
|
||||
RowPosition p = *(v + n / 2); // pivot value
|
||||
vector<RowPosition>::iterator l = v; // low address
|
||||
vector<RowPosition>::iterator h = v + (n - 1); // high address
|
||||
|
||||
sort(v, distance(v, h) + 1);
|
||||
sort(l, distance(l, v) + n);
|
||||
while (l <= h && !(fStep->cancelled()))
|
||||
{
|
||||
// Can use while here, but need check boundary and cancel status.
|
||||
if (fOrderBy->operator()(getPointer(*l), getPointer(p)))
|
||||
{
|
||||
l++;
|
||||
}
|
||||
else if (fOrderBy->operator()(getPointer(p), getPointer(*h)))
|
||||
{
|
||||
h--;
|
||||
}
|
||||
else
|
||||
{
|
||||
RowPosition t = *l; // temp value for swap
|
||||
*l++ = *h;
|
||||
*h-- = t;
|
||||
}
|
||||
}
|
||||
|
||||
sort(v, distance(v, h) + 1);
|
||||
sort(l, distance(l, v) + n);
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user