예제 #1
0
 @Override
 public void normalize(SchemaContext pc) {
   // we've already resolved everything.  now we:
   // [1] expand all wildcards and table wildcards (except for count(*))
   //     this is easy for the projection - walk down the list and look for naked wildcards or
   //     table wildcards
   List<ExpressionNode> proj = getProjection();
   proj = expandWildcards(pc, proj);
   if (projectionMetadata == null || projectionMetadata.getWidth() != proj.size())
     projectionMetadata = buildProjectionMetadata(pc, proj);
   // [2] assign aliases for everything that does not already use an alias
   //     for the projection, this is easy - just walk down the list and anything that is
   //     not a DerivedColumn must need an alias
   proj = assignProjectionAliases(pc, proj, getAliases());
   setProjection(proj);
   // for order by, group by, etc. now that the projection aliases are set, use them in any clauses
   normalizeSorts(pc);
 }
예제 #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();
    }
  }