Skip to content
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ and missing syntax gets added on demand — [open an issue](https://github.com/J
| **PostgreSQL ownership** | `DROP OWNED` · `REASSIGN OWNED` |
| **Two-phase commit** | PostgreSQL `PREPARE TRANSACTION` · `COMMIT PREPARED` · `ROLLBACK PREPARED` · MySQL `XA START`/`END`/`PREPARE`/`COMMIT`/`ROLLBACK`/`RECOVER` |
| **PostgreSQL maintenance** | `VACUUM` · `ANALYZE` · `CLUSTER` · `CHECKPOINT` |
| **Prepared-statement query plans** | `EXPLAIN EXECUTE` with optional arguments and PostgreSQL `EXPLAIN` options |
| **PostgreSQL session statements** | `DISCARD` · `LISTEN` · `UNLISTEN` · `SHOW`/`RESET TIME ZONE`/`TRANSACTION ISOLATION LEVEL`/`SESSION AUTHORIZATION` · `COPY` options from before 9.0 |
| **PostgreSQL foreign schema import** | `IMPORT FOREIGN SCHEMA … [LIMIT TO \| EXCEPT (…)] FROM SERVER … INTO … [OPTIONS (…)]` |
| **PostgreSQL table and index options** | Bare `CAST`/`EXTRACT` keys with `Dialect.POSTGRESQL` · either order of HASH partition bound options · identity sequence `LOGGED`/`UNLOGGED` |
Expand Down
23 changes: 19 additions & 4 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -5389,6 +5389,11 @@ public class CCJSqlParser extends AbstractJSqlParser<CCJSqlParser> {
}
}

private boolean isExplainPreparedExecutionDialect() {
String dialect = getAsString(Feature.dialect);
return dialect == null || Dialect.POSTGRESQL.name().equals(dialect);
}

private void validatePostgresqlExplain(ExplainStatement explain) throws ParseException {
if (explain.getTable() != null) {
throw new ParseException("PostgreSQL EXPLAIN requires a statement");
Expand Down Expand Up @@ -10479,7 +10484,7 @@ ExplainStatement MySqlExplain():
( schema=RelObjectName() | token=<S_CHAR_LITERAL> { schema = token.image; } )
{ explain.setSchemaName(schema); }
]
statement=ExplainableStatement() { explain.setStatement(statement); }
statement=ExplainableStatement(false) { explain.setStatement(statement); }
)
{ return explain; }
}
Expand All @@ -10495,8 +10500,8 @@ ExplainStatement.Option MySqlExplainFormat():
{ return mySqlExplainFormat(token); }
}

/** The statement EXPLAIN explains: a query or a DML statement. */
Statement ExplainableStatement():
/** The statement EXPLAIN explains: a query, DML, or prepared execution. */
Statement ExplainableStatement(boolean allowPreparedExecution):
{
List<WithItem<?>> with = null;
Statement statement;
Expand All @@ -10507,6 +10512,16 @@ Statement ExplainableStatement():
LOOKAHEAD({ getToken(1).kind == K_REPLACE && isMySqlUtilityDialect() })
statement = Upsert()
|
LOOKAHEAD(2, <K_EXECUTE> ColumnIdentifier(), { isExplainPreparedExecutionDialect() })
statement = Execute()
{
Execute execute = (Execute) statement;
requireDdlSyntax(allowPreparedExecution && execute.getUsingVariables() == null
&& (execute.getExprList() == null
|| execute.getExprList() instanceof ParenthesedExpressionList),
"EXPLAIN EXECUTE accepts a prepared statement name and optional parenthesized arguments");
}
|
[ LOOKAHEAD(2) with=WithList() ]
(
statement = SelectWithWithItems( with ) { if (with != null) { widen(statement, first, null); } }
Expand Down Expand Up @@ -10540,7 +10555,7 @@ ExplainStatement Explain():
LOOKAHEAD(2) "(" options = PostgresqlExplainOptions() ")" { parenthesized = true; }
| options = ExplainStatementOptions()
)
statement = ExplainableStatement()
statement = ExplainableStatement(tk.kind == K_EXPLAIN)
{
es = new ExplainStatement(tk.image, statement, options);
es.setParenthesizedOptions(parenthesized);
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/net/sf/jsqlparser/statement/ExplainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
import static org.assertj.core.api.Assertions.assertThat;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.AbstractJSqlParser.Dialect;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.delete.Delete;
import net.sf.jsqlparser.statement.execute.Execute;
import org.junit.jupiter.api.Test;

public class ExplainTest {
Expand All @@ -24,6 +26,24 @@ public void testDescribe() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("EXPLAIN SELECT * FROM mytable");
}

@Test
public void testExplainPreparedExecutionWithoutDialect() throws JSQLParserException {
ExplainStatement explain =
(ExplainStatement) assertSqlCanBeParsedAndDeparsed("EXPLAIN EXECUTE q");
assertThat(explain.getStatement()).isInstanceOf(Execute.class);
ExplainStatement bare =
(ExplainStatement) assertSqlCanBeParsedAndDeparsed("EXPLAIN EXECUTE");
assertThat(bare.getTable().getName()).isEqualTo("EXECUTE");
}

@Test
public void testExplainExecuteAsMySqlTableAndColumn() throws JSQLParserException {
ExplainStatement explain = (ExplainStatement) assertSqlCanBeParsedAndDeparsed(
"EXPLAIN EXECUTE q", true, parser -> parser.withDialect(Dialect.MYSQL));
assertThat(explain.getTable().getName()).isEqualTo("EXECUTE");
assertThat(explain.getStatement()).isNull();
}

@Test
public void testAnalyze() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("EXPLAIN ANALYZE SELECT * FROM mytable");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.ExplainStatement.Option;
import net.sf.jsqlparser.statement.ExplainStatement.OptionType;
import net.sf.jsqlparser.statement.execute.Execute;
import net.sf.jsqlparser.test.TestUtils;
import net.sf.jsqlparser.util.TablesNamesFinder;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -78,6 +79,34 @@ void rejectInvalidPostgresqlOptions(String sql) {
parser -> parser.withDialect(Dialect.POSTGRESQL).withUnsupportedStatements(false)));
}

@ParameterizedTest
@ValueSource(strings = {
"EXPLAIN EXECUTE q",
"EXPLAIN EXECUTE q (42)",
"EXPLAIN (FORMAT JSON) EXECUTE q (42)",
"EXPLAIN ANALYZE VERBOSE EXECUTE q (42, 7)"
})
void explainPreparedExecution(String sql) throws JSQLParserException {
ExplainStatement explain = assertInstanceOf(ExplainStatement.class,
TestUtils.assertSqlCanBeParsedAndDeparsed(sql, true,
parser -> parser.withDialect(Dialect.POSTGRESQL)));
Execute execute = assertInstanceOf(Execute.class, explain.getStatement());
assertEquals("q", execute.getName());
assertEquals(Execute.ExecType.EXECUTE, execute.getExecType());
}

@ParameterizedTest
@ValueSource(strings = {
"SUMMARIZE EXECUTE q",
"EXPLAIN EXECUTE q 42",
"EXPLAIN EXECUTE q USING @x",
"EXPLAIN CALL q()"
})
void rejectOtherExecutionForms(String sql) {
assertThrows(JSQLParserException.class, () -> CCJSqlParserUtil.parse(sql,
parser -> parser.withDialect(Dialect.POSTGRESQL).withUnsupportedStatements(false)));
}

@Test
void preserveRepetitionsAndLegacyMapAccess() throws JSQLParserException {
ExplainStatement explain = (ExplainStatement) TestUtils.assertSqlCanBeParsedAndDeparsed(
Expand Down
Loading