Skip to content

Commit

Permalink
JSQLParser#2119 add support INSERT OVERWRITE PARTITION
Browse files Browse the repository at this point in the history
  • Loading branch information
tt20061904 committed Jan 2, 2025
1 parent 23cd338 commit 20bfbc8
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>[2.15.1,)</version>
<version>2.18.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
67 changes: 67 additions & 0 deletions src/main/java/net/sf/jsqlparser/schema/Partition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2025 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.schema;

import net.sf.jsqlparser.expression.Expression;

import java.util.Collection;
import java.util.Objects;

public class Partition {
protected Column column;
protected Expression value;

public Partition() {

}

public Partition(Column column, Expression value) {
this.column = column;
this.value = value;
}

public static StringBuilder appendPartitionsTo(StringBuilder builder, Collection<Partition> partitions) {
int j = 0;
for (Partition partition : partitions) {
partition.appendTo(builder, j);
j++;
}
return builder;
}

public Column getColumn() {
return column;
}

public void setColumn(Column column) {
this.column = Objects.requireNonNull(column);
}

public Expression getValue() {
return value;
}

public void setValue(Expression value) {
this.value = Objects.requireNonNull(value);
}


@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPath"})
StringBuilder appendTo(StringBuilder builder, int j) {
if (j > 0) {
builder.append(", ");
}
builder.append(column.getColumnName());
if (value != null) {
builder.append(" = ").append(value);
}
return builder;
}
}
43 changes: 42 additions & 1 deletion src/main/java/net/sf/jsqlparser/statement/insert/Insert.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.sf.jsqlparser.expression.OracleHint;
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Partition;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.OutputClause;
import net.sf.jsqlparser.statement.ReturningClause;
Expand All @@ -36,11 +37,14 @@ public class Insert implements Statement {
private Table table;
private OracleHint oracleHint = null;
private ExpressionList<Column> columns;
private List<Partition> partitions;
private Select select;
private boolean onlyDefaultValues = false;
private List<UpdateSet> duplicateUpdateSets = null;
private InsertModifierPriority modifierPriority = null;
private boolean modifierIgnore = false;
private boolean overwrite = false;
private boolean tableKeyword = false;
private ReturningClause returningClause;
private List<UpdateSet> setUpdateSets = null;
private List<WithItem<?>> withItemsList;
Expand Down Expand Up @@ -103,6 +107,14 @@ public void setColumns(ExpressionList<Column> list) {
columns = list;
}

public List<Partition> getPartitions() {
return partitions;
}

public void setPartitions(List<Partition> list) {
partitions = list;
}

@Deprecated
public boolean isUseValues() {
return select != null && select instanceof Values;
Expand Down Expand Up @@ -163,6 +175,22 @@ public void setModifierIgnore(boolean modifierIgnore) {
this.modifierIgnore = modifierIgnore;
}

public boolean isOverwrite() {
return overwrite;
}

public void setOverwrite(boolean overwrite) {
this.overwrite = overwrite;
}

public boolean isTableKeyword() {
return tableKeyword;
}

public void setTableKeyword(boolean tableKeyword) {
this.tableKeyword = tableKeyword;
}

@Deprecated
public boolean isUseSet() {
return setUpdateSets != null && !setUpdateSets.isEmpty();
Expand Down Expand Up @@ -240,7 +268,14 @@ public String toString() {
if (modifierIgnore) {
sql.append("IGNORE ");
}
sql.append("INTO ");
if (overwrite) {
sql.append("OVERWRITE ");
} else {
sql.append("INTO ");
}
if (tableKeyword) {
sql.append("TABLE ");
}
sql.append(table).append(" ");

if (onlyDefaultValues) {
Expand All @@ -259,6 +294,12 @@ public String toString() {
sql.append(") ");
}

if (partitions != null) {
sql.append(" PARTITION (");
Partition.appendPartitionsTo(sql, partitions);
sql.append(") ");
}

if (outputClause != null) {
sql.append(outputClause);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import net.sf.jsqlparser.expression.ExpressionVisitor;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Partition;
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.SelectVisitor;
Expand Down Expand Up @@ -62,7 +63,14 @@ public void deParse(Insert insert) {
if (insert.isModifierIgnore()) {
buffer.append("IGNORE ");
}
buffer.append("INTO ");
if (insert.isOverwrite()) {
buffer.append("OVERWRITE ");
} else {
buffer.append("INTO ");
}
if (insert.isTableKeyword()) {
buffer.append("TABLE ");
}

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

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

if (insert.getPartitions() != null) {
buffer.append(" PARTITION (");
Partition.appendPartitionsTo(buffer, insert.getPartitions());
buffer.append(")");
}

if (insert.getOutputClause() != null) {
buffer.append(insert.getOutputClause().toString());
}
Expand Down
37 changes: 36 additions & 1 deletion src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
| <K_OVER:"OVER">
| <K_OVERFLOW:"OVERFLOW">
| <K_OVERLAPS:"OVERLAPS">
| <K_OVERWRITE:"OVERWRITE">
| <K_OPTIMIZE: "OPTIMIZE" >
| <K_PARALLEL:"PARALLEL">
| <K_PARENT:"PARENT">
Expand Down Expand Up @@ -1602,6 +1603,33 @@ List<UpdateSet> UpdateSets():
}
}

List<Partition> Partitions():
{
List<Partition> partitions = new ArrayList<Partition>();
Column tableColumn;
Expression valueExpression = null;
}
{
(
(
tableColumn=Column() [ "=" valueExpression=Expression() ]
{ partitions.add( new Partition (tableColumn, valueExpression)); }
)
)

(
LOOKAHEAD(2) (
","
tableColumn=Column() [ "=" valueExpression=Expression() ]
{ partitions.add( new Partition (tableColumn, valueExpression)); }
)
)*

{
return partitions;
}
}

Insert InsertWithWithItems( List<WithItem<?>> withItems ):
{
Insert insert;
Expand All @@ -1619,13 +1647,16 @@ Insert Insert():
List<WithItem<?>> with = null;
Column tableColumn = null;
ExpressionList<Column> columns = new ExpressionList<Column>();
List<Partition> partitions = new ArrayList<Partition>();
Expression exp = null;
ReturningClause returningClause;
Select select = null;
boolean useDuplicate = false;
Token tk = null;
InsertModifierPriority modifierPriority = null;
boolean modifierIgnore = false;
boolean overwrite = false;
boolean tableKeyword = false;

List<UpdateSet> updateSets;
List<UpdateSet> duplicateUpdateSets;
Expand All @@ -1648,7 +1679,8 @@ Insert Insert():
}
]
[ LOOKAHEAD(2) <K_IGNORE>{ modifierIgnore = true; }]
[ LOOKAHEAD(2) <K_INTO>] table=Table()
[ LOOKAHEAD(2) (<K_INTO> | <K_OVERWRITE> { insert.setOverwrite(true); }) [<K_TABLE> { insert.setTableKeyword(true); }] ] table=Table()
[ LOOKAHEAD(2) <K_PARTITION> "(" partitions=Partitions() ")" ]

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

Expand Down Expand Up @@ -1682,6 +1714,9 @@ Insert Insert():
if (!columns.isEmpty()) {
insert.setColumns(columns);
}
if (!partitions.isEmpty()) {
insert.setPartitions(partitions);
}
return insert.withWithItemsList(with)
.withSelect(select)
.withTable(table)
Expand Down
34 changes: 33 additions & 1 deletion src/test/java/net/sf/jsqlparser/statement/insert/InsertTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ public void testInsertSetWithDuplicateEliminationInDeparsing() throws JSQLParser
@Test
public void testInsertTableWithAliasIssue526() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed(
"INSERT INTO account t (name, addr, phone) SELECT * FROM user");
"INSERT INTO account AS t (name, addr, phone) SELECT * FROM user");
}

@Test
Expand Down Expand Up @@ -837,4 +837,36 @@ void testSelectAndInsertWithin2Ctes() throws JSQLParserException {
assertEquals(" inserted", withItems.get(1).getAlias().toString());
}

@Test
void testInsertOverwrite() throws JSQLParserException {
String sqlStr = "INSERT OVERWRITE TABLE t SELECT * FROM a";
Insert insert = (Insert) assertSqlCanBeParsedAndDeparsed(sqlStr);
assertEquals("t", insert.getTable().getName());
assertTrue(insert.isOverwrite());

sqlStr = "INSERT OVERWRITE TABLE t PARTITION (pt1, pt2) SELECT * FROM a";
insert = (Insert) assertSqlCanBeParsedAndDeparsed(sqlStr);
assertEquals("t", insert.getTable().getName());
assertEquals(2, insert.getPartitions().size());
assertEquals("pt1", insert.getPartitions().get(0).getColumn().getColumnName());
assertNull(insert.getPartitions().get(0).getValue());
assertTrue(insert.isOverwrite());

sqlStr = "INSERT OVERWRITE TABLE t PARTITION (pt1 = 'pt1', pt2 = 'pt2') SELECT * FROM a";
insert = (Insert) assertSqlCanBeParsedAndDeparsed(sqlStr);
assertEquals("t", insert.getTable().getName());
assertEquals(2, insert.getPartitions().size());
assertEquals("pt2", insert.getPartitions().get(1).getColumn().getColumnName());
assertEquals("'pt2'", insert.getPartitions().get(1).getValue().toString());
assertTrue(insert.isOverwrite());

sqlStr = "INSERT INTO TABLE t PARTITION (pt1 = 'pt1', pt2 = 'pt2') SELECT * FROM a";
insert = (Insert) assertSqlCanBeParsedAndDeparsed(sqlStr);
assertEquals("t", insert.getTable().getName());
assertEquals(2, insert.getPartitions().size());
assertEquals("pt1", insert.getPartitions().get(0).getColumn().getColumnName());
assertEquals("'pt1'", insert.getPartitions().get(0).getValue().toString());
assertFalse(insert.isOverwrite());
}

}

0 comments on commit 20bfbc8

Please sign in to comment.