2121package com .dtstack .flink .sql .side ;
2222
2323import com .dtstack .flink .sql .enums .ECacheType ;
24+ import com .dtstack .flink .sql .exec .FlinkSQLExec ;
2425import com .dtstack .flink .sql .parser .CreateTmpTableParser ;
2526import com .dtstack .flink .sql .side .operator .SideAsyncOperator ;
2627import com .dtstack .flink .sql .side .operator .SideWithAllCacheOperator ;
2728import com .dtstack .flink .sql .util .ClassUtil ;
2829import com .dtstack .flink .sql .util .ParseUtils ;
30+ import org .apache .calcite .sql .SqlAsOperator ;
2931import org .apache .calcite .sql .SqlBasicCall ;
3032import org .apache .calcite .sql .SqlDataTypeSpec ;
3133import org .apache .calcite .sql .SqlIdentifier ;
3537import org .apache .calcite .sql .SqlLiteral ;
3638import org .apache .calcite .sql .SqlNode ;
3739import org .apache .calcite .sql .SqlNodeList ;
38- import org .apache .calcite .sql .SqlOrderBy ;
40+ import org .apache .calcite .sql .SqlOperator ;
3941import org .apache .calcite .sql .SqlSelect ;
4042import org .apache .calcite .sql .fun .SqlCase ;
4143import org .apache .calcite .sql .parser .SqlParseException ;
@@ -100,13 +102,16 @@ public void exec(String sql, Map<String, SideTableInfo> sideTableMap, StreamTabl
100102
101103 if (preIsSideJoin ){
102104 preIsSideJoin = false ;
105+ List <String > fieldNames = null ;
103106 for (FieldReplaceInfo replaceInfo : replaceInfoList ){
107+ fieldNames = Lists .newArrayList ();
104108 replaceFieldName (pollSqlNode , replaceInfo .getMappingTable (), replaceInfo .getTargetTableName (), replaceInfo .getTargetTableAlias ());
109+ addAliasForFieldNode (pollSqlNode , fieldNames , replaceInfo .getMappingTable ());
105110 }
106111 }
107112
108113 if (pollSqlNode .getKind () == INSERT ){
109- tableEnv .sqlUpdate (pollSqlNode .toString ());
114+ FlinkSQLExec .sqlUpdate (tableEnv , pollSqlNode .toString ());
110115 if (LOG .isInfoEnabled ()){
111116 LOG .info ("exec sql: " + pollSqlNode .toString ());
112117 }
@@ -125,6 +130,68 @@ public void exec(String sql, Map<String, SideTableInfo> sideTableMap, StreamTabl
125130
126131 }
127132
133+
134+ private void addAliasForFieldNode (SqlNode pollSqlNode , List <String > fieldList , HashBasedTable <String , String , String > mappingTable ) {
135+ SqlKind sqlKind = pollSqlNode .getKind ();
136+ switch (sqlKind ) {
137+ case INSERT :
138+ SqlNode source = ((SqlInsert ) pollSqlNode ).getSource ();
139+ addAliasForFieldNode (source , fieldList , mappingTable );
140+ break ;
141+
142+ case AS :
143+ addAliasForFieldNode (((SqlBasicCall ) pollSqlNode ).getOperands ()[0 ], fieldList , mappingTable );
144+ break ;
145+
146+ case SELECT :
147+
148+ SqlNodeList selectList = ((SqlSelect ) pollSqlNode ).getSelectList ();
149+
150+ selectList .getList ().forEach (node -> {
151+ if (node .getKind () == IDENTIFIER ) {
152+ SqlIdentifier sqlIdentifier = (SqlIdentifier ) node ;
153+ if (sqlIdentifier .names .size () == 1 ) {
154+ return ;
155+ }
156+ // save real field
157+ String fieldName = sqlIdentifier .names .get (1 );
158+ if (!fieldName .endsWith ("0" ) || fieldName .endsWith ("0" ) && mappingTable .columnMap ().containsKey (fieldName )) {
159+ fieldList .add (fieldName );
160+ }
161+
162+ }
163+ });
164+
165+ for (int i = 0 ; i < selectList .getList ().size (); i ++) {
166+ SqlNode node = selectList .get (i );
167+ if (node .getKind () == IDENTIFIER ) {
168+ SqlIdentifier sqlIdentifier = (SqlIdentifier ) node ;
169+ if (sqlIdentifier .names .size () == 1 ) {
170+ return ;
171+ }
172+
173+ String name = sqlIdentifier .names .get (1 );
174+ // avoid real field pv0 convert pv
175+ if (name .endsWith ("0" ) && !fieldList .contains (name ) && !fieldList .contains (name .substring (0 , name .length () - 1 ))) {
176+ SqlOperator operator = new SqlAsOperator ();
177+ SqlParserPos sqlParserPos = new SqlParserPos (0 , 0 );
178+
179+ SqlIdentifier sqlIdentifierAlias = new SqlIdentifier (name .substring (0 , name .length () - 1 ), null , sqlParserPos );
180+ SqlNode [] sqlNodes = new SqlNode [2 ];
181+ sqlNodes [0 ] = sqlIdentifier ;
182+ sqlNodes [1 ] = sqlIdentifierAlias ;
183+ SqlBasicCall sqlBasicCall = new SqlBasicCall (operator , sqlNodes , sqlParserPos );
184+
185+ selectList .set (i , sqlBasicCall );
186+ }
187+
188+ }
189+ }
190+ break ;
191+ }
192+ }
193+
194+
128195 public AliasInfo parseASNode (SqlNode sqlNode ) throws SqlParseException {
129196 SqlKind sqlKind = sqlNode .getKind ();
130197 if (sqlKind != AS ){
@@ -305,7 +372,7 @@ private SqlNode replaceNodeInfo(SqlNode groupNode, HashBasedTable<String, String
305372 }
306373 }
307374
308- public SqlNode filterNodeWithTargetName (SqlNode sqlNode , String targetTableName ){
375+ public SqlNode filterNodeWithTargetName (SqlNode sqlNode , String targetTableName ) {
309376
310377 SqlKind sqlKind = sqlNode .getKind ();
311378 switch (sqlKind ){
@@ -342,7 +409,7 @@ public SqlNode filterNodeWithTargetName(SqlNode sqlNode, String targetTableName)
342409 }
343410
344411
345- public void setLocalSqlPluginPath (String localSqlPluginPath ){
412+ public void setLocalSqlPluginPath (String localSqlPluginPath ) {
346413 this .localSqlPluginPath = localSqlPluginPath ;
347414 }
348415
@@ -386,12 +453,12 @@ private List<SqlNode> replaceSelectStarFieldName(SqlNode selectNode, HashBasedTa
386453 }
387454 }
388455
389- private SqlNode replaceSelectFieldName (SqlNode selectNode , HashBasedTable <String , String , String > mappingTable , String tableAlias ){
390- if (selectNode .getKind () == AS ){
391- SqlNode leftNode = ((SqlBasicCall )selectNode ).getOperands ()[0 ];
456+ private SqlNode replaceSelectFieldName (SqlNode selectNode , HashBasedTable <String , String , String > mappingTable , String tableAlias ) {
457+ if (selectNode .getKind () == AS ) {
458+ SqlNode leftNode = ((SqlBasicCall ) selectNode ).getOperands ()[0 ];
392459 SqlNode replaceNode = replaceSelectFieldName (leftNode , mappingTable , tableAlias );
393- if (replaceNode != null ){
394- ((SqlBasicCall )selectNode ).getOperands ()[0 ] = replaceNode ;
460+ if (replaceNode != null ) {
461+ ((SqlBasicCall ) selectNode ).getOperands ()[0 ] = replaceNode ;
395462 }
396463
397464 return selectNode ;
@@ -470,15 +537,15 @@ private SqlNode replaceSelectFieldName(SqlNode selectNode, HashBasedTable<String
470537 for (int i =0 ; i <whenOperands .size (); i ++){
471538 SqlNode oneOperand = whenOperands .get (i );
472539 SqlNode replaceNode = replaceSelectFieldName (oneOperand , mappingTable , tableAlias );
473- if (replaceNode != null ){
540+ if (replaceNode != null ) {
474541 whenOperands .set (i , replaceNode );
475542 }
476543 }
477544
478545 for (int i =0 ; i <thenOperands .size (); i ++){
479546 SqlNode oneOperand = thenOperands .get (i );
480547 SqlNode replaceNode = replaceSelectFieldName (oneOperand , mappingTable , tableAlias );
481- if (replaceNode != null ){
548+ if (replaceNode != null ) {
482549 thenOperands .set (i , replaceNode );
483550 }
484551 }
@@ -495,19 +562,27 @@ private SqlNode replaceSelectFieldName(SqlNode selectNode, HashBasedTable<String
495562
496563 /**
497564 * Analyzing conditions are very join the dimension tables include all equivalent conditions (i.e., dimension table is the primary key definition
565+ *
498566 * @return
499567 */
500- private boolean checkJoinCondition (SqlNode conditionNode , String sideTableAlias , List <String > primaryKeys ){
501-
502- List <String > conditionFields = getConditionFields (conditionNode , sideTableAlias );
503- if (CollectionUtils .isEqualCollection (conditionFields , primaryKeys )){
568+ private boolean checkJoinCondition (SqlNode conditionNode , String sideTableAlias , SideTableInfo sideTableInfo ) {
569+ List <String > conditionFields = getConditionFields (conditionNode , sideTableAlias , sideTableInfo );
570+ if (CollectionUtils .isEqualCollection (conditionFields , convertPrimaryAlias (sideTableInfo ))){
504571 return true ;
505572 }
506573
507574 return false ;
508575 }
509576
510- public List <String > getConditionFields (SqlNode conditionNode , String specifyTableName ){
577+ private List <String > convertPrimaryAlias (SideTableInfo sideTableInfo ) {
578+ List <String > res = Lists .newArrayList ();
579+ sideTableInfo .getPrimaryKeys ().forEach (field -> {
580+ res .add (sideTableInfo .getPhysicalFields ().getOrDefault (field , field ));
581+ });
582+ return res ;
583+ }
584+
585+ public List <String > getConditionFields (SqlNode conditionNode , String specifyTableName , SideTableInfo sideTableInfo ){
511586 List <SqlNode > sqlNodeList = Lists .newArrayList ();
512587 ParseUtils .parseAnd (conditionNode , sqlNodeList );
513588 List <String > conditionFields = Lists .newArrayList ();
@@ -530,7 +605,7 @@ public List<String> getConditionFields(SqlNode conditionNode, String specifyTabl
530605 }else {
531606 throw new RuntimeException (String .format ("side table:%s join condition is wrong" , specifyTableName ));
532607 }
533-
608+ tableCol = sideTableInfo . getPhysicalFields (). getOrDefault ( tableCol , tableCol );
534609 conditionFields .add (tableCol );
535610 }
536611
@@ -561,8 +636,11 @@ public void registerTmpTable(CreateTmpTableParser.SqlParserResult result,
561636
562637 if (preIsSideJoin ){
563638 preIsSideJoin = false ;
564- for (FieldReplaceInfo replaceInfo : replaceInfoList ){
639+ List <String > fieldNames = null ;
640+ for (FieldReplaceInfo replaceInfo : replaceInfoList ) {
641+ fieldNames = Lists .newArrayList ();
565642 replaceFieldName (pollSqlNode , replaceInfo .getMappingTable (), replaceInfo .getTargetTableName (), replaceInfo .getTargetTableAlias ());
643+ addAliasForFieldNode (pollSqlNode , fieldNames , replaceInfo .getMappingTable ());
566644 }
567645 }
568646
@@ -598,6 +676,7 @@ public void registerTmpTable(CreateTmpTableParser.SqlParserResult result,
598676 }
599677 }
600678 }
679+
601680 private void joinFun (Object pollObj , Map <String , Table > localTableCache ,
602681 Map <String , SideTableInfo > sideTableMap , StreamTableEnvironment tableEnv ,
603682 List <FieldReplaceInfo > replaceInfoList ) throws Exception {
@@ -624,7 +703,7 @@ private void joinFun(Object pollObj, Map<String, Table> localTableCache,
624703 throw new RuntimeException ("can't not find side table:" + joinInfo .getRightTableName ());
625704 }
626705
627- if (!checkJoinCondition (joinInfo .getCondition (), joinInfo .getRightTableAlias (), sideTableInfo . getPrimaryKeys () )){
706+ if (!checkJoinCondition (joinInfo .getCondition (), joinInfo .getRightTableAlias (), sideTableInfo )){
628707 throw new RuntimeException ("ON condition must contain all equal fields!!!" );
629708 }
630709
@@ -650,7 +729,7 @@ private void joinFun(Object pollObj, Map<String, Table> localTableCache,
650729
651730 //join side table before keyby ===> Reducing the size of each dimension table cache of async
652731 if (sideTableInfo .isPartitionedJoin ()){
653- List <String > leftJoinColList = getConditionFields (joinInfo .getCondition (), joinInfo .getLeftTableAlias ());
732+ List <String > leftJoinColList = getConditionFields (joinInfo .getCondition (), joinInfo .getLeftTableAlias (), sideTableInfo );
654733 String [] leftJoinColArr = new String [leftJoinColList .size ()];
655734 leftJoinColArr = leftJoinColList .toArray (leftJoinColArr );
656735 adaptStream = adaptStream .keyBy (leftJoinColArr );
@@ -681,12 +760,11 @@ private void joinFun(Object pollObj, Map<String, Table> localTableCache,
681760 }
682761 }
683762
684- private boolean checkFieldsInfo (CreateTmpTableParser .SqlParserResult result , Table table ){
763+ private boolean checkFieldsInfo (CreateTmpTableParser .SqlParserResult result , Table table ) {
685764 List <String > fieldNames = new LinkedList <>();
686765 String fieldsInfo = result .getFieldsInfoStr ();
687766 String [] fields = fieldsInfo .split ("," );
688- for (int i =0 ; i < fields .length ; i ++)
689- {
767+ for (int i = 0 ; i < fields .length ; i ++) {
690768 String [] filed = fields [i ].split ("\\ s" );
691769 if (filed .length < 2 || fields .length != table .getSchema ().getColumnNames ().length ){
692770 return false ;
0 commit comments