-
Notifications
You must be signed in to change notification settings - Fork 1k
PHOENIX-7919 Support EXPLAIN FORMAT JSON #2527
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+197
−5
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
phoenix-core-client/src/main/java/org/apache/phoenix/compile/ExplainJsonRenderer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.phoenix.compile; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonInclude; | ||
| import com.fasterxml.jackson.core.JsonProcessingException; | ||
| import com.fasterxml.jackson.core.util.DefaultIndenter; | ||
| import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.fasterxml.jackson.databind.ObjectWriter; | ||
| import com.fasterxml.jackson.databind.SerializationFeature; | ||
| import java.sql.SQLException; | ||
|
|
||
| /** | ||
| * Serializes an {@link ExplainPlanAttributes} tree to a JSON document for the | ||
| * {@code EXPLAIN (FORMAT JSON) <stmt>} statement. | ||
| * <p> | ||
| * The output is pretty-printed with two space indentation for both objects and arrays. | ||
| * <p> | ||
| * The JSON layout tracks the Java field names and structure of {@link ExplainPlanAttributes}. It is | ||
| * deliberately not a stable contract and carries no version field. It is an opt-in view onto an | ||
| * internal structure, useful for tooling and assertions. | ||
| * <p> | ||
| * This class intentionally does not reuse the shared {@link org.apache.phoenix.util.JacksonUtil} | ||
| * mapper so that the general-purpose mapper configuration can change without affecting the EXPLAIN | ||
| * JSON contract. | ||
| */ | ||
| public final class ExplainJsonRenderer { | ||
|
|
||
| private static final ObjectWriter WRITER = buildWriter(); | ||
|
|
||
| private static ObjectWriter buildWriter() { | ||
| ObjectMapper mapper = new ObjectMapper(); | ||
| // Emit every field, with an explicit null for any unset value, so the JSON view is a faithful | ||
| // projection of the attributes tree. | ||
| mapper.setSerializationInclusion(JsonInclude.Include.ALWAYS); | ||
| mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); | ||
| mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); | ||
| // Jackson's stock DefaultPrettyPrinter indents object fields but uses a single space | ||
| // FixedSpaceIndenter for array elements. Set the same two space indenter on both so nested | ||
| // objects and array elements each start on their own indented line. | ||
| DefaultIndenter indenter = new DefaultIndenter(" ", "\n"); | ||
| DefaultPrettyPrinter printer = | ||
| new DefaultPrettyPrinter().withObjectIndenter(indenter).withArrayIndenter(indenter); | ||
| return mapper.writer(printer); | ||
| } | ||
|
|
||
| private ExplainJsonRenderer() { | ||
| } | ||
|
|
||
| /** | ||
| * Serialize the given attributes to a pretty-printed JSON document. | ||
| * @param attributes the plan attributes to serialize | ||
| * @return the JSON document | ||
| * @throws SQLException if serialization fails | ||
| */ | ||
| public static String render(ExplainPlanAttributes attributes) throws SQLException { | ||
| try { | ||
| return WRITER.writeValueAsString(attributes); | ||
| } catch (JsonProcessingException e) { | ||
| throw new SQLException("Failed to serialize EXPLAIN attributes as JSON", e); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
phoenix-core/src/test/java/org/apache/phoenix/query/explain/ExplainJsonOutputTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.phoenix.query.explain; | ||
|
|
||
| import static org.apache.phoenix.util.TestUtil.TEST_PROPERTIES; | ||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertFalse; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import java.sql.Connection; | ||
| import java.sql.DriverManager; | ||
| import java.sql.ResultSet; | ||
| import java.sql.Statement; | ||
| import java.util.Properties; | ||
| import org.apache.phoenix.compile.ExplainPlan; | ||
| import org.apache.phoenix.query.BaseConnectionlessQueryTest; | ||
| import org.apache.phoenix.util.PropertiesUtil; | ||
| import org.junit.BeforeClass; | ||
| import org.junit.Test; | ||
|
|
||
| /** | ||
| * End-to-end tests for {@code EXPLAIN (FORMAT JSON) <stmt>}. Exercises the | ||
| * {@code Statement.executeQuery} ResultSet path and confirms the single emitted row carries a | ||
| * pretty-printed JSON document that matches the in process {@code ExplainPlanAttributes} tree for | ||
| * the same query. | ||
|
apurtell marked this conversation as resolved.
|
||
| */ | ||
| public class ExplainJsonOutputTest extends BaseConnectionlessQueryTest { | ||
|
|
||
| private static final String QUERY = "SELECT a_string, b_string FROM atable" | ||
| + " WHERE organization_id = '00D000000000001' AND entity_id = '00E00000000001'" | ||
| + " AND x_integer = 2 AND a_integer < 5"; | ||
|
|
||
| private static ExplainOracle oracle; | ||
|
|
||
| @BeforeClass | ||
| public static synchronized void setUpOracle() throws Exception { | ||
| oracle = new ExplainOracle(); | ||
| } | ||
|
|
||
| private static Properties defaultProps() { | ||
| return PropertiesUtil.deepCopy(TEST_PROPERTIES); | ||
| } | ||
|
|
||
| /** | ||
| * Read the single VARCHAR cell of an {@code EXPLAIN (FORMAT JSON)} result set, asserting that | ||
| * exactly one row is returned. | ||
|
apurtell marked this conversation as resolved.
|
||
| */ | ||
| private static String readSingleRow(Connection conn, String explainSql) throws Exception { | ||
| try (Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(explainSql)) { | ||
| assertTrue("expected at least one row", rs.next()); | ||
| String cell = rs.getString(1); | ||
| assertFalse("expected exactly one row", rs.next()); | ||
| return cell; | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testFormatJsonMatchesInProcessAttributes() throws Exception { | ||
| try (Connection conn = DriverManager.getConnection(getUrl(), defaultProps())) { | ||
| String json = readSingleRow(conn, "EXPLAIN (FORMAT JSON) " + QUERY); | ||
| // The emitted document is pretty-printed. | ||
| assertTrue("expected pretty-printed JSON with newlines", json.contains("\n")); | ||
| assertTrue("expected two-space indentation", json.contains("\n \"")); | ||
| // The e2e ResultSet path must match the in process attributes path after normalization. | ||
| JsonNode actual = oracle.mapper().readTree(json); | ||
| new ExplainJsonNormalizer().normalize(actual); | ||
| ExplainPlan plan = ExplainPlanTestUtil.getExplainPlan(conn, QUERY); | ||
| JsonNode expected = oracle.serializeNormalized(plan.getPlanStepsAsAttributes()); | ||
| assertEquals(expected, actual); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testFormatJsonWithRegions() throws Exception { | ||
| try (Connection conn = DriverManager.getConnection(getUrl(), defaultProps())) { | ||
| String json = readSingleRow(conn, "EXPLAIN (REGIONS, FORMAT JSON) " + QUERY); | ||
| assertTrue("expected pretty-printed JSON with newlines", json.contains("\n")); | ||
| // This case confirms the (REGIONS, FORMAT JSON) combination produces a single well formed | ||
| // JSON row. | ||
| JsonNode actual = oracle.mapper().readTree(json); | ||
| new ExplainJsonNormalizer().normalize(actual); | ||
| ExplainPlan plan = ExplainPlanTestUtil.getExplainPlan(conn, QUERY); | ||
| JsonNode expected = oracle.serializeNormalized(plan.getPlanStepsAsAttributes()); | ||
| assertEquals(expected, actual); | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.