Skip to content

[FEATURE] PPL first/last aggregate function #4203

@ykmr1224

Description

@ykmr1224

Add first/last aggregate function for PPL (Calcite)

FIRST Function

Syntax: FIRST(field)
Description: Returns the first value of the specified field in natural document order
Parameters:

  • field (required): The field name to get the first value from
    Return Type: Same as input field type (nullable)
    Behavior:
  • Returns the first occurrence of the field value in the result set
  • Uses natural document order (no sorting applied)
  • Returns NULL if no records exist or field is NULL in first record

LAST Function

Syntax: LAST(field)
Description: Returns the last value of the specified field in natural document order
Parameters:

  • field (required): The field name to get the last value from
    Return Type: Same as input field type (nullable)
    Behavior:
  • Returns the last occurrence of the field value in the result set
  • Uses reverse natural document order (equivalent to sorting by _doc DESC)
  • Returns NULL if no records exist or field is NULL in last record

Usage Examples

# Basic usage
source=logs | stats first(message), last(status) by host

# Combined with other aggregations
source=metrics | stats first(cpu_usage), last(memory_usage), count(), avg(response_time) by server

# Sequential processing after sorting
source=events | sort timestamp | stats first(event_type), last(event_data) by session_id

# With eventstats (if extended)
source=transactions | eventstats first(amount) by account_id

Comparison with Related Functions

Function Ordering Use Case Parameters
FIRST(field) Natural document order Sequential processing Single field
LAST(field) Reverse document order Sequential processing Single field
EARLIEST(field) Time-based (ASC) Time-based analysis Field + optional time field
LATEST(field) Time-based (DESC) Time-based analysis Field + optional time field
TAKE(field, n) Natural document order Multiple record retrieval Field + count

Metadata

Metadata

Assignees

Labels

PPLPiped processing languagecalcitecalcite migration releatedenhancementNew feature or requestv3.3.0

Type

No type

Projects

Status

New

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions