Skip to content

Instantly share code, notes, and snippets.

@ottomata
Created February 13, 2018 05:04
Show Gist options
  • Select an option

  • Save ottomata/e760daf8ad013d358554fd300f1c3975 to your computer and use it in GitHub Desktop.

Select an option

Save ottomata/e760daf8ad013d358554fd300f1c3975 to your computer and use it in GitHub Desktop.
/** Get the intersection of field names in new and old schema, newSchema is expected to be a superset */
def orderedSelect(newSchema: StructType, oldSchema: StructType): Seq[String] = {
fieldSelect(newSchema).intersect(fieldSelect(oldSchema))
}
/** build a list of field nameswith nested struct fields */
def fieldSelect(schema: StructType): Seq[String] = {
schema.flatMap { field =>
field.dataType match {
case struct: StructType => fieldSelect(struct).map(field.name + "." + _)
case _ => Seq(field.name)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment