-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyavg.cpp
83 lines (73 loc) · 2.39 KB
/
myavg.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
#include "Vertica.h"
#include <stdio.h>
#include <fstream>
#include <iostream>
using namespace Vertica;
using namespace std;
class SumEtCount : public TransformFunction {
// Called for each partition in the table
virtual void processPartition(ServerInterface &srvInterface,
PartitionReader &inputReader,
PartitionWriter &outputWriter) {
try {
int count = 0;
vfloat sum = 0;
do {
const vfloat newValue = inputReader.getFloatRef(0);
sum += newValue;
count++;
} while (inputReader.next());
outputWriter.setFloat(0, sum);
outputWriter.setInt(1, count);
//uint64_t sumBin;
//memcpy(&sumBin, &sum, sizeof(sum));
char* sumBin = reinterpret_cast<char*> (&sum);
outputWriter.getStringRef(2).copy(sumBin, sizeof(vfloat));
outputWriter.next();
/*
try {
FILE *binFile;
binFile = fopen("/home/vertica/test.bin", "wb");
fwrite(&sum, sizeof(vfloat), sizeof(sum)/sizeof(vfloat), binFile);
fwrite(&count, sizeof(int), 1, binFile);
fclose(binFile);
ofstream txtFile;
txtFile.open("/home/vertica/test.txt", ios::out | ios::app);
txtFile << "sum" << endl;
txtFile << "count" << endl;;
txtFile.close();
ofstream fs("/home/vertica/test.bin", ios::out | ios::binary | ios::app);
fs.write(reinterpret_cast<const char*>(&sum), sizeof sum);
fs.write(reinterpret_cast<const char*>(&count), sizeof count);
fs.close();
srvInterface.log("sum: %g\n", sum);
srvInterface.log("count: %d\n", count);
} catch (exception &e) {
vt_report_error(0, "exception while making file: [%s]", e.what());
}
*/
} catch (exception &e) {
vt_report_error(0, "exception while processing: [%s]", e.what());
}
}
};
class SumEtCountFactory : public TransformFunctionFactory {
virtual void getPrototype(ServerInterface &srvInterface,
ColumnTypes &argTypes, ColumnTypes &returnType) {
argTypes.addFloat();
returnType.addFloat();
returnType.addInt();
returnType.addLongVarbinary();
}
virtual void getReturnType(ServerInterface &srvInterface,
const SizedColumnTypes &input_types,
SizedColumnTypes &output_types) {
output_types.addFloat("Sum");
output_types.addInt("Count");
output_types.addLongVarbinary(8, "Countbin");
}
virtual TransformFunction *createTransformFunction(ServerInterface &srvInterface) {
return vt_createFuncObj(srvInterface.allocator, SumEtCount);
}
};
RegisterFactory(SumEtCountFactory);