Skip to content

Commit 20bfbc8

Browse files
committed
#2119 add support INSERT OVERWRITE PARTITION
1 parent 23cd338 commit 20bfbc8

File tree

6 files changed

+194
-5
lines changed

6 files changed

+194
-5
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
<dependency>
3636
<groupId>commons-io</groupId>
3737
<artifactId>commons-io</artifactId>
38-
<version>[2.15.1,)</version>
38+
<version>2.18.0</version>
3939
<scope>test</scope>
4040
</dependency>
4141
<dependency>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2025 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
10+
package net.sf.jsqlparser.schema;
11+
12+
import net.sf.jsqlparser.expression.Expression;
13+
14+
import java.util.Collection;
15+
import java.util.Objects;
16+
17+
public class Partition {
18+
protected Column column;
19+
protected Expression value;
20+
21+
public Partition() {
22+
23+
}
24+
25+
public Partition(Column column, Expression value) {
26+
this.column = column;
27+
this.value = value;
28+
}
29+
30+
public static StringBuilder appendPartitionsTo(StringBuilder builder, Collection<Partition> partitions) {
31+
int j = 0;
32+
for (Partition partition : partitions) {
33+
partition.appendTo(builder, j);
34+
j++;
35+
}
36+
return builder;
37+
}
38+
39+
public Column getColumn() {
40+
return column;
41+
}
42+
43+
public void setColumn(Column column) {
44+
this.column = Objects.requireNonNull(column);
45+
}
46+
47+
public Expression getValue() {
48+
return value;
49+
}
50+
51+
public void setValue(Expression value) {
52+
this.value = Objects.requireNonNull(value);
53+
}
54+
55+
56+
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPath"})
57+
StringBuilder appendTo(StringBuilder builder, int j) {
58+
if (j > 0) {
59+
builder.append(", ");
60+
}
61+
builder.append(column.getColumnName());
62+
if (value != null) {
63+
builder.append(" = ").append(value);
64+
}
65+
return builder;
66+
}
67+
}

src/main/java/net/sf/jsqlparser/statement/insert/Insert.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import net.sf.jsqlparser.expression.OracleHint;
1313
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
1414
import net.sf.jsqlparser.schema.Column;
15+
import net.sf.jsqlparser.schema.Partition;
1516
import net.sf.jsqlparser.schema.Table;
1617
import net.sf.jsqlparser.statement.OutputClause;
1718
import net.sf.jsqlparser.statement.ReturningClause;
@@ -36,11 +37,14 @@ public class Insert implements Statement {
3637
private Table table;
3738
private OracleHint oracleHint = null;
3839
private ExpressionList<Column> columns;
40+
private List<Partition> partitions;
3941
private Select select;
4042
private boolean onlyDefaultValues = false;
4143
private List<UpdateSet> duplicateUpdateSets = null;
4244
private InsertModifierPriority modifierPriority = null;
4345
private boolean modifierIgnore = false;
46+
private boolean overwrite = false;
47+
private boolean tableKeyword = false;
4448
private ReturningClause returningClause;
4549
private List<UpdateSet> setUpdateSets = null;
4650
private List<WithItem<?>> withItemsList;
@@ -103,6 +107,14 @@ public void setColumns(ExpressionList<Column> list) {
103107
columns = list;
104108
}
105109

110+
public List<Partition> getPartitions() {
111+
return partitions;
112+
}
113+
114+
public void setPartitions(List<Partition> list) {
115+
partitions = list;
116+
}
117+
106118
@Deprecated
107119
public boolean isUseValues() {
108120
return select != null && select instanceof Values;
@@ -163,6 +175,22 @@ public void setModifierIgnore(boolean modifierIgnore) {
163175
this.modifierIgnore = modifierIgnore;
164176
}
165177

178+
public boolean isOverwrite() {
179+
return overwrite;
180+
}
181+
182+
public void setOverwrite(boolean overwrite) {
183+
this.overwrite = overwrite;
184+
}
185+
186+
public boolean isTableKeyword() {
187+
return tableKeyword;
188+
}
189+
190+
public void setTableKeyword(boolean tableKeyword) {
191+
this.tableKeyword = tableKeyword;
192+
}
193+
166194
@Deprecated
167195
public boolean isUseSet() {
168196
return setUpdateSets != null && !setUpdateSets.isEmpty();
@@ -240,7 +268,14 @@ public String toString() {
240268
if (modifierIgnore) {
241269
sql.append("IGNORE ");
242270
}
243-
sql.append("INTO ");
271+
if (overwrite) {
272+
sql.append("OVERWRITE ");
273+
} else {
274+
sql.append("INTO ");
275+
}
276+
if (tableKeyword) {
277+
sql.append("TABLE ");
278+
}
244279
sql.append(table).append(" ");
245280

246281
if (onlyDefaultValues) {
@@ -259,6 +294,12 @@ public String toString() {
259294
sql.append(") ");
260295
}
261296

297+
if (partitions != null) {
298+
sql.append(" PARTITION (");
299+
Partition.appendPartitionsTo(sql, partitions);
300+
sql.append(") ");
301+
}
302+
262303
if (outputClause != null) {
263304
sql.append(outputClause);
264305
}

src/main/java/net/sf/jsqlparser/util/deparser/InsertDeParser.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import net.sf.jsqlparser.expression.ExpressionVisitor;
1313
import net.sf.jsqlparser.schema.Column;
14+
import net.sf.jsqlparser.schema.Partition;
1415
import net.sf.jsqlparser.statement.insert.Insert;
1516
import net.sf.jsqlparser.statement.select.Select;
1617
import net.sf.jsqlparser.statement.select.SelectVisitor;
@@ -62,7 +63,14 @@ public void deParse(Insert insert) {
6263
if (insert.isModifierIgnore()) {
6364
buffer.append("IGNORE ");
6465
}
65-
buffer.append("INTO ");
66+
if (insert.isOverwrite()) {
67+
buffer.append("OVERWRITE ");
68+
} else {
69+
buffer.append("INTO ");
70+
}
71+
if (insert.isTableKeyword()) {
72+
buffer.append("TABLE ");
73+
}
6674

6775
buffer.append(insert.getTable().toString());
6876

@@ -82,6 +90,12 @@ public void deParse(Insert insert) {
8290
buffer.append(")");
8391
}
8492

93+
if (insert.getPartitions() != null) {
94+
buffer.append(" PARTITION (");
95+
Partition.appendPartitionsTo(buffer, insert.getPartitions());
96+
buffer.append(")");
97+
}
98+
8599
if (insert.getOutputClause() != null) {
86100
buffer.append(insert.getOutputClause().toString());
87101
}

src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
384384
| <K_OVER:"OVER">
385385
| <K_OVERFLOW:"OVERFLOW">
386386
| <K_OVERLAPS:"OVERLAPS">
387+
| <K_OVERWRITE:"OVERWRITE">
387388
| <K_OPTIMIZE: "OPTIMIZE" >
388389
| <K_PARALLEL:"PARALLEL">
389390
| <K_PARENT:"PARENT">
@@ -1602,6 +1603,33 @@ List<UpdateSet> UpdateSets():
16021603
}
16031604
}
16041605

1606+
List<Partition> Partitions():
1607+
{
1608+
List<Partition> partitions = new ArrayList<Partition>();
1609+
Column tableColumn;
1610+
Expression valueExpression = null;
1611+
}
1612+
{
1613+
(
1614+
(
1615+
tableColumn=Column() [ "=" valueExpression=Expression() ]
1616+
{ partitions.add( new Partition (tableColumn, valueExpression)); }
1617+
)
1618+
)
1619+
1620+
(
1621+
LOOKAHEAD(2) (
1622+
","
1623+
tableColumn=Column() [ "=" valueExpression=Expression() ]
1624+
{ partitions.add( new Partition (tableColumn, valueExpression)); }
1625+
)
1626+
)*
1627+
1628+
{
1629+
return partitions;
1630+
}
1631+
}
1632+
16051633
Insert InsertWithWithItems( List<WithItem<?>> withItems ):
16061634
{
16071635
Insert insert;
@@ -1619,13 +1647,16 @@ Insert Insert():
16191647
List<WithItem<?>> with = null;
16201648
Column tableColumn = null;
16211649
ExpressionList<Column> columns = new ExpressionList<Column>();
1650+
List<Partition> partitions = new ArrayList<Partition>();
16221651
Expression exp = null;
16231652
ReturningClause returningClause;
16241653
Select select = null;
16251654
boolean useDuplicate = false;
16261655
Token tk = null;
16271656
InsertModifierPriority modifierPriority = null;
16281657
boolean modifierIgnore = false;
1658+
boolean overwrite = false;
1659+
boolean tableKeyword = false;
16291660

16301661
List<UpdateSet> updateSets;
16311662
List<UpdateSet> duplicateUpdateSets;
@@ -1648,7 +1679,8 @@ Insert Insert():
16481679
}
16491680
]
16501681
[ LOOKAHEAD(2) <K_IGNORE>{ modifierIgnore = true; }]
1651-
[ LOOKAHEAD(2) <K_INTO>] table=Table()
1682+
[ LOOKAHEAD(2) (<K_INTO> | <K_OVERWRITE> { insert.setOverwrite(true); }) [<K_TABLE> { insert.setTableKeyword(true); }] ] table=Table()
1683+
[ LOOKAHEAD(2) <K_PARTITION> "(" partitions=Partitions() ")" ]
16521684

16531685
[ LOOKAHEAD(2) [<K_AS> { useAs = true; } ] name=RelObjectNameWithoutValue() { table.setAlias(new Alias(name,useAs)); }]
16541686

@@ -1682,6 +1714,9 @@ Insert Insert():
16821714
if (!columns.isEmpty()) {
16831715
insert.setColumns(columns);
16841716
}
1717+
if (!partitions.isEmpty()) {
1718+
insert.setPartitions(partitions);
1719+
}
16851720
return insert.withWithItemsList(with)
16861721
.withSelect(select)
16871722
.withTable(table)

src/test/java/net/sf/jsqlparser/statement/insert/InsertTest.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ public void testInsertSetWithDuplicateEliminationInDeparsing() throws JSQLParser
401401
@Test
402402
public void testInsertTableWithAliasIssue526() throws JSQLParserException {
403403
assertSqlCanBeParsedAndDeparsed(
404-
"INSERT INTO account t (name, addr, phone) SELECT * FROM user");
404+
"INSERT INTO account AS t (name, addr, phone) SELECT * FROM user");
405405
}
406406

407407
@Test
@@ -837,4 +837,36 @@ void testSelectAndInsertWithin2Ctes() throws JSQLParserException {
837837
assertEquals(" inserted", withItems.get(1).getAlias().toString());
838838
}
839839

840+
@Test
841+
void testInsertOverwrite() throws JSQLParserException {
842+
String sqlStr = "INSERT OVERWRITE TABLE t SELECT * FROM a";
843+
Insert insert = (Insert) assertSqlCanBeParsedAndDeparsed(sqlStr);
844+
assertEquals("t", insert.getTable().getName());
845+
assertTrue(insert.isOverwrite());
846+
847+
sqlStr = "INSERT OVERWRITE TABLE t PARTITION (pt1, pt2) SELECT * FROM a";
848+
insert = (Insert) assertSqlCanBeParsedAndDeparsed(sqlStr);
849+
assertEquals("t", insert.getTable().getName());
850+
assertEquals(2, insert.getPartitions().size());
851+
assertEquals("pt1", insert.getPartitions().get(0).getColumn().getColumnName());
852+
assertNull(insert.getPartitions().get(0).getValue());
853+
assertTrue(insert.isOverwrite());
854+
855+
sqlStr = "INSERT OVERWRITE TABLE t PARTITION (pt1 = 'pt1', pt2 = 'pt2') SELECT * FROM a";
856+
insert = (Insert) assertSqlCanBeParsedAndDeparsed(sqlStr);
857+
assertEquals("t", insert.getTable().getName());
858+
assertEquals(2, insert.getPartitions().size());
859+
assertEquals("pt2", insert.getPartitions().get(1).getColumn().getColumnName());
860+
assertEquals("'pt2'", insert.getPartitions().get(1).getValue().toString());
861+
assertTrue(insert.isOverwrite());
862+
863+
sqlStr = "INSERT INTO TABLE t PARTITION (pt1 = 'pt1', pt2 = 'pt2') SELECT * FROM a";
864+
insert = (Insert) assertSqlCanBeParsedAndDeparsed(sqlStr);
865+
assertEquals("t", insert.getTable().getName());
866+
assertEquals(2, insert.getPartitions().size());
867+
assertEquals("pt1", insert.getPartitions().get(0).getColumn().getColumnName());
868+
assertEquals("'pt1'", insert.getPartitions().get(0).getValue().toString());
869+
assertFalse(insert.isOverwrite());
870+
}
871+
840872
}

0 commit comments

Comments
 (0)