Difference between scalar SQL functions and aggregate SQL functions in SQLite - c++

What is the exact difference between scalar SQL functions and aggregate SQL functions in SQLite?
I am getting a problem with the following code when attempting to create a new function in SQLite:
int sqlite3_create_function(
sqlite3 *db,
const char *zFunctionName,
int nArg,
int eTextRep,
void *pApp,
void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
void (*xStep)(sqlite3_context*,int,sqlite3_value**),
void (*xFinal)(sqlite3_context*)
);
For a scalar SQL function, xStep and xFinal must be null, and for an aggregate SQL function, xFunc must be null. I need to know which one to use and what the difference is.

A scalar function is a function that operates on scalar values -- that is, it takes one (or more) input values as arguments directly and returns a value.
An aggregate function is a function that operates on aggregate data -- that is, it takes a complete set of data as input and returns a value that is computed from all the values in the set.
By the way, these are the standard definitions of "scalar" and "aggregate" that you can find in any dictionary, and all these links are in the top five Google search results if you search for "scalar function" and "aggregate function". That's OK, we want StackOverflow to become the "definitive" answer for questions like this, but in the interest of "teaching a man to fish" I feel compelled to remind you that you should do your own homework before asking other people to help answer your questions.

Related

How to auto run one hundred C++ functions with same parameters?

I have a C++ binary library.
There are almost one hundred C++ functions with different name, but same parameter types and same return type. And the values they parameter can be are equal.
I now want to test all of them with all of parameter values. And maybe return values to txt files.
How can I realize this? I want to store function names in a string list, and use eval() like python, but C++ doesn't have this feature.
Thank you!
In C++ you can create an array of function pointers and then loop over that array, calling each of the functions and giving always the same values to parameters from corresponding unchanging variables.

What does the :variable mean?

I am relatively new to sqlite using C++, I found a statement like
"INSERT INTO files "
"(md5path_1, md5path_2, parent)"
"VALUES (:md5_1, :md5_2, :p)"
Why are those colons used?
These are called Bind Variables
With the use of bind variables the statement remains the same, therefore there is only one statement cached as opposed to many.
This is taken from the introduction to SQLite
SQLite allows a parameter wherever a string literal, numeric constant,
or NULL is allowed. (Parameters may not be used for column or table
names.) A parameter takes one of the following forms:
?
?NNN
:AAA
$AAA
#AAA
In the examples above, NNN is an integer value
and AAA is an identifier. A parameter initially has a value of NULL.
Prior to calling sqlite3_step() for the first time or immediately
after sqlite3_reset(), the application can invoke the sqlite3_bind()
interfaces to attach values to the parameters. Each call to
sqlite3_bind() overrides prior bindings on the same parameter.
So, it's a placeholder you can bind later to a variable using sqlite3_bind()
They called "bind variables". Later in the code you'll definitely see something like
exec_query("INSERT....", val1, val2, val3)
where val1 - val3 will contain value to be inserted.
Using variables has many advantages. In case of insert statement:
It helps to avoid SQL-injections. Thus no one will be able to ruin your query by passing special statements to variables.
You don't have to compose sql-statement every time you need it. You can just use that one from your question passing new parameters each time you run it

Is storing any type of function in one variable possible?

I'm trying to make a menu array where each element is a struct that stores variables for text, key that needs to be pressed to select that item and function called on that key press (something like "Quit", 'Q', Quit()). I thought this would make things more efficient, but I can't find a way to make it work with varied function and parameter types (for example one item should be able to call a void function with no parameters, another a class int function with two parameters and so on). Is there a good way to do this or am I better off giving up on the idea?
Edit: Thank you all for your advice! The proposed solutions feel a little too complex for my newbie self, but attempting to understand them gave me some ideas! I ended up making the third variable hold an enum instead of a direct function call and then created a switch function that calls other functions based on that value.
There are actually a few ways of doing this.
One way is to use std::bind to bind all functions to void func(void) then you can store them equally.
The other way is to create a generic function/lambda which will call your function.
To store your functions you can use std::function.
Also consider overriding operator() of your classes.
The classic way of handling this is to have all the functions take the same parameters, and for those to be very flexible. For example, an integer or enum, and a pointer.
your no-parameter function is passed -1 and nullptr and ignores them
your multi-parameter function casts the pointer to a pointer to some struct or class that holds all the bits and pieces it needs (and of course your calling code made that instance and passed its address)
The reason an enum or integer is hoisted out as one of the parameters is that "command type" is a super popular thing to need, so why do all that casting and extracting to get it?
If you have a performance problem as a result of this approach, then there are others, but this has literally been used for decades in Windows.

c++: class constructor instructions inside or outside braces [duplicate]

This question already has answers here:
Why should I prefer to use member initialization lists?
(9 answers)
Closed 8 years ago.
I am following two different C++ programming courses. One is a mandatory course from my Physics degree, ROOT-oriented, and one is a course from the Informatics degree I chose on my own.
The problem is, the examples they give for constructors in classes are slightly different.
Let's pick, for example, the default ctor for a class named CDate, containing three ints, one for the day, one for the month, and one for the year. If nothing is passed to initialize the date, it is automatically set to the first of January, 1900.
The slide from the Informatics course gives this example:
CDate::CDate(void){
mDay=1;
mMonth=1;
mYear=1900;
}
While the one from my Physics course gives this:
CDate::CDate():
mDay (1),
mMonth (1),
mYear (1900)
{}
They only put things inside the braces if they have to modify some variables already created outside the braces.
Let's say, for example, we want to create a class representing an histogram, giving the minimum value, the maximum one, the number of bins, and then an array containing the values inside every single bin. The complete constructor would look like this:
histogram::histogram (const int &nBin, const float &min, const float &max):
nBin_p (nBin),
min_p (min),
max_p (max),
binContent_p ( new int[nBin_p] )
{
//initialize to zero the bin content
for (int i = 0; i < nBin_p; ++i)
binContent_p[i] = 0;
}
(the variables with _p are the actual ones of the class, defined as private in the class implementation).
I guess the "better" one is the one by the Informatics, they should know better, right?But still, I wonder, what are in a nutshell the actual differences between the two ways, as slight as they can be? When should I better use one and when the other one, or are they completely equivalent?
The informatics' approach actually sounds very C-derived, they do not take advantage of some C++ "pecularities". They just do some assignments to those member variables.
The physics rather do initialization. At first sight the difference might only seem aesthetic, and probably won't make much of a difference in practice as long as the member variables you are initializing are fundamental types.
But what if you have a member variable that is of an object type and which does not have a void constructor? To invoke a constructor with arguments for that type you can only use the Physics' approach (which is called initialization list, by the way).
The one from your Physics example is called a initialization list, it's the preferred way to initialize member variables. see here for more explanation.
The second syntax is the only option for initializing member object-variables (it's really a constructor call). For simple types, such as int, long, etc., there is only small difference in execution order, which could be always fixed by hand.

What is an appropriate naming convention for a merge function's parameters?

I am writing a C++ function that takes two Foo objects and adds the elements of the first into the second one, like so:
MyFunction(const Foo& a, Foo* b) {
for (int i = 0; i < a.bar_count(); i++) {
b->Bar(i) = b->HasBar(i) ? Average(a.Bar(i), b->Bar(i)) : a.Bar(i);
}
}
This is a toy example, obviously, but my point is: what should be the names of MyFunction, a and b? My first thought was something like MergeFoo but then I am not sure what to name the parameters because my best thought is something like merger and mergee which seems dumb.
What are good names for a, b, and MyFunction so that the API will be clear for users of this function?
I'd call them merge(), src and dest, but there are of course other good names.
from and into seem like they might be appropriate, but personally I'd make it a method of your Foo class. That means there's no ambiguous parameter order, no inconvenient parameter naming, etc... Foo::merge(const Foo& other_foo)
Incidentally, I'd consider calling it something different. Merging for me suggests that the end result is the set union of the original components, whereas you're doing some sort of moving average computation, or similar. I don't have a good alternative name to suggest, because I don't know the actual purpose or functionality of Foo, Bar or Average, so that'll have to be up to you!
That looks like an inplace zip-then-average operation. ( http://docs.python.org/2/library/functions.html#zip )
So instead of indexed access, have iterator access. Then http://www.boost.org/doc/libs/1_41_0/libs/iterator/doc/zip_iterator.html zip the two, then http://www.boost.org/doc/libs/1_35_0/libs/iterator/doc/transform_iterator.html transform the result back into the first iterator (conditional average).
This does get a bit annoying to write in C++.
iterator over boost::optional<bar>, zipped into a pair, then a boost::optional<bar> is produced by taking one or averaging if both exist.
So, left.ZipThenAverageInPlace( right )?
A common C++ naming convention would be lhs and rhs .That means left-hand-side and right-hand side. Usually, if the function modifies one of its parameters, that would be lhs. So in your example:
void MyFunction(Foo& lhs, const Foo& rhs);
Note that the order is swapped from your example.
For example, see the boost library, e.g. this: http://www.boost.org/doc/libs/1_52_0/libs/utility/operators.htm
what a maintainer would expect from 'merge' on container like objects would be functionality according to merge. what you want to accomplish is in the STL named transform with a binary operator named 'average'. if you don't use STL algorithm I'd name the function 'average' to avoid 'merge'
After considering all the options, I decided the best choice is something like AverageBarsInPlace(const Foo& new_bars, Foo* existing_bars);. I like the "InPlace" suffix from Yakk. I agree with may responses that say "don't call it Merge unless it's actually doing a merge".
However, my real take-away from the lack of consensus on this question is: this function has a poorly defined scope. I should break the function into more cohesive chunks, such as one function to do merging, and another to do averaging.
One could define a merge function as this: An instance absorbs elements released by another instance. So this leads me to use the parameter names absorber and releaser
In PHP this result in the following function definition:
public function merge(ClassName $absorber, ClassName $releaser): void