Skip to content
Open
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: 2 additions & 0 deletions core/src/main/codegen/templates/Parser.jj
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,8 @@ void AddKeyValueOption(List<SqlNode> list) :
)
<EQ>
(
value = NumericLiteral()
|
value = StringLiteral()
|
value = SimpleIdentifier()
Expand Down
7 changes: 4 additions & 3 deletions core/src/main/java/org/apache/calcite/sql/SqlHint.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,18 +197,19 @@ public enum HintOptionFormat implements Symbolizable {
* The hint options are list of key-value pairs.
* For each pair,
* the key is a simple identifier or string literal,
* the value is a string literal.
* the value is a string or numeric literal.
*/
KV_LIST
}

//~ Tools ------------------------------------------------------------------

private static String getOptionAsString(SqlNode node) {
private String getOptionAsString(SqlNode node) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why remove the static?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So that we can reference options in the requireNonNull message.

assert node instanceof SqlIdentifier || SqlUtil.isLiteral(node);
if (node instanceof SqlIdentifier) {
return ((SqlIdentifier) node).getSimple();
}
return ((SqlLiteral) node).getValueAs(String.class);
return requireNonNull(((SqlLiteral) node).toValue(),
() -> "null hint literal in " + options);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public final Fixture sql(String sql) {
/** Test case for <a href="https://issues.apache.org/jira/browse/CALCITE-7498">[CALCITE-7498]
* The parser rejects the example hints from the documentation</a>. */
@Test void testDocumentationExample() {
final String sql = "SELECT /*+ hint1, hint2(a='1', b='2') */ *\n"
final String sql = "SELECT /*+ hint1, hint2(a=1, b=2) */ *\n"
+ "FROM emp /*+ hint3(5, 'x') */\n"
+ "JOIN dept /*+ hint4(c=id), hint5 */\n"
+ "ON emp.deptno = dept.deptno";
Expand Down Expand Up @@ -201,6 +201,12 @@ public final Fixture sql(String sql) {
sql(sql).ok();
}

@Test void testQueryHintWithKeyValueNumericLiteralOptions() {
final String sql = "select /*+ hint2(a=1, b=-1, c=1.1, d=-1.1, e=1e3, f=-1e-4) */ *\n"
+ "from emp";
sql(sql).ok();
}

@Test void testNestedQueryHint() {
final String sql = "select /*+ resource(parallelism='3'), repartition(10) */ empno\n"
+ "from (select /*+ resource(mem='20Mb')*/ empno, ename from emp)";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Correlate:[[USE_HASH_JOIN inheritPath:[0] options:[ORDERS, PRODUCTS_TEMPORAL]]]
</TestCase>
<TestCase name="testDocumentationExample">
<Resource name="sql">
<![CDATA[SELECT /*+ hint1, hint2(a='1', b='2') */ *
<![CDATA[SELECT /*+ hint1, hint2(a=1, b=2) */ *
FROM emp /*+ hint3(5, 'x') */
JOIN dept /*+ hint4(c=id), hint5 */
ON emp.deptno = dept.deptno]]>
Expand Down Expand Up @@ -350,6 +350,17 @@ LogicalJoin:[[NO_HASH_JOIN inheritPath:[0, 0]]]
TableScan:[[PROPERTIES inheritPath:[0, 0, 0] options:{K1=v1, K2=v2}], [INDEX inheritPath:[0, 0, 0] options:[ENAME]]]
TableScan:[[PROPERTIES inheritPath:[0, 0, 1] options:{K1=v1, K2=v2}], [INDEX inheritPath:[0, 0, 1] options:[ENAME]]]
TableScan:[[PROPERTIES inheritPath:[0, 1, 0] options:{K1=v1, K2=v2}], [INDEX inheritPath:[0, 1, 0] options:[ENAME]]]
]]>
</Resource>
</TestCase>
<TestCase name="testQueryHintWithKeyValueNumericLiteralOptions">
<Resource name="sql">
<![CDATA[select /*+ hint2(a=1, b=-1, c=1.1, d=-1.1, e=1e3, f=-1e-4) */ *
from emp]]>
</Resource>
<Resource name="hints">
<![CDATA[
Project:[[HINT2 inheritPath:[] options:{A=1, B=-1, C=1.1, D=-1.1, E=1E3, F=-1E-4}]]
]]>
</Resource>
</TestCase>
Expand Down
4 changes: 3 additions & 1 deletion site/_docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -3598,11 +3598,13 @@ optionKey:

optionVal:
simpleIdentifier
| numericLiteral
| stringLiteral

hintOption:
simpleIdentifier
| stringLiteral
| numericLiteral
| stringLiteral
{% endhighlight %}

It is experimental in Calcite, and yet not fully implemented, what we have implemented are:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9693,7 +9693,12 @@ private static Consumer<List<? extends Throwable>> checkWarnings(
final String sql1 = "select "
+ "/*+ properties(^k1^=123, k2='v2'), no_hash_join() */ "
+ "empno, ename, deptno from emps";
sql(sql1).fails("(?s).*Encountered \"k1 = 123\" at .*");
// Allow numeric literal k/v values.
final String expected1 = "SELECT\n"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to add a few more tests with the more complex numeric literals supported, e.g. 1e-2?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would make sense, but I'll add them to SqlHintsConverterTest rather than here.

+ "/*+ `PROPERTIES`(`K1` = 123, `K2` = 'v2'), `NO_HASH_JOIN` */\n"
+ "`EMPNO`, `ENAME`, `DEPTNO`\n"
+ "FROM `EMPS`";
sql(sql1).ok(expected1);
final String sql2 = "select "
+ "/*+ properties(k1, k2^=^'v2'), no_hash_join */ "
+ "empno, ename, deptno from emps";
Expand Down
Loading