|
| 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, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.pinot.broker.requesthandler; |
| 20 | + |
| 21 | +import com.fasterxml.jackson.databind.JsonNode; |
| 22 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.Iterator; |
| 25 | +import java.util.Map; |
| 26 | +import javax.annotation.Nullable; |
| 27 | +import javax.ws.rs.core.HttpHeaders; |
| 28 | +import org.apache.pinot.common.response.BrokerResponse; |
| 29 | +import org.apache.pinot.graph.planner.CypherToSqlTranslator; |
| 30 | +import org.apache.pinot.graph.spi.GraphSchemaConfig; |
| 31 | +import org.apache.pinot.spi.auth.broker.RequesterIdentity; |
| 32 | +import org.apache.pinot.spi.trace.RequestContext; |
| 33 | +import org.apache.pinot.spi.utils.CommonConstants.Broker.Request; |
| 34 | +import org.apache.pinot.spi.utils.JsonUtils; |
| 35 | +import org.slf4j.Logger; |
| 36 | +import org.slf4j.LoggerFactory; |
| 37 | + |
| 38 | + |
| 39 | +/** |
| 40 | + * Handles graph (Cypher) query requests by translating them to SQL and delegating |
| 41 | + * to the multi-stage query engine (MSE) broker request handler. |
| 42 | + * |
| 43 | + * <p>This handler is instantiated only when the {@code pinot.graph.enabled} feature flag |
| 44 | + * is set to {@code true}. It is thread-safe because it delegates all state to the |
| 45 | + * underlying {@link BrokerRequestHandler}. |
| 46 | + */ |
| 47 | +public class GraphRequestHandler { |
| 48 | + private static final Logger LOGGER = LoggerFactory.getLogger(GraphRequestHandler.class); |
| 49 | + |
| 50 | + private final BrokerRequestHandler _requestHandler; |
| 51 | + |
| 52 | + public GraphRequestHandler(BrokerRequestHandler requestHandler) { |
| 53 | + _requestHandler = requestHandler; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Handles a graph query request by translating Cypher to SQL and delegating to the MSE handler. |
| 58 | + * |
| 59 | + * @param cypherQuery the Cypher query string |
| 60 | + * @param graphSchemaJson the inline graph schema as a JSON node |
| 61 | + * @param requesterIdentity the identity of the requester, may be null |
| 62 | + * @param requestContext the request context for tracing |
| 63 | + * @param httpHeaders the HTTP headers from the original request, may be null |
| 64 | + * @return the broker response from executing the translated SQL query |
| 65 | + * @throws Exception if translation or query execution fails |
| 66 | + */ |
| 67 | + public BrokerResponse handleRequest(String cypherQuery, JsonNode graphSchemaJson, |
| 68 | + @Nullable RequesterIdentity requesterIdentity, RequestContext requestContext, |
| 69 | + @Nullable HttpHeaders httpHeaders) |
| 70 | + throws Exception { |
| 71 | + // Parse the graph schema from JSON |
| 72 | + GraphSchemaConfig schemaConfig = parseGraphSchema(graphSchemaJson); |
| 73 | + |
| 74 | + // Translate Cypher to SQL |
| 75 | + String sql; |
| 76 | + try { |
| 77 | + CypherToSqlTranslator translator = new CypherToSqlTranslator(schemaConfig); |
| 78 | + sql = translator.translate(cypherQuery); |
| 79 | + } catch (Exception e) { |
| 80 | + LOGGER.error("Failed to translate Cypher query to SQL: {}", cypherQuery, e); |
| 81 | + throw new IllegalArgumentException("Failed to translate Cypher query to SQL: " + e.getMessage(), e); |
| 82 | + } |
| 83 | + LOGGER.debug("Translated Cypher query [{}] to SQL [{}]", cypherQuery, sql); |
| 84 | + |
| 85 | + // Build a SQL request JSON and delegate to the request handler. |
| 86 | + // The translated SQL uses JOINs, which requires the multi-stage engine. |
| 87 | + ObjectNode sqlRequestJson = JsonUtils.newObjectNode(); |
| 88 | + sqlRequestJson.put(Request.SQL, sql); |
| 89 | + |
| 90 | + // Force multi-stage engine for graph queries since they require JOINs. |
| 91 | + // Query options are passed as a semicolon-delimited string. |
| 92 | + sqlRequestJson.put(Request.QUERY_OPTIONS, |
| 93 | + Request.QueryOptionKey.USE_MULTISTAGE_ENGINE + "=true"); |
| 94 | + |
| 95 | + return _requestHandler.handleRequest(sqlRequestJson, null, requesterIdentity, requestContext, |
| 96 | + httpHeaders); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Parses the inline graph schema JSON into a {@link GraphSchemaConfig}. |
| 101 | + * |
| 102 | + * <p>Expected JSON format:</p> |
| 103 | + * <pre> |
| 104 | + * { |
| 105 | + * "vertexLabels": { |
| 106 | + * "User": { |
| 107 | + * "tableName": "users", |
| 108 | + * "primaryKey": "id", |
| 109 | + * "properties": { "id": "id", "name": "user_name" } |
| 110 | + * } |
| 111 | + * }, |
| 112 | + * "edgeLabels": { |
| 113 | + * "FOLLOWS": { |
| 114 | + * "tableName": "follows", |
| 115 | + * "sourceVertexLabel": "User", |
| 116 | + * "sourceKey": "follower_id", |
| 117 | + * "targetVertexLabel": "User", |
| 118 | + * "targetKey": "followee_id", |
| 119 | + * "properties": {} |
| 120 | + * } |
| 121 | + * } |
| 122 | + * } |
| 123 | + * </pre> |
| 124 | + */ |
| 125 | + static GraphSchemaConfig parseGraphSchema(JsonNode schemaJson) { |
| 126 | + if (schemaJson == null) { |
| 127 | + throw new IllegalArgumentException("Graph schema JSON must not be null"); |
| 128 | + } |
| 129 | + |
| 130 | + // Parse vertex labels |
| 131 | + Map<String, GraphSchemaConfig.VertexLabel> vertexLabels = new HashMap<>(); |
| 132 | + JsonNode vertexLabelsNode = schemaJson.get("vertexLabels"); |
| 133 | + if (vertexLabelsNode != null && vertexLabelsNode.isObject()) { |
| 134 | + Iterator<Map.Entry<String, JsonNode>> fields = vertexLabelsNode.fields(); |
| 135 | + while (fields.hasNext()) { |
| 136 | + Map.Entry<String, JsonNode> entry = fields.next(); |
| 137 | + String labelName = entry.getKey(); |
| 138 | + JsonNode labelNode = entry.getValue(); |
| 139 | + |
| 140 | + String tableName = getRequiredField(labelNode, "tableName", "vertexLabels." + labelName); |
| 141 | + String primaryKey = getRequiredField(labelNode, "primaryKey", "vertexLabels." + labelName); |
| 142 | + Map<String, String> properties = parseStringMap(labelNode.get("properties")); |
| 143 | + |
| 144 | + vertexLabels.put(labelName, new GraphSchemaConfig.VertexLabel(tableName, primaryKey, properties)); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + // Parse edge labels |
| 149 | + Map<String, GraphSchemaConfig.EdgeLabel> edgeLabels = new HashMap<>(); |
| 150 | + JsonNode edgeLabelsNode = schemaJson.get("edgeLabels"); |
| 151 | + if (edgeLabelsNode != null && edgeLabelsNode.isObject()) { |
| 152 | + Iterator<Map.Entry<String, JsonNode>> fields = edgeLabelsNode.fields(); |
| 153 | + while (fields.hasNext()) { |
| 154 | + Map.Entry<String, JsonNode> entry = fields.next(); |
| 155 | + String labelName = entry.getKey(); |
| 156 | + JsonNode labelNode = entry.getValue(); |
| 157 | + |
| 158 | + String tableName = getRequiredField(labelNode, "tableName", "edgeLabels." + labelName); |
| 159 | + String sourceVertexLabel = getRequiredField(labelNode, "sourceVertexLabel", "edgeLabels." + labelName); |
| 160 | + String sourceKey = getRequiredField(labelNode, "sourceKey", "edgeLabels." + labelName); |
| 161 | + String targetVertexLabel = getRequiredField(labelNode, "targetVertexLabel", "edgeLabels." + labelName); |
| 162 | + String targetKey = getRequiredField(labelNode, "targetKey", "edgeLabels." + labelName); |
| 163 | + Map<String, String> properties = parseStringMap(labelNode.get("properties")); |
| 164 | + |
| 165 | + edgeLabels.put(labelName, new GraphSchemaConfig.EdgeLabel( |
| 166 | + tableName, sourceVertexLabel, sourceKey, targetVertexLabel, targetKey, properties)); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + if (vertexLabels.isEmpty()) { |
| 171 | + throw new IllegalArgumentException("Graph schema must define at least one vertex label"); |
| 172 | + } |
| 173 | + if (edgeLabels.isEmpty()) { |
| 174 | + throw new IllegalArgumentException("Graph schema must define at least one edge label"); |
| 175 | + } |
| 176 | + |
| 177 | + return new GraphSchemaConfig(vertexLabels, edgeLabels); |
| 178 | + } |
| 179 | + |
| 180 | + private static String getRequiredField(JsonNode node, String fieldName, String parentPath) { |
| 181 | + JsonNode field = node.get(fieldName); |
| 182 | + if (field == null || field.isNull()) { |
| 183 | + throw new IllegalArgumentException("Missing required field '" + fieldName + "' in " + parentPath); |
| 184 | + } |
| 185 | + return field.asText(); |
| 186 | + } |
| 187 | + |
| 188 | + private static Map<String, String> parseStringMap(JsonNode node) { |
| 189 | + Map<String, String> map = new HashMap<>(); |
| 190 | + if (node != null && node.isObject()) { |
| 191 | + Iterator<Map.Entry<String, JsonNode>> fields = node.fields(); |
| 192 | + while (fields.hasNext()) { |
| 193 | + Map.Entry<String, JsonNode> entry = fields.next(); |
| 194 | + map.put(entry.getKey(), entry.getValue().asText()); |
| 195 | + } |
| 196 | + } |
| 197 | + return map; |
| 198 | + } |
| 199 | +} |
0 commit comments