Пример #1
0
  @Test
  public void testQuery_子查询_all模式() throws SqlParserException {
    String sql =
        "SELECT * FROM TABLE1 WHERE ID > ALL (SELECT ID FROM TABLE2 WHERE TABLE2.NAME = TABLE1.NAME)";
    IQueryTree qc = (IQueryTree) optimizer.optimizeAndAssignment(sql, null, extraCmd, true);
    Assert.assertTrue(qc instanceof IMerge);

    IQuery query1 = (IQuery) ((IMerge) qc).getSubNodes().get(0);
    IQuery query2 = (IQuery) ((IMerge) qc).getSubNodes().get(1);

    IFunction subquery1 = (IFunction) query1.getSubqueryFilter().getArgs().get(1);
    IFunction subquery2 = (IFunction) query2.getSubqueryFilter().getArgs().get(1);
    Assert.assertTrue(subquery1 == subquery2);
    Assert.assertTrue(subquery1.getArgs().get(0) instanceof IQuery);
    IQuery merge = (IQuery) subquery1.getArgs().get(0);
    IFunction func = (IFunction) merge.getColumns().get(0);
    Assert.assertEquals("MAX", func.getFunctionName());
  }
Пример #2
0
  @Test
  public void testQuery_子查询_not_exist模式() throws SqlParserException {
    String sql =
        "SELECT * FROM TABLE1 WHERE NOT EXISTS (SELECT ID FROM TABLE2 WHERE TABLE2.NAME = TABLE1.NAME)";
    IQueryTree qc = (IQueryTree) optimizer.optimizeAndAssignment(sql, null, extraCmd, true);
    Assert.assertTrue(qc instanceof IMerge);

    IQuery query1 = (IQuery) ((IMerge) qc).getSubNodes().get(0);
    IQuery query2 = (IQuery) ((IMerge) qc).getSubNodes().get(1);

    IFunction subquery1 = (IFunction) query1.getSubqueryFilter().getArgs().get(0);
    IFunction subquery2 = (IFunction) query2.getSubqueryFilter().getArgs().get(0);
    Assert.assertTrue(subquery1 == subquery2);
    Assert.assertEquals("NOT", subquery1.getFunctionName());
    // 结构为: NOT(func) -> FILTER -> SUBQUERY_SCALAR(func) -> subquery
    Assert.assertTrue(
        (IQuery)
                ((IFunction) ((IBooleanFilter) subquery1.getArgs().get(0)).getArgs().get(0))
                    .getArgs()
                    .get(0)
            instanceof IQuery);
  }