Github user liancheng commented on a diff in the pull request:

    https://github.com/apache/spark/pull/14957#discussion_r84559521
  
    --- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/FileSourceStrategy.scala
 ---
    @@ -126,4 +136,52 @@ object FileSourceStrategy extends Strategy with 
Logging {
     
         case _ => Nil
       }
    +
    +  private def generateStructFieldsContainsNesting(projects: 
Seq[Expression],
    +                                      totalSchema: StructType) : 
Seq[StructField] = {
    +    def generateStructField(curField: List[String],
    +                             node: Expression) : Seq[StructField] = {
    +      node match {
    +        case ai: GetArrayItem =>
    +          // Here we drop the previous for simplify array and map support.
    +          // Same strategy in GetArrayStructFields and GetMapValue
    +          generateStructField(List.empty[String], ai.child)
    +        case asf: GetArrayStructFields =>
    +          generateStructField(List.empty[String], asf.child)
    +        case mv: GetMapValue =>
    +          generateStructField(List.empty[String], mv.child)
    +        case attr: AttributeReference =>
    +          Seq(getFieldRecursively(totalSchema, attr.name :: curField))
    +        case sf: GetStructField =>
    +          generateStructField(sf.name.get :: curField, sf.child)
    +        case _ =>
    +          if (node.children.nonEmpty) {
    +            node.children.flatMap(child => generateStructField(curField, 
child))
    +          } else {
    +            Seq.empty[StructField]
    +          }
    +      }
    +    }
    +
    +    def getFieldRecursively(totalSchema: StructType,
    +                            name: List[String]): StructField = {
    +      if (name.length > 1) {
    +        val curField = name.head
    +        val curFieldType = totalSchema(curField)
    +        curFieldType.dataType match {
    +          case st: StructType =>
    +            val newField = getFieldRecursively(StructType(st.fields), 
name.drop(1))
    +            StructField(curFieldType.name, StructType(Seq(newField)),
    +              curFieldType.nullable, curFieldType.metadata)
    +          case _ =>
    +            throw new IllegalArgumentException(s"""Field "$curField" is 
not struct field.""")
    +        }
    +      } else {
    +        totalSchema(name.head)
    +      }
    +    }
    --- End diff --
    
    This function can be simplified to:
    
    ```scala
    def getNestedField(schema: StructType, path: Seq[String]): StructField = {
      require(path.nonEmpty, "<error message>")
    
      path.tail.foldLeft(schema(path.head)) { (field, name) =>
        field.dataType match {
          case t: StructType => t(name)
          case _ => ??? // Throw exception here
        }
      }
    }
    ```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to