|
36 | 36 |
|
37 | 37 | package com.dtstack.flink.sql.util; |
38 | 38 |
|
39 | | -import com.dtstack.flink.sql.side.JoinInfo; |
40 | | -import org.apache.calcite.sql.SqlBasicCall; |
41 | | -import org.apache.calcite.sql.SqlDataTypeSpec; |
42 | | -import org.apache.calcite.sql.SqlIdentifier; |
43 | | -import org.apache.calcite.sql.SqlJoin; |
44 | | -import org.apache.calcite.sql.SqlKind; |
45 | | -import org.apache.calcite.sql.SqlLiteral; |
46 | | -import org.apache.calcite.sql.SqlNode; |
47 | | -import org.apache.calcite.sql.SqlNodeList; |
| 39 | +import org.apache.calcite.sql.*; |
48 | 40 | import org.apache.calcite.sql.fun.SqlCase; |
| 41 | +import org.apache.calcite.sql.parser.SqlParserPos; |
49 | 42 | import org.apache.commons.lang3.StringUtils; |
50 | | -import org.apache.flink.calcite.shaded.com.google.common.collect.HashBasedTable; |
| 43 | +import org.apache.flink.calcite.shaded.com.google.common.collect.Lists; |
51 | 44 |
|
52 | 45 | import java.util.List; |
53 | 46 | import java.util.Map; |
54 | 47 |
|
55 | 48 | import static org.apache.calcite.sql.SqlKind.*; |
56 | | -import static org.apache.calcite.sql.SqlKind.OTHER; |
57 | 49 |
|
58 | 50 | /** |
59 | 51 | * @Auther: jiangjunjie |
60 | 52 | * @Date: 2019-06-30 14:57 |
61 | 53 | * @Description: |
62 | 54 | */ |
63 | 55 | public class ParseUtils { |
| 56 | + public static void parseSideWhere(SqlNode whereNode, Map<String, String> physicalFields, List<String> whereConditionList) { |
| 57 | + SqlKind sqlKind = whereNode.getKind(); |
| 58 | + if ((sqlKind == SqlKind.OR || sqlKind == SqlKind.AND) && ((SqlBasicCall) whereNode).getOperandList().size() == 2) { |
| 59 | + SqlNode[] sqlOperandsList = ((SqlBasicCall) whereNode).getOperands(); |
| 60 | + // whereNode是一颗先解析or再解析and的二叉树。二叉树中序遍历,先左子树,其次中间节点,最后右子树 |
| 61 | + parseSideWhere(sqlOperandsList[0], physicalFields, whereConditionList); |
| 62 | + whereConditionList.add(sqlKind.name()); |
| 63 | + parseSideWhere(sqlOperandsList[1], physicalFields, whereConditionList); |
| 64 | + } else { |
| 65 | + SqlIdentifier sqlIdentifier = (SqlIdentifier) ((SqlBasicCall) whereNode).getOperands()[0]; |
| 66 | + String fieldName = null; |
| 67 | + if (sqlIdentifier.names.size() == 1) { |
| 68 | + fieldName = sqlIdentifier.getComponent(0).getSimple(); |
| 69 | + } else { |
| 70 | + fieldName = sqlIdentifier.getComponent(1).getSimple(); |
| 71 | + } |
| 72 | + if (physicalFields.containsKey(fieldName)) { |
| 73 | + String sideFieldName = physicalFields.get(fieldName); |
| 74 | + // clone SqlIdentifier node |
| 75 | + SqlParserPos sqlParserPos = new SqlParserPos(0, 0); |
| 76 | + SqlIdentifier sqlIdentifierClone = new SqlIdentifier("", null, sqlParserPos); |
| 77 | + List<String> namesClone = Lists.newArrayList(); |
| 78 | + for(String name :sqlIdentifier.names){ |
| 79 | + namesClone.add(name); |
| 80 | + } |
| 81 | + sqlIdentifierClone.setNames(namesClone,null); |
| 82 | + // clone SqlBasicCall node |
| 83 | + SqlBasicCall sqlBasicCall = (SqlBasicCall)whereNode; |
| 84 | + SqlNode[] sqlNodes = sqlBasicCall.getOperands(); |
| 85 | + SqlNode[] sqlNodesClone = new SqlNode[sqlNodes.length]; |
| 86 | + for (int i = 0; i < sqlNodes.length; i++) { |
| 87 | + sqlNodesClone[i] = sqlNodes[i]; |
| 88 | + } |
| 89 | + SqlBasicCall sqlBasicCallClone = new SqlBasicCall(sqlBasicCall.getOperator(), sqlNodesClone, sqlParserPos); |
| 90 | + // 替换维表中真实字段名 |
| 91 | + List<String> names = Lists.newArrayList(); |
| 92 | + names.add(sideFieldName); |
| 93 | + sqlIdentifierClone.setNames(names, null); |
| 94 | + |
| 95 | + sqlBasicCallClone.setOperand(0, sqlIdentifierClone); |
| 96 | + whereConditionList.add(sqlBasicCallClone.toString()); |
| 97 | + } else { |
| 98 | + // 如果字段不是维表中字段,删除字段前的链接符 |
| 99 | + if (whereConditionList.size() >= 1) { |
| 100 | + whereConditionList.remove(whereConditionList.size() - 1); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
64 | 106 | public static void parseAnd(SqlNode conditionNode, List<SqlNode> sqlNodeList){ |
65 | 107 | if(conditionNode.getKind() == SqlKind.AND && ((SqlBasicCall)conditionNode).getOperandList().size()==2){ |
66 | 108 | parseAnd(((SqlBasicCall)conditionNode).getOperands()[0], sqlNodeList); |
|
0 commit comments