Skip to content

Commit

Permalink
fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
gatorsmile committed Jun 18, 2017
1 parent bc8e638 commit d59b234
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,7 @@ primaryExpression
| CASE whenClause+ (ELSE elseExpression=expression)? END #searchedCase
| CASE value=expression whenClause+ (ELSE elseExpression=expression)? END #simpleCase
| CAST '(' expression AS dataType ')' #cast
| STRUCT '(' (argument+=namedExpression (',' argument+=namedExpression)*)? ')' #struct
| FIRST '(' expression (IGNORE NULLS)? ')' #first
| LAST '(' expression (IGNORE NULLS)? ')' #last
| POSITION '(' substr=valueExpression IN str=valueExpression ')' #position
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ package object dsl {
case Seq() => UnresolvedStar(None)
case target => UnresolvedStar(Option(target))
}
def namedStruct(e: Expression*): Expression = CreateNamedStruct(e)

def callFunction[T, U](
func: T => U,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,13 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging
Cast(expression(ctx.expression), visitSparkDataType(ctx.dataType))
}

/**
* Create a [[CreateStruct]] expression.
*/
override def visitStruct(ctx: StructContext): Expression = withOrigin(ctx) {
CreateStruct(ctx.argument.asScala.map(expression))
}

/**
* Create a [[First]] expression.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ class ExpressionParserSuite extends PlanTest {
assertEqual("foo(distinct a, b)", 'foo.distinctFunction('a, 'b))
assertEqual("grouping(distinct a, b)", 'grouping.distinctFunction('a, 'b))
assertEqual("`select`(all a, b)", 'select.function('a, 'b))
assertEqual("foo(a as x, b as e)", 'foo.function('a as 'x, 'b as 'e))
}

test("window function expressions") {
Expand Down Expand Up @@ -330,7 +329,9 @@ class ExpressionParserSuite extends PlanTest {
assertEqual("a.b", UnresolvedAttribute("a.b"))
assertEqual("`select`.b", UnresolvedAttribute("select.b"))
assertEqual("(a + b).b", ('a + 'b).getField("b")) // This will fail analysis.
assertEqual("struct(a, b).b", 'struct.function('a, 'b).getField("b"))
assertEqual(
"struct(a, b).b",
namedStruct(NamePlaceholder, 'a, NamePlaceholder, 'b).getField("b"))
}

test("reference") {
Expand Down
7 changes: 7 additions & 0 deletions sql/core/src/test/resources/sql-tests/inputs/struct.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,10 @@ SELECT ID, STRUCT(ST.*,CAST(ID AS STRING) AS E) NST FROM tbl_x;

-- Prepend a column to a struct
SELECT ID, STRUCT(CAST(ID AS STRING) AS AA, ST.*) NST FROM tbl_x;

-- Select a column from a struct
SELECT ID, STRUCT(ST.*).C NST FROM tbl_x;
SELECT ID, STRUCT(ST.C, ST.D).D NST FROM tbl_x;

-- Select an alias from a struct
SELECT ID, STRUCT(ST.C as STC, ST.D as STD).STD FROM tbl_x;
32 changes: 31 additions & 1 deletion sql/core/src/test/resources/sql-tests/results/struct.sql.out
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- Automatically generated by SQLQueryTestSuite
-- Number of queries: 6
-- Number of queries: 9


-- !query 0
Expand Down Expand Up @@ -58,3 +58,33 @@ struct<ID:int,NST:struct<AA:string,C:string,D:string>>
1 {"AA":"1","C":"gamma","D":"delta"}
2 {"AA":"2","C":"epsilon","D":"eta"}
3 {"AA":"3","C":"theta","D":"iota"}


-- !query 6
SELECT ID, STRUCT(ST.*).C NST FROM tbl_x
-- !query 6 schema
struct<ID:int,NST:string>
-- !query 6 output
1 gamma
2 epsilon
3 theta


-- !query 7
SELECT ID, STRUCT(ST.C, ST.D).D NST FROM tbl_x
-- !query 7 schema
struct<ID:int,NST:string>
-- !query 7 output
1 delta
2 eta
3 iota


-- !query 8
SELECT ID, STRUCT(ST.C as STC, ST.D as STD).STD FROM tbl_x
-- !query 8 schema
struct<ID:int,named_struct(STC, ST.C AS `C` AS `STC`, STD, ST.D AS `D` AS `STD`).STD:string>
-- !query 8 output
1 delta
2 eta
3 iota

0 comments on commit d59b234

Please sign in to comment.