|
55 | 55 | import java.util.regex.Pattern; |
56 | 56 |
|
57 | 57 | import static java.util.Collections.emptySet; |
| 58 | +import static java.util.Map.entry; |
58 | 59 | import static org.elasticsearch.common.logging.LoggerMessageFormat.format; |
59 | 60 | import static org.elasticsearch.test.ListMatcher.matchesList; |
60 | 61 | import static org.elasticsearch.test.MapMatcher.assertMap; |
@@ -648,6 +649,134 @@ public void testErrorMessageForInvalidIntervalParams() throws IOException { |
648 | 649 | ); |
649 | 650 | } |
650 | 651 |
|
| 652 | + public void testErrorMessageForArrayValuesInParams() throws IOException { |
| 653 | + ResponseException re = expectThrows( |
| 654 | + ResponseException.class, |
| 655 | + () -> runEsql(RequestObjectBuilder.jsonBuilder().query("row a = 1 | eval x = ?").params("[{\"n1\": [5, 6, 7]}]")) |
| 656 | + ); |
| 657 | + assertThat( |
| 658 | + EntityUtils.toString(re.getResponse().getEntity()), |
| 659 | + containsString("Failed to parse params: [1:45] n1=[5, 6, 7] is not supported as a parameter") |
| 660 | + ); |
| 661 | + } |
| 662 | + |
| 663 | + public void testNamedParamsForIdentifierAndIdentifierPatterns() throws IOException { |
| 664 | + bulkLoadTestData(10); |
| 665 | + // positive |
| 666 | + var query = requestObjectBuilder().query( |
| 667 | + format( |
| 668 | + null, |
| 669 | + "from {} | eval x1 = ?n1 | where ?n2 == x1 | stats xx2 = ?fn1(?n3) by ?n4 | keep ?n4, ?n5 | sort ?n4", |
| 670 | + testIndexName() |
| 671 | + ) |
| 672 | + ) |
| 673 | + .params( |
| 674 | + "[{\"n1\" : {\"value\" : \"integer\" , \"kind\" : \"identifier\"}}," |
| 675 | + + "{\"n2\" : {\"value\" : \"short\" , \"kind\" : \"identifier\"}}, " |
| 676 | + + "{\"n3\" : {\"value\" : \"double\" , \"kind\" : \"identifier\"}}," |
| 677 | + + "{\"n4\" : {\"value\" : \"boolean\" , \"kind\" : \"identifier\"}}, " |
| 678 | + + "{\"n5\" : {\"value\" : \"xx*\" , \"kind\" : \"pattern\"}}, " |
| 679 | + + "{\"fn1\" : {\"value\" : \"max\" , \"kind\" : \"identifier\"}}]" |
| 680 | + ); |
| 681 | + Map<String, Object> result = runEsql(query); |
| 682 | + Map<String, String> colA = Map.of("name", "boolean", "type", "boolean"); |
| 683 | + Map<String, String> colB = Map.of("name", "xx2", "type", "double"); |
| 684 | + assertEquals(List.of(colA, colB), result.get("columns")); |
| 685 | + assertEquals(List.of(List.of(false, 9.1), List.of(true, 8.1)), result.get("values")); |
| 686 | + |
| 687 | + // missing params |
| 688 | + ResponseException re = expectThrows( |
| 689 | + ResponseException.class, |
| 690 | + () -> runEsqlSync( |
| 691 | + requestObjectBuilder().query( |
| 692 | + format( |
| 693 | + null, |
| 694 | + "from {} | eval x1 = ?n1 | where ?n2 == x1 | stats xx2 = max(?n3) by ?n4 | keep ?n4, ?n5 | sort ?n4", |
| 695 | + testIndexName() |
| 696 | + ) |
| 697 | + ).params("[]") |
| 698 | + ) |
| 699 | + ); |
| 700 | + String error = re.getMessage(); |
| 701 | + assertThat(error, containsString("ParsingException")); |
| 702 | + assertThat(error, containsString("Unknown query parameter [n1]")); |
| 703 | + |
| 704 | + // param inside backquote is not recognized as a param |
| 705 | + Map<String, Integer> commandsWithLineNumber = Map.ofEntries( |
| 706 | + entry("eval x1 = `?n1`", 33), |
| 707 | + entry("where `?n1` == 1", 29), |
| 708 | + entry("stats x = max(n2) by `?n1`", 44), |
| 709 | + entry("stats x = max(`?n1`) by n2", 37), |
| 710 | + entry("keep `?n1`", 28), |
| 711 | + entry("sort `?n1`", 28) |
| 712 | + ); |
| 713 | + for (Map.Entry<String, Integer> command : commandsWithLineNumber.entrySet()) { |
| 714 | + re = expectThrows( |
| 715 | + ResponseException.class, |
| 716 | + () -> runEsqlSync( |
| 717 | + requestObjectBuilder().query(format(null, "from {} | {}", testIndexName(), command.getKey())) |
| 718 | + .params( |
| 719 | + "[{\"n1\" : {\"value\" : \"integer\" , \"kind\" : \"identifier\"}}," |
| 720 | + + "{\"n2\" : {\"value\" : \"short\" , \"kind\" : \"identifier\"}}]" |
| 721 | + ) |
| 722 | + ) |
| 723 | + ); |
| 724 | + error = re.getMessage(); |
| 725 | + assertThat(error, containsString("VerificationException")); |
| 726 | + assertThat(error, containsString("line 1:" + command.getValue() + ": Unknown column [?n1]")); |
| 727 | + } |
| 728 | + |
| 729 | + commandsWithLineNumber = Map.ofEntries( |
| 730 | + entry("rename ?n1 as ?n2", 30), |
| 731 | + entry("enrich idx2 ON ?n1 WITH ?n2 = ?n3", 38), |
| 732 | + entry("keep ?n1", 28), |
| 733 | + entry("drop ?n1", 28) |
| 734 | + ); |
| 735 | + for (Map.Entry<String, Integer> command : commandsWithLineNumber.entrySet()) { |
| 736 | + re = expectThrows( |
| 737 | + ResponseException.class, |
| 738 | + () -> runEsqlSync( |
| 739 | + requestObjectBuilder().query(format(null, "from {} | {}", testIndexName(), command.getKey())) |
| 740 | + .params( |
| 741 | + "[{\"n1\" : {\"value\" : \"`n1`\" , \"kind\" : \"identifier\"}}," |
| 742 | + + "{\"n2\" : {\"value\" : \"`n2`\" , \"kind\" : \"identifier\"}}, " |
| 743 | + + "{\"n3\" : {\"value\" : \"`n3`\" , \"kind\" : \"identifier\"}}]" |
| 744 | + ) |
| 745 | + ) |
| 746 | + ); |
| 747 | + error = re.getMessage(); |
| 748 | + assertThat(error, containsString("VerificationException")); |
| 749 | + assertThat(error, containsString("line 1:" + command.getValue() + ": Unknown column [`n1`]")); |
| 750 | + } |
| 751 | + |
| 752 | + // param cannot be used as a command name |
| 753 | + Map<String, String> paramsAsCommandNames = Map.ofEntries( |
| 754 | + entry("eval", "x = 1"), |
| 755 | + entry("where", "x == 1"), |
| 756 | + entry("stats", "x = count(*)"), |
| 757 | + entry("keep", "x"), |
| 758 | + entry("drop", "x"), |
| 759 | + entry("rename", "x as y"), |
| 760 | + entry("sort", "x"), |
| 761 | + entry("dissect", "x \"%{foo}\""), |
| 762 | + entry("grok", "x \"%{WORD:foo}\""), |
| 763 | + entry("enrich", "idx2 ON x"), |
| 764 | + entry("mvExpand", "x") |
| 765 | + ); |
| 766 | + for (Map.Entry<String, String> command : paramsAsCommandNames.entrySet()) { |
| 767 | + re = expectThrows( |
| 768 | + ResponseException.class, |
| 769 | + () -> runEsqlSync( |
| 770 | + requestObjectBuilder().query(format(null, "from {} | ?cmd {}", testIndexName(), command.getValue())) |
| 771 | + .params("[{\"cmd\" : {\"value\" : \"" + command.getKey() + "\", \"kind\" : \"identifier\"}}]") |
| 772 | + ) |
| 773 | + ); |
| 774 | + error = re.getMessage(); |
| 775 | + assertThat(error, containsString("ParsingException")); |
| 776 | + assertThat(error, containsString("line 1:23: mismatched input '?cmd' expecting {'dissect', 'drop'")); |
| 777 | + } |
| 778 | + } |
| 779 | + |
651 | 780 | public void testErrorMessageForLiteralDateMathOverflow() throws IOException { |
652 | 781 | List<String> dateMathOverflowExpressions = List.of( |
653 | 782 | "2147483647 day + 1 day", |
@@ -687,14 +816,6 @@ private void assertExceptionForDateMath(String dateMathString, String errorSubst |
687 | 816 | assertThat(re.getResponse().getStatusLine().getStatusCode(), equalTo(400)); |
688 | 817 | } |
689 | 818 |
|
690 | | - public void testErrorMessageForArrayValuesInParams() throws IOException { |
691 | | - ResponseException re = expectThrows( |
692 | | - ResponseException.class, |
693 | | - () -> runEsql(requestObjectBuilder().query("row a = 1 | eval x = ?").params("[{\"n1\": [5, 6, 7]}]")) |
694 | | - ); |
695 | | - assertThat(EntityUtils.toString(re.getResponse().getEntity()), containsString("n1=[5, 6, 7] is not supported as a parameter")); |
696 | | - } |
697 | | - |
698 | 819 | public void testComplexFieldNames() throws IOException { |
699 | 820 | bulkLoadTestData(1); |
700 | 821 | // catch verification exception, field names not found |
|
0 commit comments