difin commented on code in PR #6421:
URL: https://github.com/apache/hive/pull/6421#discussion_r3070064755
##########
beeline/src/java/org/apache/hive/beeline/Commands.java:
##########
@@ -1183,6 +1183,141 @@ public boolean sh(String line) {
}
}
+ public boolean nlsql(String line) {
+ if (line == null || line.length() == 0) {
+ return false;
+ }
+
+ if (!line.startsWith("nlsql")) {
+ return false;
+ }
+
+ String nlQuery = line.substring("nlsql".length()).trim();
+ if (nlQuery.isEmpty()) {
+ return beeLine.error("Usage: !nlsql <natural language query>");
+ }
+
+ // Must be connected
+ if (beeLine.getDatabaseConnection() == null ||
beeLine.getDatabaseConnection().getUrl() == null) {
+ return beeLine.error("Not connected. Use !connect first.");
+ }
+
+ // Locate the Python script
+ String hiveHome = System.getenv("HIVE_HOME");
+ String scriptPath;
+ if (hiveHome != null) {
+ scriptPath = hiveHome + File.separator + "scripts" + File.separator +
+ "nlsql" + File.separator + "nlsql_agent.py";
+ } else {
+ scriptPath = "scripts" + File.separator + "nlsql" + File.separator +
+ "nlsql_agent.py";
+ }
+
+ if (!new File(scriptPath).exists()) {
+ return beeLine.error("nlsql script not found at: " + scriptPath +
+ ". Set HIVE_HOME or ensure the script exists.");
+ }
+
+ // ANSI colors
+ String RED = "\u001B[31m";
+ String CYAN = "\u001B[36m";
+ String BOLD = "\u001B[1m";
+ String DIM = "\u001B[2m";
+ String RESET = "\u001B[0m";
+
+ beeLine.output(DIM + "Generating SQL for: " + RESET + CYAN + nlQuery +
RESET);
+
+ try {
+ // Get current database name
+ String database = "default";
+ try {
+ database = beeLine.getDatabaseConnection().getConnection().getSchema();
+ if (database == null || database.isEmpty()) {
+ database = "default";
+ }
+ } catch (Exception e) {
+ // ignore, use default
+ }
+
+ // HMS REST Catalog URL (configurable via METASTORE_REST_URL env var)
+ String metastoreUrl = System.getenv("METASTORE_REST_URL");
+ if (metastoreUrl == null || metastoreUrl.isEmpty()) {
+ metastoreUrl = "http://localhost:9001/iceberg";
+ }
+
+ ProcessBuilder pb = new ProcessBuilder(
+ "python3", scriptPath,
+ "--query", nlQuery,
+ "--database", database,
+ "--metastore-url", metastoreUrl
+ );
+ // Pass through environment variables for LLM configuration
+ Map<String, String> env = pb.environment();
+ String[] envKeys = {"ANTHROPIC_BASE_URL", "ANTHROPIC_AUTH_TOKEN",
+ "ANTHROPIC_API_KEY", "ANTHROPIC_MODEL", "METASTORE_REST_URL",
+ "MCP_SERVER_URL"};
+ for (String key : envKeys) {
+ String val = System.getenv(key);
+ if (val != null) {
+ env.put(key, val);
+ }
+ }
+
+ pb.redirectErrorStream(false);
+ Process process = pb.start();
+ process.getOutputStream().close();
+
+ // Read stdout (the generated SQL)
+ StringBuilder sqlBuilder = new StringBuilder();
+ try (BufferedReader reader = new BufferedReader(
+ new InputStreamReader(process.getInputStream()))) {
+ String outputLine;
+ while ((outputLine = reader.readLine()) != null) {
+ if (sqlBuilder.length() > 0) {
+ sqlBuilder.append("\n");
+ }
+ sqlBuilder.append(outputLine);
+ }
+ }
+
+ // Read stderr (errors/warnings)
+ StringBuilder errBuilder = new StringBuilder();
+ try (BufferedReader reader = new BufferedReader(
+ new InputStreamReader(process.getErrorStream()))) {
+ String errLine;
+ while ((errLine = reader.readLine()) != null) {
+ errBuilder.append(errLine).append("\n");
+ }
+ }
Review Comment:
These lines are identical to lines 1270-1281, should we have a helper method
to reduce code duplication?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]