|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.sql.calcite.standalone; |
| 7 | + |
| 8 | +import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_WEBLOGS; |
| 9 | +import static org.opensearch.sql.util.MatcherUtils.rows; |
| 10 | +import static org.opensearch.sql.util.MatcherUtils.schema; |
| 11 | +import static org.opensearch.sql.util.MatcherUtils.verifyDataRows; |
| 12 | +import static org.opensearch.sql.util.MatcherUtils.verifySchema; |
| 13 | + |
| 14 | +import java.io.IOException; |
| 15 | +import org.json.JSONObject; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | +import org.opensearch.client.Request; |
| 18 | +import org.opensearch.sql.legacy.TestsConstants; |
| 19 | + |
| 20 | +public class CalcitePPLCaseFunctionIT extends CalcitePPLIntegTestCase { |
| 21 | + @Override |
| 22 | + public void init() throws IOException { |
| 23 | + super.init(); |
| 24 | + loadIndex(Index.WEBLOG); |
| 25 | + appendDataForBadResponse(); |
| 26 | + } |
| 27 | + |
| 28 | + private void appendDataForBadResponse() throws IOException { |
| 29 | + Request request1 = new Request("PUT", "/" + TEST_INDEX_WEBLOGS + "/_doc/7?refresh=true"); |
| 30 | + request1.setJsonEntity( |
| 31 | + "{\"host\": \"::1\", \"method\": \"GET\", \"url\": \"/history/apollo/\", \"response\":" |
| 32 | + + " \"301\", \"bytes\": \"6245\"}"); |
| 33 | + client().performRequest(request1); |
| 34 | + Request request2 = |
| 35 | + new Request("PUT", "/" + TestsConstants.TEST_INDEX_WEBLOGS + "/_doc/8?refresh=true"); |
| 36 | + request2.setJsonEntity( |
| 37 | + "{\"host\": \"0.0.0.2\", \"method\": \"GET\", \"url\":" |
| 38 | + + " \"/shuttle/missions/sts-73/mission-sts-73.html\", \"response\": \"500\", \"bytes\":" |
| 39 | + + " \"4085\"}"); |
| 40 | + client().performRequest(request2); |
| 41 | + Request request3 = |
| 42 | + new Request("PUT", "/" + TestsConstants.TEST_INDEX_WEBLOGS + "/_doc/9?refresh=true"); |
| 43 | + request3.setJsonEntity( |
| 44 | + "{\"host\": \"::3\", \"method\": \"GET\", \"url\": \"/shuttle/countdown/countdown.html\"," |
| 45 | + + " \"response\": \"403\", \"bytes\": \"3985\"}"); |
| 46 | + client().performRequest(request3); |
| 47 | + Request request4 = |
| 48 | + new Request("PUT", "/" + TestsConstants.TEST_INDEX_WEBLOGS + "/_doc/10?refresh=true"); |
| 49 | + request4.setJsonEntity( |
| 50 | + "{\"host\": \"1.2.3.5\", \"method\": \"GET\", \"url\": \"/history/voyager2/\"," |
| 51 | + + " \"response\": null, \"bytes\": \"4321\"}"); |
| 52 | + client().performRequest(request4); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testCaseWhenWithCast() { |
| 57 | + JSONObject actual = |
| 58 | + executeQuery( |
| 59 | + String.format( |
| 60 | + """ |
| 61 | + source=%s |
| 62 | + | eval status = |
| 63 | + case( |
| 64 | + cast(response as int) >= 200 AND cast(response as int) < 300, "Success", |
| 65 | + cast(response as int) >= 300 AND cast(response as int) < 400, "Redirection", |
| 66 | + cast(response as int) >= 400 AND cast(response as int) < 500, "Client Error", |
| 67 | + cast(response as int) >= 500 AND cast(response as int) < 600, "Server Error" |
| 68 | + else concat("Incorrect HTTP status code for", url)) |
| 69 | + | where status != "Success" |
| 70 | + """, |
| 71 | + TEST_INDEX_WEBLOGS)); |
| 72 | + verifySchema( |
| 73 | + actual, |
| 74 | + schema("host", "ip"), |
| 75 | + schema("method", "string"), |
| 76 | + schema("url", "string"), |
| 77 | + schema("response", "string"), |
| 78 | + schema("bytes", "string"), |
| 79 | + schema("status", "string")); |
| 80 | + verifyDataRows( |
| 81 | + actual, |
| 82 | + rows("::1", "GET", "6245", "301", "/history/apollo/", "Redirection"), |
| 83 | + rows( |
| 84 | + "0.0.0.2", |
| 85 | + "GET", |
| 86 | + "4085", |
| 87 | + "500", |
| 88 | + "/shuttle/missions/sts-73/mission-sts-73.html", |
| 89 | + "Server Error"), |
| 90 | + rows("::3", "GET", "3985", "403", "/shuttle/countdown/countdown.html", "Client Error"), |
| 91 | + rows( |
| 92 | + "1.2.3.5", |
| 93 | + "GET", |
| 94 | + "4321", |
| 95 | + null, |
| 96 | + "/history/voyager2/", |
| 97 | + "Incorrect HTTP status code for/history/voyager2/")); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void testCaseWhenNoElse() { |
| 102 | + JSONObject actual = |
| 103 | + executeQuery( |
| 104 | + String.format( |
| 105 | + """ |
| 106 | + source=%s |
| 107 | + | eval status = |
| 108 | + case( |
| 109 | + cast(response as int) >= 200 AND cast(response as int) < 300, "Success", |
| 110 | + cast(response as int) >= 300 AND cast(response as int) < 400, "Redirection", |
| 111 | + cast(response as int) >= 400 AND cast(response as int) < 500, "Client Error", |
| 112 | + cast(response as int) >= 500 AND cast(response as int) < 600, "Server Error") |
| 113 | + | where isnull(status) OR status != "Success" |
| 114 | + """, |
| 115 | + TEST_INDEX_WEBLOGS)); |
| 116 | + verifySchema( |
| 117 | + actual, |
| 118 | + schema("host", "ip"), |
| 119 | + schema("method", "string"), |
| 120 | + schema("url", "string"), |
| 121 | + schema("response", "string"), |
| 122 | + schema("bytes", "string"), |
| 123 | + schema("status", "string")); |
| 124 | + verifyDataRows( |
| 125 | + actual, |
| 126 | + rows("::1", "GET", "6245", "301", "/history/apollo/", "Redirection"), |
| 127 | + rows( |
| 128 | + "0.0.0.2", |
| 129 | + "GET", |
| 130 | + "4085", |
| 131 | + "500", |
| 132 | + "/shuttle/missions/sts-73/mission-sts-73.html", |
| 133 | + "Server Error"), |
| 134 | + rows("::3", "GET", "3985", "403", "/shuttle/countdown/countdown.html", "Client Error"), |
| 135 | + rows("1.2.3.5", "GET", "4321", null, "/history/voyager2/", null)); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + public void testCaseWhenWithIn() { |
| 140 | + JSONObject actual = |
| 141 | + executeQuery( |
| 142 | + String.format( |
| 143 | + """ |
| 144 | + source=%s |
| 145 | + | eval status = |
| 146 | + case( |
| 147 | + response in ('200'), "Success", |
| 148 | + response in ('300', '301'), "Redirection", |
| 149 | + response in ('400', '403'), "Client Error", |
| 150 | + response in ('500', '505'), "Server Error" |
| 151 | + else concat("Incorrect HTTP status code for", url)) |
| 152 | + | where status != "Success" |
| 153 | + """, |
| 154 | + TEST_INDEX_WEBLOGS)); |
| 155 | + verifySchema( |
| 156 | + actual, |
| 157 | + schema("host", "ip"), |
| 158 | + schema("method", "string"), |
| 159 | + schema("url", "string"), |
| 160 | + schema("response", "string"), |
| 161 | + schema("bytes", "string"), |
| 162 | + schema("status", "string")); |
| 163 | + verifyDataRows( |
| 164 | + actual, |
| 165 | + rows("::1", "GET", "6245", "301", "/history/apollo/", "Redirection"), |
| 166 | + rows( |
| 167 | + "0.0.0.2", |
| 168 | + "GET", |
| 169 | + "4085", |
| 170 | + "500", |
| 171 | + "/shuttle/missions/sts-73/mission-sts-73.html", |
| 172 | + "Server Error"), |
| 173 | + rows("::3", "GET", "3985", "403", "/shuttle/countdown/countdown.html", "Client Error"), |
| 174 | + rows( |
| 175 | + "1.2.3.5", |
| 176 | + "GET", |
| 177 | + "4321", |
| 178 | + null, |
| 179 | + "/history/voyager2/", |
| 180 | + "Incorrect HTTP status code for/history/voyager2/")); |
| 181 | + } |
| 182 | + |
| 183 | + @Test |
| 184 | + public void testCaseWhenInFilter() { |
| 185 | + JSONObject actual = |
| 186 | + executeQuery( |
| 187 | + String.format( |
| 188 | + """ |
| 189 | + source=%s |
| 190 | + | where not true = |
| 191 | + case( |
| 192 | + response in ('200'), true, |
| 193 | + response in ('300', '301'), false, |
| 194 | + response in ('400', '403'), false, |
| 195 | + response in ('500', '505'), false |
| 196 | + else false) |
| 197 | + """, |
| 198 | + TEST_INDEX_WEBLOGS)); |
| 199 | + verifySchema( |
| 200 | + actual, |
| 201 | + schema("host", "ip"), |
| 202 | + schema("method", "string"), |
| 203 | + schema("url", "string"), |
| 204 | + schema("response", "string"), |
| 205 | + schema("bytes", "string")); |
| 206 | + verifyDataRows( |
| 207 | + actual, |
| 208 | + rows("::1", "GET", "6245", "301", "/history/apollo/"), |
| 209 | + rows("0.0.0.2", "GET", "4085", "500", "/shuttle/missions/sts-73/mission-sts-73.html"), |
| 210 | + rows("::3", "GET", "3985", "403", "/shuttle/countdown/countdown.html"), |
| 211 | + rows("1.2.3.5", "GET", "4321", null, "/history/voyager2/")); |
| 212 | + } |
| 213 | + |
| 214 | + @Test |
| 215 | + public void testCaseWhenInSubquery() { |
| 216 | + JSONObject actual = |
| 217 | + executeQuery( |
| 218 | + String.format( |
| 219 | + """ |
| 220 | + source=%s |
| 221 | + | where response in [ |
| 222 | + source = %s |
| 223 | + | eval new_response = case( |
| 224 | + response in ('200'), "201", |
| 225 | + response in ('300', '301'), "301", |
| 226 | + response in ('400', '403'), "403", |
| 227 | + response in ('500', '505'), "500" |
| 228 | + else concat("Incorrect HTTP status code for", url)) |
| 229 | + | fields new_response |
| 230 | + ] |
| 231 | + """, |
| 232 | + TEST_INDEX_WEBLOGS, TEST_INDEX_WEBLOGS)); |
| 233 | + verifySchema( |
| 234 | + actual, |
| 235 | + schema("host", "ip"), |
| 236 | + schema("method", "string"), |
| 237 | + schema("url", "string"), |
| 238 | + schema("response", "string"), |
| 239 | + schema("bytes", "string")); |
| 240 | + verifyDataRows( |
| 241 | + actual, |
| 242 | + rows("::1", "GET", "6245", "301", "/history/apollo/"), |
| 243 | + rows("0.0.0.2", "GET", "4085", "500", "/shuttle/missions/sts-73/mission-sts-73.html"), |
| 244 | + rows("::3", "GET", "3985", "403", "/shuttle/countdown/countdown.html")); |
| 245 | + } |
| 246 | +} |
0 commit comments