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

    https://github.com/apache/spark/pull/14957#discussion_r84592865
  
    --- 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 --
    
    The func getFieldRecursively here need the return value which is a 
StructField contains all nested relation in path. For example:
    The fullSchema is:
    ```
    root
     |-- col: struct (nullable = true)
     |    |-- s1: struct (nullable = true)
     |    |    |-- s1_1: long (nullable = true)
     |    |    |-- s1_2: long (nullable = true)
     |    |-- str: string (nullable = true)
     |-- num: long (nullable = true)
     |-- str: string (nullable = true)
    ```
    the func should return:
    ```
    
StructField(col,StructType(StructField(s1,StructType(StructField(s1_1,LongType,true)),true)),true)
    ```
    So maybe I can't use the simplified func getnestedField because it returns 
only the last StructField:
    ```
    StructField(s1_1,LongType,true)
    ```
    



---
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