|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.phoenix.query.explain; |
| 19 | + |
| 20 | +import static org.apache.phoenix.util.TestUtil.TEST_PROPERTIES; |
| 21 | +import static org.junit.Assert.assertEquals; |
| 22 | +import static org.junit.Assert.assertFalse; |
| 23 | +import static org.junit.Assert.assertTrue; |
| 24 | + |
| 25 | +import com.fasterxml.jackson.databind.JsonNode; |
| 26 | +import java.sql.Connection; |
| 27 | +import java.sql.DriverManager; |
| 28 | +import java.sql.ResultSet; |
| 29 | +import java.sql.Statement; |
| 30 | +import java.util.Properties; |
| 31 | +import org.apache.phoenix.compile.ExplainPlan; |
| 32 | +import org.apache.phoenix.query.BaseConnectionlessQueryTest; |
| 33 | +import org.apache.phoenix.util.PropertiesUtil; |
| 34 | +import org.junit.BeforeClass; |
| 35 | +import org.junit.Test; |
| 36 | + |
| 37 | +/** |
| 38 | + * End-to-end tests for {@code EXPLAIN (FORMAT JSON) <stmt>}. Exercises the |
| 39 | + * {@code Statement.executeQuery} ResultSet path and confirms the single emitted row carries a |
| 40 | + * pretty-printed JSON document that matches the in process {@code ExplainPlanAttributes} tree for |
| 41 | + * the same query. |
| 42 | + */ |
| 43 | +public class ExplainJsonOutputTest extends BaseConnectionlessQueryTest { |
| 44 | + |
| 45 | + private static final String QUERY = "SELECT a_string, b_string FROM atable" |
| 46 | + + " WHERE organization_id = '00D000000000001' AND entity_id = '00E00000000001'" |
| 47 | + + " AND x_integer = 2 AND a_integer < 5"; |
| 48 | + |
| 49 | + private static ExplainOracle oracle; |
| 50 | + |
| 51 | + @BeforeClass |
| 52 | + public static synchronized void setUpOracle() throws Exception { |
| 53 | + oracle = new ExplainOracle(); |
| 54 | + } |
| 55 | + |
| 56 | + private static Properties defaultProps() { |
| 57 | + return PropertiesUtil.deepCopy(TEST_PROPERTIES); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Read the single VARCHAR cell of an {@code EXPLAIN (FORMAT JSON)} result set, asserting that |
| 62 | + * exactly one row is returned. |
| 63 | + */ |
| 64 | + private static String readSingleRow(Connection conn, String explainSql) throws Exception { |
| 65 | + try (Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(explainSql)) { |
| 66 | + assertTrue("expected at least one row", rs.next()); |
| 67 | + String cell = rs.getString(1); |
| 68 | + assertFalse("expected exactly one row", rs.next()); |
| 69 | + return cell; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testFormatJsonMatchesInProcessAttributes() throws Exception { |
| 75 | + try (Connection conn = DriverManager.getConnection(getUrl(), defaultProps())) { |
| 76 | + String json = readSingleRow(conn, "EXPLAIN (FORMAT JSON) " + QUERY); |
| 77 | + // The emitted document is pretty-printed. |
| 78 | + assertTrue("expected pretty-printed JSON with newlines", json.contains("\n")); |
| 79 | + assertTrue("expected two-space indentation", json.contains("\n \"")); |
| 80 | + // The e2e ResultSet path must match the in process attributes path after normalization. |
| 81 | + JsonNode actual = oracle.mapper().readTree(json); |
| 82 | + new ExplainJsonNormalizer().normalize(actual); |
| 83 | + ExplainPlan plan = ExplainPlanTestUtil.getExplainPlan(conn, QUERY); |
| 84 | + JsonNode expected = oracle.serializeNormalized(plan.getPlanStepsAsAttributes()); |
| 85 | + assertEquals(expected, actual); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void testFormatJsonWithRegions() throws Exception { |
| 91 | + try (Connection conn = DriverManager.getConnection(getUrl(), defaultProps())) { |
| 92 | + String json = readSingleRow(conn, "EXPLAIN (REGIONS, FORMAT JSON) " + QUERY); |
| 93 | + assertTrue("expected pretty-printed JSON with newlines", json.contains("\n")); |
| 94 | + // This case confirms the (REGIONS, FORMAT JSON) combination produces a single well formed |
| 95 | + // JSON row. |
| 96 | + JsonNode actual = oracle.mapper().readTree(json); |
| 97 | + new ExplainJsonNormalizer().normalize(actual); |
| 98 | + ExplainPlan plan = ExplainPlanTestUtil.getExplainPlan(conn, QUERY); |
| 99 | + JsonNode expected = oracle.serializeNormalized(plan.getPlanStepsAsAttributes()); |
| 100 | + assertEquals(expected, actual); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments