Exemplo n.º 1
0
 @Test
 public void test_两表join_orderby_groupby_limit条件() {
   TableNode table = new TableNode("TABLE1");
   JoinNode join = table.join("TABLE2", "ID", "ID");
   join.select(
       OptimizerUtils.createColumnFromString("TABLE1.ID AS JID"),
       OptimizerUtils.createColumnFromString("CONCAT(TABLE1.NAME,TABLE1.SCHOOL) AS JNAME"));
   join.orderBy("JID");
   join.groupBy("JNAME");
   join.having("COUNT(JID) > 0");
   IQueryTree qc = (IQueryTree) optimizer.optimizeAndAssignment(join, null, extraCmd);
   Assert.assertTrue(qc instanceof IMerge);
   Assert.assertEquals(QUERY_CONCURRENCY.CONCURRENT, ((IMerge) qc).getQueryConcurrency()); // 串行
 }
Exemplo n.º 2
0
 @Test
 public void test_两表join_单独limit条件_不做并行() {
   TableNode table = new TableNode("TABLE1");
   JoinNode join = table.join("TABLE2", "ID", "ID");
   join.select(
       OptimizerUtils.createColumnFromString("TABLE1.ID AS JID"),
       OptimizerUtils.createColumnFromString("CONCAT(TABLE1.NAME,TABLE1.SCHOOL) AS JNAME"));
   join.limit(10, 20);
   IQueryTree qc = (IQueryTree) optimizer.optimizeAndAssignment(join, null, extraCmd);
   Assert.assertTrue(qc instanceof IMerge);
   Assert.assertEquals(QUERY_CONCURRENCY.SEQUENTIAL, ((IMerge) qc).getQueryConcurrency()); // 串行
   IJoin jn = (IJoin) ((IMerge) qc).getSubNodes().get(0);
   Assert.assertEquals("0", jn.getLimitFrom().toString());
   Assert.assertEquals("30", jn.getLimitTo().toString());
 }
  @Override
  public Object scalarCalucate(IRowSet kvPair, ExecutionContext ec) throws TddlRuntimeException {

    IQueryTree query = this.getQueryPlan();

    if (query.isCorrelatedSubquery()) {
      QueryTreeNode ast = OptimizerUtils.convertPlanToAst(query);
      // 获取correlated column
      List<ISelectable> columnsCorrelated = ast.getColumnsCorrelated();
      Map<Long, Object> correlatedValues = new HashMap();
      // 从rowset中找到对应的column进行替换
      for (ISelectable column : columnsCorrelated) {
        Object value = getValueByIColumnWithException(kvPair, column);
        correlatedValues.put(column.getCorrelateOnFilterId(), value);
      }
      // 替换correlated column
      OptimizerContext.getContext()
          .getOptimizer()
          .assignmentSubquery(ast, correlatedValues, ec.getExtraCmds());
      // 重新生成执行计划
      query =
          (IQueryTree)
              OptimizerContext.getContext()
                  .getOptimizer()
                  .optimizePlan(ast, ec.getParams(), ec.getExtraCmds());
    }

    return this.compute(new Object[] {query}, ec);
  }
Exemplo n.º 4
0
  private static IFilter typeConvert(IFilter root) {
    IBooleanFilter bf = (IBooleanFilter) root;
    // 如果是id in (xx)
    if (bf.getValues() != null && bf.getColumn() instanceof ISelectable) {
      for (int i = 0; i < bf.getValues().size(); i++) {
        bf.getValues()
            .set(
                i,
                OptimizerUtils.convertType(
                    bf.getValues().get(i), ((ISelectable) bf.getColumn()).getDataType()));
      }
    } else {
      // 如果是 1 = id情况
      if (FilterUtils.isConstValue(bf.getColumn()) && !FilterUtils.isConstValue(bf.getValue())) {
        DATA_TYPE type = null;
        if (bf.getValue() instanceof IColumn) {
          type = ((IColumn) bf.getValue()).getDataType();
        }

        if (bf.getValue() instanceof IFunction) {
          type = ((IFunction) bf.getValue()).getDataType();
        }

        bf.setColumn(OptimizerUtils.convertType(bf.getColumn(), type));
      }

      // 如果是 id = 1情况
      if (FilterUtils.isConstValue(bf.getValue()) && !FilterUtils.isConstValue(bf.getColumn())) {
        DATA_TYPE type = null;
        if (bf.getColumn() instanceof IColumn) {
          type = ((IColumn) bf.getColumn()).getDataType();
        }

        if (bf.getColumn() instanceof IFunction) {
          type = ((IFunction) bf.getColumn()).getDataType();
        }

        bf.setValue(OptimizerUtils.convertType(bf.getValue(), type));
      }
    }
    return bf;
  }