Skip to content

Commit 90b9052

Browse files
committed
SQL: [Tests] Improve tests regarding integer arg
Improve tests regarding the verification of the integer arg (non-rational number) for string functions. (cherry picked from commit 30eba04)
1 parent 387740a commit 90b9052

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/analysis/analyzer/VerifierErrorMessagesTests.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
import org.elasticsearch.xpack.sql.analysis.index.IndexResolution;
1212
import org.elasticsearch.xpack.sql.analysis.index.IndexResolverTests;
1313
import org.elasticsearch.xpack.sql.expression.function.FunctionRegistry;
14+
import org.elasticsearch.xpack.sql.expression.function.scalar.math.Round;
15+
import org.elasticsearch.xpack.sql.expression.function.scalar.math.Truncate;
16+
import org.elasticsearch.xpack.sql.expression.function.scalar.string.Char;
17+
import org.elasticsearch.xpack.sql.expression.function.scalar.string.Space;
1418
import org.elasticsearch.xpack.sql.expression.predicate.conditional.Coalesce;
1519
import org.elasticsearch.xpack.sql.expression.predicate.conditional.Greatest;
1620
import org.elasticsearch.xpack.sql.expression.predicate.conditional.IfNull;
@@ -23,7 +27,9 @@
2327
import org.elasticsearch.xpack.sql.type.EsField;
2428
import org.elasticsearch.xpack.sql.type.TypesTests;
2529

30+
import java.util.Arrays;
2631
import java.util.LinkedHashMap;
32+
import java.util.Locale;
2733
import java.util.Map;
2834

2935
import static java.util.Collections.emptyMap;
@@ -471,13 +477,18 @@ public void testInvalidTypeForStringFunction_WithOneArgString() {
471477
}
472478

473479
public void testInvalidTypeForStringFunction_WithOneArgNumeric() {
474-
assertEquals("1:8: argument of [CHAR('foo')] must be [integer], found value ['foo'] type [keyword]",
475-
error("SELECT CHAR('foo')"));
480+
String functionName = randomFrom(Arrays.asList(Char.class, Space.class)).getSimpleName().toUpperCase(Locale.ROOT);
481+
assertEquals("1:8: argument of [" + functionName + "('foo')] must be [integer], found value ['foo'] type [keyword]",
482+
error("SELECT " + functionName + "('foo')"));
483+
assertEquals("1:8: argument of [" + functionName + "(1.2)] must be [integer], found value [1.2] type [double]",
484+
error("SELECT " + functionName + "(1.2)"));
476485
}
477486

478487
public void testInvalidTypeForNestedStringFunctions_WithOneArg() {
479-
assertEquals("1:14: argument of [CHAR('foo')] must be [integer], found value ['foo'] type [keyword]",
480-
error("SELECT ASCII(CHAR('foo'))"));
488+
assertEquals("1:15: argument of [SPACE('foo')] must be [integer], found value ['foo'] type [keyword]",
489+
error("SELECT LENGTH(SPACE('foo'))"));
490+
assertEquals("1:15: argument of [SPACE(1.2)] must be [integer], found value [1.2] type [double]",
491+
error("SELECT LENGTH(SPACE(1.2))"));
481492
}
482493

483494
public void testInvalidTypeForNumericFunction_WithOneArg() {
@@ -498,10 +509,13 @@ public void testInvalidTypeForStringFunction_WithTwoArgs() {
498509
}
499510

500511
public void testInvalidTypeForNumericFunction_WithTwoArgs() {
501-
assertEquals("1:8: first argument of [TRUNCATE('foo', 2)] must be [numeric], found value ['foo'] type [keyword]",
502-
error("SELECT TRUNCATE('foo', 2)"));
503-
assertEquals("1:8: second argument of [TRUNCATE(1.2, 'bar')] must be [integer], found value ['bar'] type [keyword]",
504-
error("SELECT TRUNCATE(1.2, 'bar')"));
512+
String functionName = randomFrom(Arrays.asList(Round.class, Truncate.class)).getSimpleName().toUpperCase(Locale.ROOT);
513+
assertEquals("1:8: first argument of [" + functionName + "('foo', 2)] must be [numeric], found value ['foo'] type [keyword]",
514+
error("SELECT " + functionName + "('foo', 2)"));
515+
assertEquals("1:8: second argument of [" + functionName + "(1.2, 'bar')] must be [integer], found value ['bar'] type [keyword]",
516+
error("SELECT " + functionName + "(1.2, 'bar')"));
517+
assertEquals("1:8: second argument of [" + functionName + "(1.2, 3.4)] must be [integer], found value [3.4] type [double]",
518+
error("SELECT " + functionName + "(1.2, 3.4)"));
505519
}
506520

507521
public void testInvalidTypeForBooleanFuntion_WithTwoArgs() {
@@ -540,9 +554,13 @@ public void testInvalidTypeForSubString() {
540554

541555
assertEquals("1:8: second argument of [SUBSTRING('foo', 'bar', 3)] must be [integer], found value ['bar'] type [keyword]",
542556
error("SELECT SUBSTRING('foo', 'bar', 3)"));
557+
assertEquals("1:8: second argument of [SUBSTRING('foo', 1.2, 3)] must be [integer], found value [1.2] type [double]",
558+
error("SELECT SUBSTRING('foo', 1.2, 3)"));
543559

544560
assertEquals("1:8: third argument of [SUBSTRING('foo', 2, 'bar')] must be [integer], found value ['bar'] type [keyword]",
545561
error("SELECT SUBSTRING('foo', 2, 'bar')"));
562+
assertEquals("1:8: third argument of [SUBSTRING('foo', 2, 3.4)] must be [integer], found value [3.4] type [double]",
563+
error("SELECT SUBSTRING('foo', 2, 3.4)"));
546564
}
547565

548566
public void testInvalidTypeForFunction_WithFourArgs() {

0 commit comments

Comments
 (0)