22#include "BaseFunction.h"
23#include "velox/exec/tests/utils/AssertQueryBuilder.h"
24#include "velox/exec/tests/utils/PlanBuilder.h"
25#include "velox/exec/tests/utils/TempDirectoryPath.h"
26#include "velox/vector/tests/utils/VectorTestBase.h"
28using namespace facebook::velox;
29using namespace facebook::velox::test;
30using namespace facebook::velox::exec::test;
31using namespace facebook::velox::memory;
53 const SelectivityVector& rows,
54 std::vector<VectorPtr>& args,
56 exec::EvalCtx& context,
57 VectorPtr& output)
const override {
58 BaseVector::ensureWritable(rows, type, context.pool(), output);
61 int* inputValues = input->as<FlatVector<int>>()->values()->asMutable<
int>();
63 int inputSize = rows.size();
65 std::vector<int> result(rows.size());
67 for (
int i = 0; i < inputSize; i++) {
68 result[i] = (inputValues[i] >= 3) ? 1 : 0;
71 VectorMaker maker{context.pool()};
72 output = maker.flatVector<
int>(result, INTEGER());
79 static std::vector<std::shared_ptr<exec::FunctionSignature>>
signatures() {
80 return {exec::FunctionSignatureBuilder()
81 .argumentType(
"INTEGER")
82 .returnType(
"INTEGER")
91 return "change_rating";
131 const SelectivityVector& rows,
132 std::vector<VectorPtr>& args,
134 exec::EvalCtx& context,
135 VectorPtr& output)
const override {
136 BaseVector::ensureWritable(rows, type, context.pool(), output);
138 auto input = args[0];
139 int* inputValues = input->as<FlatVector<int>>()->values()->asMutable<
int>();
141 int inputSize = rows.size();
143 std::vector<std::vector<int>> result(rows.size(), std::vector<int>(1));
145 for (
int i = 0; i < inputSize; i++) {
146 result[i][0] = inputValues[i];
149 VectorMaker maker{context.pool()};
150 output = maker.arrayVector<
int>(result, INTEGER());
157 static std::vector<std::shared_ptr<exec::FunctionSignature>>
signatures() {
158 return {exec::FunctionSignatureBuilder()
159 .argumentType(
"INTEGER")
160 .returnType(
"array(INTEGER)")
169 return "convert_int_array";
209 const SelectivityVector& rows,
210 std::vector<VectorPtr>& args,
212 exec::EvalCtx& context,
213 VectorPtr& output)
const override {
214 BaseVector::ensureWritable(rows, type, context.pool(), output);
216 auto input = args[0];
218 input->as<FlatVector<float>>()->values()->asMutable<
float>();
220 int inputSize = rows.size();
222 std::vector<std::vector<float>> result(rows.size(), std::vector<float>(1));
224 for (
int i = 0; i < inputSize; i++) {
225 result[i][0] = inputValues[i];
228 VectorMaker maker{context.pool()};
229 output = maker.arrayVector<
float>(result, REAL());
236 static std::vector<std::shared_ptr<exec::FunctionSignature>>
signatures() {
237 return {exec::FunctionSignatureBuilder()
238 .argumentType(
"REAL")
239 .returnType(
"array(REAL)")
248 return "convert_float_array";
288 const SelectivityVector& rows,
289 std::vector<VectorPtr>& args,
291 exec::EvalCtx& context,
292 VectorPtr& output)
const override {
293 BaseVector::ensureWritable(rows, type, context.pool(), output);
295 auto input = args[0];
296 double* inputValues =
297 input->as<FlatVector<double>>()->values()->asMutable<
double>();
299 int inputSize = rows.size();
301 std::vector<std::vector<float>> result(rows.size(), std::vector<float>(1));
303 for (
int i = 0; i < inputSize; i++) {
304 result[i][0] = inputValues[i];
307 VectorMaker maker{context.pool()};
308 output = maker.arrayVector<
float>(result, REAL());
315 static std::vector<std::shared_ptr<exec::FunctionSignature>>
signatures() {
316 return {exec::FunctionSignatureBuilder()
317 .argumentType(
"DOUBLE")
318 .returnType(
"array(REAL)")
327 return "convert_double_to_float_array";
367 const SelectivityVector& rows,
368 std::vector<VectorPtr>& args,
370 exec::EvalCtx& context,
371 VectorPtr& output)
const override {
372 BaseVector::ensureWritable(rows, type, context.pool(), output);
376 BaseVector* input = args[0].get();
378 exec::LocalDecodedVector inputHolder(context, *input, rows);
379 auto decodedInputArray = inputHolder.get();
380 auto baseInputArray =
381 decodedInputArray->base()->as<ArrayVector>()->elements();
383 double* inputValues = baseInputArray->values()->asMutable<
double>();
385 int numRawInput = rows.size();
386 int numInput = rows.countSelected();
387 int numElements = baseInputArray->size();
388 int sizeOfArray = numElements / numInput;
390 std::vector<std::vector<float>> result(
391 numRawInput, std::vector<float>(sizeOfArray));
392 int processedIndex = 0;
393 for (
int i = 0; i < numRawInput; i++) {
394 if (!rows.isValid(i)) {
399 inputValues + processedIndex * sizeOfArray,
400 inputValues + (processedIndex + 1) * sizeOfArray,
402 [](
double val) {
return static_cast<float>(val); });
406 VectorMaker maker{context.pool()};
407 auto localResult = maker.arrayVector<
float>(result, REAL());
408 context.moveOrCopyResult(localResult, rows, output);
415 static std::vector<std::shared_ptr<exec::FunctionSignature>>
signatures() {
416 return {exec::FunctionSignatureBuilder()
417 .argumentType(
"array(DOUBLE)")
418 .returnType(
"array(REAL)")
427 return "convert_double_array_to_float_array";
453 std::ifstream fs(path, std::ios::in | std::ios::binary);
455 std::cerr <<
"Cannot open " << path << std::endl;
459 fs.seekg(0, std::ios::end);
460 size_t size =
static_cast<size_t>(fs.tellg());
461 fs.seekg(0, std::ios::beg);
463 fs.read(data.data(), size);
473 std::string lowerStr = str;
474 std::transform(lowerStr.begin(), lowerStr.end(), lowerStr.begin(), ::tolower);
475 return (lowerStr ==
"true");
484 char const* val = getenv(key.c_str());
485 return val == NULL ? std::string() : std::string(val);
495 std::ifstream file(path);
496 if (!file.is_open()) {
497 std::cerr <<
"Cannot open " << path << std::endl;
501 std::getline(file, line);
502 numRows = std::stoi(line);
503 std::getline(file, line);
504 numCols = std::stoi(line);
516 const std::vector<std::vector<T>>& vec2D,
520 for (
const auto& row : vec2D) {
521 totalSize += row.size();
525 T* flatArray =
new T[totalSize];
529 for (
const auto& row : vec2D) {
530 std::copy(row.begin(), row.end(), ptr);
556 std::istringstream stream(input);
560 while (stream >> word) {
std::string getEnvVar(std::string const &key)
Retrieves the value of an environment variable.
Definition UtilFunction.h:483
void readDataStats(const std::string &path, int &numRows, int &numCols)
Reads the number of rows and columns from a data statistics file.
Definition UtilFunction.h:494
int countWords(const std::string &input)
Counts the number of words in a string.
Definition UtilFunction.h:555
bool stringToBool(const std::string &str)
Converts a string to a boolean value.
Definition UtilFunction.h:472
T * flattenVectorToPointer(const std::vector< std::vector< T > > &vec2D, size_t &totalSize)
Flattens a 2D vector into a 1D array.
Definition UtilFunction.h:515
std::string LoadBytesFromFile(const std::string &path)
Loads bytes from a file into a string.
Definition UtilFunction.h:452
void apply(const SelectivityVector &rows, std::vector< VectorPtr > &args, const TypePtr &type, exec::EvalCtx &context, VectorPtr &output) const override
Applies the function to change ratings to binary values.
Definition UtilFunction.h:52
void setWeight()
Sets the weight for the function.
Definition UtilFunction.h:106
float * getTensor() const override
Returns the tensor associated with the function.
Definition UtilFunction.h:98
ChangeRating()
Default constructor for ChangeRating.
Definition UtilFunction.h:42
static std::string getName()
Returns the name of the function.
Definition UtilFunction.h:90
static std::vector< std::shared_ptr< exec::FunctionSignature > > signatures()
Returns the function signatures.
Definition UtilFunction.h:79
void setWeight()
Sets the weight for the function.
Definition UtilFunction.h:442
void apply(const SelectivityVector &rows, std::vector< VectorPtr > &args, const TypePtr &type, exec::EvalCtx &context, VectorPtr &output) const override
Applies the function to convert a double array to a float array.
Definition UtilFunction.h:366
static std::vector< std::shared_ptr< exec::FunctionSignature > > signatures()
Returns the function signatures.
Definition UtilFunction.h:415
static std::string getName()
Returns the name of the function.
Definition UtilFunction.h:426
ConvertDoubleArrayToFloatArray()
Default constructor for ConvertDoubleArrayToFloatArray.
Definition UtilFunction.h:356
float * getTensor() const override
Returns the tensor associated with the function.
Definition UtilFunction.h:434
static std::vector< std::shared_ptr< exec::FunctionSignature > > signatures()
Returns the function signatures.
Definition UtilFunction.h:315
static std::string getName()
Returns the name of the function.
Definition UtilFunction.h:326
void apply(const SelectivityVector &rows, std::vector< VectorPtr > &args, const TypePtr &type, exec::EvalCtx &context, VectorPtr &output) const override
Applies the function to convert a double vector to a float array.
Definition UtilFunction.h:287
float * getTensor() const override
Returns the tensor associated with the function.
Definition UtilFunction.h:334
ConvertDoubleToFloatArray()
Default constructor for ConvertDoubleToFloatArray.
Definition UtilFunction.h:277
void setWeight()
Sets the weight for the function.
Definition UtilFunction.h:342
void apply(const SelectivityVector &rows, std::vector< VectorPtr > &args, const TypePtr &type, exec::EvalCtx &context, VectorPtr &output) const override
Applies the function to convert a float vector to a float array.
Definition UtilFunction.h:208
void setWeight()
Sets the weight for the function.
Definition UtilFunction.h:263
static std::vector< std::shared_ptr< exec::FunctionSignature > > signatures()
Returns the function signatures.
Definition UtilFunction.h:236
ConvertToFloatArray()
Default constructor for ConvertToFloatArray.
Definition UtilFunction.h:198
static std::string getName()
Returns the name of the function.
Definition UtilFunction.h:247
float * getTensor() const override
Returns the tensor associated with the function.
Definition UtilFunction.h:255
static std::vector< std::shared_ptr< exec::FunctionSignature > > signatures()
Returns the function signatures.
Definition UtilFunction.h:157
void setWeight()
Sets the weight for the function.
Definition UtilFunction.h:184
void apply(const SelectivityVector &rows, std::vector< VectorPtr > &args, const TypePtr &type, exec::EvalCtx &context, VectorPtr &output) const override
Applies the function to convert an integer vector to an integer array.
Definition UtilFunction.h:130
float * getTensor() const override
Returns the tensor associated with the function.
Definition UtilFunction.h:176
ConvertToIntArray()
Default constructor for ConvertToIntArray.
Definition UtilFunction.h:120
static std::string getName()
Returns the name of the function.
Definition UtilFunction.h:168
A base class for machine learning functions, inheriting from Velox's VectorFunction.
Definition BaseFunction.h:9