|
5 | 5 |
|
6 | 6 | package org.opensearch.sql.calcite.validate.converters; |
7 | 7 |
|
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.List; |
| 10 | +import java.util.stream.Collectors; |
| 11 | +import java.util.stream.Stream; |
| 12 | +import org.apache.calcite.rel.core.Aggregate; |
8 | 13 | import org.apache.calcite.rel.core.Correlate; |
9 | 14 | import org.apache.calcite.rel.core.Join; |
10 | 15 | import org.apache.calcite.rel.core.JoinRelType; |
| 16 | +import org.apache.calcite.rel.hint.RelHint; |
11 | 17 | import org.apache.calcite.rel.rel2sql.RelToSqlConverter; |
12 | 18 | import org.apache.calcite.sql.JoinConditionType; |
13 | 19 | import org.apache.calcite.sql.JoinType; |
14 | 20 | import org.apache.calcite.sql.SqlDialect; |
| 21 | +import org.apache.calcite.sql.SqlHint; |
| 22 | +import org.apache.calcite.sql.SqlIdentifier; |
15 | 23 | import org.apache.calcite.sql.SqlJoin; |
16 | 24 | import org.apache.calcite.sql.SqlLiteral; |
17 | 25 | import org.apache.calcite.sql.SqlNode; |
| 26 | +import org.apache.calcite.sql.SqlNodeList; |
| 27 | +import org.apache.calcite.sql.parser.SqlParserPos; |
18 | 28 |
|
19 | 29 | /** |
20 | 30 | * An extension of {@link RelToSqlConverter} to convert a relation algebra tree, translated from a |
@@ -107,4 +117,56 @@ protected Result visitAntiOrSemiJoin(Join e) { |
107 | 117 |
|
108 | 118 | return result(join, leftResult, rightResult); |
109 | 119 | } |
| 120 | + |
| 121 | + @Override |
| 122 | + public Result visit(Aggregate e) { |
| 123 | + Result r = super.visit(e); |
| 124 | + if (!e.getHints().isEmpty()) { |
| 125 | + List<SqlNode> hints = |
| 126 | + e.getHints().stream() |
| 127 | + .map(relHint -> (SqlNode) toSqlHint(relHint, POS)) |
| 128 | + .collect(Collectors.toCollection(ArrayList::new)); |
| 129 | + r.asSelect().setHints(SqlNodeList.of(POS, hints)); |
| 130 | + } |
| 131 | + return r; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Converts a RelHint to a SqlHint. |
| 136 | + * |
| 137 | + * <p>Copied from {@link RelToSqlConverter#toSqlHint(RelHint, SqlParserPos)} (as Calcite 1.41) as |
| 138 | + * it is private there |
| 139 | + */ |
| 140 | + private static SqlHint toSqlHint(RelHint hint, SqlParserPos pos) { |
| 141 | + if (hint.kvOptions != null) { |
| 142 | + return new SqlHint( |
| 143 | + pos, |
| 144 | + new SqlIdentifier(hint.hintName, pos), |
| 145 | + SqlNodeList.of( |
| 146 | + pos, |
| 147 | + hint.kvOptions.entrySet().stream() |
| 148 | + .flatMap( |
| 149 | + e -> |
| 150 | + Stream.of( |
| 151 | + new SqlIdentifier(e.getKey(), pos), |
| 152 | + SqlLiteral.createCharString(e.getValue(), pos))) |
| 153 | + .collect(Collectors.toList())), |
| 154 | + SqlHint.HintOptionFormat.KV_LIST); |
| 155 | + } else if (hint.listOptions != null) { |
| 156 | + return new SqlHint( |
| 157 | + pos, |
| 158 | + new SqlIdentifier(hint.hintName, pos), |
| 159 | + SqlNodeList.of( |
| 160 | + pos, |
| 161 | + hint.listOptions.stream() |
| 162 | + .map(e -> SqlLiteral.createCharString(e, pos)) |
| 163 | + .collect(Collectors.toList())), |
| 164 | + SqlHint.HintOptionFormat.LITERAL_LIST); |
| 165 | + } |
| 166 | + return new SqlHint( |
| 167 | + pos, |
| 168 | + new SqlIdentifier(hint.hintName, pos), |
| 169 | + SqlNodeList.EMPTY, |
| 170 | + SqlHint.HintOptionFormat.EMPTY); |
| 171 | + } |
110 | 172 | } |
0 commit comments