Skip to content
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

#2119 add support INSERT OVERWRITE PARTITION #2135

Merged
merged 4 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public class ParserKeywordsUtils {
{"OUTER", RESTRICTED_JSQLPARSER},
{"OUTPUT", RESTRICTED_JSQLPARSER},
{"OPTIMIZE ", RESTRICTED_JSQLPARSER},
{"OVERWRITE ", RESTRICTED_JSQLPARSER},
{"PIVOT", RESTRICTED_JSQLPARSER},
{"PLUS", RESTRICTED_JSQLPARSER},
{"PREFERRING", RESTRICTED_JSQLPARSER},
Expand Down
66 changes: 66 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,66 @@
/*-
* #%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 void appendPartitionsTo(StringBuilder builder,
Collection<Partition> partitions) {
int j = 0;
for (Partition partition : partitions) {
partition.appendTo(builder, j);
j++;
}
}

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"})
void appendTo(StringBuilder builder, int j) {
if (j > 0) {
builder.append(", ");
}
builder.append(column.getColumnName());
if (value != null) {
builder.append(" = ").append(value);
}
}
}
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
45 changes: 42 additions & 3 deletions 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,11 +1679,16 @@ Insert Insert():
}
]
[ LOOKAHEAD(2) <K_IGNORE>{ modifierIgnore = true; }]
[ LOOKAHEAD(2) <K_INTO>] table=Table()
[ LOOKAHEAD(2) (
<K_OVERWRITE> <K_TABLE> { insert.setOverwrite(true); insert.setTableKeyword(true); }
| <K_INTO> [ LOOKAHEAD(2) <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)); }]

[LOOKAHEAD(2) "(" columns=ColumnList() ")" ]
[ LOOKAHEAD(2) "(" columns=ColumnList() ")" ]

[ outputClause = OutputClause() { insert.setOutputClause(outputClause); } ]

Expand Down Expand Up @@ -1682,6 +1718,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 Expand Up @@ -2045,7 +2084,7 @@ String RelObjectNameWithoutValue() :
{ Token tk = null; }
{
( tk=<DATA_TYPE> | tk=<S_IDENTIFIER> | tk=<S_QUOTED_IDENTIFIER> | tk=<K_DATE_LITERAL> | tk=<K_DATETIMELITERAL> | tk=<K_STRING_FUNCTION_NAME> | tk=<K_ISOLATION> | tk=<K_TIME_KEY_EXPR> | tk=<K_TEXT_LITERAL>
| tk="ACTION" | tk="ACTIVE" | tk="ADD" | tk="ADVANCE" | tk="ADVISE" | tk="AGAINST" | tk="ALGORITHM" | tk="ALTER" | tk="ANALYZE" | tk="APPLY" | tk="APPROXIMATE" | tk="ARCHIVE" | tk="ARRAY" | tk="ASC" | tk="AT" | tk="AUTHORIZATION" | tk="AUTO" | tk="AUTO_INCREMENT" | tk="BASE64" | tk="BEGIN" | tk="BERNOULLI" | tk="BINARY" | tk="BIT" | tk="BLOCK" | tk="BROWSE" | tk="BUFFERS" | tk="BY" | tk="BYTE" | tk="BYTES" | tk="CACHE" | tk="CALL" | tk="CASCADE" | tk="CASE" | tk="CAST" | tk="CHANGE" | tk="CHANGES" | tk="CHAR" | tk="CHARACTER" | tk="CHECKPOINT" | tk="CLOSE" | tk="COLLATE" | tk="COLUMN" | tk="COLUMNS" | tk="COMMENT" | tk="COMMIT" | tk="CONCURRENTLY" | tk="CONFLICT" | tk="CONSTRAINTS" | tk="CONVERT" | tk="COSTS" | tk="COUNT" | tk="CS" | tk="CYCLE" | tk="DATA" | tk="DATABASE" | tk="DATETIME" | tk="DBA_RECYCLEBIN" | tk="DDL" | tk="DECLARE" | tk="DEFERRABLE" | tk="DELAYED" | tk="DELETE" | tk="DESC" | tk="DESCRIBE" | tk="DISABLE" | tk="DISCONNECT" | tk="DIV" | tk="DML" | tk="DO" | tk="DOMAIN" | tk="DROP" | tk="DUMP" | tk="DUPLICATE" | tk="ELEMENTS" | tk="EMIT" | tk="ENABLE" | tk="ENCRYPTION" | tk="END" | tk="ENGINE" | tk="ERROR" | tk="ESCAPE" | tk="EXCLUDE" | tk="EXEC" | tk="EXECUTE" | tk="EXPLAIN" | tk="EXPLICIT" | tk="EXTENDED" | tk="EXTRACT" | tk="FILTER" | tk="FIRST" | tk="FLUSH" | tk="FN" | tk="FOLLOWING" | tk="FORMAT" | tk="FULLTEXT" | tk="FUNCTION" | tk="GRANT" | tk="GROUP_CONCAT" | tk="GUARD" | tk="HASH" | tk="HIGH_PRIORITY" | tk="HISTORY" | tk="HOPPING" | tk="INCLUDE" | tk="INCLUDE_NULL_VALUES" | tk="INCREMENT" | tk="INDEX" | tk="INSERT" | tk="INTERLEAVE" | tk="INTERPRET" | tk="INVALIDATE" | tk="ISNULL" | tk="JSON" | tk="JSON_ARRAY" | tk="JSON_ARRAYAGG" | tk="JSON_OBJECT" | tk="JSON_OBJECTAGG" | tk="KEEP" | tk="KEY" | tk="KEYS" | tk="LAST" | tk="LEADING" | tk="LESS" | tk="LINK" | tk="LOCAL" | tk="LOCK" | tk="LOCKED" | tk="LOG" | tk="LONGTEXT" | tk="LOOP" | tk="LOW_PRIORITY" | tk="MATCH" | tk="MATCHED" | tk="MATCH_ALL" | tk="MATCH_ANY" | tk="MATCH_PHRASE" | tk="MATCH_PHRASE_PREFIX" | tk="MATCH_REGEXP" | tk="MATERIALIZED" | tk="MAX" | tk="MAXVALUE" | tk="MEDIUMTEXT" | tk="MEMBER" | tk="MERGE" | tk="MIN" | tk="MINVALUE" | tk="MODIFY" | tk="MOVEMENT" | tk="NEXT" | tk="NO" | tk="NOCACHE" | tk="NOKEEP" | tk="NOLOCK" | tk="NOMAXVALUE" | tk="NOMINVALUE" | tk="NOORDER" | tk="NOTHING" | tk="NOTNULL" | tk="NOVALIDATE" | tk="NOWAIT" | tk="NULLS" | tk="OF" | tk="OFF" | tk="OPEN" | tk="OVER" | tk="OVERFLOW" | tk="OVERLAPS" | tk="PARALLEL" | tk="PARENT" | tk="PARTITION" | tk="PATH" | tk="PERCENT" | tk="PLACING" | tk="PLAN" | tk="PRECEDING" | tk="PRIMARY" | tk="PURGE" | tk="QUERY" | tk="QUICK" | tk="QUIESCE" | tk="RANGE" | tk="RAW" | tk="READ" | tk="RECURSIVE" | tk="RECYCLEBIN" | tk="REFERENCES" | tk="REFRESH" | tk="REGEXP" | tk="REGEXP_LIKE" | tk="REGISTER" | tk="REMOTE" | tk="RENAME" | tk="REPEATABLE" | tk="REPLACE" | tk="RESET" | tk="RESPECT" | tk="RESTART" | tk="RESTRICT" | tk="RESTRICTED" | tk="RESUMABLE" | tk="RESUME" | tk="RETURN" | tk="RLIKE" | tk="ROLLBACK" | tk="ROLLUP" | tk="ROOT" | tk="ROW" | tk="ROWS" | tk="RR" | tk="RS" | tk="SAFE_CAST" | tk="SAVEPOINT" | tk="SCHEMA" | tk="SECURE" | tk="SEED" | tk="SEPARATOR" | tk="SEQUENCE" | tk="SESSION" | tk="SETS" | tk="SHARE" | tk="SHOW" | tk="SHUTDOWN" | tk="SIBLINGS" | tk="SIGNED" | tk="SIMILAR" | tk="SIZE" | tk="SKIP" | tk="STORED" | tk="STRING" | tk="STRUCT" | tk="SUMMARIZE" | tk="SUSPEND" | tk="SWITCH" | tk="SYNONYM" | tk="SYSTEM" | tk="TABLE" | tk="TABLESPACE" | tk="TEMP" | tk="TEMPORARY" | tk="TEXT" | tk="THAN" | tk="THEN" | tk="TIMEOUT" | tk="TIMESTAMPTZ" | tk="TIMEZONE" | tk="TINYTEXT" | tk="TO" | tk="TRIGGER" | tk="TRUNCATE" | tk="TRY_CAST" | tk="TUMBLING" | tk="TYPE" | tk="UNLOGGED" | tk="UNQIESCE" | tk="UNSIGNED" | tk="UPDATE" | tk="UPSERT" | tk="UR" | tk="USER" | tk="VALIDATE" | tk="VERBOSE" | tk="VIEW" | tk="VOLATILE" | tk="WAIT" | tk="WITHIN" | tk="WITHOUT" | tk="WITHOUT_ARRAY_WRAPPER" | tk="WORK" | tk="XML" | tk="XMLAGG" | tk="XMLDATA" | tk="XMLSCHEMA" | tk="XMLTEXT" | tk="XSINIL" | tk="YAML" | tk="YES" | tk="ZONE" )
| tk="ACTION" | tk="ACTIVE" | tk="ADD" | tk="ADVANCE" | tk="ADVISE" | tk="AGAINST" | tk="ALGORITHM" | tk="ALTER" | tk="ANALYZE" | tk="APPLY" | tk="APPROXIMATE" | tk="ARCHIVE" | tk="ARRAY" | tk="ASC" | tk="AT" | tk="AUTHORIZATION" | tk="AUTO" | tk="AUTO_INCREMENT" | tk="BASE64" | tk="BEGIN" | tk="BERNOULLI" | tk="BINARY" | tk="BIT" | tk="BLOCK" | tk="BROWSE" | tk="BUFFERS" | tk="BY" | tk="BYTE" | tk="BYTES" | tk="CACHE" | tk="CALL" | tk="CASCADE" | tk="CASE" | tk="CAST" | tk="CHANGE" | tk="CHANGES" | tk="CHAR" | tk="CHARACTER" | tk="CHECKPOINT" | tk="CLOSE" | tk="COLLATE" | tk="COLUMN" | tk="COLUMNS" | tk="COMMENT" | tk="COMMIT" | tk="CONCURRENTLY" | tk="CONFLICT" | tk="CONSTRAINTS" | tk="CONVERT" | tk="COSTS" | tk="COUNT" | tk="CS" | tk="CYCLE" | tk="DATA" | tk="DATABASE" | tk="DATETIME" | tk="DBA_RECYCLEBIN" | tk="DDL" | tk="DECLARE" | tk="DEFERRABLE" | tk="DELAYED" | tk="DELETE" | tk="DESC" | tk="DESCRIBE" | tk="DISABLE" | tk="DISCONNECT" | tk="DIV" | tk="DML" | tk="DO" | tk="DOMAIN" | tk="DROP" | tk="DUMP" | tk="DUPLICATE" | tk="ELEMENTS" | tk="EMIT" | tk="ENABLE" | tk="ENCRYPTION" | tk="END" | tk="ENGINE" | tk="ERROR" | tk="ESCAPE" | tk="EXCLUDE" | tk="EXEC" | tk="EXECUTE" | tk="EXPLAIN" | tk="EXPLICIT" | tk="EXTENDED" | tk="EXTRACT" | tk="FILTER" | tk="FIRST" | tk="FLUSH" | tk="FN" | tk="FOLLOWING" | tk="FORMAT" | tk="FULLTEXT" | tk="FUNCTION" | tk="GRANT" | tk="GROUP_CONCAT" | tk="GUARD" | tk="HASH" | tk="HIGH_PRIORITY" | tk="HISTORY" | tk="HOPPING" | tk="INCLUDE" | tk="INCLUDE_NULL_VALUES" | tk="INCREMENT" | tk="INDEX" | tk="INSERT" | tk="INTERLEAVE" | tk="INTERPRET" | tk="INVALIDATE" | tk="ISNULL" | tk="JSON" | tk="JSON_ARRAY" | tk="JSON_ARRAYAGG" | tk="JSON_OBJECT" | tk="JSON_OBJECTAGG" | tk="KEEP" | tk="KEY" | tk="KEYS" | tk="LAST" | tk="LEADING" | tk="LESS" | tk="LINK" | tk="LOCAL" | tk="LOCK" | tk="LOCKED" | tk="LOG" | tk="LONGTEXT" | tk="LOOP" | tk="LOW_PRIORITY" | tk="MATCH" | tk="MATCHED" | tk="MATCH_ALL" | tk="MATCH_ANY" | tk="MATCH_PHRASE" | tk="MATCH_PHRASE_PREFIX" | tk="MATCH_REGEXP" | tk="MATERIALIZED" | tk="MAX" | tk="MAXVALUE" | tk="MEDIUMTEXT" | tk="MEMBER" | tk="MERGE" | tk="MIN" | tk="MINVALUE" | tk="MODIFY" | tk="MOVEMENT" | tk="NEXT" | tk="NO" | tk="NOCACHE" | tk="NOKEEP" | tk="NOLOCK" | tk="NOMAXVALUE" | tk="NOMINVALUE" | tk="NOORDER" | tk="NOTHING" | tk="NOTNULL" | tk="NOVALIDATE" | tk="NOWAIT" | tk="NULLS" | tk="OF" | tk="OFF" | tk="OPEN" | tk="OVER" | tk="OVERFLOW" | tk="OVERLAPS" | tk="OVERWRITE" | tk="PARALLEL" | tk="PARENT" | tk="PARTITION" | tk="PATH" | tk="PERCENT" | tk="PLACING" | tk="PLAN" | tk="PRECEDING" | tk="PRIMARY" | tk="PURGE" | tk="QUERY" | tk="QUICK" | tk="QUIESCE" | tk="RANGE" | tk="RAW" | tk="READ" | tk="RECURSIVE" | tk="RECYCLEBIN" | tk="REFERENCES" | tk="REFRESH" | tk="REGEXP" | tk="REGEXP_LIKE" | tk="REGISTER" | tk="REMOTE" | tk="RENAME" | tk="REPEATABLE" | tk="REPLACE" | tk="RESET" | tk="RESPECT" | tk="RESTART" | tk="RESTRICT" | tk="RESTRICTED" | tk="RESUMABLE" | tk="RESUME" | tk="RETURN" | tk="RLIKE" | tk="ROLLBACK" | tk="ROLLUP" | tk="ROOT" | tk="ROW" | tk="ROWS" | tk="RR" | tk="RS" | tk="SAFE_CAST" | tk="SAVEPOINT" | tk="SCHEMA" | tk="SECURE" | tk="SEED" | tk="SEPARATOR" | tk="SEQUENCE" | tk="SESSION" | tk="SETS" | tk="SHARE" | tk="SHOW" | tk="SHUTDOWN" | tk="SIBLINGS" | tk="SIGNED" | tk="SIMILAR" | tk="SIZE" | tk="SKIP" | tk="STORED" | tk="STRING" | tk="STRUCT" | tk="SUMMARIZE" | tk="SUSPEND" | tk="SWITCH" | tk="SYNONYM" | tk="SYSTEM" | tk="TABLE" | tk="TABLESPACE" | tk="TEMP" | tk="TEMPORARY" | tk="TEXT" | tk="THAN" | tk="THEN" | tk="TIMEOUT" | tk="TIMESTAMPTZ" | tk="TIMEZONE" | tk="TINYTEXT" | tk="TO" | tk="TRIGGER" | tk="TRUNCATE" | tk="TRY_CAST" | tk="TUMBLING" | tk="TYPE" | tk="UNLOGGED" | tk="UNQIESCE" | tk="UNSIGNED" | tk="UPDATE" | tk="UPSERT" | tk="UR" | tk="USER" | tk="VALIDATE" | tk="VERBOSE" | tk="VIEW" | tk="VOLATILE" | tk="WAIT" | tk="WITHIN" | tk="WITHOUT" | tk="WITHOUT_ARRAY_WRAPPER" | tk="WORK" | tk="XML" | tk="XMLAGG" | tk="XMLDATA" | tk="XMLSCHEMA" | tk="XMLTEXT" | tk="XSINIL" | tk="YAML" | tk="YES" | tk="ZONE" )
{ return tk.image; }
}

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\nTABLE 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\tTABLE 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());
}

}
Loading