cherry-pick: #39723 #41164 #41331 #41546 because later problem is intro by prev one, so put them together when using fold constant by be, the return type of substring('123456',1, 3) would changed to be text, which we want it to be 3 remove windowframe in window expression to avoid folding constant on be