/**
  * destination table : a table whose columns are getting queried intermediate table : a table
  * which is only used as a link between cube and destination table
  *
  * @param alias
  * @param tblName
  * @param isOptional pass false when it's a destination table pass true when it's an intermediate
  *     table when join chain destination is being added, this will be false.
  * @param isChainedDimension pass true when you're adding the dimension as a joinchain
  *     destination, pass false when this table is mentioned by name in the user query
  * @return true if added
  * @throws LensException
  */
 private boolean addQueriedTable(
     String alias, String tblName, boolean isOptional, boolean isChainedDimension)
     throws LensException {
   alias = alias.toLowerCase();
   if (cubeTbls.containsKey(alias)) {
     return true;
   }
   try {
     if (metastoreClient.isCube(tblName)) {
       if (cube != null) {
         if (!cube.getName().equalsIgnoreCase(tblName)) {
           throw new LensException(
               LensCubeErrorCode.MORE_THAN_ONE_CUBE.getLensErrorInfo(), cube.getName(), tblName);
         }
       }
       cube = metastoreClient.getCube(tblName);
       cubeTbls.put(alias, (AbstractCubeTable) cube);
     } else if (metastoreClient.isDimension(tblName)) {
       Dimension dim = metastoreClient.getDimension(tblName);
       if (!isOptional) {
         dimensions.add(dim);
       }
       if (!isChainedDimension) {
         nonChainedDimensions.add(dim);
       }
       cubeTbls.put(alias, dim);
     } else {
       return false;
     }
   } catch (HiveException e) {
     return false;
   }
   return true;
 }
 private Map<Dimension, CandidateDim> pickCandidateDimsToQuery(Set<Dimension> dimensions)
     throws LensException {
   Map<Dimension, CandidateDim> dimsToQuery = new HashMap<Dimension, CandidateDim>();
   if (!dimensions.isEmpty()) {
     for (Dimension dim : dimensions) {
       if (candidateDims.get(dim) != null && candidateDims.get(dim).size() > 0) {
         CandidateDim cdim = candidateDims.get(dim).iterator().next();
         log.info(
             "Available candidate dims are:{}, picking up {} for querying.",
             candidateDims.get(dim),
             cdim.dimtable);
         dimsToQuery.put(dim, cdim);
       } else {
         String reason = "";
         if (dimPruningMsgs.get(dim) != null && !dimPruningMsgs.get(dim).isEmpty()) {
           ByteArrayOutputStream out = null;
           try {
             ObjectMapper mapper = new ObjectMapper();
             out = new ByteArrayOutputStream();
             mapper.writeValue(out, dimPruningMsgs.get(dim).getJsonObject());
             reason = out.toString("UTF-8");
           } catch (Exception e) {
             throw new LensException("Error writing dim pruning messages", e);
           } finally {
             if (out != null) {
               try {
                 out.close();
               } catch (IOException e) {
                 throw new LensException(e);
               }
             }
           }
         }
         log.error(
             "Query rewrite failed due to NO_CANDIDATE_DIM_AVAILABLE, Cause {}",
             dimPruningMsgs.get(dim).toJsonObject());
         throw new NoCandidateDimAvailableException(dimPruningMsgs.get(dim));
       }
     }
   }
   return dimsToQuery;
 }
  private boolean addJoinChain(String alias, boolean isOptional) throws LensException {
    boolean retVal = false;
    String aliasLowerCaseStr = alias.toLowerCase();
    JoinChain joinchain = null;

    if (getCube() != null) {
      JoinChain chainByName = getCube().getChainByName(aliasLowerCaseStr);
      if (chainByName != null) {
        joinchain = chainByName;
        retVal = true;
      }
    }

    if (!retVal) {
      for (Dimension table : dimensions) {
        JoinChain chainByName = table.getChainByName(aliasLowerCaseStr);
        if (chainByName != null) {
          joinchain = chainByName;
          retVal = true;
          break;
        }
      }
    }

    if (retVal) {
      joinchains.put(aliasLowerCaseStr, new JoinChain(joinchain));
      String destTable = joinchain.getDestTable();
      boolean added = addQueriedTable(alias, destTable, isOptional, true);
      if (!added) {
        log.info("Queried tables do not exist. Missing tables:{}", destTable);
        throw new LensException(LensCubeErrorCode.NEITHER_CUBE_NOR_DIMENSION.getLensErrorInfo());
      }
      log.info("Added join chain for {}", destTable);
      return true;
    }

    return retVal;
  }
 public void addExprToAlias(ASTNode expr, ASTNode alias) {
   exprToAlias.put(HQLParser.getString(expr).trim(), alias.getText().toLowerCase());
 }
 public void setJoinCond(QBJoinTree qb, String cond) {
   joinConds.put(qb, cond);
 }