Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions tinyquery/evaluator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1270,6 +1270,22 @@ def test_first(self):
])
)

def test_last(self):
# Test over the equivalent of a GROUP BY
self.assert_query_result(
'SELECT LAST(val1) FROM test_table',
self.make_context([
('f0_', tq_types.INT, [2])
])
)
# Test over something repeated
self.assert_query_result(
'SELECT LAST(QUANTILES(val1, 3)) FROM test_table',
self.make_context([
('f0_', tq_types.INT, [8])
])
)

# TODO(colin): test behavior on empty list in both cases

def test_left(self):
Expand Down
21 changes: 20 additions & 1 deletion tinyquery/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,24 @@ def _evaluate(self, num_rows, column):
values=values)


class LastFunction(AggregateFunction):
def check_types(self, rep_list_type):
return rep_list_type

def _evaluate(self, num_rows, column):
values = []
if len(column.values) == 0:
values = [None]

if column.mode == tq_modes.REPEATED:
values = [repeated_row[-1] if len(repeated_row) > 0 else None
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible for column.values to be empty in repeated mode? If so, I think this comprehension will die because repeated_row will be None which you can't take the len of.

Assuming that situation is possible, can you add a test-case for it?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, looking at this code it looks like the returned value can have NULL's in it if the row is empty. Does this match what bigquery does? The documentation doesn't seem to say.

It would be great if you could have a test-case for that as well.

for repeated_row in column.values]
else:
values = [column.values[-1]]
return context.Column(type=column.type, mode=tq_modes.NULLABLE,
values=values)


class NoArgFunction(ScalarFunction):
def __init__(self, func, return_type=tq_types.INT):
self.func = func
Expand Down Expand Up @@ -1304,7 +1322,8 @@ def _evaluate(self, num_rows, json_expressions, json_paths):
'count_distinct': CountDistinctFunction(),
'stddev_samp': StddevSampFunction(),
'quantiles': QuantilesFunction(),
'first': FirstFunction()
'first': FirstFunction(),
'last': LastFunction(),
}


Expand Down