Пример #1
0
 private void normalizeSorts(
     SchemaContext pc, Map<RewriteKey, ExpressionNode> projMap, Edge<?, ?> in) {
   @SuppressWarnings("unchecked")
   MultiEdge<?, SortingSpecification> min = (MultiEdge<?, SortingSpecification>) in;
   for (SortingSpecification ss : min.getMulti()) {
     ExpressionNode target = ss.getTarget();
     if (target instanceof AliasInstance) continue;
     else if (target instanceof LiteralExpression) {
       // i.e. order by 1,2,3
       LiteralExpression le = (LiteralExpression) target;
       Object value = le.getValue(pc);
       if (value instanceof Long) {
         Long index = (Long) value;
         if ((index.intValue() - 1) < projection.size()) {
           target = ExpressionUtils.getTarget(projection.get(index.intValue() - 1));
         }
       }
     }
     ExpressionNode inProjection = projMap.get(target.getRewriteKey());
     if (inProjection != null) {
       ExpressionAlias ea = (ExpressionAlias) inProjection.getParent();
       AliasInstance ai = ea.buildAliasInstance();
       ss.getTargetEdge().set(ai);
     }
   }
 }
Пример #2
0
 private LanguageNode fold(LanguageNode in, EngineToken variety) {
   FunctionCall fc = (FunctionCall) in;
   ArrayList<ExpressionNode> grouped = new ArrayList<ExpressionNode>();
   ArrayList<ExpressionNode> same = new ArrayList<ExpressionNode>();
   ArrayList<ExpressionNode> others = new ArrayList<ExpressionNode>();
   for (ExpressionNode p : fc.getParameters()) {
     if (EngineConstant.FUNCTION.has(p, variety)) {
       if (p.isGrouped()) grouped.add(p);
       else same.add(p);
     } else {
       others.add(p);
     }
   }
   if (same.isEmpty() || !grouped.isEmpty()) return in;
   // if others is empty, this is op(op(a,b),op(c,d)), build op(a,b,c,d)
   // if others is not empty, this is op(a,op(b,c)) or op(op(a,b),c), build op(a,b,c)
   ArrayList<ExpressionNode> allsubs = new ArrayList<ExpressionNode>();
   for (ExpressionNode p : same) {
     FunctionCall pfc = (FunctionCall) p;
     allsubs.addAll(pfc.getParameters());
   }
   allsubs.addAll(others);
   // we're going to group this as well
   FunctionCall ofc = new FunctionCall(fc.getFunctionName(), allsubs);
   ofc.setGrouped();
   return ofc;
 }
 public static SelectStatement filterEntryProjection(SelectStatement in, PartitionEntry jre)
     throws PEException {
   PartitionEntry actual = jre.getActualEntry();
   SelectStatement expecting = null;
   if (actual instanceof OriginalPartitionEntry)
     expecting = ((OriginalPartitionEntry) actual).getChildCopy();
   else expecting = actual.getJoinQuery(null);
   ListSet<ColumnKey> ec = new ListSet<ColumnKey>();
   for (ExpressionNode en : expecting.getProjection()) {
     ExpressionNode targ = ExpressionUtils.getTarget(en);
     if (targ instanceof ColumnInstance) {
       ColumnKey was = ((ColumnInstance) targ).getColumnKey();
       ColumnKey isnow = in.getMapper().copyColumnKeyForward(was);
       if (isnow == null) {
         throw new SchemaException(Pass.PLANNER, "Lost column during lookup table join");
       }
       ec.add(isnow);
     } else if (targ instanceof FunctionCall) {
       ExpressionNode exn = targ;
       RewriteKey rk = in.getMapper().mapExpressionToColumn(exn);
       while (rk == null && (exn.getParent() instanceof ExpressionNode)) {
         exn = (ExpressionNode) exn.getParent();
         rk = in.getMapper().mapExpressionToColumn(exn);
       }
       if (rk != null) {
         ec.add((ColumnKey) rk);
       }
     }
   }
   for (Iterator<ExpressionNode> iter = in.getProjectionEdge().iterator(); iter.hasNext(); ) {
     ExpressionNode en = ExpressionUtils.getTarget(iter.next());
     if (en instanceof ColumnInstance) {
       ColumnKey ck = ((ColumnInstance) en).getColumnKey();
       if (!ec.contains(ck)) iter.remove();
     }
   }
   return in;
 }
Пример #4
0
 private List<ExpressionNode> assignProjectionAliases(
     SchemaContext pc, List<ExpressionNode> in, AliasInformation ai) {
   ArrayList<ExpressionNode> np = new ArrayList<ExpressionNode>();
   for (ExpressionNode e : in) {
     if (e instanceof ExpressionAlias) {
       // already has an alias
       ExpressionAlias ea = (ExpressionAlias) e;
       if (ea.isSynthetic()) {
         // check for dup aliases
         UnqualifiedName unq = ea.getAlias().getNameAlias();
         if (ai.isDuplicateAlias(unq)) {
           NameAlias prefix = ea.getTarget().buildAlias(pc);
           ea.setAlias(ai.buildNewAlias(prefix.getNameAlias()));
         }
       } else if (ea.getAlias() instanceof StringLiteralAlias) {
         // mysql 5.5 docs state it is OK specify as a column alias as either a identifier or
         // string quoted literal in the projection
         // so here we convert the StringLiteralAlias 'foo' into NameAlias `foo`
         final StringLiteralAlias stringLit = (StringLiteralAlias) ea.getAlias();
         if (!stringLit.get().isEmpty()) {
           final UnqualifiedName unq = new UnqualifiedName(stringLit.get(), true);
           ea.setAlias(unq);
         } else {
           ea.setAlias(ai.buildNewAlias(null));
         }
       }
       np.add(e);
     } else {
       // see if we can build something reasonable
       NameAlias prefix = e.buildAlias(pc);
       np.add(
           new ExpressionAlias(e, new NameAlias(ai.buildNewAlias(prefix.getNameAlias())), true));
     }
   }
   return np;
 }
Пример #5
0
  private ProjectionInfo buildProjectionMetadata(SchemaContext pc, List<ExpressionNode> proj) {
    Emitter emitter = Singletons.require(HostService.class).getDBNative().getEmitter();
    try {
      emitter.setOptions(EmitOptions.RESULTSETMETADATA);
      emitter.pushContext(pc.getTokens());
      ProjectionInfo pi = new ProjectionInfo(proj.size());
      for (int i = 0; i < proj.size(); i++) {
        ExpressionNode e = proj.get(i);
        String columnName = null;
        String aliasName = null;
        ColumnInstance ci = null;

        if (e.getSourceLocation() != null && e.getSourceLocation().isComputed()) {
          aliasName = e.getSourceLocation().getText();
        }

        if (e instanceof ExpressionAlias) {
          ExpressionAlias ea = (ExpressionAlias) e;
          Alias aname = ea.getAlias();
          if (aname != null) aliasName = PEStringUtils.dequote(aname.getSQL());
          ExpressionNode cname = ea.getTarget();
          StringBuilder buf = new StringBuilder();
          emitter.emitExpression(pc, cname, buf);
          columnName = buf.toString();
          if (cname instanceof ColumnInstance) {
            ci = (ColumnInstance) cname;
          } else {
            aliasName = PEStringUtils.dequote(aliasName);
            columnName = aliasName;
          }
        } else if (e instanceof ColumnInstance) {
          ci = (ColumnInstance) e;
          StringBuilder buf = new StringBuilder();
          emitter.emitExpression(pc, e, buf);
          columnName = buf.toString();
          aliasName = PEStringUtils.dequote(columnName);
        } else {
          if (aliasName != null) {
            // via above
            columnName = aliasName;
          } else {
            StringBuilder buf = new StringBuilder();
            emitter.emitExpression(pc, e, buf);
            columnName =
                (e instanceof LiteralExpression)
                    ? PEStringUtils.dequote(buf.toString())
                    : buf.toString();
            aliasName = columnName;
          }
        }
        ColumnInfo colInfo =
            pi.addColumn(i + 1, columnName, (aliasName == null ? columnName : aliasName));
        if (ci != null) {
          String tblName = null;
          String dbName = null;
          Column<?> backingColumn = ci.getColumn();
          TableKey tk = ci.getTableInstance().getTableKey();
          if (tk instanceof MTTableKey) {
            MTTableKey mtk = (MTTableKey) tk;
            tblName = mtk.getScope().getName().getUnqualified().getUnquotedName().get();
            PETenant tenant = mtk.getScope().getTenant(pc);
            PEDatabase pdb = tenant.getDatabase(pc);
            if (pdb.getMTMode() == MultitenantMode.ADAPTIVE)
              dbName = tenant.getName().getUnqualified().getUnquotedName().get();
            else dbName = pdb.getName().getUnqualified().getUnquotedName().get();
          } else {
            Table<?> tab = tk.getTable();
            if (tab.isInfoSchema()) {
              dbName = PEConstants.INFORMATION_SCHEMA_DBNAME;
            } else {
              Database<?> tabDb = tab.getDatabase(pc);
              if (tab.isTempTable() && (tabDb == null)) {
                tabDb = pc.getCurrentDatabase(false);
                if (tabDb == null) {
                  tabDb = pc.getAnyNonSchemaDatabase();
                }
                final TempTable tabAstempTable = ((TempTable) tab);
                tabAstempTable.setDatabase(pc, (PEDatabase) tabDb, true);
                tabAstempTable.refreshColumnLookupTable();
              }

              if (tabDb != null) {
                dbName = tabDb.getName().getUnqualified().getUnquotedName().get();
              }
            }
            tblName = tab.getName(pc).getUnqualified().getUnquotedName().get();
          }
          if (tblName != null) colInfo.setDatabaseAndTable(dbName, tblName);
          if (backingColumn instanceof PEColumn) {
            // set flags
            PEColumn pec = (PEColumn) backingColumn;
            if (!pec.isNotNullable() || pec.isNullable())
              colInfo.setAttribute(ColumnAttribute.NULLABLE);
            if (pec.isAutoIncrement()) colInfo.setAttribute(ColumnAttribute.AUTO_INCREMENT);
            if (pec.isKeyPart()) {
              colInfo.setAttribute(ColumnAttribute.KEY_PART);
              if (pec.isPrimaryKeyPart()) colInfo.setAttribute(ColumnAttribute.PRIMARY_KEY_PART);
              if (pec.isUniquePart()) colInfo.setAttribute(ColumnAttribute.UNIQUE_PART);
            }
          }
        }
      }
      return pi;
    } finally {
      emitter.popContext();
    }
  }
Пример #6
0
 public AndedParts(LanguageNode owning, TableKey tk, List<Part> inparts) {
   this(owning, tk, inparts, false);
   ((ExpressionNode) owning).setGrouped();
 }
Пример #7
0
 public OredParts(LanguageNode parent, TableKey tk, Collection<Part> p) {
   super(parent, tk);
   parts = new ListSet<Part>();
   parts.addAll(p);
   ((ExpressionNode) parent).setGrouped();
 }