Martin Thoma
Martin Thoma

Reputation: 136625

How can I use Snowflakes "least" with Pypika?

Snowflake has a function called LEAST which can be applied to two columns and returns the lower value for each row.

How can I use that function when building a SnowflakeQuery with pypika?

Upvotes: 0

Views: 341

Answers (1)

user13472370
user13472370

Reputation:

Pypika allows you to define additional functions not provided in their default function lists.

To define LEAST in it, the following form can be used, mirroring pypika.functions.Concat that also uses a similar variable-argument signature:

class Least(Function):
    def __init__(self, *terms, **kwargs):
        super(Least, self).__init__('LEAST', *terms, **kwargs)

Example usage:

>>> from pypika.functions import Function

>>> class Least(Function):
...     def __init__(self, *terms, **kwargs):
...         super(Least, self).__init__('LEAST', *terms, **kwargs)
... 

>>> q = Query.from_(customers).select(
...     customers.id,
...     Least(3, 2, 1).as_('least_example'),
... )

>>> q
SELECT "id",LEAST(3,2,1) "least_example" FROM "customers"

Upvotes: 3

Related Questions