Java Code Examples for org.apache.calcite.plan.RelOptPlanner#CannotPlanException

The following examples show how to use org.apache.calcite.plan.RelOptPlanner#CannotPlanException . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CalcitePrepareImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
<T> CalciteSignature<T> prepare_(
    Context context,
    Query<T> query,
    Type elementType,
    long maxRowCount) {
  if (SIMPLE_SQLS.contains(query.sql)) {
    return simplePrepare(context, query.sql);
  }
  final JavaTypeFactory typeFactory = context.getTypeFactory();
  CalciteCatalogReader catalogReader =
      new CalciteCatalogReader(
          context.getRootSchema(),
          context.getDefaultSchemaPath(),
          typeFactory,
          context.config());
  final List<Function1<Context, RelOptPlanner>> plannerFactories =
      createPlannerFactories();
  if (plannerFactories.isEmpty()) {
    throw new AssertionError("no planner factories");
  }
  RuntimeException exception = Util.FoundOne.NULL;
  for (Function1<Context, RelOptPlanner> plannerFactory : plannerFactories) {
    final RelOptPlanner planner = plannerFactory.apply(context);
    if (planner == null) {
      throw new AssertionError("factory returned null planner");
    }
    try {
      return prepare2_(context, query, elementType, maxRowCount,
          catalogReader, planner);
    } catch (RelOptPlanner.CannotPlanException e) {
      exception = e;
    }
  }
  throw exception;
}
 
Example 2
Source File: PrelTransformer.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
/**
 *  Given a relNode tree for SELECT statement, convert to Dremio Logical RelNode tree.
 * @param relNode
 * @return
 * @throws SqlUnsupportedException
 * @throws RelConversionException
 */
public static Rel convertToDrel(SqlHandlerConfig config, final RelNode relNode) throws SqlUnsupportedException, RelConversionException {

  try {
    final RelNode trimmed = trimFields(relNode, true, config.getContext().getPlannerSettings().isRelPlanningEnabled());
    final RelNode preLog = transform(config, PlannerType.HEP_AC, PlannerPhase.PRE_LOGICAL, trimmed, trimmed.getTraitSet(), true);

    final RelTraitSet logicalTraits = preLog.getTraitSet().plus(Rel.LOGICAL);
    final RelNode adjusted = transform(config, PlannerType.VOLCANO, PlannerPhase.LOGICAL, preLog, logicalTraits, true);

    final Catalog catalog = config.getContext().getCatalog();
    if (catalog instanceof CachingCatalog) {
      config.getObserver().tablesCollected(catalog.getAllRequestedTables());
    }

    final RelNode intermediateNode;
    if (config.getContext().getPlannerSettings().removeRowCountAdjustment()) {
      intermediateNode = adjusted.accept(new RelShuttleImpl() {
          @Override
          public RelNode visit(TableScan scan) {
            if (scan instanceof FilesystemScanDrel) {
              FilesystemScanDrel scanDrel = (FilesystemScanDrel) scan;
              return new FilesystemScanDrel(
                scanDrel.getCluster(),
                scanDrel.getTraitSet(),
                scanDrel.getTable(),
                scanDrel.getPluginId(),
                scanDrel.getTableMetadata(),
                scanDrel.getProjectedColumns(),
                1.0);
            }
            return super.visit(scan);
          }
        });
    } else {
      intermediateNode = adjusted;
    }

    RelNode postLogical;
    if (config.getContext().getPlannerSettings().isRelPlanningEnabled()) {
      final RelNode decorrelatedNode = DremioRelDecorrelator.decorrelateQuery(intermediateNode, DremioRelFactories.LOGICAL_BUILDER.create(intermediateNode.getCluster(), null), true, true);
      final RelNode jdbcPushDown = transform(config, PlannerType.HEP_AC, PlannerPhase.RELATIONAL_PLANNING, decorrelatedNode, decorrelatedNode.getTraitSet().plus(Rel.LOGICAL), true);
      postLogical = jdbcPushDown.accept(new ShortenJdbcColumnAliases()).accept(new ConvertJdbcLogicalToJdbcRel(DremioRelFactories.LOGICAL_BUILDER));
    } else {
      postLogical = intermediateNode;
    }

    // Do Join Planning.
    final RelNode preConvertedRelNode = transform(config, PlannerType.HEP_BOTTOM_UP, PlannerPhase.JOIN_PLANNING_MULTI_JOIN, postLogical, postLogical.getTraitSet(), true);
    final RelNode convertedRelNode = transform(config, PlannerType.HEP_BOTTOM_UP, PlannerPhase.JOIN_PLANNING_OPTIMIZATION, preConvertedRelNode, preConvertedRelNode.getTraitSet(), true);

    FlattenRelFinder flattenFinder = new FlattenRelFinder();
    final RelNode flattendPushed;
    if (flattenFinder.run(convertedRelNode)) {
      flattendPushed = transform(config, PlannerType.VOLCANO, PlannerPhase.FLATTEN_PUSHDOWN,
        convertedRelNode, convertedRelNode.getTraitSet(), true);
    } else {
      flattendPushed = convertedRelNode;
    }

    final Rel drel = (Rel) flattendPushed;

    if (drel instanceof TableModify) {
      throw new UnsupportedOperationException("TableModify " + drel);
    } else {
      final Optional<SubstitutionInfo> acceleration = findUsedMaterializations(config, drel);
      if (acceleration.isPresent()) {
        config.getObserver().planAccelerated(acceleration.get());
      }
      return drel;
    }
  } catch (RelOptPlanner.CannotPlanException ex) {
    logger.error(ex.getMessage(), ex);

    if(JoinUtils.checkCartesianJoin(relNode, Lists.<Integer>newArrayList(), Lists.<Integer>newArrayList(), Lists.<Boolean>newArrayList())) {
      throw new UnsupportedRelOperatorException("This query cannot be planned\u2014possibly due to use of an unsupported feature.");
    } else {
      throw ex;
    }
  }
}