This is an automated email from the ASF dual-hosted git repository.

dengzhhu653 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hive.git


The following commit(s) were added to refs/heads/master by this push:
     new b828b70e67f HIVE-30241: Beeline -f corrupts statements from script 
files (#6947)
b828b70e67f is described below

commit b828b70e67f47d23aa67a7c9cdb873e98ae1bc16
Author: dengzh <[email protected]>
AuthorDate: Wed Nov 18 08:46:18 2026 +0800

    HIVE-30241: Beeline -f corrupts statements from script files (#6947)
---
 .../src/java/org/apache/hive/beeline/BeeLine.java  |  13 ++-
 .../hive/beeline/TestBeeLineScriptTerminal.java    | 121 +++++++++++++++++++++
 2 files changed, 133 insertions(+), 1 deletion(-)

diff --git a/beeline/src/java/org/apache/hive/beeline/BeeLine.java 
b/beeline/src/java/org/apache/hive/beeline/BeeLine.java
index 65d9aeeb2f3..9ab50d4ceba 100644
--- a/beeline/src/java/org/apache/hive/beeline/BeeLine.java
+++ b/beeline/src/java/org/apache/hive/beeline/BeeLine.java
@@ -39,6 +39,7 @@
 import java.net.JarURLConnection;
 import java.net.URL;
 import java.net.URLConnection;
+import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
@@ -1543,7 +1544,17 @@ private InputStream prepareInputStream(InputStream 
inputStream) {
   protected Terminal buildTerminal(InputStream inputStream) throws IOException 
{
     Terminal terminal;
     if (inputStream != null) { // typically when there is a file script to 
read from
-      terminal = TerminalBuilder.builder().streams(inputStream, 
getErrorStream()).build();
+      // Do not use TerminalBuilder.builder().streams(...).build() with the 
default provider order: native
+      // providers (ffm/jni/jna) implement newTerminal() by opening a PTY and 
return PosixPtyTerminal, which
+      // corrupts or drops lines from large -f scripts. Force the exec 
provider so JLine uses a non-PTY
+      // stream terminal with TYPE_DUMB (ExecTerminalProvider.newTerminal); 
UTF-8 matches getCommands(File).
+      terminal = TerminalBuilder.builder()
+          .name("beeline")
+          .provider(TerminalBuilder.PROP_PROVIDER_EXEC)
+          .type(Terminal.TYPE_DUMB)
+          .streams(inputStream, getErrorStream())
+          .encoding(StandardCharsets.UTF_8)
+          .build();
     } else { // no input stream, normal operation: proper behavior needs a 
system terminal (which needs system streams)
       terminal = 
TerminalBuilder.builder().system(true).dumb(false).streams(System.in, 
System.err).build();
     }
diff --git 
a/beeline/src/test/org/apache/hive/beeline/TestBeeLineScriptTerminal.java 
b/beeline/src/test/org/apache/hive/beeline/TestBeeLineScriptTerminal.java
new file mode 100644
index 00000000000..295c6a62d3f
--- /dev/null
+++ b/beeline/src/test/org/apache/hive/beeline/TestBeeLineScriptTerminal.java
@@ -0,0 +1,121 @@
+/*
+ * 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.hive.beeline;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.jline.reader.LineReader;
+import org.jline.terminal.Terminal;
+import org.jline.terminal.TerminalBuilder;
+import org.jline.terminal.impl.AbstractPosixTerminal;
+import org.jline.terminal.spi.TerminalExt;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.RepeatedTest;
+
+/**
+ * Regression test for HIVE-30241.
+ *
+ */
+public class TestBeeLineScriptTerminal {
+
+  @RepeatedTest(6)
+  public void testScriptFileReaderPreservesLines() throws Exception {
+    List<String> expected = scriptLines();
+    byte[] script = scriptBytesWithoutTrailingNewline(expected);
+
+    try (CapturingBeeLine beeLine = new CapturingBeeLine()) {
+      ByteArrayOutputStream err = new ByteArrayOutputStream();
+      beeLine.setOutputStream(new PrintStream(new ByteArrayOutputStream(), 
true, StandardCharsets.UTF_8));
+      beeLine.setErrorStream(new PrintStream(err, true, 
StandardCharsets.UTF_8));
+      beeLine.getOpts().setScriptFile("hive-30241.sql");
+
+      LineReader reader = beeLine.getFileLineReader(new 
ByteArrayInputStream(script));
+      assertScriptTerminal(reader.getTerminal());
+      Assertions.assertEquals(0, beeLine.execute(reader, false));
+      Assertions.assertEquals(expected.size(), beeLine.lines.size());
+      for (int i = 0; i < expected.size(); i++) {
+        Assertions.assertEquals(expected.get(i), beeLine.lines.get(i), "line " 
+ i);
+      }
+    }
+  }
+
+  /**
+   * The script terminal must be the non-PTY exec terminal. These checks fail 
if
+   * {@code buildTerminal} goes back to {@code 
TerminalBuilder.builder().streams(...).build()}.
+   */
+  private void assertScriptTerminal(Terminal terminal) {
+    Assertions.assertFalse(terminal instanceof AbstractPosixTerminal,
+        "script input must not use a PTY terminal (HIVE-30241), got "
+            + terminal.getClass().getName());
+    Assertions.assertEquals(TerminalBuilder.PROP_PROVIDER_EXEC, ((TerminalExt) 
terminal).getProvider().name());
+    Assertions.assertEquals(Terminal.TYPE_DUMB, terminal.getType());
+    Assertions.assertEquals(StandardCharsets.UTF_8, terminal.encoding());
+    Assertions.assertEquals(StandardCharsets.UTF_8, terminal.inputEncoding());
+    Assertions.assertEquals("beeline", terminal.getName());
+  }
+
+  private List<String> scriptLines() {
+    List<String> lines = new ArrayList<>();
+    lines.add("SELECT '" + "x".repeat(10_000) + "' AS long_literal;");
+    for (int i = 0; i < 500; i++) {
+      lines.add("EXPLAIN SELECT name, count(*) AS c_5 FROM 
beeline_large_script_db.t_5 "
+          + "WHERE id >= 5 AND note LIKE '%--%' AND label = '中文' GROUP BY 
name;");
+      lines.add("EXPLAIN SELECT name, count(*) AS c_5 FROM 
beeline_large_script_db.t_5 "
+          + "WHERE id >= 5 AND note LIKE '%--%' AND label = '中文' GROUP BY 
name;");
+      lines.add("EXPLAIN SELECT name, count(*) AS c_5 FROM 
beeline_large_script_db.t_5 "
+          + "WHERE id >= 5 AND note LIKE '%--%' AND label = '中文' GROUP BY 
name;");
+    }
+    lines.add("SELECT 'final-without-newline' AS tail;");
+    return lines;
+  }
+
+  /** Join lines with '\n' and leave the last line without a newline, as a 
real script file can. */
+  private byte[] scriptBytesWithoutTrailingNewline(List<String> lines) {
+    StringBuilder script = new StringBuilder();
+    for (int i = 0; i < lines.size(); i++) {
+      if (i > 0) {
+        script.append('\n');
+      }
+      script.append(lines.get(i));
+    }
+    return script.toString().getBytes(StandardCharsets.UTF_8);
+  }
+
+  /**
+   * Subclass of {@link BeeLine}. Records each line {@code execute}
+   * reads instead of sending it to HiveServer2.
+   */
+  private static final class CapturingBeeLine extends BeeLine {
+    private final List<String> lines = new ArrayList<>();
+
+    @Override
+    boolean dispatch(String line) {
+      if (line == null) {
+        return super.dispatch(line);
+      }
+      lines.add(line);
+      return true;
+    }
+  }
+}

Reply via email to