@Test
 public void testSimpleB() throws Throwable {
   SchemaContext db = buildSchema(TestName.MULTI, schema);
   PEPersistentGroup group = db.getCurrentDatabase().getDefaultStorage(db);
   stmtTest(
       db,
       "select desc from A group by id",
       SelectStatement.class,
       bes(
           new ProjectingExpectedStep(
               ExecutionType.SELECT,
               group,
               "temp1",
               TransientExecutionEngine.AGGREGATION,
               StaticDistributionModel.MODEL_NAME,
               new String[] {},
               new String[][] {},
               "SELECT `A`.`desc` AS A1d1_7,`A`.`id` AS A1i0_8",
               "FROM `A`",
               "GROUP BY A1i0_8 ASC"),
           new ProjectingExpectedStep(
               ExecutionType.SELECT,
               null,
               "SELECT temp1.A1d1_7 AS t2A0",
               "FROM temp1",
               "ORDER BY temp1.A1i0_8 ASC")));
 }
 @Test
 public void testComplexA() throws Throwable {
   SchemaContext db = buildSchema(TestName.MULTI, schema);
   PEPersistentGroup group = db.getCurrentDatabase().getDefaultStorage(db);
   stmtTest(
       db,
       "select n.desc,r.desc from N n, R r where n.id = r.id group by r.flags",
       SelectStatement.class,
       bes(
           bpes(
               bes(
                   new ProjectingExpectedStep(
                       "SELECT n.`desc` AS nd1_3,n.`id` AS ni0_4 FROM `N` AS n",
                       group,
                       "temp1",
                       TransientExecutionEngine.LARGE,
                       StaticDistributionModel.MODEL_NAME,
                       new String[] {"ni0_4"})),
               bes(
                   new ProjectingExpectedStep(
                       "SELECT r.`desc` AS rd1_4,r.`flags` AS rf2_5,r.`id` AS ri0_6 FROM `R` AS r",
                       group,
                       "temp2",
                       TransientExecutionEngine.LARGE,
                       StaticDistributionModel.MODEL_NAME,
                       new String[] {"ri0_6"}))),
           new ProjectingExpectedStep(
               "SELECT temp1.nd1_3 AS t3n0_11,temp2.rd1_4 AS t4r0_12,temp2.rf2_5 AS t4r1_13 FROM temp1, temp2 WHERE temp1.ni0_4 = temp2.ri0_6",
               TransientExecutionEngine.LARGE,
               "temp3",
               TransientExecutionEngine.LARGE,
               StaticDistributionModel.MODEL_NAME,
               new String[] {"t4r1_13"}),
           new ProjectingExpectedStep(
               ExecutionType.SELECT,
               "SELECT temp3.t3n0_11 AS t5t0,temp3.t4r0_12 AS t5t1,temp3.t4r1_13 AS t5t2 FROM temp3 GROUP BY t5t2 ASC",
               TransientExecutionEngine.LARGE,
               "temp4",
               TransientExecutionEngine.AGGREGATION,
               StaticDistributionModel.MODEL_NAME,
               new String[] {}),
           new ProjectingExpectedStep(
               "SELECT temp4.t5t0 AS t6t0,temp4.t5t1 AS t6t1 FROM temp4 ORDER BY temp4.t5t2 ASC",
               null)));
 }
 // varieties of order by:
 // order by non ref col
 // order by ref col
 // order by ref expr (via alias instance)
 // order by non ref expr (expr in order by)
 // going to do all of these twice: once on the random table, and once on the bcast table
 @Test
 public void testNonRefColRandom() throws Throwable {
   SchemaContext db = buildSchema(TestName.MULTI, schema);
   PEPersistentGroup group = db.getCurrentDatabase().getDefaultStorage(db);
   stmtTest(
       db,
       "select desc from R group by id",
       SelectStatement.class,
       bes(
           new ProjectingExpectedStep(
               ExecutionType.SELECT,
               group,
               "temp1",
               TransientExecutionEngine.LARGE,
               StaticDistributionModel.MODEL_NAME,
               new String[] {"R1i0_8"},
               new String[][] {{"R1i0_8"}},
               "SELECT `R`.`desc` AS R1d1_7,`R`.`id` AS R1i0_8",
               "FROM `R`"),
           new ProjectingExpectedStep(
               ExecutionType.SELECT,
               TransientExecutionEngine.LARGE,
               "temp2",
               TransientExecutionEngine.AGGREGATION,
               StaticDistributionModel.MODEL_NAME,
               new String[] {},
               new String[][] {},
               "SELECT temp1.R1d1_7 AS t2R0,temp1.R1i0_8 AS t2R1",
               "FROM temp1",
               "GROUP BY t2R1 ASC"),
           new ProjectingExpectedStep(
               ExecutionType.SELECT,
               null,
               "SELECT temp2.t2R0 AS t3t0",
               "FROM temp2",
               "ORDER BY temp2.t2R1 ASC")));
 }
 @Test
 public void testComplexC() throws Throwable {
   SchemaContext db = buildSchema(TestName.MULTI, schema);
   PEPersistentGroup group = db.getCurrentDatabase().getDefaultStorage(db);
   stmtTest(
       db,
       "select id, concat(desc, slug) as complete from A group by complete",
       SelectStatement.class,
       bes(
           new ProjectingExpectedStep(
               ExecutionType.SELECT,
               group,
               "temp1",
               TransientExecutionEngine.LARGE,
               StaticDistributionModel.MODEL_NAME,
               new String[] {"complete"},
               new String[][] {},
               "SELECT `A`.`id` AS A1i0_5,concat( `A`.`desc`,`A`.`slug` )  AS complete",
               "FROM `A`"),
           new ProjectingExpectedStep(
               ExecutionType.SELECT,
               TransientExecutionEngine.LARGE,
               "temp2",
               TransientExecutionEngine.AGGREGATION,
               StaticDistributionModel.MODEL_NAME,
               new String[] {},
               new String[][] {},
               "SELECT temp1.A1i0_5 AS t2A0,temp1.complete AS complete",
               "FROM temp1",
               "GROUP BY complete ASC"),
           new ProjectingExpectedStep(
               ExecutionType.SELECT,
               null,
               "SELECT temp2.t2A0 AS t3t0,temp2.complete AS complete",
               "FROM temp2",
               "ORDER BY complete ASC")));
 }
Exemplo n.º 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();
    }
  }