Skip to content

Commit

Permalink
Add a generic function compiler to handle IGNORE NULLS in window func…
Browse files Browse the repository at this point in the history
…tions
  • Loading branch information
laserkaplan committed Dec 6, 2023
1 parent f99ef24 commit 9555d78
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
25 changes: 24 additions & 1 deletion tests/unit/sqlalchemy/test_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import pytest
from sqlalchemy import Column, Integer, MetaData, String, Table, insert, select
from sqlalchemy import Column, Integer, MetaData, String, Table, insert, select, func
from sqlalchemy.schema import CreateTable
from sqlalchemy.sql import column, table

Expand Down Expand Up @@ -113,3 +113,26 @@ def test_table_clause(dialect):
statement = select(table("user", column("id"), column("name"), column("description")))
query = statement.compile(dialect=dialect)
assert str(query) == 'SELECT user.id, user.name, user.description \nFROM user'


@pytest.mark.parametrize(
'function,element',
[
('first_value', func.first_value),
('last_value', func.last_value),
('nth_value', func.nth_value),
('lead', func.lead),
('lag', func.lag),
]
)
def test_ignore_nulls(dialect, function, element):
statement = select(
element(
table_without_catalog.c.id,
ignore_nulls=True,
).over(partition_by=table_without_catalog.c.name).label('window')
)
query = statement.compile(dialect=dialect)
assert str(query) == \
f'SELECT {function}("table".id) IGNORE NULLS OVER (PARTITION BY "table".name) AS window '\
f'\nFROM "table"'
37 changes: 37 additions & 0 deletions trino/sqlalchemy/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.sql import compiler
from sqlalchemy.sql.base import DialectKWArgs
from sqlalchemy.sql.functions import GenericFunction

# https://trino.io/docs/current/language/reserved.html
RESERVED_WORDS = {
Expand Down Expand Up @@ -125,6 +127,41 @@ def add_catalog(sql, table):
sql = f'"{catalog}".{sql}'
return sql

class GenericIgnoreNulls(GenericFunction):
ignore_nulls = False

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if kwargs.get('ignore_nulls'):
self.ignore_nulls = True

class FirstValue(GenericIgnoreNulls):
name = 'first_value'

class LastValue(GenericIgnoreNulls):
name = 'last_value'

class NthValue(GenericIgnoreNulls):
name = 'nth_value'

class Lead(GenericIgnoreNulls):
name = 'lead'

class Lag(GenericIgnoreNulls):
name = 'lag'

@staticmethod
@compiles(FirstValue)
@compiles(LastValue)
@compiles(NthValue)
@compiles(Lead)
@compiles(Lag)
def compile_ignore_nulls(element, compiler, **kwargs):
compiled = f'{element.name}({compiler.process(element.clauses)})'
if element.ignore_nulls:
compiled += ' IGNORE NULLS'
return compiled


class TrinoDDLCompiler(compiler.DDLCompiler):
pass
Expand Down

0 comments on commit 9555d78

Please sign in to comment.