From 03ea0b3a0837a60afb21ac85fd4bbf13be0ac0bd Mon Sep 17 00:00:00 2001 From: Konstantin Tretyakov Date: Mon, 10 Dec 2018 03:20:02 +0100 Subject: [PATCH] Fix the "greatest" example. The current code does not pass `**kw` down to further compiler.process calls, thus the example does not work when invoked with, `literal_binds=True`. Besides, the calls to `process` each argument twice are wasteful, and reusing the built-in `case` expression instead of hard-coding the SQL statements seems slightly nicer, doesn't it? --- lib/sqlalchemy/ext/compiler.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/sqlalchemy/ext/compiler.py b/lib/sqlalchemy/ext/compiler.py index b56943b2b4..28fe45964d 100644 --- a/lib/sqlalchemy/ext/compiler.py +++ b/lib/sqlalchemy/ext/compiler.py @@ -336,7 +336,7 @@ that is of the highest value - its equivalent to Python's ``max`` function. A SQL standard version versus a CASE based version which only accommodates two arguments:: - from sqlalchemy.sql import expression + from sqlalchemy.sql import expression, case from sqlalchemy.ext.compiler import compiles from sqlalchemy.types import Numeric @@ -353,12 +353,7 @@ accommodates two arguments:: @compiles(greatest, 'oracle') def case_greatest(element, compiler, **kw): arg1, arg2 = list(element.clauses) - return "CASE WHEN %s > %s THEN %s ELSE %s END" % ( - compiler.process(arg1), - compiler.process(arg2), - compiler.process(arg1), - compiler.process(arg2), - ) + return compiler.process(case([(arg1 > arg2, arg1)], else_=arg2), **kw) Example usage:: -- 2.47.3