private void parseInputQuery ( String query ) { List < String > projectedColumns = new ArrayList <> ( ) ; if ( StringUtils . isNotBlank ( query ) ) { String queryLowerCase = query . toLowerCase ( ) ; int startIndex = queryLowerCase . indexOf ( "select " ) + 7 ; int endIndex = queryLowerCase . indexOf ( " from " ) ; if ( startIndex >= 0 && endIndex >= 0 ) { String columnProjection = query . substring ( startIndex , endIndex ) ; this . setInputColumnProjection ( columnProjection ) ; // parse the select list StringBuffer sb = new StringBuffer ( ) ; int bracketCount = 0 ; for ( int i = 0 ; i < columnProjection . length ( ) ; i ++ ) { char c = columnProjection . charAt ( i ) ; if ( c == ' ' ) { bracketCount ++ ; } if ( c == ' ' ) { bracketCount -- ; } if ( bracketCount != 0 ) { sb . append ( c ) ; } else { if ( c != ' ' ) { sb . append ( c ) ; } else { projectedColumns . add ( sb . toString ( ) ) ; sb = new StringBuffer ( ) ; } } } projectedColumns . add ( sb . toString ( ) ) ; } } if ( this . isSelectAllColumns ( ) ) { List < String > columnList = this . getMetadataColumnList ( ) ; for ( String columnName : columnList ) { ColumnAttributes col = new ColumnAttributes ( ) ; col . setColumnName ( columnName ) ; col . setAliasName ( columnName ) ; col . setSourceColumnName ( columnName ) ; this . addToColumnAliasMap ( col ) ; } } else { for ( String projectedColumn : projectedColumns ) { String column = projectedColumn . trim ( ) ; String alias = null ; String sourceColumn = column ; int spaceOccurences = StringUtils . countMatches ( column . trim ( ) , " " ) ; if ( spaceOccurences > 0 ) { // separate column and alias if they are separated by "as" // or space int lastSpaceIndex = column . toLowerCase ( ) . lastIndexOf ( " as " ) ; sourceColumn = column . substring ( 0 , lastSpaceIndex ) ; alias = column . substring ( lastSpaceIndex + 4 ) ; } // extract column name if projection has table name in it String columnName = sourceColumn ; if ( sourceColumn . contains ( "." ) ) { columnName = sourceColumn . substring ( sourceColumn . indexOf ( "." ) + 1 ) ; } ColumnAttributes col = new ColumnAttributes ( ) ; col . setColumnName ( columnName ) ; col . setAliasName ( alias ) ; col . setSourceColumnName ( sourceColumn ) ; this . addToColumnAliasMap ( col ) ; } } }
Parse query provided in pull file Set input column projection - column projection in the input query Set columnAlias map - column and its alias mentioned in input query