private LanguageNode handleOrFunction(FunctionCall fc) {
      ArrayList<Part> subparts = new ArrayList<Part>();
      for (ExpressionNode en : fc.getParameters()) {
        Part p = state.get(en);
        if (p == null) {
          broadening();
          return fc;
        }
        subparts.add(p);
      }
      // now to figure out what we have.  we may have a a bunch of incomplete subexprs,
      // in which case we took a = 1 or a = 2 or a = 3 => a part collection of incompletes
      // or we may have (a = 1 and b = 2) or (a =3 and b =4) ... - likewise
      // or they maybe complete.  regardless, just build a partcollection and move on.
      // sort subparts by table key; if there's more than one let's just set broadening for now
      TableKey tk = null;
      for (Part p : subparts) {
        if (tk == null) tk = p.getTableKey();
        else if (!tk.equals(p.getTableKey())) {
          broadening();
          return fc;
        }
      }
      OredParts op = parent.buildOredParts(fc, subparts);
      if (op.isComplete()) setComplete(op);

      state.put(fc, op);
      return fc;
    }
 private EqualityPart makeNewEqualityPart(
     EqualityPart existing, PEColumn c, ConstantExpression litex) {
   TableKey tk = existing.getColumn().getColumnKey().getTableKey();
   ColumnInstance nc = new ColumnInstance(c, tk.toInstance());
   FunctionCall eq = new FunctionCall(FunctionName.makeEquals(), nc, litex);
   EqualityPart eqp = buildEqualityPart(eq, nc, litex);
   return eqp;
 }
 protected TableKey assertSingleTableKey(List<Part> comps) {
   TableKey tk = null;
   for (Part p : comps) {
     if (tk == null) tk = p.getTableKey();
     else if (!tk.equals(p.getTableKey()))
       throw new SchemaException(Pass.PLANNER, "Mixed table keys for key collector");
   }
   return tk;
 }
 @Override
 public ExecutionStep buildSingleKeyStep(
     SchemaContext pc, TableKey tk, DistributionKey kv, DMLStatement sql) throws PEException {
   return ProjectingExecutionStep.build(
       pc,
       getDatabase(pc),
       getStorageGroup(pc),
       tk.getAbstractTable().getDistributionVector(pc),
       kv,
       sql,
       distKeyExplain);
 }
 @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));
   }
 }
  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();
    }
  }