-
Notifications
You must be signed in to change notification settings - Fork 63
/
ssq.cpp
156 lines (123 loc) · 4.33 KB
/
ssq.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* Copyright (C) 2017 MariaDB Corporaton
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; version 2 of
the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA. */
#include <sstream>
#include <cstring>
#include <typeinfo>
#include "ssq.h"
#include "bytestream.h"
#include "objectreader.h"
using namespace mcsv1sdk;
#define DATATYPE double
struct ssq_data
{
uint64_t scale;
DATATYPE sumsq;
ssq_data() : scale(0) {}
};
#define OUT_TYPE int64_t
mcsv1_UDAF::ReturnCode ssq::init(mcsv1Context* context,
ColumnDatum* colTypes)
{
if (context->getParameterCount() < 1)
{
// The error message will be prepended with
// "The storage engine for the table doesn't support "
context->setErrorMessage("ssq() with 0 arguments");
return mcsv1_UDAF::ERROR;
}
if (context->getParameterCount() > 1)
{
context->setErrorMessage("ssq() with more than 1 argument");
return mcsv1_UDAF::ERROR;
}
if (!(isNumeric(colTypes[0].dataType)))
{
// The error message will be prepended with
// "The storage engine for the table doesn't support "
context->setErrorMessage("ssq() with non-numeric argument");
return mcsv1_UDAF::ERROR;
}
context->setUserDataSize(sizeof(ssq_data));
context->setResultType(CalpontSystemCatalog::DOUBLE);
context->setColWidth(8);
context->setScale(context->getScale() * 2);
context->setPrecision(19);
context->setRunFlag(mcsv1sdk::UDAF_IGNORE_NULLS);
return mcsv1_UDAF::SUCCESS;
}
mcsv1_UDAF::ReturnCode ssq::reset(mcsv1Context* context)
{
struct ssq_data* data = (struct ssq_data*)context->getUserData()->data;
if (data)
{
data->scale = 0;
data->sumsq = 0;
}
return mcsv1_UDAF::SUCCESS;
}
mcsv1_UDAF::ReturnCode ssq::nextValue(mcsv1Context* context, ColumnDatum* valsIn)
{
static_any::any& valIn = valsIn[0].columnData;
struct ssq_data* data = (struct ssq_data*)context->getUserData()->data;
if (context->isParamNull(0) || valIn.empty())
{
return mcsv1_UDAF::SUCCESS;
}
DATATYPE val = convertAnyTo<double>(valIn);
// For decimal types, we need to move the decimal point.
uint32_t scale = valsIn[0].scale;
if (val != 0 && scale > 0)
{
val /= pow(10.0, (double)scale);
}
data->sumsq += val * val;
return mcsv1_UDAF::SUCCESS;
}
mcsv1_UDAF::ReturnCode ssq::subEvaluate(mcsv1Context* context, const UserData* userDataIn)
{
// If we turn off UDAF_IGNORE_NULLS in init(), then NULLS may be sent here in cases of Joins.
// When a NULL value is sent here, userDataIn will be NULL, so check for NULLS.
if (context->isParamNull(0))
{
return mcsv1_UDAF::SUCCESS;
}
struct ssq_data* outData = (struct ssq_data*)context->getUserData()->data;
struct ssq_data* inData = (struct ssq_data*)userDataIn->data;
outData->sumsq += inData->sumsq;
return mcsv1_UDAF::SUCCESS;
}
mcsv1_UDAF::ReturnCode ssq::evaluate(mcsv1Context* context, static_any::any& valOut)
{
struct ssq_data* data = (struct ssq_data*)context->getUserData()->data;
valOut = data->sumsq;
return mcsv1_UDAF::SUCCESS;
}
mcsv1_UDAF::ReturnCode ssq::dropValue(mcsv1Context* context, ColumnDatum* valsDropped)
{
static_any::any& valIn = valsDropped[0].columnData;
struct ssq_data* data = (struct ssq_data*)context->getUserData()->data;
if (valIn.empty())
{
return mcsv1_UDAF::SUCCESS; // Ought not happen when UDAF_IGNORE_NULLS is on.
}
DATATYPE val = convertAnyTo<double>(valIn);
// For decimal types, we need to move the decimal point.
uint32_t scale = valsDropped[0].scale;
if (val != 0 && scale > 0)
{
val /= pow(10.0, (double)scale);
}
data->sumsq -= val * val;
return mcsv1_UDAF::SUCCESS;
}