1 /* Copyright (C) 2017 MariaDB Corporation
2 
3    This program is free software; you can redistribute it and/or
4    modify it under the terms of the GNU General Public License
5    as published by the Free Software Foundation; version 2 of
6    the License.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
16    MA 02110-1301, USA. */
17 
18 #include "allnull.h"
19 
20 using namespace mcsv1sdk;
21 
22 struct allnull_data
23 {
24     uint64_t	totalQuantity;
25     uint64_t	totalNulls;
26 };
27 
28 #define OUT_TYPE int64_t
init(mcsv1Context * context,ColumnDatum * colTypes)29 mcsv1_UDAF::ReturnCode allnull::init(mcsv1Context* context,
30                                      ColumnDatum* colTypes)
31 {
32     context->setUserDataSize(sizeof(allnull_data));
33 
34     if (context->getParameterCount() < 1)
35     {
36         // The error message will be prepended with
37         // "The storage engine for the table doesn't support "
38         context->setErrorMessage("allnull() with 0 arguments");
39         return mcsv1_UDAF::ERROR;
40     }
41 
42     context->setResultType(execplan::CalpontSystemCatalog::TINYINT);
43 
44     return mcsv1_UDAF::SUCCESS;
45 }
46 
reset(mcsv1Context * context)47 mcsv1_UDAF::ReturnCode allnull::reset(mcsv1Context* context)
48 {
49     struct allnull_data* data = (struct allnull_data*)context->getUserData()->data;
50     data->totalQuantity = 0;
51     data->totalNulls    = 0;
52     return mcsv1_UDAF::SUCCESS;
53 }
54 
nextValue(mcsv1Context * context,ColumnDatum * valsIn)55 mcsv1_UDAF::ReturnCode allnull::nextValue(mcsv1Context* context, ColumnDatum* valsIn)
56 {
57     struct allnull_data* data = (struct allnull_data*)context->getUserData()->data;
58 
59     for (size_t i = 0; i < context->getParameterCount(); i++)
60     {
61         data->totalQuantity++;
62 
63         if (context->isParamNull(0))
64         {
65             data->totalNulls++;
66         }
67     }
68 
69     return mcsv1_UDAF::SUCCESS;
70 }
71 
subEvaluate(mcsv1Context * context,const UserData * userDataIn)72 mcsv1_UDAF::ReturnCode allnull::subEvaluate(mcsv1Context* context, const UserData* userDataIn)
73 {
74     struct allnull_data* outData = (struct allnull_data*)context->getUserData()->data;
75     struct allnull_data* inData = (struct allnull_data*)userDataIn->data;
76     outData->totalQuantity += inData->totalQuantity;
77     outData->totalNulls += inData->totalNulls;
78     return mcsv1_UDAF::SUCCESS;
79 }
80 
evaluate(mcsv1Context * context,static_any::any & valOut)81 mcsv1_UDAF::ReturnCode allnull::evaluate(mcsv1Context* context, static_any::any& valOut)
82 {
83     OUT_TYPE allNull;
84     struct allnull_data* data = (struct allnull_data*)context->getUserData()->data;
85     allNull = data->totalQuantity > 0 && data->totalNulls == data->totalQuantity;
86     valOut = allNull;
87     return mcsv1_UDAF::SUCCESS;
88 }
89 
90 
91