@Override
 public void plan(SchemaContext pc, ExecutionSequence es, BehaviorConfiguration config)
     throws PEException {
   Database<?> ondb = null;
   for (TableKey tk : tables) {
     if (ondb == null) ondb = tk.getTable().getDatabase(pc);
     else if (!ondb.equals(tk.getTable().getDatabase(pc)))
       throw new SchemaException(Pass.PLANNER, "Unable to show table status across databases");
   }
   String origCommand = buildInitialShowStatusCommand();
   if (onGroup.isSingleSiteGroup()) {
     es.append(ProjectingExecutionStep.build(pc, ondb, onGroup, origCommand));
   } else {
     //		System.out.println(origCommand);
     TempGroupManager tgm = new TempGroupManager();
     TempTable t1 = buildFirstTempTable(pc, tgm.getGroup(true));
     if (!pc.hasCurrentDatabase() && !t1.hasDatabase(pc)) {
       // if no current database is set and the temp table has no database
       // we need to set one
       t1.setDatabase(pc, (PEDatabase) ondb, false);
     }
     SelectStatement firstSelect = buildFirstSelect(pc, t1);
     DistributionKeyTemplate dkt = buildKeyTemplate();
     for (int i = 0; i < firstSelect.getProjectionEdge().size(); i++)
       dkt.addColumn(firstSelect.getProjectionEdge().get(i), i);
     DistributionVector sourceVect = buildVector(pc);
     tgm.plan(pc);
     es.append(
         RedistributionExecutionStep.build(
             pc, ondb, onGroup, origCommand, sourceVect, t1, t1.getStorageGroup(pc), dkt, null));
     SelectStatement aggCommand = buildAggCommand(firstSelect);
     //		System.out.println(aggCommand.getSQL());
     es.append(
         ProjectingExecutionStep.build(
             pc, ondb, t1.getStorageGroup(pc), null, null, aggCommand, null));
   }
 }
Esempio n. 2
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();
    }
  }
  @Override
  public void onStatement(String sql, SourcePosition sp, Statement s) throws Throwable {
    saveIntermediateCheckpoint();
    globalCounters.increment(GlobalCounters.PROCESSED_STATEMENTS);
    if (s instanceof EmptyStatement) {
      globalCounters.increment(GlobalCounters.EMPTY_STATEMENTS);
      return;
    }
    if (!s.isDML()) {
      trackNonDML(s, sql);
      return;
    }
    final Emitter emitter = Singletons.require(HostService.class).getDBNative().getEmitter();
    emitter.setOptions(emitOptions);
    final StringBuilder buf = new StringBuilder();
    final DMLStatement dmls = (DMLStatement) s;

    final DMLCounters incCounter = lookupDMLCounter(dmls);

    if (dmls instanceof InsertIntoValuesStatement) {
      final InsertIntoValuesStatement iivs = (InsertIntoValuesStatement) dmls;
      emitter.emitInsertPrefix(tee.getPersistenceContext(), iivs, buf);
      final String prefix = buf.toString();
      InsertEntry ie = inserts.get(prefix);
      if (ie == null) {
        final Database<?> db = dmls.getDatabase(tee.getPersistenceContext());
        ie =
            new InsertEntry(
                corpus,
                prefix,
                iivs.getColumnSpecification().size(),
                iivs.getClass().getSimpleName(),
                (db == null ? null : db.getName().get()));
        inserts.put(prefix, ie);
      }
      ie.bump(iivs.getValues().size());
    } else {
      emitter.emitStatement(tee.getPersistenceContext(), dmls, buf);
      final String p = buf.toString();
      NonInsertEntry se = byParam.get(p);
      if (se == null) {
        String shrunk = null;
        int litCount = -1;
        if (!(dmls instanceof InsertIntoValuesStatement)) {
          final CandidateParser cp = new CandidateParser(sql);
          if (cp.shrink()) {
            shrunk = cp.getShrunk();
            // also verify we get the same number of literals
            final ValueManager valueManager = tee.getPersistenceContext().getValueManager();
            litCount = cp.getLiterals().size();
            if (litCount != valueManager.getNumberOfLiterals()) {
              final ValueManager.CacheStatus cacheStatus = valueManager.getCacheStatus();
              String reason;
              switch (cacheStatus) {
                case NOCACHE_DYNAMIC_FUNCTION:
                  reason = "contains a non-cacheable dynamic function";
                  break;
                case NOCACHE_TOO_MANY_LITERALS:
                  reason = "literal count exceeded configured max_cached_plan_literals";
                  break;
                case CACHEABLE:
                default:
                  reason = "unknown";
              }
              logError(
                  sql,
                  sp,
                  "Mismatched literal size; parse="
                      + valueManager.getNumberOfLiterals()
                      + "/shrink="
                      + litCount
                      + " , reason="
                      + reason,
                  false);
            }
          } else {
            logError(sql, sp, "Unable to shrink", false);
          }
        }
        final Database<?> db = dmls.getDatabase(tee.getPersistenceContext());
        se =
            new NonInsertEntry(
                corpus,
                sql,
                dmls.getClass().getSimpleName(),
                (db == null ? null : db.getName().get()),
                litCount,
                incCounter);
        byParam.put(p, se);
        if (shrunk != null) {
          globalCounters.increment(GlobalCounters.SHRINK_CACHE_ADDS);
          byShrunk.put(shrunk, se);
        }
      }
      if (se.populationObject.getLiteralCount() >= 0) {
        literalCounts.sample(se.populationObject.getLiteralCount());
      }
      se.bump(dmlBreakout, literalCounts);
    }
  }
Esempio n. 4
0
 public UserDatabase getUserDatabase(SchemaContext sc) {
   if (database == null) return null;
   return database.getPersistent(sc);
 }