GeezerVison is showing github.com — #2119 add support INSERT OVERWRITE PARTITION by tt20061904 · Pull Request #2135 · JSQLParser/JSqlParser
#2119 add support INSERT OVERWRITE PARTITION
#2135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement . We’ll occasionally send you account related emails.
Already on GitHub?
Sign in
to your account
Uh oh!
There was an error while loading. Please reload this page.
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter
Filter by extension
.java
(4)
.jjt
(1)
.xml
(1)
All 3 file types selected
Only manifest files
Viewed files
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Hide whitespace
Diff view
Hide whitespace
#2119 add support INSERT OVERWRITE PARTITION
Loading branch information
Uh oh!
There was an error while loading. Please reload this page.
commit 20bfbc85f7fb2760e323b48d13e5040cf8bc677f
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
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
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;
}
}
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
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
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());
}
}
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.