Updated on 2026-11-06 GMT+08:00

lead

Function

The lead function is used to retrieve the value from the nth row below the current row within the window.

Usage Restrictions

The usage restrictions for window functions are as follows:

  • Window functions can only appear in SELECT statements.
  • Window functions cannot be nested with other window functions or aggregate functions.
  • Window functions cannot be used together with aggregate functions at the same level.

Syntax

lead(<expr>[, bigint <offset>[, <default>]]) over([partition_clause] orderby_clause)

Parameters

Parameter

Mandatory

Description

expr

Yes

The expression for which the result is to be computed and returned.

offset

No

Offset, a BIGINT type constant with a value greater than or equal to 0. A value of 0 indicates the current row, 1 indicates the next row, and so on. The default value is 1. If the input value is of type STRING or DOUBLE, it is implicitly converted to BIGINT type before computation.

default

No

A constant, default value is NULL.

The default value when the range specified by offset exceeds the boundary. Must be of the same data type as expr. If expr is not a constant, it is evaluated based on the current row.

partition_clause

No

Specifies the partition. Rows with the same value in the partition column are considered to be within the same window.

orderby_clause

No

Specifies how data is sorted within a window.

Return Values

The data type of the parameter.

Examples

Example data:

To facilitate understanding of the function's usage, source data is provided along with function-related examples. Create the logs table and add data with the following example command:

create table logs(
  cookieid string,
  createtime string,
  url string )  
STORED AS parquet; 

Add data as follows:

cookie1 2015-04-10 10:00:02 url2 
cookie1 2015-04-10 10:00:00 url1 
cookie1 2015-04-10 10:03:04 url3 
cookie1 2015-04-10 10:50:05 url6 
cookie1 2015-04-10 11:00:00 url7 
cookie1 2015-04-10 10:10:00 url4 
cookie1 2015-04-10 10:50:01 url5 
cookie2 2015-04-10 10:00:02 url22 
cookie2 2015-04-10 10:00:00 url11 
cookie2 2015-04-10 10:03:04 url33 
cookie2 2015-04-10 10:50:05 url66 
cookie2 2015-04-10 11:00:00 url77 
cookie2 2015-04-10 10:10:00 url44 
cookie2 2015-04-10 10:50:01 url55 

Group all records by cookieid, sort by createtime in ascending order, and return the values from the 2nd row and 1st row below the current row within the window. Example command:

SELECT cookieid, createtime, url,
       LEAD(createtime, 2) OVER(PARTITION BY cookieid ORDER BY createtime) AS next_2_time,
       LEAD(createtime, 1, '1970-01-01 00:00:00') OVER(PARTITION BY cookieid ORDER BY createtime) AS next_1_time
FROM logs;

-- Result:
cookieid createtime         url  next_2_time          next_1_time
cookie1 2015-04-10 10:00:00 url1 2015-04-10 10:03:04  2015-04-10 10:00:02
cookie1 2015-04-10 10:00:02 url2 2015-04-10 10:10:00  2015-04-10 10:03:04
cookie1 2015-04-10 10:03:04 url3 2015-04-10 10:50:01  2015-04-10 10:10:00
cookie1 2015-04-10 10:10:00 url4 2015-04-10 10:50:05  2015-04-10 10:50:01
cookie1 2015-04-10 10:50:01 url5 2015-04-10 11:00:00  2015-04-10 10:50:05
cookie1 2015-04-10 10:50:05 url6 NULL                 2015-04-10 11:00:00
cookie1 2015-04-10 11:00:00 url7 NULL                 1970-01-01 00:00:00
cookie2 2015-04-10 10:00:00 url11 2015-04-10 10:03:04 2015-04-10 10:00:02
cookie2 2015-04-10 10:00:02 url22 2015-04-10 10:10:00 2015-04-10 10:03:04
cookie2 2015-04-10 10:03:04 url33 2015-04-10 10:50:01 2015-04-10 10:10:00
cookie2 2015-04-10 10:10:00 url44 2015-04-10 10:50:05 2015-04-10 10:50:01
cookie2 2015-04-10 10:50:01 url55 2015-04-10 11:00:00 2015-04-10 10:50:05
cookie2 2015-04-10 10:50:05 url66 NULL                2015-04-10 11:00:00
cookie2 2015-04-10 11:00:00 url77 NULL                1970-01-01 00:00:00