Skip to content

Instantly share code, notes, and snippets.

@szleven
szleven / findColumns.scala
Created May 15, 2025 06:20
Find columns in deeply nested DataFrame structure
import org.apache.spark.sql.types._
import org.apache.spark.sql._
def findColumn(df: DataFrame, predicate: String => Boolean): Seq[String] = {
def iterate(schema: StructType, matches: Seq[String] = Seq.empty, currentPath: String = ""): Seq[String] = {
val prefix = if (currentPath.nonEmpty) currentPath + "." else ""
schema.flatMap {
case StructField(name, struct: StructType, _, _) => iterate(struct, matches, prefix + name)