-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat: support OPTION hints after MERGE and INSERT ... VALUES, and the OPTIMIZE FOR (@var UNKNOWN) parameter form #2486
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
Open
fudianchn
wants to merge
1
commit into
JSQLParser:master
Choose a base branch
from
fudianchn:feat/option-hints-followup
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/main/java/net/sf/jsqlparser/expression/UnknownVariable.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /*- | ||
| * #%L | ||
| * JSQLParser library | ||
| * %% | ||
| * Copyright (C) 2004 - 2019 JSQLParser | ||
| * %% | ||
| * Dual licensed under GNU LGPL 2.1 or Apache License 2.0. | ||
| * #L% | ||
| */ | ||
| package net.sf.jsqlparser.expression; | ||
|
|
||
| import net.sf.jsqlparser.parser.ASTNodeAccessImpl; | ||
|
|
||
| /** | ||
| * A user variable followed by the bare {@code UNKNOWN} keyword, as used in the SQL Server (T-SQL) | ||
| * {@code OPTIMIZE FOR (@variable_name UNKNOWN)} query hint parameter form, see | ||
| * <a href="https://learn.microsoft.com/en-us/sql/t-sql/queries/hints-transact-sql-query">Hints | ||
| * (Transact-SQL) - Query Hints</a>. | ||
| */ | ||
| public class UnknownVariable extends ASTNodeAccessImpl implements Expression { | ||
|
|
||
| private UserVariable variable; | ||
|
|
||
| public UnknownVariable() { | ||
| // empty constructor | ||
| } | ||
|
|
||
| public UnknownVariable(UserVariable variable) { | ||
| this.variable = variable; | ||
| } | ||
|
|
||
| public UserVariable getVariable() { | ||
| return variable; | ||
| } | ||
|
|
||
| public void setVariable(UserVariable variable) { | ||
| this.variable = variable; | ||
| } | ||
|
|
||
| public UnknownVariable withVariable(UserVariable variable) { | ||
| this.setVariable(variable); | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public <T, S> T accept(ExpressionVisitor<T> expressionVisitor, S context) { | ||
| return expressionVisitor.visit(this, context); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return variable.toString() + " UNKNOWN"; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,8 +12,11 @@ | |
| import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed; | ||
|
|
||
| import net.sf.jsqlparser.JSQLParserException; | ||
| import net.sf.jsqlparser.expression.UnknownVariable; | ||
| import net.sf.jsqlparser.parser.CCJSqlParserUtil; | ||
| import net.sf.jsqlparser.statement.Statement; | ||
| import net.sf.jsqlparser.statement.insert.Insert; | ||
| import net.sf.jsqlparser.statement.merge.Merge; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
|
|
@@ -147,6 +150,55 @@ public void testOptionInUpdateAndDelete() throws JSQLParserException { | |
| Assertions.assertNotNull(((net.sf.jsqlparser.statement.update.Update) update).getOption()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testOptionOptimizeForUnknownParameter() throws JSQLParserException { | ||
| String sql = "SELECT * FROM t WHERE c = @p OPTION (OPTIMIZE FOR (@p UNKNOWN))"; | ||
| PlainSelect plainSelect = | ||
| (PlainSelect) assertSqlCanBeParsedAndDeparsed(sql, true); | ||
| OptionHint optimizeFor = plainSelect.getOption().getOptionHints().get(0); | ||
| Assertions.assertEquals(1, optimizeFor.getParameters().size()); | ||
| Assertions.assertTrue(optimizeFor.getParameters().get(0) instanceof UnknownVariable); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Assertions should provide |
||
| Assertions.assertEquals("@p UNKNOWN", optimizeFor.getParameters().get(0).toString()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testOptionOptimizeForMixedParameters() throws JSQLParserException { | ||
| String sql = "SELECT * FROM t WHERE c = @p OPTION (OPTIMIZE FOR (@p = 1, @q UNKNOWN))"; | ||
| PlainSelect plainSelect = | ||
| (PlainSelect) assertSqlCanBeParsedAndDeparsed(sql, true); | ||
| OptionHint optimizeFor = plainSelect.getOption().getOptionHints().get(0); | ||
| Assertions.assertEquals(2, optimizeFor.getParameters().size()); | ||
| Assertions.assertEquals("@p = 1", optimizeFor.getParameters().get(0).toString()); | ||
| Assertions.assertTrue(optimizeFor.getParameters().get(1) instanceof UnknownVariable); | ||
| Assertions.assertEquals("@q UNKNOWN", optimizeFor.getParameters().get(1).toString()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testOptionAfterInsertValues() throws JSQLParserException { | ||
| String sql = "INSERT INTO t (a) VALUES (1) OPTION (RECOMPILE)"; | ||
| Statement statement = assertSqlCanBeParsedAndDeparsed(sql, true); | ||
| Insert insert = (Insert) statement; | ||
| Assertions.assertNotNull(insert.getSelect().getOption()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testOptionAfterMergeStatement() throws JSQLParserException { | ||
| String sql = | ||
| "MERGE INTO t USING s ON t.id = s.id WHEN MATCHED THEN UPDATE SET a = s.a OPTION (HASH JOIN)"; | ||
| Statement statement = assertSqlCanBeParsedAndDeparsed(sql, true); | ||
| Merge merge = (Merge) statement; | ||
| Assertions.assertNotNull(merge.getOption()); | ||
| Assertions.assertEquals("HASH JOIN", merge.getOption().getOptionHints().get(0).getName()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testOptionAfterMergeOutputClause() throws JSQLParserException { | ||
| assertSqlCanBeParsedAndDeparsed( | ||
| "MERGE INTO t USING s ON t.id = s.id WHEN MATCHED THEN UPDATE SET a = s.a " | ||
| + "OUTPUT deleted.a OPTION (HASH JOIN)", | ||
| true); | ||
| } | ||
|
|
||
| @Test | ||
| public void testOptionAsIdentifierStillWorks() throws JSQLParserException { | ||
| // OPTION stays a non-reserved keyword usable as column and table name | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really need an extra class for this? Why not just carry a normal "Identifier" or "String" along? I would love to avoid especially the extra methods in the Visitors.