|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.sql.opensearch.executor; |
| 7 | + |
| 8 | +import com.google.common.collect.ImmutableList; |
| 9 | +import java.sql.Connection; |
| 10 | +import java.sql.DriverManager; |
| 11 | +import java.sql.ResultSet; |
| 12 | +import java.sql.ResultSetMetaData; |
| 13 | +import java.sql.Statement; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.LinkedHashMap; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Map; |
| 18 | +import java.util.Properties; |
| 19 | +import java.util.concurrent.TimeUnit; |
| 20 | +import org.apache.calcite.DataContext; |
| 21 | +import org.apache.calcite.config.CalciteConnectionConfig; |
| 22 | +import org.apache.calcite.jdbc.CalciteConnection; |
| 23 | +import org.apache.calcite.linq4j.Enumerable; |
| 24 | +import org.apache.calcite.linq4j.Linq4j; |
| 25 | +import org.apache.calcite.rel.RelCollations; |
| 26 | +import org.apache.calcite.rel.type.RelDataType; |
| 27 | +import org.apache.calcite.rel.type.RelDataTypeFactory; |
| 28 | +import org.apache.calcite.schema.ScannableTable; |
| 29 | +import org.apache.calcite.schema.Schema; |
| 30 | +import org.apache.calcite.schema.SchemaPlus; |
| 31 | +import org.apache.calcite.schema.Statistic; |
| 32 | +import org.apache.calcite.schema.Statistics; |
| 33 | +import org.apache.calcite.schema.impl.AbstractSchema; |
| 34 | +import org.apache.calcite.sql.SqlCall; |
| 35 | +import org.apache.calcite.sql.SqlNode; |
| 36 | +import org.apache.calcite.sql.type.SqlTypeName; |
| 37 | +import org.checkerframework.checker.nullness.qual.Nullable; |
| 38 | +import org.openjdk.jmh.annotations.Benchmark; |
| 39 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 40 | +import org.openjdk.jmh.annotations.Fork; |
| 41 | +import org.openjdk.jmh.annotations.Level; |
| 42 | +import org.openjdk.jmh.annotations.Measurement; |
| 43 | +import org.openjdk.jmh.annotations.Mode; |
| 44 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 45 | +import org.openjdk.jmh.annotations.Param; |
| 46 | +import org.openjdk.jmh.annotations.Scope; |
| 47 | +import org.openjdk.jmh.annotations.Setup; |
| 48 | +import org.openjdk.jmh.annotations.State; |
| 49 | +import org.openjdk.jmh.annotations.TearDown; |
| 50 | +import org.openjdk.jmh.annotations.Warmup; |
| 51 | +import org.openjdk.jmh.infra.Blackhole; |
| 52 | + |
| 53 | +/** |
| 54 | + * Measures the cost of materializing a Calcite JDBC {@link ResultSet} into {@code Map<String, |
| 55 | + * ExprValue>} rows under a wide schema. Reading each cell by column name — the pre-fix path in |
| 56 | + * {@code OpenSearchExecutionEngine.buildResultSet} — triggers Avatica's case-insensitive linear |
| 57 | + * scan of the column metadata on every call and makes materialization O(rows × columns²). Reading |
| 58 | + * by JDBC ordinal — the current path — is linear. |
| 59 | + * |
| 60 | + * <p>Run with: {@code ./gradlew :benchmarks:jmh -Pjmh.includes=BuildResultSetBenchmark}. The two |
| 61 | + * {@code @Benchmark} methods below mirror the two code paths; the ratio between them should grow |
| 62 | + * with the column count. |
| 63 | + */ |
| 64 | +@Warmup(iterations = 1) |
| 65 | +@Measurement(iterations = 3) |
| 66 | +@BenchmarkMode(Mode.AverageTime) |
| 67 | +@OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 68 | +@State(Scope.Benchmark) |
| 69 | +@Fork(value = 1) |
| 70 | +public class BuildResultSetBenchmark { |
| 71 | + |
| 72 | + @Param({"100", "1000", "5000"}) |
| 73 | + private int columns; |
| 74 | + |
| 75 | + @Param({"100"}) |
| 76 | + private int rows; |
| 77 | + |
| 78 | + private Connection connection; |
| 79 | + private String selectAll; |
| 80 | + |
| 81 | + @Setup(Level.Trial) |
| 82 | + public void setupTrial() throws Exception { |
| 83 | + Properties info = new Properties(); |
| 84 | + // Match the engine's default (case-sensitive=false) so getObject(String) hits the same |
| 85 | + // equalsIgnoreCase scan it does at runtime. |
| 86 | + info.setProperty("caseSensitive", "false"); |
| 87 | + connection = DriverManager.getConnection("jdbc:calcite:", info); |
| 88 | + SchemaPlus root = connection.unwrap(CalciteConnection.class).getRootSchema(); |
| 89 | + SchemaPlus bench = root.add("BENCH", new AbstractSchema()); |
| 90 | + bench.add("T", new WideTable(columns, rows)); |
| 91 | + StringBuilder sb = new StringBuilder("SELECT "); |
| 92 | + for (int i = 1; i <= columns; i++) { |
| 93 | + if (i > 1) sb.append(','); |
| 94 | + sb.append("\"c").append(i).append('"'); |
| 95 | + } |
| 96 | + sb.append(" FROM \"BENCH\".\"T\""); |
| 97 | + selectAll = sb.toString(); |
| 98 | + } |
| 99 | + |
| 100 | + @TearDown(Level.Trial) |
| 101 | + public void tearDown() throws Exception { |
| 102 | + if (connection != null) connection.close(); |
| 103 | + } |
| 104 | + |
| 105 | + /** Pre-fix behaviour: read each cell by column name. */ |
| 106 | + @Benchmark |
| 107 | + public void materializeByName(Blackhole bh) throws Exception { |
| 108 | + try (Statement stmt = connection.createStatement(); |
| 109 | + ResultSet rs = stmt.executeQuery(selectAll)) { |
| 110 | + ResultSetMetaData md = rs.getMetaData(); |
| 111 | + int cc = md.getColumnCount(); |
| 112 | + List<Map<String, Object>> all = new ArrayList<>(rows); |
| 113 | + while (rs.next()) { |
| 114 | + Map<String, Object> row = new LinkedHashMap<>(cc); |
| 115 | + for (int i = 1; i <= cc; i++) { |
| 116 | + String name = md.getColumnName(i); |
| 117 | + row.put(name, rs.getObject(name)); |
| 118 | + } |
| 119 | + all.add(row); |
| 120 | + } |
| 121 | + bh.consume(all); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + /** Current behaviour: read each cell by JDBC ordinal, matching the fix for issue #5939. */ |
| 126 | + @Benchmark |
| 127 | + public void materializeByOrdinal(Blackhole bh) throws Exception { |
| 128 | + try (Statement stmt = connection.createStatement(); |
| 129 | + ResultSet rs = stmt.executeQuery(selectAll)) { |
| 130 | + ResultSetMetaData md = rs.getMetaData(); |
| 131 | + int cc = md.getColumnCount(); |
| 132 | + String[] names = new String[cc]; |
| 133 | + for (int i = 1; i <= cc; i++) { |
| 134 | + names[i - 1] = md.getColumnName(i); |
| 135 | + } |
| 136 | + List<Map<String, Object>> all = new ArrayList<>(rows); |
| 137 | + while (rs.next()) { |
| 138 | + Map<String, Object> row = new LinkedHashMap<>(cc); |
| 139 | + for (int i = 1; i <= cc; i++) { |
| 140 | + row.put(names[i - 1], rs.getObject(i)); |
| 141 | + } |
| 142 | + all.add(row); |
| 143 | + } |
| 144 | + bh.consume(all); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + /** In-memory table with {@code columns} VARCHAR fields and {@code rows} identical string rows. */ |
| 149 | + static class WideTable implements ScannableTable { |
| 150 | + private final int columns; |
| 151 | + private final int rows; |
| 152 | + |
| 153 | + WideTable(int columns, int rows) { |
| 154 | + this.columns = columns; |
| 155 | + this.rows = rows; |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public RelDataType getRowType(RelDataTypeFactory factory) { |
| 160 | + RelDataTypeFactory.Builder builder = factory.builder(); |
| 161 | + for (int i = 1; i <= columns; i++) { |
| 162 | + builder.add("c" + i, SqlTypeName.VARCHAR); |
| 163 | + } |
| 164 | + return builder.build(); |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public Enumerable<@Nullable Object[]> scan(DataContext root) { |
| 169 | + Object[][] data = new Object[rows][columns]; |
| 170 | + for (int r = 0; r < rows; r++) { |
| 171 | + for (int c = 0; c < columns; c++) { |
| 172 | + data[r][c] = "v"; |
| 173 | + } |
| 174 | + } |
| 175 | + return Linq4j.asEnumerable(data); |
| 176 | + } |
| 177 | + |
| 178 | + @Override |
| 179 | + public Statistic getStatistic() { |
| 180 | + return Statistics.of((double) rows, ImmutableList.of(), RelCollations.createSingleton(0)); |
| 181 | + } |
| 182 | + |
| 183 | + @Override |
| 184 | + public Schema.TableType getJdbcTableType() { |
| 185 | + return Schema.TableType.TABLE; |
| 186 | + } |
| 187 | + |
| 188 | + @Override |
| 189 | + public boolean isRolledUp(String column) { |
| 190 | + return false; |
| 191 | + } |
| 192 | + |
| 193 | + @Override |
| 194 | + public boolean rolledUpColumnValidInsideAgg( |
| 195 | + String column, |
| 196 | + SqlCall call, |
| 197 | + @Nullable SqlNode parent, |
| 198 | + @Nullable CalciteConnectionConfig config) { |
| 199 | + return false; |
| 200 | + } |
| 201 | + } |
| 202 | +} |
0 commit comments