Skip to content

Commit d59b234

Browse files
committed
fix.
1 parent bc8e638 commit d59b234

File tree

6 files changed

+50
-3
lines changed

6 files changed

+50
-3
lines changed

sql/catalyst/src/main/antlr4/org/apache/spark/sql/catalyst/parser/SqlBase.g4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,7 @@ primaryExpression
561561
| CASE whenClause+ (ELSE elseExpression=expression)? END #searchedCase
562562
| CASE value=expression whenClause+ (ELSE elseExpression=expression)? END #simpleCase
563563
| CAST '(' expression AS dataType ')' #cast
564+
| STRUCT '(' (argument+=namedExpression (',' argument+=namedExpression)*)? ')' #struct
564565
| FIRST '(' expression (IGNORE NULLS)? ')' #first
565566
| LAST '(' expression (IGNORE NULLS)? ')' #last
566567
| POSITION '(' substr=valueExpression IN str=valueExpression ')' #position

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/dsl/package.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ package object dsl {
168168
case Seq() => UnresolvedStar(None)
169169
case target => UnresolvedStar(Option(target))
170170
}
171+
def namedStruct(e: Expression*): Expression = CreateNamedStruct(e)
171172

172173
def callFunction[T, U](
173174
func: T => U,

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,13 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging
10601060
Cast(expression(ctx.expression), visitSparkDataType(ctx.dataType))
10611061
}
10621062

1063+
/**
1064+
* Create a [[CreateStruct]] expression.
1065+
*/
1066+
override def visitStruct(ctx: StructContext): Expression = withOrigin(ctx) {
1067+
CreateStruct(ctx.argument.asScala.map(expression))
1068+
}
1069+
10631070
/**
10641071
* Create a [[First]] expression.
10651072
*/

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/parser/ExpressionParserSuite.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,6 @@ class ExpressionParserSuite extends PlanTest {
231231
assertEqual("foo(distinct a, b)", 'foo.distinctFunction('a, 'b))
232232
assertEqual("grouping(distinct a, b)", 'grouping.distinctFunction('a, 'b))
233233
assertEqual("`select`(all a, b)", 'select.function('a, 'b))
234-
assertEqual("foo(a as x, b as e)", 'foo.function('a as 'x, 'b as 'e))
235234
}
236235

237236
test("window function expressions") {
@@ -330,7 +329,9 @@ class ExpressionParserSuite extends PlanTest {
330329
assertEqual("a.b", UnresolvedAttribute("a.b"))
331330
assertEqual("`select`.b", UnresolvedAttribute("select.b"))
332331
assertEqual("(a + b).b", ('a + 'b).getField("b")) // This will fail analysis.
333-
assertEqual("struct(a, b).b", 'struct.function('a, 'b).getField("b"))
332+
assertEqual(
333+
"struct(a, b).b",
334+
namedStruct(NamePlaceholder, 'a, NamePlaceholder, 'b).getField("b"))
334335
}
335336

336337
test("reference") {

sql/core/src/test/resources/sql-tests/inputs/struct.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,10 @@ SELECT ID, STRUCT(ST.*,CAST(ID AS STRING) AS E) NST FROM tbl_x;
1818

1919
-- Prepend a column to a struct
2020
SELECT ID, STRUCT(CAST(ID AS STRING) AS AA, ST.*) NST FROM tbl_x;
21+
22+
-- Select a column from a struct
23+
SELECT ID, STRUCT(ST.*).C NST FROM tbl_x;
24+
SELECT ID, STRUCT(ST.C, ST.D).D NST FROM tbl_x;
25+
26+
-- Select an alias from a struct
27+
SELECT ID, STRUCT(ST.C as STC, ST.D as STD).STD FROM tbl_x;

sql/core/src/test/resources/sql-tests/results/struct.sql.out

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
-- Automatically generated by SQLQueryTestSuite
2-
-- Number of queries: 6
2+
-- Number of queries: 9
33

44

55
-- !query 0
@@ -58,3 +58,33 @@ struct<ID:int,NST:struct<AA:string,C:string,D:string>>
5858
1 {"AA":"1","C":"gamma","D":"delta"}
5959
2 {"AA":"2","C":"epsilon","D":"eta"}
6060
3 {"AA":"3","C":"theta","D":"iota"}
61+
62+
63+
-- !query 6
64+
SELECT ID, STRUCT(ST.*).C NST FROM tbl_x
65+
-- !query 6 schema
66+
struct<ID:int,NST:string>
67+
-- !query 6 output
68+
1 gamma
69+
2 epsilon
70+
3 theta
71+
72+
73+
-- !query 7
74+
SELECT ID, STRUCT(ST.C, ST.D).D NST FROM tbl_x
75+
-- !query 7 schema
76+
struct<ID:int,NST:string>
77+
-- !query 7 output
78+
1 delta
79+
2 eta
80+
3 iota
81+
82+
83+
-- !query 8
84+
SELECT ID, STRUCT(ST.C as STC, ST.D as STD).STD FROM tbl_x
85+
-- !query 8 schema
86+
struct<ID:int,named_struct(STC, ST.C AS `C` AS `STC`, STD, ST.D AS `D` AS `STD`).STD:string>
87+
-- !query 8 output
88+
1 delta
89+
2 eta
90+
3 iota

0 commit comments

Comments
 (0)