Skip to content

Commit 617cbed

Browse files
committed
Extract V2 engine delegation into delegateToV2Engine method
Restore original logging (query anonymization, explain fallback) that was lost when the fallback logic was inlined into the analytics router lambda. Signed-off-by: Kai Huang <ahkcs@amazon.com>
1 parent aacb418 commit 617cbed

1 file changed

Lines changed: 40 additions & 19 deletions

File tree

legacy/src/main/java/org/opensearch/sql/legacy/plugin/RestSqlAction.java

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import org.opensearch.sql.legacy.request.SqlRequestParam;
6262
import org.opensearch.sql.legacy.rewriter.matchtoterm.VerificationException;
6363
import org.opensearch.sql.legacy.utils.JsonPrettyFormatter;
64+
import org.opensearch.sql.legacy.utils.QueryDataAnonymizer;
6465
import org.opensearch.sql.sql.domain.SQLQueryRequest;
6566
import org.opensearch.transport.client.Client;
6667
import org.opensearch.transport.client.node.NodeClient;
@@ -158,32 +159,52 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli
158159
final SQLQueryRequest finalRequest = newSqlRequest;
159160
return channel -> {
160161
if (!analyticsRouter.apply(finalRequest, channel)) {
161-
// Not an analytics query — delegate to normal SQL engine
162-
try {
163-
newSqlQueryHandler
164-
.prepareRequest(
165-
finalRequest,
166-
(ch, ex) -> {
167-
try {
168-
Format fmt = SqlRequestParam.getFormat(request.params());
169-
QueryAction qa = explainRequest(client, sqlRequest, fmt);
170-
executeSqlRequest(request, qa, client, ch);
171-
} catch (Exception e) {
172-
handleException(ch, e);
173-
}
174-
},
175-
this::handleException)
176-
.accept(channel);
177-
} catch (Exception e) {
178-
handleException(channel, e);
179-
}
162+
delegateToV2Engine(request, client, sqlRequest, finalRequest, format, channel);
180163
}
181164
};
182165
} catch (Exception e) {
183166
return channel -> handleException(channel, e);
184167
}
185168
}
186169

170+
/** Delegate a SQL query to the V2 engine with legacy fallback. */
171+
private void delegateToV2Engine(
172+
RestRequest request,
173+
NodeClient client,
174+
SqlRequest sqlRequest,
175+
SQLQueryRequest sqlQueryRequest,
176+
Format format,
177+
RestChannel channel) {
178+
try {
179+
newSqlQueryHandler
180+
.prepareRequest(
181+
sqlQueryRequest,
182+
(restChannel, exception) -> {
183+
try {
184+
if (sqlQueryRequest.isExplainRequest()) {
185+
LOG.info(
186+
"Request is falling back to old SQL engine due to: "
187+
+ exception.getMessage());
188+
}
189+
LOG.info(
190+
"[{}] Request {} is not supported and falling back to old SQL engine",
191+
QueryContext.getRequestId(),
192+
sqlQueryRequest);
193+
LOG.info(
194+
"Request Query: {}", QueryDataAnonymizer.anonymizeData(sqlRequest.getSql()));
195+
QueryAction queryAction = explainRequest(client, sqlRequest, format);
196+
executeSqlRequest(request, queryAction, client, restChannel);
197+
} catch (Exception e) {
198+
handleException(restChannel, e);
199+
}
200+
},
201+
this::handleException)
202+
.accept(channel);
203+
} catch (Exception e) {
204+
handleException(channel, e);
205+
}
206+
}
207+
187208
private void handleException(RestChannel restChannel, Exception exception) {
188209
logAndPublishMetrics(exception);
189210
if (exception instanceof OpenSearchException) {

0 commit comments

Comments
 (0)