xiedeyantu commented on code in PR #4796:
URL: https://github.com/apache/calcite/pull/4796#discussion_r2828441695


##########
ubenchmark/src/jmh/java/org/apache/calcite/benchmarks/UnionTreeBenchmark.java:
##########
@@ -0,0 +1,184 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.benchmarks;
+
+import org.apache.calcite.plan.hep.HepMatchOrder;
+import org.apache.calcite.plan.hep.HepPlanner;
+import org.apache.calcite.plan.hep.HepProgram;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.logical.LogicalUnion;
+import org.apache.calcite.rel.rules.CoreRules;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.schema.SchemaPlus;
+import org.apache.calcite.schema.impl.AbstractTable;
+import org.apache.calcite.sql.fun.SqlStdOperatorTable;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.tools.Frameworks;
+import org.apache.calcite.tools.RelBuilder;
+
+import com.google.common.collect.ImmutableList;
+
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.BenchmarkMode;
+import org.openjdk.jmh.annotations.Fork;
+import org.openjdk.jmh.annotations.Level;
+import org.openjdk.jmh.annotations.Measurement;
+import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.annotations.OutputTimeUnit;
+import org.openjdk.jmh.annotations.Param;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.Setup;
+import org.openjdk.jmh.annotations.State;
+import org.openjdk.jmh.annotations.Threads;
+import org.openjdk.jmh.annotations.Warmup;
+import org.openjdk.jmh.runner.Runner;
+import org.openjdk.jmh.runner.RunnerException;
+import org.openjdk.jmh.runner.options.Options;
+import org.openjdk.jmh.runner.options.OptionsBuilder;
+
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Benchmark that constructs a synthetic query plan consisting of a large 
UNION ALL tree.
+ *
+ * <p>Each UNION input branch contains layers of Projects and Filters that are 
intentionally
+ *  simplifiable, so that typical planner rules (e.g., Project/Filter 
simplification)
+ *  are repeatedly applicable.
+ *
+ * <p>This benchmark primarily measures planner overhead under heavy rule 
activity: matching and
+ * firing rules, replacing RelNodes, and traversing the evolving plan during 
optimization.
+ * It also simulates a multiphase optimization flow by running multiple 
HepPrograms sequentially.
+ */
+
+@Fork(value = 1, jvmArgsPrepend = {"-Xss200m"})
+@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
+@Warmup(iterations = 3, time = 1, timeUnit = TimeUnit.SECONDS)
+@BenchmarkMode(Mode.AverageTime)
+@OutputTimeUnit(TimeUnit.MILLISECONDS)
+@State(Scope.Thread)
+@Threads(1)
+public class UnionTreeBenchmark {
+
+  @Param({"100", "1000", "5000", "10000"})
+  int unionNum;
+
+  private RelBuilder builder;
+
+  @Setup(Level.Trial)
+  public void setup() {
+    SchemaPlus rootSchema = Frameworks.createRootSchema(true);
+    rootSchema.add("EMP", new AbstractTable() {
+      @Override public RelDataType getRowType(RelDataTypeFactory typeFactory) {
+        return typeFactory.builder()
+            .add("EMPNO", SqlTypeName.INTEGER)
+            .add("ENAME", SqlTypeName.VARCHAR)
+            .add("JOB", SqlTypeName.VARCHAR)
+            .add("MGR", SqlTypeName.INTEGER)
+            .add("HIREDATE", SqlTypeName.DATE)
+            .add("SAL", SqlTypeName.INTEGER)
+            .add("COMM", SqlTypeName.INTEGER)
+            .add("DEPTNO", SqlTypeName.INTEGER)
+            .build();
+      }
+    });
+
+    builder =
+        RelBuilder.create(Frameworks.newConfigBuilder()
+            .defaultSchema(rootSchema)
+            .build());
+  }
+
+  // select ENAME, i as cat_id, 'i' as cat_name, i as require_free_postage,
+  // 0 as require_15return, 0 as require_48hour, 1 as require_insurance
+  // from emp
+  // where EMPNO = i and MGR >= 0 and MGR <= 0 and ENAME = 'Y' and SAL = i
+  private RelNode makeSelectBranch(int i) {
+    return builder.scan("EMP")
+        .filter(
+            builder.and(
+                builder.equals(builder.field("EMPNO"), builder.literal(i)),
+                builder.call(SqlStdOperatorTable.GREATER_THAN_OR_EQUAL,

Review Comment:
   The builder also supports methods like `greaterThanOrEqual` and 
`lessThanOrEqual`. A small suggestion. Whether to change it or not is up to 
your preference.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to