postgresql allows for comparing result columns via `LEAST`
sqlite allows for comparing result columns via `min`  

so the correct query is...

   postgresql - ... order by least(col_a, col_b)
   sqlite - ... order by min(col_a, col_b)

I tossed together a custom compiler that works for my use-case (hardcoded 
to 2 columns and a date field), but it's pretty simple/bad

does anyone have a better idea how to approach this?


====


class min_date(expression.FunctionElement):
    type = sqlalchemy.types.DateTime()
    name = 'min_date'


@compiles(min_date)
def min_date__default(element, compiler, **kw):
    # return compiler.visit_function(element)
    """
    # just return the first date
    """
    args = list(element.clauses)
    return compiler.process(args[0])


@compiles(min_date, 'postgresql')
def min_date__postgresql(element, compiler, **kw):
    """
    # select least(col_a, col_b);
    """
    args = list(element.clauses)
    return "LEAST(%s, %s)" % (
        compiler.process(args[0]),
        compiler.process(args[1]),
    )


@compiles(min_date, 'sqlite')
def min_date__sqlite(element, compiler, **kw):
    """
    # select min(col_a, col_b);
    """
    args = list(element.clauses)
    return "min(%s, %s)" % (
        compiler.process(args[0]),
        compiler.process(args[1]),
    )

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to