Java Code Examples for org.apache.calcite.plan.RelOptCluster#create()

The following examples show how to use org.apache.calcite.plan.RelOptCluster#create() . 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: TestPushLimitToPruneableScan.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() {
  MockitoAnnotations.initMocks(this);

  when(optionManager.getOption(eq(ExecConstants.SLICE_TARGET)))
  .thenReturn(ExecConstants.SLICE_TARGET_OPTION.getDefault());
  when(optionManager.getOption(eq(PlannerSettings.ENABLE_LEAF_LIMITS.getOptionName())))
  .thenReturn(PlannerSettings.ENABLE_LEAF_LIMITS.getDefault());
  when(optionManager.getOption(eq(PlannerSettings.ENABLE_TRIVIAL_SINGULAR.getOptionName())))
  .thenReturn(PlannerSettings.ENABLE_TRIVIAL_SINGULAR.getDefault());
  when(optionManager.getOptionValidatorListing()).thenReturn(mock(OptionValidatorListing.class));
  ClusterResourceInformation info = mock(ClusterResourceInformation.class);
  when(info.getExecutorNodeCount()).thenReturn(1);

  plannerSettings = new PlannerSettings(DremioTest.DEFAULT_SABOT_CONFIG,
    optionManager, () -> info);
  cluster = RelOptCluster.create(new VolcanoPlanner(plannerSettings), REX_BUILDER);
  metadata = new TableMetadataImpl(pluginId, DATASET_CONFIG, "testuser", MaterializedSplitsPointer.of(0, Arrays.asList(
      TEST_PARTITION_CHUNK_METADATA_1, TEST_PARTITION_CHUNK_METADATA_2, TEST_PARTITION_CHUNK_METADATA_3
       ), 3));
}
 
Example 2
Source File: TestSimpleLimitExchangeRemover.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Before
public void setup() {
  optionList = new OptionList();
  optionList.add(ExecConstants.SLICE_TARGET_OPTION.getDefault());
  optionList.add(PlannerSettings.ENABLE_LEAF_LIMITS.getDefault());
  optionList.add(PlannerSettings.ENABLE_TRIVIAL_SINGULAR.getDefault());
  optionManager = mock(OptionManager.class);

  when(optionManager.getOptionValidatorListing()).thenReturn(mock(OptionValidatorListing.class));
  when(optionManager.getNonDefaultOptions()).thenReturn(optionList);
  when(optionManager.getOption(eq(ExecConstants.SLICE_TARGET)))
    .thenReturn(ExecConstants.SLICE_TARGET_OPTION.getDefault());
  when(optionManager.getOption(eq(PlannerSettings.ENABLE_LEAF_LIMITS.getOptionName())))
    .thenReturn(PlannerSettings.ENABLE_LEAF_LIMITS.getDefault());
  when(optionManager.getOption(eq(PlannerSettings.ENABLE_TRIVIAL_SINGULAR.getOptionName())))
    .thenReturn(PlannerSettings.ENABLE_TRIVIAL_SINGULAR.getDefault());
  ClusterResourceInformation info = mock(ClusterResourceInformation.class);
  when(info.getExecutorNodeCount()).thenReturn(1);

  plannerSettings = new PlannerSettings(DremioTest.DEFAULT_SABOT_CONFIG,
    optionManager, () -> info);
  cluster = RelOptCluster.create(new VolcanoPlanner(plannerSettings), rexBuilder);
}
 
Example 3
Source File: SqlToRelTestBase.java    From calcite with Apache License 2.0 6 votes vote down vote up
protected SqlToRelConverter createSqlToRelConverter(
    final SqlValidator validator,
    final Prepare.CatalogReader catalogReader,
    final RelDataTypeFactory typeFactory,
    final SqlToRelConverter.Config config) {
  final RexBuilder rexBuilder = new RexBuilder(typeFactory);
  RelOptCluster cluster =
      RelOptCluster.create(getPlanner(), rexBuilder);
  if (clusterFactory != null) {
    cluster = clusterFactory.apply(cluster);
  }
  RelOptTable.ViewExpander viewExpander =
      new MockViewExpander(validator, catalogReader, cluster, config);
  return new SqlToRelConverter(viewExpander, validator, catalogReader, cluster,
      StandardConvertletTable.INSTANCE, config);
}
 
Example 4
Source File: BatsOptimizerTest.java    From Bats with Apache License 2.0 5 votes vote down vote up
static RelNode testSqlToRelConverter(RelOptPlanner planner) throws Exception {
    RexBuilder rexBuilder = createRexBuilder();
    RelOptCluster cluster = RelOptCluster.create(planner, rexBuilder);
    RelOptTable.ViewExpander viewExpander = ViewExpanders.simpleContext(cluster);

    Pair<SqlNode, SqlValidator> pair = testSqlValidator();
    SqlNode sqlNode = pair.left;
    SqlValidator validator = pair.right;
    CatalogReader catalogReader = createCalciteCatalogReader();
    SqlRexConvertletTable convertletTable = StandardConvertletTable.INSTANCE;
    SqlToRelConverter.Config config = SqlToRelConverter.Config.DEFAULT;
    // 不转换成EnumerableTableScan,而是LogicalTableScan
    config = SqlToRelConverter.configBuilder().withConvertTableAccess(false).build();

    SqlToRelConverter converter = new SqlToRelConverter(viewExpander, validator, catalogReader, cluster,
            convertletTable, config);

    boolean needsValidation = false;
    boolean top = false;
    RelRoot root = converter.convertQuery(sqlNode, needsValidation, top);
    RelNode relNode = root.rel;

    String plan = RelOptUtil.toString(relNode);
    System.out.println("Logical Plan:");
    System.out.println("------------------------------------------------------------------");
    System.out.println(plan);
    System.out.println();

    // testPrograms(root.rel);

    return relNode;
}
 
Example 5
Source File: SqlConverter.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public SqlConverter(
    final PlannerSettings settings,
    final SqlOperatorTable operatorTable,
    final FunctionContext functionContext,
    final MaterializationDescriptorProvider materializationProvider,
    final FunctionImplementationRegistry functions,
    final UserSession session,
    final AttemptObserver observer,
    final Catalog catalog,
    final SubstitutionProviderFactory factory,
    final SabotConfig config,
    final ScanResult scanResult
    ) {
  this.nestingLevel = 0;
  this.flattenCounter = new FlattenOpCounter();
  this.observer = observer;
  this.settings = settings;
  this.functionContext = functionContext;
  this.functions = functions;
  this.session = Preconditions.checkNotNull(session, "user session is required");
  this.parserConfig = ParserConfig.newInstance(session, settings);
  this.isInnerQuery = false;
  this.typeFactory = JavaTypeFactoryImpl.INSTANCE;
  this.catalogReader = new DremioCatalogReader(catalog, typeFactory);
  this.opTab = new ChainedSqlOperatorTable(ImmutableList.<SqlOperatorTable>of(operatorTable, this.catalogReader));
  this.costFactory = (settings.useDefaultCosting()) ? null : new DremioCost.Factory();
  this.validator = new SqlValidatorImpl(flattenCounter, opTab, this.catalogReader, typeFactory, DremioSqlConformance.INSTANCE);
  validator.setIdentifierExpansion(true);
  this.materializations = new MaterializationList(this, session, materializationProvider);
  this.substitutions = AccelerationAwareSubstitutionProvider.of(factory.getSubstitutionProvider(config, materializations, this.settings.options));
  this.planner = DremioVolcanoPlanner.of(this);
  this.cluster = RelOptCluster.create(planner, new DremioRexBuilder(typeFactory));
  this.cluster.setMetadataProvider(DefaultRelMetadataProvider.INSTANCE);
  this.viewExpansionContext = new ViewExpansionContext(session.getCredentials().getUserName());
  this.config = config;
  this.scanResult = scanResult;
}
 
Example 6
Source File: TestIndexBasedPruning.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
  MockitoAnnotations.initMocks(this);
  when(optionManager.getOptionValidatorListing()).thenReturn(mock(OptionValidatorListing.class));
  when(optionManager.getOption(eq(PlannerSettings.FILTER_MAX_SELECTIVITY_ESTIMATE_FACTOR.getOptionName())))
          .thenReturn(PlannerSettings.FILTER_MAX_SELECTIVITY_ESTIMATE_FACTOR.getDefault());
  when(optionManager.getOption(eq(PlannerSettings.FILTER_MIN_SELECTIVITY_ESTIMATE_FACTOR.getOptionName())))
          .thenReturn(PlannerSettings.FILTER_MIN_SELECTIVITY_ESTIMATE_FACTOR.getDefault());
  OptionList optionList = new OptionList();
  optionList.add(PlannerSettings.FILTER_MAX_SELECTIVITY_ESTIMATE_FACTOR.getDefault());
  optionList.add(PlannerSettings.FILTER_MIN_SELECTIVITY_ESTIMATE_FACTOR.getDefault());
  when(optionManager.getNonDefaultOptions()).thenReturn(optionList);

  ClusterResourceInformation info = mock(ClusterResourceInformation.class);
  when(info.getExecutorNodeCount()).thenReturn(1);

  plannerSettings = new PlannerSettings(DremioTest.DEFAULT_SABOT_CONFIG, optionManager,
    () -> info);

  RelOptCluster cluster = RelOptCluster.create(new VolcanoPlanner(plannerSettings), REX_BUILDER);
  SplitsPointer splitsPointer = new TestSplitsPointer(0, Arrays.asList(TEST_PARTITION_CHUNK_METADATA_1, TEST_PARTITION_CHUNK_METADATA_2), 2);

  TableMetadata indexPrunableMetadata = new TableMetadataImpl(pluginId, DATASET_CONFIG, "testuser", splitsPointer);
  SourceType newType = mock(SourceType.class);
  when(newType.value()).thenReturn("TestSource");
  when(pluginId.getType()).thenReturn(newType);

  indexPrunableScan = new TestScanRel(cluster, TRAITS, table, pluginId, indexPrunableMetadata, PROJECTED_COLUMNS, 0, false);
  filter = new FilterRel(cluster, TRAITS, indexPrunableScan, rexNode);
  rule = new PruneScanRuleBase.PruneScanRuleFilterOnScan<>(pluginId.getType(), TestScanRel.class, mock(OptimizerRulesContext.class));
}
 
Example 7
Source File: TestRelMdRowCount.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws Exception {
  final LegacyKVStoreProvider storeProvider = new LegacyKVStoreProvider() {
    @Override
    public <K, V, T extends LegacyKVStore<K, V>, U extends KVStore<K, V>>
    T getStore(Class<? extends LegacyStoreCreationFunction<K, V, T, U>> creator) {
      LegacyKVStore<?,?> store = mock(LegacyKVStore.class);
      when(store.find()).thenReturn(Collections.emptyList());
      return (T) store;
    }

    @Override
    public void start() throws Exception {

    }

    @Override
    public void close() throws Exception {

    }
  };
  final OptionValidatorListing optionValidatorListing = new OptionValidatorListingImpl(DremioTest.CLASSPATH_SCAN_RESULT);
  SystemOptionManager som = new SystemOptionManager(optionValidatorListing, new LogicalPlanPersistence(DremioTest.DEFAULT_SABOT_CONFIG, DremioTest.CLASSPATH_SCAN_RESULT), () -> storeProvider, false);
  OptionManager optionManager = OptionManagerWrapper.Builder.newBuilder()
    .withOptionManager(new DefaultOptionManager(optionValidatorListing))
    .withOptionManager(som)
    .build();
  som.start();

  ClusterResourceInformation info = mock(ClusterResourceInformation.class);
  when(info.getExecutorNodeCount()).thenReturn(1);
  PlannerSettings plannerSettings =
    new PlannerSettings(DremioTest.DEFAULT_SABOT_CONFIG, optionManager, () -> info);
  cluster = RelOptCluster.create(new VolcanoPlanner(plannerSettings), rexBuilder);
  cluster.setMetadataProvider(DefaultRelMetadataProvider.INSTANCE);
}
 
Example 8
Source File: TestDremioPlanners.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private void checkCancelFlag(RelOptPlanner planner) {
  expectedException.expect(UserException.class);
  expectedException.expectMessage("Query was cancelled because planning time exceeded");
  RelOptCluster cluster = RelOptCluster.create(planner, new RexBuilder(SqlTypeFactoryImpl.INSTANCE));
  RelNode root = new NoneRel(cluster);
  planner.setRoot(root);
  planner.findBestExp();
}
 
Example 9
Source File: TestSplitCountChecker.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws Exception {
  OptionList optionList = new OptionList();
  optionList.add(ExecConstants.SLICE_TARGET_OPTION.getDefault());
  optionList.add(PlannerSettings.ENABLE_LEAF_LIMITS.getDefault());
  optionList.add(PlannerSettings.ENABLE_TRIVIAL_SINGULAR.getDefault());
  final OptionManager optionManager = new TestingOptionManager(mock(OptionValidatorListing.class), optionList);

  ClusterResourceInformation info = mock(ClusterResourceInformation.class);
  when(info.getExecutorNodeCount()).thenReturn(1);

  final PlannerSettings plannerSettings =
    new PlannerSettings(DremioTest.DEFAULT_SABOT_CONFIG, optionManager, () -> info);
  cluster = RelOptCluster.create(new VolcanoPlanner(plannerSettings), rexBuilder);
}
 
Example 10
Source File: PlannerImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public RelRoot expandView(RelDataType rowType, String queryString,
    List<String> schemaPath, List<String> viewPath) {
  if (planner == null) {
    ready();
  }
  SqlParser parser = SqlParser.create(queryString, parserConfig);
  SqlNode sqlNode;
  try {
    sqlNode = parser.parseQuery();
  } catch (SqlParseException e) {
    throw new RuntimeException("parse failed", e);
  }

  final CalciteCatalogReader catalogReader =
      createCatalogReader().withSchemaPath(schemaPath);
  final SqlValidator validator = createSqlValidator(catalogReader);

  final RexBuilder rexBuilder = createRexBuilder();
  final RelOptCluster cluster = RelOptCluster.create(planner, rexBuilder);
  final SqlToRelConverter.Config config = SqlToRelConverter
      .configBuilder()
      .withConfig(sqlToRelConverterConfig)
      .withTrimUnusedFields(false)
      .build();
  final SqlToRelConverter sqlToRelConverter =
      new SqlToRelConverter(this, validator,
          catalogReader, cluster, convertletTable, config);

  final RelRoot root =
      sqlToRelConverter.convertQuery(sqlNode, true, false);
  final RelRoot root2 =
      root.withRel(sqlToRelConverter.flattenTypes(root.rel, true));
  final RelBuilder relBuilder =
      config.getRelBuilderFactory().create(cluster, null);
  return root2.withRel(
      RelDecorrelator.decorrelateQuery(root.rel, relBuilder));
}
 
Example 11
Source File: TraitPropagationTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static RelNode run(PropAction action, RuleSet rules)
    throws Exception {

  FrameworkConfig config = Frameworks.newConfigBuilder()
      .ruleSets(rules).build();

  final Properties info = new Properties();
  final Connection connection = DriverManager
      .getConnection("jdbc:calcite:", info);
  final CalciteServerStatement statement = connection
      .createStatement().unwrap(CalciteServerStatement.class);
  final CalcitePrepare.Context prepareContext =
        statement.createPrepareContext();
  final JavaTypeFactory typeFactory = prepareContext.getTypeFactory();
  CalciteCatalogReader catalogReader =
        new CalciteCatalogReader(prepareContext.getRootSchema(),
            prepareContext.getDefaultSchemaPath(),
            typeFactory,
            prepareContext.config());
  final RexBuilder rexBuilder = new RexBuilder(typeFactory);
  final RelOptPlanner planner = new VolcanoPlanner(config.getCostFactory(),
      config.getContext());

  // set up rules before we generate cluster
  planner.clearRelTraitDefs();
  planner.addRelTraitDef(RelCollationTraitDef.INSTANCE);
  planner.addRelTraitDef(ConventionTraitDef.INSTANCE);

  planner.clear();
  for (RelOptRule r : rules) {
    planner.addRule(r);
  }

  final RelOptCluster cluster = RelOptCluster.create(planner, rexBuilder);
  return action.apply(cluster, catalogReader,
      prepareContext.getRootSchema().plus());
}
 
Example 12
Source File: BatsOptimizerTest.java    From Bats with Apache License 2.0 4 votes vote down vote up
static RelOptCluster createRelOptCluster(RelOptPlanner planner, RexBuilder rexBuilder) {
    RelOptCluster cluster = RelOptCluster.create(planner, rexBuilder);
    return cluster;
}
 
Example 13
Source File: SqlConverter.java    From Bats with Apache License 2.0 4 votes vote down vote up
private void initCluster() {
  cluster = RelOptCluster.create(planner, new DrillRexBuilder(typeFactory));
}
 
Example 14
Source File: CodeGenerationBenchmark.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Setup(Level.Trial)
public void setup() {
  planInfos = new PlanInfo[queries];
  VolcanoPlanner planner = new VolcanoPlanner();
  planner.addRelTraitDef(ConventionTraitDef.INSTANCE);
  planner.addRule(FilterToCalcRule.INSTANCE);
  planner.addRule(ProjectToCalcRule.INSTANCE);
  planner.addRule(EnumerableRules.ENUMERABLE_CALC_RULE);
  planner.addRule(EnumerableRules.ENUMERABLE_JOIN_RULE);
  planner.addRule(EnumerableRules.ENUMERABLE_VALUES_RULE);

  RelDataTypeFactory typeFactory =
      new JavaTypeFactoryImpl(org.apache.calcite.rel.type.RelDataTypeSystem.DEFAULT);
  RelOptCluster cluster = RelOptCluster.create(planner, new RexBuilder(typeFactory));
  RelTraitSet desiredTraits =
      cluster.traitSet().replace(EnumerableConvention.INSTANCE);

  RelBuilder relBuilder = RelFactories.LOGICAL_BUILDER.create(cluster, null);
  // Generates queries of the following form depending on the configuration parameters.
  // SELECT `t`.`name`
  // FROM (VALUES  (1, 'Value0')) AS `t` (`id`, `name`)
  // INNER JOIN (VALUES  (1, 'Value1')) AS `t` (`id`, `name`) AS `t0` ON `t`.`id` = `t0`.`id`
  // INNER JOIN (VALUES  (2, 'Value2')) AS `t` (`id`, `name`) AS `t1` ON `t`.`id` = `t1`.`id`
  // INNER JOIN (VALUES  (3, 'Value3')) AS `t` (`id`, `name`) AS `t2` ON `t`.`id` = `t2`.`id`
  // INNER JOIN ...
  // WHERE
  //  `t`.`name` = 'name0' OR
  //  `t`.`name` = 'name1' OR
  //  `t`.`name` = 'name2' OR
  //  ...
  //  OR `t`.`id` = 0
  // The last disjunction (i.e, t.id = $i) is what makes the queries different from one another
  // by assigning a different constant literal.
  for (int i = 0; i < queries; i++) {
    relBuilder.values(new String[]{"id", "name"}, 1, "Value" + 0);
    for (int j = 1; j <= joins; j++) {
      relBuilder
          .values(new String[]{"id", "name"}, j, "Value" + j)
          .join(JoinRelType.INNER, "id");
    }

    List<RexNode> disjunctions = new ArrayList<>();
    for (int j = 0; j < whereClauseDisjunctions; j++) {
      disjunctions.add(
          relBuilder.equals(
              relBuilder.field("name"),
              relBuilder.literal("name" + j)));
    }
    disjunctions.add(
        relBuilder.equals(
            relBuilder.field("id"),
            relBuilder.literal(i)));
    RelNode query =
        relBuilder
            .filter(relBuilder.or(disjunctions))
            .project(relBuilder.field("name"))
            .build();

    RelNode query0 = planner.changeTraits(query, desiredTraits);
    planner.setRoot(query0);

    PlanInfo info = new PlanInfo();
    EnumerableRel plan = (EnumerableRel) planner.findBestExp();

    EnumerableRelImplementor relImplementor =
        new EnumerableRelImplementor(plan.getCluster().getRexBuilder(), new HashMap<>());
    info.classExpr = relImplementor.implementRoot(plan, EnumerableRel.Prefer.ARRAY);
    info.javaCode =
        Expressions.toString(info.classExpr.memberDeclarations, "\n", false);

    ICompilerFactory compilerFactory;
    try {
      compilerFactory = CompilerFactoryFactory.getDefaultCompilerFactory();
    } catch (Exception e) {
      throw new IllegalStateException(
          "Unable to instantiate java compiler", e);
    }
    IClassBodyEvaluator cbe = compilerFactory.newClassBodyEvaluator();
    cbe.setClassName(info.classExpr.name);
    cbe.setExtendedClass(Utilities.class);
    cbe.setImplementedInterfaces(
        plan.getRowType().getFieldCount() == 1
            ? new Class[]{Bindable.class, Typed.class}
            : new Class[]{ArrayBindable.class});
    cbe.setParentClassLoader(EnumerableInterpretable.class.getClassLoader());
    info.cbe = cbe;
    planInfos[i] = info;
  }

}
 
Example 15
Source File: CalcitePrepareImpl.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Factory method for cluster. */
protected RelOptCluster createCluster(RelOptPlanner planner,
    RexBuilder rexBuilder) {
  return RelOptCluster.create(planner, rexBuilder);
}
 
Example 16
Source File: PlannerTests.java    From calcite with Apache License 2.0 4 votes vote down vote up
static RelOptCluster newCluster(VolcanoPlanner planner) {
  final RelDataTypeFactory typeFactory =
      new SqlTypeFactoryImpl(org.apache.calcite.rel.type.RelDataTypeSystem.DEFAULT);
  return RelOptCluster.create(planner, new RexBuilder(typeFactory));
}