Jewish history has many examples of great figures of dubious ancestry.
The Mishnaic sage Rabbi Meir seems to have been a descendant of Roman royalty (a quite decadent lineage, from the perspective of holiness).
His was said to be the soul of Esau the son of Isaac–a great soul (Jacob's twin!) that had fallen to the depths of kelipah and could be reclaimed by holiness only by a Roman convert to Judaism.
Rabbi Akiva stemmed of coarse and simple stock and had non-Jewish ancestors.
It was said that such a great soul, which shared many features with the soul of Moses, could only be born out of such circumstances.
The birth of Moses himself has a certain profane element to it.
Moses was born from the marriage of Amram with his aunt, Yocheved.
Although this was a permissible union at the time, following the giving of the Torah, it is counted among the most severe of incestuous relationships.
But the most markedly dubious ancestry is that of the Messiah.
His is an ancestry that includes the unions of Judah and Tamar, Boaz and Ruth, David and Bathsheba, Solomon and Naamah the Amonite.
The soul of David, which is the fourth leg of the "divine chariot," and the soul of the Messiah, which is "lofty, exalted, and exceedingly high,” are the loftiest of souls but are imprisoned in the depths of kelipah and have to pass through the twilight zone between holiness and profanity to be extracted from captivity.
They are like pearls that lie buried in refuse heaps.
Tzaddikim do not frequent refuse heaps, not even in the search for pearls; that's not their line.
It is people who seek filth and wallow in filth who dig there, and, for the most part, they come away with filth.
But if a pearl is there, they might come up with a pearl.
The parents, then, do not create the soul; they only provide it with a garment.
In the words of the Talmud, "There are three partners to [the creation of] a person….is father germinates the white element….His mother germinates the red element….And God places within him a spirit and soul."
God provides the soul, and He does not always ask the other partners which soul they want.
Some parents might be just as unsatisfied with a lofty soul as other parents might be with a lowly one, as in the famous parable of the hen who hatches the eggs of a goose.
Some parents may be unhappy with a holy son, just as others may be rendered miserable by an unholy one–but, as we said, the question of which soul should enter which body does not depend on them.
–Rabbi Adin Steinsaltz
From Opening the Tanya by Rabbi Adin Steinsaltz