How to get the last non-null value that came before the current row
You can use a subquery:
select
  t1.ts,
  case when a is null then
    (select a 
     from tab t2 
     where t2.ts &lt; t1.ts and a is not null 
     order by a desc
     limit 1)
  else a end as a
from
  tab t1;



ts |  a
-: | -:
 1 |  1
 2 |  2
 3 |  2
 4 |  2
 5 |  3


db&lt;&gt;fiddle here