Aggregate and Scalar functions with the same name

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Aggregate and Scalar functions with the same name

Keith Medcalf

Ok, I see why you cannot use the same name.  Queries of the form:

select func(x) from y;

would be ambiguous if there were both an aggregate func and a scalar func, and the vdbe code generator uses the type of func (whether aggregate or scaler) to determine whether to interpret the query as an aggregate (returning a single value) or scalar (return one value per row).  Simple example, but there are many others where choosing the intended version of func would change the query result possibly in a way other than intended.

If you phrase a query that requires an aggregate function (such as select x, func(y) from x group by x) then if func is scalar an error is thrown.  If you did a select x, func(y) from x and func is an aggregate function no such problem arises as long as func is a valid aggregate for singletons.

So unless something is definitely not working as intended, I think that you should not be able to declare functions in both aggregate and scalar form with the same name and the current code is correct.

> -----Original Message-----
> From: sqlite-users [mailto:[hidden email]]
> On Behalf Of Keith Medcalf
> Sent: Thursday, 9 March, 2017 15:33
> To: SQLite mailing list
> Subject: Re: [sqlite] More built-in functions for basic math
>
>
> I get complaints from the compiler about duplicate definitions of macro's
> and functions ...
>
> As an aside, I am trying to walk db->aFunc as follows:
>
>     sqlite3 *db = sqlite3_context_db_handle(context);
>     Hash *h = &(db->aFunc);
>     HashElem *p;
>     FuncDef *d;
>     char *functype;
>     for (p = h->first; p; p = p->next)
>     {
>         d = p->data;
>         functype = d->xFinalize ? "aggregate" : "scalar";
>         printf("%s  %s %d %d\n", d->zName, functype, d->nArg, d-
> >funcFlags);
>         while (d->pNext)
>         {
>             d = d->pNext;
>             functype = d->xFinalize ? "aggregate" : "scalar";
>             printf("%s* %s %d %d\n", d->zName, functype, d->nArg, d-
> >funcFlags);
>         }
>     }
>
> but for functions which have both a scalar and aggregate form only the
> aggregate is showing up (actually, only the last version registered is
> showing up -- so which one shows up is scalar/aggregate is determined by
> which one was defined last).
>
> When I asked you about this you said that I simply need to register the
> function twice, once as an aggregate function and once as a scaler
> function, but it does not appear to actually keep the two different
> definitions.  Is this a bug or working as intended?
>
>
> > -----Original Message-----
> > From: sqlite-users [mailto:[hidden email]]
> > On Behalf Of Richard Hipp
> > Sent: Thursday, 9 March, 2017 13:08
> > To: SQLite mailing list
> > Subject: Re: [sqlite] More built-in functions for basic math
> >
> > On 3/9/17, Keith Medcalf <[hidden email]> wrote:
> > >
> > > note that fileio.c and shathree.c are inlined into shell.c, so in
> order
> > to
> > > remove them I have to modify shell.c to ifdef them out when I am
> > building.
> > >
> >
> > Seriously?  I have no trouble loading the external fileio.c and
> > shathree.c extensions on top of the similar extensions that are built
> > into the shell.  Nothing about the shell code has to change.  What
> > part of that is not working for you?
> >
> > --
> > D. Richard Hipp
> > [hidden email]
> > _______________________________________________
> > sqlite-users mailing list
> > [hidden email]
> > http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
>
>
>
> _______________________________________________
> sqlite-users mailing list
> [hidden email]
> http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users



_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: Aggregate and Scalar functions with the same name

Richard Hipp-3
Note that you can have aggregates and scalars with the same name, as
long as they have a different number of arguments.

This comes up with the min() and max() functions.  The two-or-more
argument versions of these functions are scalars.  The one-argument
version is an aggregate.

On 3/9/17, Keith Medcalf <[hidden email]> wrote:

>
> Ok, I see why you cannot use the same name.  Queries of the form:
>
> select func(x) from y;
>
> would be ambiguous if there were both an aggregate func and a scalar func,
> and the vdbe code generator uses the type of func (whether aggregate or
> scaler) to determine whether to interpret the query as an aggregate
> (returning a single value) or scalar (return one value per row).  Simple
> example, but there are many others where choosing the intended version of
> func would change the query result possibly in a way other than intended.
>
> If you phrase a query that requires an aggregate function (such as select x,
> func(y) from x group by x) then if func is scalar an error is thrown.  If
> you did a select x, func(y) from x and func is an aggregate function no such
> problem arises as long as func is a valid aggregate for singletons.
>
> So unless something is definitely not working as intended, I think that you
> should not be able to declare functions in both aggregate and scalar form
> with the same name and the current code is correct.
>
>> -----Original Message-----
>> From: sqlite-users [mailto:[hidden email]]
>> On Behalf Of Keith Medcalf
>> Sent: Thursday, 9 March, 2017 15:33
>> To: SQLite mailing list
>> Subject: Re: [sqlite] More built-in functions for basic math
>>
>>
>> I get complaints from the compiler about duplicate definitions of macro's
>> and functions ...
>>
>> As an aside, I am trying to walk db->aFunc as follows:
>>
>>     sqlite3 *db = sqlite3_context_db_handle(context);
>>     Hash *h = &(db->aFunc);
>>     HashElem *p;
>>     FuncDef *d;
>>     char *functype;
>>     for (p = h->first; p; p = p->next)
>>     {
>>         d = p->data;
>>         functype = d->xFinalize ? "aggregate" : "scalar";
>>         printf("%s  %s %d %d\n", d->zName, functype, d->nArg, d-
>> >funcFlags);
>>         while (d->pNext)
>>         {
>>             d = d->pNext;
>>             functype = d->xFinalize ? "aggregate" : "scalar";
>>             printf("%s* %s %d %d\n", d->zName, functype, d->nArg, d-
>> >funcFlags);
>>         }
>>     }
>>
>> but for functions which have both a scalar and aggregate form only the
>> aggregate is showing up (actually, only the last version registered is
>> showing up -- so which one shows up is scalar/aggregate is determined by
>> which one was defined last).
>>
>> When I asked you about this you said that I simply need to register the
>> function twice, once as an aggregate function and once as a scaler
>> function, but it does not appear to actually keep the two different
>> definitions.  Is this a bug or working as intended?
>>
>>
>> > -----Original Message-----
>> > From: sqlite-users [mailto:[hidden email]]
>> > On Behalf Of Richard Hipp
>> > Sent: Thursday, 9 March, 2017 13:08
>> > To: SQLite mailing list
>> > Subject: Re: [sqlite] More built-in functions for basic math
>> >
>> > On 3/9/17, Keith Medcalf <[hidden email]> wrote:
>> > >
>> > > note that fileio.c and shathree.c are inlined into shell.c, so in
>> order
>> > to
>> > > remove them I have to modify shell.c to ifdef them out when I am
>> > building.
>> > >
>> >
>> > Seriously?  I have no trouble loading the external fileio.c and
>> > shathree.c extensions on top of the similar extensions that are built
>> > into the shell.  Nothing about the shell code has to change.  What
>> > part of that is not working for you?
>> >
>> > --
>> > D. Richard Hipp
>> > [hidden email]
>> > _______________________________________________
>> > sqlite-users mailing list
>> > [hidden email]
>> > http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
>>
>>
>>
>> _______________________________________________
>> sqlite-users mailing list
>> [hidden email]
>> http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
>
>
>
> _______________________________________________
> sqlite-users mailing list
> [hidden email]
> http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
>


--
D. Richard Hipp
[hidden email]
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users