[1.7.x] Fixed #24015 -- Factorized create_index_sql expression

Backport of 6072f17d0 from master, with one test reinforced.
Thanks Tim Graham for the review.