Peak and Valleys similar to argrelextrema in SQL

I am new to SQL. I want to find the peak and valleys in a timeseries data. I have a clickhouse DB and I can pull data into python where I can process this as mentioned in argrelextrema explained

However I am looking for a solution in SQL so I don’t have to transfer huge data between Clickhouse and python

I created a sample dataset in sqlfiddle

For reference,

Expected result
-- val is_pv
-- 1   1
-- 2   0
-- 3   0
-- 4   0
-- 5   0
-- 5   0
-- 5   0
-- 6   0
-- 7   1
-- 5   0
-- 3   0
-- 1   0
-- 1   1
-- 3   0
-- 5   0
-- 5   1
-- 3   1