Skip to content

Commit

Permalink
ESQL: Merge Configuration (elastic#111653)
Browse files Browse the repository at this point in the history
This merges esql-core's `Configuration` into esql's `EsqlConfiguration`
and then renamed esql's configuration to `Configuration`. Now there's
only one configuration! Not two layers, one that was copied from ql.
After this we can make changes to our configuration to better suite our
needs much more easilly.

It also centralizes a place to grab test configuration into
`ConfigurationTestUtils` in esql's test fixtures. Most of that was
scattered between a test fixture and the serialization tests.
  • Loading branch information
nik9000 authored Aug 7, 2024
1 parent 7e5960b commit b7ccf43
Show file tree
Hide file tree
Showing 55 changed files with 361 additions and 387 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.elasticsearch.xpack.esql.core.expression.predicate.regex.RLikePattern;
import org.elasticsearch.xpack.esql.core.expression.predicate.regex.WildcardLike;
import org.elasticsearch.xpack.esql.core.expression.predicate.regex.WildcardPattern;
import org.elasticsearch.xpack.esql.core.session.Configuration;
import org.elasticsearch.xpack.esql.core.tree.Source;
import org.elasticsearch.xpack.esql.core.type.DataType;
import org.elasticsearch.xpack.esql.core.type.EsField;
Expand All @@ -51,7 +50,6 @@
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumSet;
Expand All @@ -78,22 +76,11 @@

public final class TestUtils {

public static final ZoneId UTC = ZoneId.of("Z");
public static final Configuration TEST_CFG = new Configuration(UTC, null, null);

private static final String MATCHER_TYPE_CONTAINS = "CONTAINS";
private static final String MATCHER_TYPE_REGEX = "REGEX";

private TestUtils() {}

public static Configuration randomConfiguration() {
return new Configuration(randomZone(), randomAlphaOfLength(10), randomAlphaOfLength(10));
}

public static Configuration randomConfiguration(ZoneId zoneId) {
return new Configuration(zoneId, randomAlphaOfLength(10), randomAlphaOfLength(10));
}

public static Literal of(Object value) {
return of(Source.EMPTY, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,58 +5,53 @@
* 2.0.
*/

package org.elasticsearch.xpack.esql.session;
package org.elasticsearch.xpack.esql;

import org.elasticsearch.common.breaker.CircuitBreaker;
import org.elasticsearch.common.breaker.NoopCircuitBreaker;
import org.elasticsearch.common.collect.Iterators;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.BigArrays;
import org.elasticsearch.compute.data.Block;
import org.elasticsearch.compute.data.BlockFactory;
import org.elasticsearch.compute.data.BlockStreamInput;
import org.elasticsearch.compute.data.BlockUtils;
import org.elasticsearch.compute.data.ElementType;
import org.elasticsearch.compute.lucene.DataPartitioning;
import org.elasticsearch.core.Releasables;
import org.elasticsearch.test.AbstractWireSerializingTestCase;
import org.elasticsearch.xpack.esql.Column;
import org.elasticsearch.xpack.esql.action.ParseTables;
import org.elasticsearch.xpack.esql.core.type.DataType;
import org.elasticsearch.xpack.esql.planner.PlannerUtils;
import org.elasticsearch.xpack.esql.plugin.QueryPragmas;
import org.elasticsearch.xpack.esql.session.Configuration;

import java.time.ZoneId;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import static org.apache.lucene.tests.util.LuceneTestCase.random;
import static org.apache.lucene.tests.util.LuceneTestCase.randomLocale;
import static org.elasticsearch.test.ESTestCase.between;
import static org.elasticsearch.test.ESTestCase.frequently;
import static org.elasticsearch.test.ESTestCase.randomAlphaOfLength;
import static org.elasticsearch.test.ESTestCase.randomAlphaOfLengthBetween;
import static org.elasticsearch.test.ESTestCase.randomBoolean;
import static org.elasticsearch.test.ESTestCase.randomFrom;
import static org.elasticsearch.test.ESTestCase.randomIntBetween;
import static org.elasticsearch.test.ESTestCase.randomNonNegativeInt;
import static org.elasticsearch.test.ESTestCase.randomRealisticUnicodeOfLength;
import static org.elasticsearch.test.ESTestCase.randomZone;
import static org.elasticsearch.xpack.esql.EsqlTestUtils.randomLiteral;
import static org.elasticsearch.xpack.esql.session.EsqlConfiguration.QUERY_COMPRESS_THRESHOLD_CHARS;
import static org.elasticsearch.xpack.esql.session.Configuration.QUERY_COMPRESS_THRESHOLD_CHARS;

public class EsqlConfigurationSerializationTests extends AbstractWireSerializingTestCase<EsqlConfiguration> {

@Override
protected Writeable.Reader<EsqlConfiguration> instanceReader() {
return in -> new EsqlConfiguration(
new BlockStreamInput(in, new BlockFactory(new NoopCircuitBreaker(CircuitBreaker.REQUEST), BigArrays.NON_RECYCLING_INSTANCE))
);
}

private static QueryPragmas randomQueryPragmas() {
return new QueryPragmas(
Settings.builder().put(QueryPragmas.DATA_PARTITIONING.getKey(), randomFrom(DataPartitioning.values())).build()
);
}

public static EsqlConfiguration randomConfiguration() {
/**
* Test utilities for building random {@link Configuration}s.
*/
public class ConfigurationTestUtils {
public static Configuration randomConfiguration() {
int len = randomIntBetween(1, 300) + (frequently() ? 0 : QUERY_COMPRESS_THRESHOLD_CHARS);
return randomConfiguration(randomRealisticUnicodeOfLength(len), randomTables());
}

public static EsqlConfiguration randomConfiguration(String query, Map<String, Map<String, Column>> tables) {
public static Configuration randomConfiguration(String query, Map<String, Map<String, Column>> tables) {
var zoneId = randomZone();
var locale = randomLocale(random());
var username = randomAlphaOfLengthBetween(1, 10);
Expand All @@ -65,7 +60,7 @@ public static EsqlConfiguration randomConfiguration(String query, Map<String, Ma
var defaultTruncation = randomNonNegativeInt();
boolean profile = randomBoolean();

return new EsqlConfiguration(
return new Configuration(
zoneId,
locale,
username,
Expand All @@ -79,6 +74,15 @@ public static EsqlConfiguration randomConfiguration(String query, Map<String, Ma
);
}

private static QueryPragmas randomQueryPragmas() {
return new QueryPragmas(
Settings.builder().put(QueryPragmas.DATA_PARTITIONING.getKey(), randomFrom(DataPartitioning.values())).build()
);
}

/**
* Build random "tables" to use for {@link Configuration#tables()}.
*/
public static Map<String, Map<String, Column>> randomTables() {
if (randomBoolean()) {
return Map.of();
Expand Down Expand Up @@ -129,78 +133,4 @@ static Map<String, Column> randomColumns() {
}
}
}

@Override
protected EsqlConfiguration createTestInstance() {
return randomConfiguration();
}

@Override
protected EsqlConfiguration mutateInstance(EsqlConfiguration in) {
ZoneId zoneId = in.zoneId();
Locale locale = in.locale();
String username = in.username();
String clusterName = in.clusterName();
QueryPragmas pragmas = in.pragmas();
int resultTruncationMaxSize = in.resultTruncationMaxSize();
int resultTruncationDefaultSize = in.resultTruncationDefaultSize();
String query = in.query();
boolean profile = in.profile();
Map<String, Map<String, Column>> tables = in.tables();
switch (between(0, 9)) {
case 0 -> zoneId = randomValueOtherThan(zoneId, () -> randomZone().normalized());
case 1 -> locale = randomValueOtherThan(in.locale(), () -> randomLocale(random()));
case 2 -> username = randomAlphaOfLength(15);
case 3 -> clusterName = randomAlphaOfLength(15);
case 4 -> pragmas = new QueryPragmas(
Settings.builder().put(QueryPragmas.EXCHANGE_BUFFER_SIZE.getKey(), between(1, 10)).build()
);
case 5 -> resultTruncationMaxSize += randomIntBetween(3, 10);
case 6 -> resultTruncationDefaultSize += randomIntBetween(3, 10);
case 7 -> query += randomAlphaOfLength(2);
case 8 -> profile = false == profile;
case 9 -> {
while (true) {
Map<String, Map<String, Column>> newTables = null;
try {
newTables = randomTables();
if (false == tables.equals(newTables)) {
tables = newTables;
newTables = null;
break;
}
} finally {
if (newTables != null) {
Releasables.close(
Releasables.wrap(
Iterators.flatMap(
newTables.values().iterator(),
columns -> Iterators.map(columns.values().iterator(), Column::values)
)
)
);
}
}
}
}
}
return new EsqlConfiguration(
zoneId,
locale,
username,
clusterName,
pragmas,
resultTruncationMaxSize,
resultTruncationDefaultSize,
query,
profile,
tables
);

}

@Override
protected NamedWriteableRegistry getNamedWriteableRegistry() {
return new NamedWriteableRegistry(Block.getNamedWriteables());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import org.elasticsearch.xpack.esql.core.expression.Literal;
import org.elasticsearch.xpack.esql.core.expression.ReferenceAttribute;
import org.elasticsearch.xpack.esql.core.expression.predicate.Range;
import org.elasticsearch.xpack.esql.core.session.Configuration;
import org.elasticsearch.xpack.esql.core.tree.Source;
import org.elasticsearch.xpack.esql.core.type.DataType;
import org.elasticsearch.xpack.esql.core.type.DateUtils;
Expand All @@ -59,7 +58,7 @@
import org.elasticsearch.xpack.esql.plan.logical.local.LocalSupplier;
import org.elasticsearch.xpack.esql.plugin.EsqlPlugin;
import org.elasticsearch.xpack.esql.plugin.QueryPragmas;
import org.elasticsearch.xpack.esql.session.EsqlConfiguration;
import org.elasticsearch.xpack.esql.session.Configuration;
import org.elasticsearch.xpack.esql.stats.Metrics;
import org.elasticsearch.xpack.esql.stats.SearchStats;
import org.elasticsearch.xpack.versionfield.Version;
Expand Down Expand Up @@ -178,10 +177,6 @@ public static Literal of(Object value) {
return of(Source.EMPTY, value);
}

public static Configuration randomConfiguration() {
return new Configuration(randomZone(), randomAlphaOfLength(10), randomAlphaOfLength(10));
}

/**
* Utility method for creating 'in-line' Literals (out of values instead of expressions).
*/
Expand Down Expand Up @@ -254,14 +249,14 @@ public boolean isIndexed(String field) {

private static final Map<String, Map<String, Column>> TABLES = tables();

public static final EsqlConfiguration TEST_CFG = configuration(new QueryPragmas(Settings.EMPTY));
public static final Configuration TEST_CFG = configuration(new QueryPragmas(Settings.EMPTY));

public static final Verifier TEST_VERIFIER = new Verifier(new Metrics());

private EsqlTestUtils() {}

public static EsqlConfiguration configuration(QueryPragmas pragmas, String query) {
return new EsqlConfiguration(
public static Configuration configuration(QueryPragmas pragmas, String query) {
return new Configuration(
DateUtils.UTC,
Locale.US,
null,
Expand All @@ -275,11 +270,11 @@ public static EsqlConfiguration configuration(QueryPragmas pragmas, String query
);
}

public static EsqlConfiguration configuration(QueryPragmas pragmas) {
public static Configuration configuration(QueryPragmas pragmas) {
return configuration(pragmas, StringUtils.EMPTY);
}

public static EsqlConfiguration configuration(String query) {
public static Configuration configuration(String query) {
return configuration(new QueryPragmas(Settings.EMPTY), query);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import org.elasticsearch.xpack.esql.core.rule.ParameterizedRuleExecutor;
import org.elasticsearch.xpack.esql.core.rule.Rule;
import org.elasticsearch.xpack.esql.core.rule.RuleExecutor;
import org.elasticsearch.xpack.esql.core.session.Configuration;
import org.elasticsearch.xpack.esql.core.tree.Source;
import org.elasticsearch.xpack.esql.core.type.DataType;
import org.elasticsearch.xpack.esql.core.type.EsField;
Expand Down Expand Up @@ -80,6 +79,7 @@
import org.elasticsearch.xpack.esql.plan.logical.local.EsqlProject;
import org.elasticsearch.xpack.esql.plan.logical.local.LocalRelation;
import org.elasticsearch.xpack.esql.plan.logical.local.LocalSupplier;
import org.elasticsearch.xpack.esql.session.Configuration;
import org.elasticsearch.xpack.esql.stats.FeatureMetric;
import org.elasticsearch.xpack.esql.type.EsqlDataTypeConverter;
import org.elasticsearch.xpack.esql.type.MultiTypeEsField;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

import org.elasticsearch.xpack.esql.expression.function.EsqlFunctionRegistry;
import org.elasticsearch.xpack.esql.index.IndexResolution;
import org.elasticsearch.xpack.esql.session.EsqlConfiguration;
import org.elasticsearch.xpack.esql.session.Configuration;

public record AnalyzerContext(
EsqlConfiguration configuration,
Configuration configuration,
EsqlFunctionRegistry functionRegistry,
IndexResolution indexResolution,
EnrichResolution enrichResolution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.elasticsearch.xpack.esql.optimizer.LogicalPlanOptimizer;
import org.elasticsearch.xpack.esql.plan.physical.PhysicalPlan;
import org.elasticsearch.xpack.esql.planner.Mapper;
import org.elasticsearch.xpack.esql.session.EsqlConfiguration;
import org.elasticsearch.xpack.esql.session.Configuration;
import org.elasticsearch.xpack.esql.session.EsqlSession;
import org.elasticsearch.xpack.esql.session.IndexResolver;
import org.elasticsearch.xpack.esql.session.Result;
Expand Down Expand Up @@ -49,7 +49,7 @@ public PlanExecutor(IndexResolver indexResolver) {
public void esql(
EsqlQueryRequest request,
String sessionId,
EsqlConfiguration cfg,
Configuration cfg,
EnrichPolicyResolver enrichPolicyResolver,
BiConsumer<PhysicalPlan, ActionListener<Result>> runPhase,
ActionListener<Result> listener
Expand Down
Loading

0 comments on commit b7ccf43

Please sign in to comment.