fqaiser94 commented on a change in pull request #27066:
URL: https://github.com/apache/spark/pull/27066#discussion_r412937174



##########
File path: sql/core/src/test/resources/sql-tests/results/with_fields.sql.out
##########
@@ -0,0 +1,188 @@
+-- Automatically generated by SQLQueryTestSuite
+-- Number of queries: 22
+
+
+-- !query
+CREATE TEMPORARY VIEW struct_level_1 AS VALUES
+  (1, CAST(STRUCT(1, NULL, 3) AS struct<a:int,b:int,c:int>))
+  AS T(id, a)
+-- !query schema
+struct<>
+-- !query output
+
+
+
+-- !query
+CREATE TEMPORARY VIEW null_struct_level_1 AS VALUES
+  (CAST(NULL AS struct<a:int,b:int,c:int>))
+  AS T(a)
+-- !query schema
+struct<>
+-- !query output
+
+
+
+-- !query
+CREATE TEMPORARY VIEW struct_level_2 AS VALUES
+  (CAST(STRUCT(STRUCT(1, NULL, 3)) AS struct<a:struct<a:int,b:int,c:int>>))
+  AS T(a)
+-- !query schema
+struct<>
+-- !query output
+
+
+
+-- !query
+SELECT WITH_FIELDS(id, 'd', 4) AS a FROM struct_level_1
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.sql.AnalysisException
+cannot resolve 'with_fields(struct_level_1.`id`, 'd', 4)' due to data type 
mismatch: Only struct is allowed to appear at first position, got: integer.; 
line 1 pos 7
+
+
+-- !query
+SELECT WITH_FIELDS(a, 1, 2) AS a FROM struct_level_1
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.sql.AnalysisException
+cannot resolve 'with_fields(struct_level_1.`a`, 1, 2)' due to data type 
mismatch: Only foldable string expressions are allowed to appear at even 
position.; line 1 pos 7
+
+
+-- !query
+SELECT WITH_FIELDS(a, NULL, 2) AS a FROM struct_level_1
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.sql.AnalysisException
+cannot resolve 'with_fields(struct_level_1.`a`, NULL, 2)' due to data type 
mismatch: Field name should not be null.; line 1 pos 7
+
+
+-- !query
+SELECT WITH_FIELDS(a, 'd') AS a FROM struct_level_1
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.sql.AnalysisException
+cannot resolve 'with_fields(struct_level_1.`a`, 'd')' due to data type 
mismatch: with_fields expects an odd number of arguments.; line 1 pos 7
+
+
+-- !query
+SELECT WITH_FIELDS(a) AS a FROM struct_level_1
+-- !query schema
+struct<a:struct<a:int,b:int,c:int>>
+-- !query output
+{"a":1,"b":null,"c":3}
+
+
+-- !query
+SELECT WITH_FIELDS(a, 'd', 4) AS a FROM struct_level_1
+-- !query schema
+struct<a:struct<a:int,b:int,c:int,d:int>>
+-- !query output
+{"a":1,"b":null,"c":3,"d":4}
+
+
+-- !query
+SELECT WITH_FIELDS(a, 'd', 4) a FROM null_struct_level_1
+-- !query schema
+struct<a:struct<a:int,b:int,c:int,d:int>>
+-- !query output
+{"a":null,"b":null,"c":null,"d":4}
+
+
+-- !query
+SELECT WITH_FIELDS(a, 'd', NULL) AS a FROM struct_level_1
+-- !query schema
+struct<a:struct<a:int,b:int,c:int,d:null>>
+-- !query output
+{"a":1,"b":null,"c":3,"d":null}
+
+
+-- !query
+SELECT WITH_FIELDS(a, 'd', 4, 'e', 5) AS a FROM struct_level_1
+-- !query schema
+struct<a:struct<a:int,b:int,c:int,d:int,e:int>>
+-- !query output
+{"a":1,"b":null,"c":3,"d":4,"e":5}
+
+
+-- !query
+SELECT WITH_FIELDS(a, 'a', WITH_FIELDS(a.a, 'd', 4)) AS a FROM struct_level_2
+-- !query schema
+struct<a:struct<a:struct<a:int,b:int,c:int,d:int>>>
+-- !query output
+{"a":{"a":1,"b":null,"c":3,"d":4}}
+
+
+-- !query
+SELECT WITH_FIELDS(a, 'b', 2) AS a FROM struct_level_1
+-- !query schema
+struct<a:struct<a:int,b:int,c:int>>
+-- !query output
+{"a":1,"b":2,"c":3}
+
+
+-- !query
+SELECT WITH_FIELDS(a, 'b', 2) AS a FROM null_struct_level_1
+-- !query schema
+struct<a:struct<a:int,b:int,c:int>>
+-- !query output
+{"a":null,"b":2,"c":null}
+
+
+-- !query
+SELECT WITH_FIELDS(a, 'c', NULL) AS a FROM struct_level_1
+-- !query schema
+struct<a:struct<a:int,b:int,c:null>>
+-- !query output
+{"a":1,"b":null,"c":null}
+
+
+-- !query
+SELECT WITH_FIELDS(a, 'a', 10, 'b', 20) AS a FROM struct_level_1
+-- !query schema
+struct<a:struct<a:int,b:int,c:int>>
+-- !query output
+{"a":10,"b":20,"c":3}
+
+
+-- !query
+SELECT WITH_FIELDS(a, 'a', WITH_FIELDS(a.a, 'b', 2)) AS a FROM struct_level_2
+-- !query schema
+struct<a:struct<a:struct<a:int,b:int,c:int>>>
+-- !query output
+{"a":{"a":1,"b":2,"c":3}}
+
+
+-- !query
+SELECT WITH_FIELDS(a, 'b', 100) AS a FROM (VALUES (NAMED_STRUCT('a', 1, 'b', 
2, 'b', 3)) AS nested_struct(a))
+-- !query schema
+struct<a:struct<a:int,b:int,b:int>>
+-- !query output
+{"a":1,"b":100,"b":100}
+
+
+-- !query
+SELECT WITH_FIELDS(a, 'b', 2, 'b', 20) AS a FROM struct_level_1
+-- !query schema
+struct<a:struct<a:int,b:int,c:int>>
+-- !query output
+{"a":1,"b":20,"c":3}

Review comment:
       For a field name that does already exist in the struct, you can only 
**replace** that fieldname even if you pass in multiple fields with the same 
name (notice how "b" only appears once in the output). 

##########
File path: sql/core/src/test/resources/sql-tests/results/with_fields.sql.out
##########
@@ -0,0 +1,188 @@
+-- Automatically generated by SQLQueryTestSuite
+-- Number of queries: 22
+
+
+-- !query
+CREATE TEMPORARY VIEW struct_level_1 AS VALUES
+  (1, CAST(STRUCT(1, NULL, 3) AS struct<a:int,b:int,c:int>))
+  AS T(id, a)
+-- !query schema
+struct<>
+-- !query output
+
+
+
+-- !query
+CREATE TEMPORARY VIEW null_struct_level_1 AS VALUES
+  (CAST(NULL AS struct<a:int,b:int,c:int>))
+  AS T(a)
+-- !query schema
+struct<>
+-- !query output
+
+
+
+-- !query
+CREATE TEMPORARY VIEW struct_level_2 AS VALUES
+  (CAST(STRUCT(STRUCT(1, NULL, 3)) AS struct<a:struct<a:int,b:int,c:int>>))
+  AS T(a)
+-- !query schema
+struct<>
+-- !query output
+
+
+
+-- !query
+SELECT WITH_FIELDS(id, 'd', 4) AS a FROM struct_level_1
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.sql.AnalysisException
+cannot resolve 'with_fields(struct_level_1.`id`, 'd', 4)' due to data type 
mismatch: Only struct is allowed to appear at first position, got: integer.; 
line 1 pos 7
+
+
+-- !query
+SELECT WITH_FIELDS(a, 1, 2) AS a FROM struct_level_1
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.sql.AnalysisException
+cannot resolve 'with_fields(struct_level_1.`a`, 1, 2)' due to data type 
mismatch: Only foldable string expressions are allowed to appear at even 
position.; line 1 pos 7
+
+
+-- !query
+SELECT WITH_FIELDS(a, NULL, 2) AS a FROM struct_level_1
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.sql.AnalysisException
+cannot resolve 'with_fields(struct_level_1.`a`, NULL, 2)' due to data type 
mismatch: Field name should not be null.; line 1 pos 7
+
+
+-- !query
+SELECT WITH_FIELDS(a, 'd') AS a FROM struct_level_1
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.sql.AnalysisException
+cannot resolve 'with_fields(struct_level_1.`a`, 'd')' due to data type 
mismatch: with_fields expects an odd number of arguments.; line 1 pos 7
+
+
+-- !query
+SELECT WITH_FIELDS(a) AS a FROM struct_level_1
+-- !query schema
+struct<a:struct<a:int,b:int,c:int>>
+-- !query output
+{"a":1,"b":null,"c":3}
+
+
+-- !query
+SELECT WITH_FIELDS(a, 'd', 4) AS a FROM struct_level_1
+-- !query schema
+struct<a:struct<a:int,b:int,c:int,d:int>>
+-- !query output
+{"a":1,"b":null,"c":3,"d":4}
+
+
+-- !query
+SELECT WITH_FIELDS(a, 'd', 4) a FROM null_struct_level_1
+-- !query schema
+struct<a:struct<a:int,b:int,c:int,d:int>>
+-- !query output
+{"a":null,"b":null,"c":null,"d":4}
+
+
+-- !query
+SELECT WITH_FIELDS(a, 'd', NULL) AS a FROM struct_level_1
+-- !query schema
+struct<a:struct<a:int,b:int,c:int,d:null>>
+-- !query output
+{"a":1,"b":null,"c":3,"d":null}
+
+
+-- !query
+SELECT WITH_FIELDS(a, 'd', 4, 'e', 5) AS a FROM struct_level_1
+-- !query schema
+struct<a:struct<a:int,b:int,c:int,d:int,e:int>>
+-- !query output
+{"a":1,"b":null,"c":3,"d":4,"e":5}
+
+
+-- !query
+SELECT WITH_FIELDS(a, 'a', WITH_FIELDS(a.a, 'd', 4)) AS a FROM struct_level_2
+-- !query schema
+struct<a:struct<a:struct<a:int,b:int,c:int,d:int>>>
+-- !query output
+{"a":{"a":1,"b":null,"c":3,"d":4}}
+
+
+-- !query
+SELECT WITH_FIELDS(a, 'b', 2) AS a FROM struct_level_1
+-- !query schema
+struct<a:struct<a:int,b:int,c:int>>
+-- !query output
+{"a":1,"b":2,"c":3}
+
+
+-- !query
+SELECT WITH_FIELDS(a, 'b', 2) AS a FROM null_struct_level_1
+-- !query schema
+struct<a:struct<a:int,b:int,c:int>>
+-- !query output
+{"a":null,"b":2,"c":null}
+
+
+-- !query
+SELECT WITH_FIELDS(a, 'c', NULL) AS a FROM struct_level_1
+-- !query schema
+struct<a:struct<a:int,b:int,c:null>>
+-- !query output
+{"a":1,"b":null,"c":null}
+
+
+-- !query
+SELECT WITH_FIELDS(a, 'a', 10, 'b', 20) AS a FROM struct_level_1
+-- !query schema
+struct<a:struct<a:int,b:int,c:int>>
+-- !query output
+{"a":10,"b":20,"c":3}
+
+
+-- !query
+SELECT WITH_FIELDS(a, 'a', WITH_FIELDS(a.a, 'b', 2)) AS a FROM struct_level_2
+-- !query schema
+struct<a:struct<a:struct<a:int,b:int,c:int>>>
+-- !query output
+{"a":{"a":1,"b":2,"c":3}}
+
+
+-- !query
+SELECT WITH_FIELDS(a, 'b', 100) AS a FROM (VALUES (NAMED_STRUCT('a', 1, 'b', 
2, 'b', 3)) AS nested_struct(a))
+-- !query schema
+struct<a:struct<a:int,b:int,b:int>>
+-- !query output
+{"a":1,"b":100,"b":100}
+
+
+-- !query
+SELECT WITH_FIELDS(a, 'b', 2, 'b', 20) AS a FROM struct_level_1
+-- !query schema
+struct<a:struct<a:int,b:int,c:int>>
+-- !query output
+{"a":1,"b":20,"c":3}
+
+
+-- !query
+SELECT WITH_FIELDS(a, 'b', 2, 'd', 4) AS a FROM struct_level_1
+-- !query schema
+struct<a:struct<a:int,b:int,c:int,d:int>>
+-- !query output
+{"a":1,"b":2,"c":3,"d":4}
+
+
+-- !query
+SELECT WITH_FIELDS(a, 'd', 4, 'd', 5) AS a FROM struct_level_1
+-- !query schema
+struct<a:struct<a:int,b:int,c:int,d:int,d:int>>
+-- !query output
+{"a":1,"b":null,"c":3,"d":4,"d":5}

Review comment:
       For a field name that does **not** already exist in the struct, you can 
**add** multiple fields with that same name (notice how "d" appears twice in 
the output). 




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to