Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.test.TestClustersThreadFilter;
import org.elasticsearch.test.cluster.ElasticsearchCluster;
import org.elasticsearch.test.cluster.util.Version;
import org.elasticsearch.xpack.esql.qa.rest.FieldExtractorTestCase;
import org.hamcrest.Matcher;
import org.junit.ClassRule;
Expand Down Expand Up @@ -40,4 +41,16 @@ protected Matcher<Integer> pidMatcher() {
return preference == MappedFieldType.FieldExtractPreference.STORED ? anyOf(equalTo(111), nullValue()) : nullValue(Integer.class);
}

@Override
protected void canUsePragmasOk() {
String bwc = System.getProperty("tests.old_cluster_version");
if (bwc == null) {
bwc = System.getProperty("tests.serverless.bwc_stack_version");
}
if (bwc == null) {
throw new AssertionError("can't find bwc version");
}
Version oldVersion = Version.fromString(bwc);
assumeTrue("pragma ok not supported", oldVersion.onOrAfter("8.16.0"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ public FieldExtractorIT(MappedFieldType.FieldExtractPreference preference) {
protected String getTestRestCluster() {
return cluster.getHttpAddresses();
}

@Override
protected void canUsePragmasOk() {
// always ok
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ public FieldExtractorIT(MappedFieldType.FieldExtractPreference preference) {
protected String getTestRestCluster() {
return cluster.getHttpAddresses();
}

@Override
protected void canUsePragmasOk() {
// always can use
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;

import org.apache.http.util.EntityUtils;
import org.elasticsearch.Build;
import org.elasticsearch.Version;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
Expand All @@ -35,7 +34,6 @@
import org.elasticsearch.xpack.esql.core.type.DataType;
import org.elasticsearch.xpack.esql.plugin.QueryPragmas;
import org.hamcrest.Matcher;
import org.junit.Before;

import java.io.IOException;
import java.math.BigDecimal;
Expand Down Expand Up @@ -83,17 +81,6 @@ public static List<Object[]> args() throws Exception {

protected FieldExtractorTestCase(MappedFieldType.FieldExtractPreference preference) {
this.preference = preference;
if (preference != null) {
assumeTrue("Requires pragma", Build.current().isSnapshot());
}
}

@Before
public void notOld() {
assumeTrue(
"support changed pretty radically in 8.12 so we don't test against 8.11",
getCachedNodesVersions().stream().allMatch(v -> Version.fromString(v).onOrAfter(Version.V_8_12_0))
);
}

public void testTextField() throws IOException {
Expand Down Expand Up @@ -1784,10 +1771,14 @@ private String deyaml(String err) {
private Map<String, Object> runEsql(String query) throws IOException {
RestEsqlTestCase.RequestObjectBuilder request = new RestEsqlTestCase.RequestObjectBuilder().query(query);
if (preference != null) {
canUsePragmasOk();
request = request.pragmas(
Settings.builder().put(QueryPragmas.FIELD_EXTRACT_PREFERENCE.getKey(), preference.toString()).build()
);
request.pragmasOk();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to guard this for 8.16+ because accept_pragma_risks is only available in 8.16+, and FieldExtractorTestCase can run in a mixed cluster.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call.

}
return runEsqlSync(request);
}

protected abstract void canUsePragmasOk();
}
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,14 @@ public RequestObjectBuilder keepOnCompletion(boolean value) throws IOException {
return this;
}

/**
* Allow sending pragmas even in non-snapshot builds.
*/
public RequestObjectBuilder pragmasOk() throws IOException {
builder.field("accept_pragma_risks", true);
return this;
}

Boolean keepOnCompletion() {
return keepOnCompletion;
}
Expand Down