Example #1
0
 public AbstractCubeTable getQueriedTable(String alias) {
   if (cube != null && cube.getName().equalsIgnoreCase(qb.getTabNameForAlias((alias)))) {
     return (AbstractCubeTable) cube;
   }
   for (Dimension dim : dimensions) {
     if (dim.getName().equalsIgnoreCase(qb.getTabNameForAlias(alias))) {
       return dim;
     }
   }
   return null;
 }
Example #2
0
  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;
  }
Example #3
0
 String getQBFromString(CandidateFact fact, Map<Dimension, CandidateDim> dimsToQuery)
     throws LensException {
   String fromString;
   if (getJoinAST() == null) {
     if (cube != null) {
       if (dimensions.size() > 0) {
         throw new LensException(LensCubeErrorCode.NO_JOIN_CONDITION_AVAILABLE.getLensErrorInfo());
       }
       fromString = fact.getStorageString(getAliasForTableName(cube.getName()));
     } else {
       if (dimensions.size() != 1) {
         throw new LensException(LensCubeErrorCode.NO_JOIN_CONDITION_AVAILABLE.getLensErrorInfo());
       }
       Dimension dim = dimensions.iterator().next();
       fromString = dimsToQuery.get(dim).getStorageString(getAliasForTableName(dim.getName()));
     }
   } else {
     StringBuilder builder = new StringBuilder();
     getQLString(qb.getQbJoinTree(), builder, fact, dimsToQuery);
     fromString = builder.toString();
   }
   return fromString;
 }
Example #4
0
 static void extractTabAliasForCol(CubeQueryContext cubeql, TrackQueriedColumns tqc)
     throws LensException {
   Map<String, String> colToTableAlias = cubeql.getColToTableAlias();
   Set<String> columns = tqc.getTblAliasToColumns().get(CubeQueryContext.DEFAULT_TABLE);
   if (columns == null) {
     return;
   }
   for (String col : columns) {
     boolean inCube = false;
     if (cubeql.getCube() != null) {
       Set<String> cols = cubeql.getCube().getAllFieldNames();
       if (cols.contains(col.toLowerCase())) {
         String cubeAlias = cubeql.getAliasForTableName(cubeql.getCube().getName());
         colToTableAlias.put(col.toLowerCase(), cubeAlias);
         tqc.addColumnsQueried(cubeAlias, col.toLowerCase());
         inCube = true;
       }
     }
     for (Dimension dim : cubeql.getDimensions()) {
       if (dim.getAllFieldNames().contains(col.toLowerCase())) {
         if (!inCube) {
           String prevDim = colToTableAlias.get(col.toLowerCase());
           if (prevDim != null && !prevDim.equals(dim.getName())) {
             throw new LensException(
                 LensCubeErrorCode.AMBIGOUS_DIM_COLUMN.getLensErrorInfo(),
                 col,
                 prevDim,
                 dim.getName());
           }
           String dimAlias = cubeql.getAliasForTableName(dim.getName());
           colToTableAlias.put(col.toLowerCase(), dimAlias);
           tqc.addColumnsQueried(dimAlias, col.toLowerCase());
         } else {
           // throw error because column is in both cube and dimension table
           throw new LensException(
               LensCubeErrorCode.AMBIGOUS_CUBE_COLUMN.getLensErrorInfo(),
               col,
               cubeql.getCube().getName(),
               dim.getName());
         }
       }
     }
     if (colToTableAlias.get(col.toLowerCase()) == null) {
       throw new LensException(LensCubeErrorCode.COLUMN_NOT_FOUND.getLensErrorInfo(), col);
     }
   }
 }
  /**
   * Find all de-normalized columns, if these columns are not directly available in candidate
   * tables, query will be replaced with the corresponding table reference
   */
  @Override
  public void rewriteContext(CubeQueryContext cubeql) throws LensException {
    DenormalizationContext denormCtx = cubeql.getDeNormCtx();
    if (denormCtx == null) {
      // Adds all the reference dimensions as eligible for denorm fields
      denormCtx = new DenormalizationContext(cubeql);
      cubeql.setDeNormCtx(denormCtx);
      // add ref columns in cube
      addRefColsQueried(cubeql, cubeql, denormCtx);
      // add ref columns from expressions
      for (Set<ExpressionContext> ecSet : cubeql.getExprCtx().getAllExprsQueried().values()) {
        for (ExpressionContext ec : ecSet) {
          for (ExprSpecContext esc : ec.getAllExprs()) {
            addRefColsQueried(cubeql, esc, denormCtx);
          }
        }
      }
    } else if (!denormCtx.tableToRefCols.isEmpty()) {
      // In the second iteration of denorm resolver
      // candidate tables which require denorm fields and the refernces are no
      // more valid will be pruned
      if (cubeql.getCube() != null && !cubeql.getCandidateFacts().isEmpty()) {
        for (Iterator<CandidateFact> i = cubeql.getCandidateFacts().iterator(); i.hasNext(); ) {
          CandidateFact cfact = i.next();
          if (denormCtx.tableToRefCols.containsKey(cfact.getName())) {
            for (ReferencedQueriedColumn refcol : denormCtx.tableToRefCols.get(cfact.getName())) {
              if (denormCtx.getReferencedCols().get(refcol.col.getName()).isEmpty()) {
                log.info(
                    "Not considering fact table:{} as column {} is not available",
                    cfact,
                    refcol.col);
                cubeql.addFactPruningMsgs(
                    cfact.fact, CandidateTablePruneCause.columnNotFound(refcol.col.getName()));
                i.remove();
              }
            }
          }
        }
        if (cubeql.getCandidateFacts().size() == 0) {
          throw new LensException(
              LensCubeErrorCode.NO_FACT_HAS_COLUMN.getLensErrorInfo(),
              cubeql.getColumnsQueried(cubeql.getCube().getName()).toString());
        }
        cubeql.pruneCandidateFactSet(CandidateTablePruneCode.COLUMN_NOT_FOUND);
      }
      if (cubeql.getDimensions() != null && !cubeql.getDimensions().isEmpty()) {
        for (Dimension dim : cubeql.getDimensions()) {
          for (Iterator<CandidateDim> i = cubeql.getCandidateDimTables().get(dim).iterator();
              i.hasNext(); ) {
            CandidateDim cdim = i.next();
            if (denormCtx.tableToRefCols.containsKey(cdim.getName())) {
              for (ReferencedQueriedColumn refcol : denormCtx.tableToRefCols.get(cdim.getName())) {
                if (denormCtx.getReferencedCols().get(refcol.col.getName()).isEmpty()) {
                  log.info(
                      "Not considering dim table:{} as column {} is not available",
                      cdim,
                      refcol.col);
                  cubeql.addDimPruningMsgs(
                      dim,
                      cdim.dimtable,
                      CandidateTablePruneCause.columnNotFound(refcol.col.getName()));
                  i.remove();
                }
              }
            }
          }

          if (cubeql.getCandidateDimTables().get(dim).size() == 0) {
            throw new LensException(
                LensCubeErrorCode.NO_DIM_HAS_COLUMN.getLensErrorInfo(),
                dim.toString(),
                cubeql.getColumnsQueried(dim.getName()).toString());
          }
        }
      }
    }
  }