SqlQueryToColumnInfoFunction implements Function<String,List<ColumnInfo>> { @Override public List<ColumnInfo> apply(String sqlQuery) { Preconditions.checkNotNull(sqlQuery); Connection connection = null; List<ColumnInfo> columnInfos = null; try { connection = ConnectionUtil.getConnection(this.configuration); final Statement statement = connection.createStatement(); final PhoenixStatement pstmt = statement.unwrap(PhoenixStatement.class); final QueryPlan queryPlan = pstmt.compileQuery(sqlQuery); final List<? extends ColumnProjector> projectedColumns = queryPlan.getProjector().getColumnProjectors(); columnInfos = Lists.newArrayListWithCapacity(projectedColumns.size()); columnInfos = Lists.transform(projectedColumns, new Function<ColumnProjector,ColumnInfo>() { @Override public ColumnInfo apply(final ColumnProjector columnProjector) { return new ColumnInfo(columnProjector.getName(), columnProjector.getExpression().getDataType().getSqlType()); } }); } catch (SQLException e) { LOG.error(String.format(" Error [%s] parsing SELECT query [%s] ",e.getMessage(),sqlQuery)); throw new RuntimeException(e); } finally { if(connection != null) { try { connection.close(); } catch(SQLException sqle) { LOG.error("Error closing connection!!"); throw new RuntimeException(sqle); } } } return columnInfos; } SqlQueryToColumnInfoFunction(final Configuration configuration); @Override List<ColumnInfo> apply(String sqlQuery);  }
@Test public void testValidSelectQuery() throws SQLException { String ddl = "CREATE TABLE EMPLOYEE " + " (id integer not null, name varchar, age integer,location varchar " + " CONSTRAINT pk PRIMARY KEY (id))\n"; createTestTable(getUrl(), ddl); final String selectQuery = "SELECT name as a ,age AS b,UPPER(location) AS c FROM EMPLOYEE"; final ColumnInfo NAME_COLUMN = new ColumnInfo("A", Types.VARCHAR); final ColumnInfo AGE_COLUMN = new ColumnInfo("B", Types.INTEGER); final ColumnInfo LOCATION_COLUMN = new ColumnInfo("C", Types.VARCHAR); final List<ColumnInfo> expectedColumnInfos = ImmutableList.of(NAME_COLUMN, AGE_COLUMN,LOCATION_COLUMN); final List<ColumnInfo> actualColumnInfos = function.apply(selectQuery); Assert.assertEquals(expectedColumnInfos, actualColumnInfos); }