Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions x-pack/plugin/esql/qa/testFixtures/src/main/resources/ip.csv-spec
Original file line number Diff line number Diff line change
Expand Up @@ -432,3 +432,13 @@ required_feature: esql.agg_values
[fe80::cae2:65ff:fece:feb9, fe80::cae2:65ff:fece:fec0, fe80::cae2:65ff:fece:fec1, fe81::cae2:65ff:fece:feb9, fe82::cae2:65ff:fece:fec0] | epsilon
fe80::cae2:65ff:fece:feb9 | gamma
;

implictCastingStringToIP
from hosts | where mv_first(ip0) == "127.0.0.1" | keep host, ip0;

host:keyword | ip0:ip
alpha | 127.0.0.1
beta | 127.0.0.1
beta | 127.0.0.1
beta | 127.0.0.1
;
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,11 @@ version:version | name:keyword
null | lllll
5.2.9 | mmmmm
;

implictCastingStringToVersion
from apps | where version == "1.2.3.4" | sort name | keep name, version;

name:keyword | version:version
aaaaa | 1.2.3.4
hhhhh | 1.2.3.4
;
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
import static org.elasticsearch.xpack.ql.type.DataTypes.LONG;
import static org.elasticsearch.xpack.ql.type.DataTypes.NESTED;
import static org.elasticsearch.xpack.ql.type.DataTypes.TEXT;
import static org.elasticsearch.xpack.ql.type.DataTypes.VERSION;

public class Analyzer extends ParameterizedRuleExecutor<LogicalPlan, AnalyzerContext> {
// marker list of attributes for plans that do not have any concrete fields to return, but have other computed columns to return
Expand Down Expand Up @@ -846,14 +847,14 @@ private static Expression processBinaryOperator(BinaryOperator<?, ?, ?, ?> o) {

if (left.dataType() == KEYWORD
&& left.foldable()
&& (right.dataType().isNumeric() || right.dataType() == DATETIME)
&& (isSupportedCastingForBinaryComparison(right.dataType()))
&& ((left instanceof EsqlScalarFunction) == false)) {
targetDataType = right.dataType();
from = left;
}
if (right.dataType() == KEYWORD
&& right.foldable()
&& (left.dataType().isNumeric() || left.dataType() == DATETIME)
&& (isSupportedCastingForBinaryComparison(left.dataType()))
&& ((right instanceof EsqlScalarFunction) == false)) {
targetDataType = left.dataType();
from = right;
Expand All @@ -867,6 +868,10 @@ private static Expression processBinaryOperator(BinaryOperator<?, ?, ?, ?> o) {
return childrenChanged ? o.replaceChildren(newChildren) : o;
}

private static boolean isSupportedCastingForBinaryComparison(DataType type) {
Comment thread
fang-xing-esql marked this conversation as resolved.
Outdated
return type.isNumeric() || type == DATETIME || type == IP || type == VERSION;
Comment thread
fang-xing-esql marked this conversation as resolved.
Outdated
}

public static Expression castStringLiteral(Expression from, DataType target) {
assert from.foldable();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,8 @@ public static BytesRef stringToVersion(BytesRef field) {
return new Version(field.utf8ToString()).toBytesRef();
}

public static Version stringToVersion(String field) {
return new Version(field);
public static BytesRef stringToVersion(String field) {
return new Version(field).toBytesRef();
}

public static String versionToString(BytesRef field) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.elasticsearch.tasks.CancellableTask;
import org.elasticsearch.tasks.TaskId;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.junit.annotations.TestLogging;
import org.elasticsearch.threadpool.FixedExecutorBuilder;
import org.elasticsearch.threadpool.TestThreadPool;
import org.elasticsearch.threadpool.ThreadPool;
Expand Down Expand Up @@ -139,7 +140,7 @@
*
* To log the results logResults() should return "true".
*/
// @TestLogging(value = "org.elasticsearch.xpack.esql:TRACE,org.elasticsearch.compute:TRACE", reason = "debug")
@TestLogging(value = "org.elasticsearch.xpack.esql:TRACE,org.elasticsearch.compute:TRACE", reason = "debug")
Comment thread
fang-xing-esql marked this conversation as resolved.
Outdated
public class CsvTests extends ESTestCase {

private static final Logger LOGGER = LogManager.getLogger(CsvTests.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package org.elasticsearch.xpack.esql.parser;

import org.apache.lucene.util.BytesRef;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.common.Randomness;
import org.elasticsearch.core.Tuple;
Expand Down Expand Up @@ -49,6 +50,7 @@
import org.elasticsearch.xpack.ql.plan.logical.Project;
import org.elasticsearch.xpack.ql.type.DataType;
import org.elasticsearch.xpack.ql.type.DataTypes;
import org.elasticsearch.xpack.ql.util.StringUtils;
import org.elasticsearch.xpack.versionfield.Version;

import java.math.BigInteger;
Expand Down Expand Up @@ -867,18 +869,19 @@ public void testUsageOfProject() {

public void testInputParams() {
LogicalPlan stm = statement(
"row x = ?, y = ?, a = ?, b = ?, c = ?",
"row x = ?, y = ?, a = ?, b = ?, c = ?, d = ?",
List.of(
new TypedParamValue("integer", 1),
new TypedParamValue("keyword", "2"),
new TypedParamValue("date_period", "2 days"),
new TypedParamValue("time_duration", "4 hours"),
new TypedParamValue("version", "1.2.3")
new TypedParamValue("version", "1.2.3"),
new TypedParamValue("ip", "127.0.0.1")
)
);
assertThat(stm, instanceOf(Row.class));
Row row = (Row) stm;
assertThat(row.fields().size(), is(5));
assertThat(row.fields().size(), is(6));

NamedExpression field = row.fields().get(0);
assertThat(field.name(), is("x"));
Expand Down Expand Up @@ -908,8 +911,15 @@ public void testInputParams() {
assertThat(field.name(), is("c"));
assertThat(field, instanceOf(Alias.class));
alias = (Alias) field;
assertThat(alias.child().fold().getClass(), is(Version.class));
assertThat(alias.child().fold().toString(), is("1.2.3"));
assertThat(alias.child().fold().getClass(), is(BytesRef.class));
assertThat(alias.child().fold().toString(), is(new Version("1.2.3").toBytesRef().toString()));

field = row.fields().get(5);
assertThat(field.name(), is("d"));
assertThat(field, instanceOf(Alias.class));
alias = (Alias) field;
assertThat(alias.child().fold().getClass(), is(BytesRef.class));
assertThat(alias.child().fold().toString(), is(StringUtils.parseIP("127.0.0.1").toString()));
}

public void testWrongIntervalParams() {
Expand Down