|
22 | 22 | import com.google.common.collect.Lists; |
23 | 23 | import com.google.common.collect.Maps; |
24 | 24 | import org.apache.calcite.sql.SqlBasicCall; |
| 25 | +import org.apache.calcite.sql.SqlCharStringLiteral; |
25 | 26 | import org.apache.calcite.sql.SqlIdentifier; |
26 | 27 | import org.apache.calcite.sql.SqlInsert; |
27 | 28 | import org.apache.calcite.sql.SqlJoin; |
|
35 | 36 | import java.util.List; |
36 | 37 | import java.util.Map; |
37 | 38 |
|
38 | | -import static org.apache.calcite.sql.SqlKind.*; |
| 39 | +import static org.apache.calcite.sql.SqlKind.IDENTIFIER; |
| 40 | +import static org.apache.calcite.sql.SqlKind.LITERAL; |
| 41 | +import static org.apache.calcite.sql.SqlKind.OR; |
39 | 42 |
|
40 | 43 | /** |
41 | 44 | * |
|
47 | 50 | public class SidePredicatesParser { |
48 | 51 |
|
49 | 52 | private FlinkPlanner flinkPlanner = new FlinkPlanner(); |
| 53 | + private static final String QUOTE = "'"; |
50 | 54 |
|
51 | 55 | public void fillPredicatesForSideTable(String exeSql, Map<String, AbstractSideTableInfo> sideTableMap) throws SqlParseException { |
52 | 56 | SqlNode sqlNode = flinkPlanner.getParser().parse(exeSql); |
@@ -140,26 +144,57 @@ private void extractPredicateInfo(SqlNode whereNode, List<PredicateInfo> predica |
140 | 144 | } |
141 | 145 | } |
142 | 146 |
|
143 | | - private void fillPredicateInfoToList(SqlBasicCall whereNode, List<PredicateInfo> predicatesInfoList, String operatorName, SqlKind operatorKind, |
144 | | - int fieldIndex, int conditionIndex) { |
| 147 | + private void fillPredicateInfoToList( |
| 148 | + SqlBasicCall whereNode, |
| 149 | + List<PredicateInfo> predicatesInfoList, |
| 150 | + String operatorName, |
| 151 | + SqlKind operatorKind, |
| 152 | + int fieldIndex, |
| 153 | + int conditionIndex) { |
145 | 154 | SqlNode sqlNode = whereNode.getOperands()[fieldIndex]; |
146 | 155 | if (sqlNode.getKind() == SqlKind.IDENTIFIER) { |
147 | 156 | SqlIdentifier fieldFullPath = (SqlIdentifier) sqlNode; |
148 | 157 | if (fieldFullPath.names.size() == 2) { |
149 | 158 | String ownerTable = fieldFullPath.names.get(0); |
150 | 159 | String fieldName = fieldFullPath.names.get(1); |
151 | | - String content = (operatorKind == SqlKind.BETWEEN) ? whereNode.getOperands()[conditionIndex].toString() + " AND " + |
152 | | - whereNode.getOperands()[2].toString() : whereNode.getOperands()[conditionIndex].toString(); |
153 | 160 |
|
154 | | - if (StringUtils.containsIgnoreCase(content,SqlKind.CASE.toString())) { |
| 161 | + String condition; |
| 162 | + SqlNode[] conditionNodes = whereNode.getOperands(); |
| 163 | + SqlNode literal = conditionNodes[conditionIndex]; |
| 164 | + if (operatorKind == SqlKind.BETWEEN) { |
| 165 | + condition = literal.toString() + " AND " + conditionNodes[2].toString(); |
| 166 | + } else { |
| 167 | + if (literal instanceof SqlCharStringLiteral) { |
| 168 | + condition = removeCoding((SqlCharStringLiteral) literal); |
| 169 | + } else { |
| 170 | + condition = literal.toString(); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + if (StringUtils.containsIgnoreCase(condition, SqlKind.CASE.toString())) { |
155 | 175 | return; |
156 | 176 | } |
157 | 177 |
|
158 | | - PredicateInfo predicateInfo = PredicateInfo.builder().setOperatorName(operatorName).setOperatorKind(operatorKind.toString()) |
159 | | - .setOwnerTable(ownerTable).setFieldName(fieldName).setCondition(content).build(); |
| 178 | + PredicateInfo predicateInfo = |
| 179 | + PredicateInfo.builder() |
| 180 | + .setOperatorName(operatorName) |
| 181 | + .setOperatorKind(operatorKind.toString()) |
| 182 | + .setOwnerTable(ownerTable) |
| 183 | + .setFieldName(fieldName) |
| 184 | + .setCondition(condition) |
| 185 | + .build(); |
160 | 186 | predicatesInfoList.add(predicateInfo); |
161 | 187 | } |
162 | 188 | } |
163 | 189 | } |
164 | 190 |
|
| 191 | + /** |
| 192 | + * 去掉_UTF16前缀,只获取字符本身。要不然中文编码会有问题。例如 _UTF16'甲' 去掉后返回结果为 '甲'。 |
| 193 | + * |
| 194 | + * @param stringLiteral |
| 195 | + * @return |
| 196 | + */ |
| 197 | + private String removeCoding(SqlCharStringLiteral stringLiteral) { |
| 198 | + return QUOTE + stringLiteral.getNlsString().getValue() + QUOTE; |
| 199 | + } |
165 | 200 | } |
0 commit comments