Fixes #6042 -- Add escape character to modulo function (#6044)

* Added excaping character for modulo function

* Fixed MSSQL Median computation

Co-authored-by: Pere Miquel Brull <peremiquelbrull@gmail.com>
This commit is contained in:
Teddy 2022-07-13 14:43:56 +02:00 committed by GitHub
parent d097199d2f
commit 5d584731ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 8 deletions

View File

@ -54,6 +54,13 @@ def _(elements, compiler, **kwargs):
return "approx_percentile(%s, 0.5)" % col
@compiles(MedianFn, Dialects.MSSQL)
def _(elements, compiler, **kwargs):
"""Median computation for MSSQL"""
col, _ = [compiler.process(element, **kwargs) for element in elements.clauses]
return "percentile_cont(0.5) WITHIN GROUP (ORDER BY %s ASC) OVER()" % col
@compiles(MedianFn, Dialects.SQLite)
def _(elements, compiler, **kwargs):
col, table = [element for element in elements.clauses]

View File

@ -42,13 +42,7 @@ def validate_and_compile(element, compiler, **kw):
@compiles(ModuloFn)
def _(element, compiler, **kw):
value, base = validate_and_compile(element, compiler, **kw)
return f"{value} % {base}"
@compiles(ModuloFn, Dialects.MSSQL)
def _(element, compiler, **kw):
"""Generic modulo function"""
value, base = validate_and_compile(element, compiler, **kw)
return f"{value} %% {base}"
@ -62,7 +56,7 @@ def _(element, compiler, **kw):
@compiles(ModuloFn, Dialects.Oracle)
@compiles(ModuloFn, Dialects.Presto)
def _(element, compiler, **kw):
"""Modulo function for specific dialect"""
value, base = validate_and_compile(element, compiler, **kw)
return f"MOD({value}, {base})"
@ -72,3 +66,10 @@ def _(element, compiler, **kw):
"""Handles modulo function for ClickHouse"""
value, base = validate_and_compile(element, compiler, **kw)
return f"modulo({value}, {base})"
@compiles(ModuloFn, Dialects.SQLite)
def _(element, compiler, **kw):
"""SQLite modulo function"""
value, base = validate_and_compile(element, compiler, **kw)
return f"{value} % {base}"