private String getSql(HplsqlParser.Cmp_stmtContext ctx, StringBuilder conn, int idx) throws Exception {
    StringBuilder sql = new StringBuilder();
    String table = null;
    String query = null;
    if (ctx.cmp_source(idx).table_name() != null) {
      table = evalPop(ctx.cmp_source(idx).table_name()).toString();
    }
    else {
      query = evalPop(ctx.cmp_source(idx).select_stmt()).toString();
    }
    if (ctx.cmp_source(idx).T_AT() != null) {
      conn.append(ctx.cmp_source(idx).qident().getText());
    }
    else if (table != null) {
      conn.append(exec.getObjectConnection(ctx.cmp_source(idx).table_name().getText()));
    }  
    else {
      conn.append(exec.getStatementConnection());
    }
    sql.append("SELECT ");
    sql.append(getSelectList(ctx, conn.toString(), table, query));
    sql.append(" FROM ");
    if (table != null) {
      sql.append(table);
      if (ctx.cmp_source(idx).where_clause() != null) {
        sql.append(" " + evalPop(ctx.cmp_source(idx).where_clause()).toString());
      }
    }
    else {
      sql.append("(");
      sql.append(query);
      sql.append(") t");
    }
    return sql.toString();
  }
Define the SQL query to access data
hplsql/src/main/java/org/apache/hive/hplsql/Cmp.java