Skip to content

Commit

Permalink
[FLINK-21026][flink-sql-parser] Align column list specification synta…
Browse files Browse the repository at this point in the history
…x with hive in INSERT statement

This closes apache#14726
  • Loading branch information
docete committed Jan 26, 2021
1 parent a9d2b76 commit 35247bb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,9 @@ SqlNode RichSqlInsert() :
table = extend(table, extendList);
}
]
[
<PARTITION> PartitionSpecCommaList(partitionList)
]
[
LOOKAHEAD(2)
{ final Pair<SqlNodeList, SqlNodeList> p; }
Expand All @@ -983,9 +986,6 @@ SqlNode RichSqlInsert() :
}
}
]
[
<PARTITION> PartitionSpecCommaList(partitionList)
]
source = OrderedQueryOrExpr(ExprContext.ACCEPT_QUERY) {
return new RichSqlInsert(s.end(source), keywordList, extendedKeywordList, table, source,
columnList, partitionList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,15 @@ public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
final int opLeft = getOperator().getLeftPrec();
final int opRight = getOperator().getRightPrec();
getTargetTable().unparse(writer, opLeft, opRight);
if (getTargetColumnList() != null) {
getTargetColumnList().unparse(writer, opLeft, opRight);
}
writer.newlineAndIndent();
if (staticPartitions != null && staticPartitions.size() > 0) {
writer.keyword("PARTITION");
staticPartitions.unparse(writer, opLeft, opRight);
writer.newlineAndIndent();
}
if (getTargetColumnList() != null) {
getTargetColumnList().unparse(writer, opLeft, opRight);
}
writer.newlineAndIndent();
getSource().unparse(writer, 0, 0);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -866,59 +866,66 @@ public void testDropTemporaryIfExists() {

@Test
public void testInsertPartitionSpecs() {
final String sql1 = "insert into emps(x,y) partition (x='ab', y='bc') select * from emps";
final String sql1 = "insert into emps partition (x='ab', y='bc') (x,y) select * from emps";
final String expected =
"INSERT INTO `EMPS` (`X`, `Y`)\n"
"INSERT INTO `EMPS` "
+ "PARTITION (`X` = 'ab', `Y` = 'bc')\n"
+ "(`X`, `Y`)\n"
+ "(SELECT *\n"
+ "FROM `EMPS`)";
sql(sql1).ok(expected);
final String sql2 =
"insert into emp (empno, ename, job, mgr, hiredate,\n"
+ " sal, comm, deptno, slacker)\n"
"insert into emp\n"
+ "partition(empno='1', job='job')\n"
+ "(empno, ename, job, mgr, hiredate,\n"
+ " sal, comm, deptno, slacker)\n"
+ "select 'nom', 0, timestamp '1970-01-01 00:00:00',\n"
+ " 1, 1, 1, false\n"
+ "from (values 'a')";
sql(sql2)
.ok(
"INSERT INTO `EMP` (`EMPNO`, `ENAME`, `JOB`, `MGR`, `HIREDATE`, `SAL`,"
+ " `COMM`, `DEPTNO`, `SLACKER`)\n"
"INSERT INTO `EMP` "
+ "PARTITION (`EMPNO` = '1', `JOB` = 'job')\n"
+ "(`EMPNO`, `ENAME`, `JOB`, `MGR`, `HIREDATE`, `SAL`,"
+ " `COMM`, `DEPTNO`, `SLACKER`)\n"
+ "(SELECT 'nom', 0, TIMESTAMP '1970-01-01 00:00:00', 1, 1, 1, FALSE\n"
+ "FROM (VALUES (ROW('a'))))");
final String sql3 =
"insert into empnullables (empno, ename)\n"
"insert into empnullables\n"
+ "partition(ename='b')\n"
+ "(empno, ename)\n"
+ "select 1 from (values 'a')";
sql(sql3)
.ok(
"INSERT INTO `EMPNULLABLES` (`EMPNO`, `ENAME`)\n"
"INSERT INTO `EMPNULLABLES` "
+ "PARTITION (`ENAME` = 'b')\n"
+ "(`EMPNO`, `ENAME`)\n"
+ "(SELECT 1\n"
+ "FROM (VALUES (ROW('a'))))");
}

@Test
public void testInsertCaseSensitivePartitionSpecs() {
final String expected =
"INSERT INTO `emps` (`x`, `y`)\n"
"INSERT INTO `emps` "
+ "PARTITION (`x` = 'ab', `y` = 'bc')\n"
+ "(`x`, `y`)\n"
+ "(SELECT *\n"
+ "FROM `EMPS`)";
sql("insert into \"emps\"(\"x\",\"y\") "
+ "partition (\"x\"='ab', \"y\"='bc') select * from emps")
sql("insert into \"emps\" "
+ "partition (\"x\"='ab', \"y\"='bc')(\"x\",\"y\") select * from emps")
.ok(expected);
}

@Test
public void testInsertExtendedColumnAsStaticPartition1() {
final String expected =
"INSERT INTO `EMPS` EXTEND (`Z` BOOLEAN) (`X`, `Y`)\n"
"INSERT INTO `EMPS` EXTEND (`Z` BOOLEAN) "
+ "PARTITION (`Z` = 'ab')\n"
+ "(`X`, `Y`)\n"
+ "(SELECT *\n"
+ "FROM `EMPS`)";
sql("insert into emps(z boolean)(x,y) partition (z='ab') select * from emps").ok(expected);
sql("insert into emps(z boolean) partition (z='ab') (x,y) select * from emps").ok(expected);
}

@Test(expected = SqlParseException.class)
Expand All @@ -940,8 +947,9 @@ public void testInsertOverwrite() {
// partitioned
final String sql1 = "INSERT OVERWRITE myTbl PARTITION (p1='v1',p2='v2') SELECT * FROM src";
final String expected1 =
"INSERT OVERWRITE `MYTBL`\n"
"INSERT OVERWRITE `MYTBL` "
+ "PARTITION (`P1` = 'v1', `P2` = 'v2')\n"
+ "\n"
+ "(SELECT *\n"
+ "FROM `SRC`)";
sql(sql1).ok(expected1);
Expand Down

0 comments on commit 35247bb

Please sign in to comment.