|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.sql.sql; |
| 7 | + |
| 8 | +import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_DATE_TIME; |
| 9 | +import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_DATE_TIME_NESTED; |
| 10 | + |
| 11 | +import java.io.IOException; |
| 12 | +import java.util.Locale; |
| 13 | +import org.json.JSONArray; |
| 14 | +import org.json.JSONObject; |
| 15 | +import org.junit.Assert; |
| 16 | +import org.junit.Ignore; |
| 17 | +import org.junit.Test; |
| 18 | +import org.opensearch.sql.legacy.SQLIntegTestCase; |
| 19 | + |
| 20 | +public class ComplexTimestampQueryIT extends SQLIntegTestCase { |
| 21 | + @Override |
| 22 | + protected void init() throws Exception { |
| 23 | + loadIndex(SQLIntegTestCase.Index.DATETIME); |
| 24 | + loadIndex(SQLIntegTestCase.Index.DATETIME_NESTED); |
| 25 | + } |
| 26 | + |
| 27 | + /** See: <a href="https://github.com/opensearch-project/sql/issues/3159">3159</a> */ |
| 28 | + @Test |
| 29 | + public void joinWithTimestampFieldsSchema() throws IOException { |
| 30 | + String query = |
| 31 | + String.format( |
| 32 | + Locale.ROOT, |
| 33 | + "SELECT one.login_time, two.login_time " |
| 34 | + + "FROM %s AS one JOIN %s AS two " |
| 35 | + + "ON one._id = two._id", |
| 36 | + TEST_INDEX_DATE_TIME, |
| 37 | + TEST_INDEX_DATE_TIME); |
| 38 | + |
| 39 | + JSONObject result = executeQuery(query); |
| 40 | + JSONArray schema = result.getJSONArray("schema"); |
| 41 | + |
| 42 | + Assert.assertFalse(schema.isEmpty()); |
| 43 | + for (int i = 0; i < schema.length(); i++) { |
| 44 | + JSONObject column = schema.getJSONObject(i); |
| 45 | + Assert.assertEquals("timestamp", column.getString("type")); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /** Control for joinWithTimestampFieldsSchema */ |
| 50 | + @Test |
| 51 | + public void nonJoinTimestampFieldsSchema() throws IOException { |
| 52 | + String query = |
| 53 | + String.format( |
| 54 | + Locale.ROOT, "SELECT one.login_time " + "FROM %s AS one", TEST_INDEX_DATE_TIME); |
| 55 | + |
| 56 | + JSONObject result = executeQuery(query); |
| 57 | + JSONArray schema = result.getJSONArray("schema"); |
| 58 | + |
| 59 | + Assert.assertFalse(schema.isEmpty()); |
| 60 | + for (int i = 0; i < schema.length(); i++) { |
| 61 | + JSONObject column = schema.getJSONObject(i); |
| 62 | + Assert.assertEquals("timestamp", column.getString("type")); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /** See: <a href="https://github.com/opensearch-project/sql/issues/3204">3204</a> */ |
| 67 | + // TODO currently out of scope due to V1/V2 engine feature mismatch. Should be fixed with Calcite. |
| 68 | + @Test |
| 69 | + @Ignore |
| 70 | + public void joinTimestampComparison() throws IOException { |
| 71 | + String query = |
| 72 | + String.format( |
| 73 | + Locale.ROOT, |
| 74 | + "SELECT one.login_time, two.login_time " |
| 75 | + + "FROM %s AS one JOIN %s AS two " |
| 76 | + + "ON one._id = two._id " |
| 77 | + + "WHERE one.login_time > timestamp('2018-05-07 00:00:00')", |
| 78 | + TEST_INDEX_DATE_TIME, |
| 79 | + TEST_INDEX_DATE_TIME); |
| 80 | + |
| 81 | + JSONObject result = executeQuery(query); |
| 82 | + Assert.assertEquals(1, result.getJSONArray("datarows").length()); |
| 83 | + } |
| 84 | + |
| 85 | + /** Control for joinTimestampComparison */ |
| 86 | + @Test |
| 87 | + public void nonJoinTimestampComparison() throws IOException { |
| 88 | + String query = |
| 89 | + String.format( |
| 90 | + Locale.ROOT, |
| 91 | + "SELECT login_time " |
| 92 | + + "FROM %s " |
| 93 | + + "WHERE login_time > timestamp('2018-05-07 00:00:00')", |
| 94 | + TEST_INDEX_DATE_TIME); |
| 95 | + |
| 96 | + JSONObject result = executeQuery(query); |
| 97 | + System.err.println(result.getJSONArray("datarows").toString()); |
| 98 | + Assert.assertEquals(1, result.getJSONArray("datarows").length()); |
| 99 | + } |
| 100 | + |
| 101 | + /** See: <a href="https://github.com/opensearch-project/sql/issues/1545">1545</a> */ |
| 102 | + @Test |
| 103 | + public void selectDatetimeWithNested() throws IOException { |
| 104 | + String query = |
| 105 | + String.format( |
| 106 | + Locale.ROOT, |
| 107 | + "SELECT tab.login_time " + "FROM %s AS tab, tab.projects AS pro", |
| 108 | + TEST_INDEX_DATE_TIME_NESTED); |
| 109 | + |
| 110 | + JSONObject result = executeQuery(query); |
| 111 | + JSONArray schema = result.getJSONArray("schema"); |
| 112 | + |
| 113 | + Assert.assertFalse(schema.isEmpty()); |
| 114 | + for (int i = 0; i < schema.length(); i++) { |
| 115 | + JSONObject column = schema.getJSONObject(i); |
| 116 | + Assert.assertEquals("timestamp", column.getString("type")); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + /** Control for selectDatetimeWithNested */ |
| 121 | + @Test |
| 122 | + public void selectDatetimeWithoutNested() throws IOException { |
| 123 | + String query = |
| 124 | + String.format( |
| 125 | + Locale.ROOT, "SELECT tab.login_time " + "FROM %s AS tab", TEST_INDEX_DATE_TIME_NESTED); |
| 126 | + |
| 127 | + JSONObject result = executeQuery(query); |
| 128 | + JSONArray schema = result.getJSONArray("schema"); |
| 129 | + |
| 130 | + Assert.assertFalse(schema.isEmpty()); |
| 131 | + for (int i = 0; i < schema.length(); i++) { |
| 132 | + JSONObject column = schema.getJSONObject(i); |
| 133 | + Assert.assertEquals("timestamp", column.getString("type")); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments