This is an automated email from the ASF dual-hosted git repository.
difin 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 f0f53bce640 HIVE-29296: Remove getPrimaryKeys codes from beeline
module (#6157)
f0f53bce640 is described below
commit f0f53bce64024ad29a2d47d045962409762f8639
Author: Butao Zhang <[email protected]>
AuthorDate: Fri Nov 7 22:50:15 2025 +0800
HIVE-29296: Remove getPrimaryKeys codes from beeline module (#6157)
---
beeline/src/java/org/apache/hive/beeline/Rows.java | 58 ----------
.../org/apache/hive/beeline/TableOutputFormat.java | 12 +-
.../apache/hive/beeline/TestTableOutputFormat.java | 122 ---------------------
3 files changed, 2 insertions(+), 190 deletions(-)
diff --git a/beeline/src/java/org/apache/hive/beeline/Rows.java
b/beeline/src/java/org/apache/hive/beeline/Rows.java
index 7926f31cf81..7572ea4560e 100644
--- a/beeline/src/java/org/apache/hive/beeline/Rows.java
+++ b/beeline/src/java/org/apache/hive/beeline/Rows.java
@@ -29,13 +29,9 @@
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
-import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
-import java.util.HashMap;
import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
import org.apache.hadoop.hive.common.cli.EscapeCRLFHelper;
@@ -46,8 +42,6 @@
abstract class Rows implements Iterator {
protected final BeeLine beeLine;
final ResultSetMetaData rsMeta;
- final boolean[] primaryKeyColumns;
- boolean isPrimaryKeyColumnsInitialized;
final NumberFormat numberFormat;
private boolean convertBinaryArrayToString;
private final String nullStr;
@@ -62,9 +56,6 @@ abstract class Rows implements Iterator {
numberFormat = new DecimalFormat(beeLine.getOpts().getNumberFormat());
}
this.convertBinaryArrayToString =
beeLine.getOpts().getConvertBinaryArrayToString();
-
- int count = this.rsMeta.getColumnCount();
- primaryKeyColumns = new boolean[count];
}
@Override
@@ -78,55 +69,6 @@ public void remove() {
*/
abstract void normalizeWidths();
- /**
- * Return whether the specified column (0-based index) is
- * a primary key. Since this method depends on whether the
- * JDBC driver property implements {@link ResultSetMetaData#getTableName}
(many do not), it
- * is not reliable for all databases.
- */
- boolean isPrimaryKeyCol(int col) {
- if (!isPrimaryKeyColumnsInitialized) {
- initializePrimaryKeyMetadata();
- }
-
- return primaryKeyColumns[col];
- }
-
- private void initializePrimaryKeyMetadata() {
- Map<String, List<String>> tablePrimaryKeys = new HashMap<>();
-
- try {
- for (int i = 0; i < primaryKeyColumns.length; i++) {
- String table = rsMeta.getTableName(i + 1);
- String column = rsMeta.getColumnName(i + 1);
-
- if (table == null || table.isEmpty() || column == null ||
column.isEmpty()) {
- continue;
- }
-
- if (!tablePrimaryKeys.containsKey(table)) {
- try (ResultSet pks =
beeLine.getDatabaseConnection().getDatabaseMetaData().getPrimaryKeys(
-
beeLine.getDatabaseConnection().getDatabaseMetaData().getConnection().getCatalog(),
null, table)) {
-
- List<String> pkNames = new ArrayList<>();
-
- while (pks.next()) {
- pkNames.add(pks.getString("COLUMN_NAME"));
- }
-
- tablePrimaryKeys.put(table, pkNames);
- }
- }
-
- primaryKeyColumns[i] = tablePrimaryKeys.get(table).contains(column);
- }
- } catch (SQLException e) {
- // Do nothing. We cannot decide if the given column is a primary key so
we keep it as false
- }
-
- isPrimaryKeyColumnsInitialized = true;
- }
-
class Row {
final String[] values;
final boolean isMeta;
diff --git a/beeline/src/java/org/apache/hive/beeline/TableOutputFormat.java
b/beeline/src/java/org/apache/hive/beeline/TableOutputFormat.java
index bd2a408e0d7..c5965b5ea17 100644
--- a/beeline/src/java/org/apache/hive/beeline/TableOutputFormat.java
+++ b/beeline/src/java/org/apache/hive/beeline/TableOutputFormat.java
@@ -122,18 +122,10 @@ ColorBuffer getOutputString(Rows rows, Rows.Row row,
String delim) {
if (row.isMeta) {
v = beeLine.getColorBuffer().center(row.values[i], row.sizes[i]);
- if (beeLine.getOpts().getColor() && rows.isPrimaryKeyCol(i)) {
- buf.cyan(v.getMono());
- } else {
- buf.bold(v.getMono());
- }
+ buf.bold(v.getMono());
} else {
v = beeLine.getColorBuffer().pad(row.values[i], row.sizes[i]);
- if (beeLine.getOpts().getColor() && rows.isPrimaryKeyCol(i)) {
- buf.cyan(v.getMono());
- } else {
- buf.append(v.getMono());
- }
+ buf.append(v.getMono());
}
}
diff --git
a/beeline/src/test/org/apache/hive/beeline/TestTableOutputFormat.java
b/beeline/src/test/org/apache/hive/beeline/TestTableOutputFormat.java
index eb8259f0644..cc1a4f891fa 100644
--- a/beeline/src/test/org/apache/hive/beeline/TestTableOutputFormat.java
+++ b/beeline/src/test/org/apache/hive/beeline/TestTableOutputFormat.java
@@ -21,8 +21,6 @@
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyInt;
-import java.sql.Connection;
-import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
@@ -31,9 +29,6 @@
import static org.junit.Assert.assertEquals;
import org.junit.Test;
-import static org.mockito.ArgumentMatchers.anyString;
-import static org.mockito.ArgumentMatchers.eq;
-import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.when;
import org.mockito.invocation.InvocationOnMock;
@@ -42,7 +37,6 @@
public class TestTableOutputFormat {
- private static final String CYAN = "\033[1;36m";
private static final String GREEN = "\033[1;32m";
private final String[][] mockRowData = {
@@ -53,12 +47,8 @@ public class TestTableOutputFormat {
};
private BeelineMock mockBeeline;
- private DatabaseConnection dbConnection;
- private Connection mockSqlConnection;
- private DatabaseMetaData mockDatabaseMetaData;
private ResultSet mockResultSet;
private ResultSetMetaData mockResultSetMetaData;
- private ResultSet mockMetadataResultSet;
private TestBufferedRows.MockRow mockRow;
/**
@@ -85,7 +75,6 @@ public final void testPrint() throws SQLException {
@Test
public void testColoringWithNoTableMetadata() throws SQLException {
setupMockData();
- when(mockResultSetMetaData.getTableName(anyInt())).thenReturn(null);
BufferedRows bfRows = new BufferedRows(mockBeeline, mockResultSet);
TableOutputFormat instance = new TableOutputFormat(mockBeeline);
@@ -97,27 +86,6 @@ public void testColoringWithNoTableMetadata() throws
SQLException {
assertColumnHasColor(allPrintedLines, 2, GREEN);
}
- /**
- * The primary key has one column. It should be CYAN. The second column
should be green
- */
- @Test
- public void testSingleColumnPrimaryKey() throws SQLException {
- setupMockData();
- mockBeeline.getOpts().setColor(true);
-
- BufferedRows bfRows = new BufferedRows(mockBeeline, mockResultSet);
- TableOutputFormat instance = new TableOutputFormat(mockBeeline);
-
- instance.print(bfRows);
-
- List<String> allPrintedLines = mockBeeline.getAllPrintedLines();
-
- // PK column is CYAN
- assertColumnHasColor(allPrintedLines, 1, CYAN);
- // non-PK column is green
- assertColumnHasColor(allPrintedLines, 2, GREEN);
- }
-
/**
* Default behavior: coloring is disabled
*/
@@ -136,77 +104,6 @@ public void testMonoTableOutputFormat() throws
SQLException {
}
}
- /**
- * No primary key column - all columns should be green
- */
- @Test
- public void testColoredWithoutPrimaryKey() throws SQLException {
- setupMockData();
- mockBeeline.getOpts().setColor(true);
- when(mockMetadataResultSet.next()).thenReturn(false);
-
- BufferedRows bfRows = new BufferedRows(mockBeeline, mockResultSet);
- TableOutputFormat instance = new TableOutputFormat(mockBeeline);
-
- instance.print(bfRows);
-
- List<String> allPrintedLines = mockBeeline.getAllPrintedLines();
-
- assertColumnHasColor(allPrintedLines, 1, GREEN);
- assertColumnHasColor(allPrintedLines, 2, GREEN);
- }
-
- /**
- * The output contains one single table. And both columns are part of the PK
- */
- @Test
- public void testColoredWithPrimaryKeyHasMultipleColumns() throws
SQLException {
- setupMockData();
- mockBeeline.getOpts().setColor(true);
-
when(mockMetadataResultSet.next()).thenReturn(true).thenReturn(true).thenReturn(false);
-
- BufferedRows bfRows = new BufferedRows(mockBeeline, mockResultSet);
- TableOutputFormat instance = new TableOutputFormat(mockBeeline);
-
- instance.print(bfRows);
-
- List<String> allPrintedLines = mockBeeline.getAllPrintedLines();
-
- // PK column are CYAN
- assertColumnHasColor(allPrintedLines, 1, CYAN);
- assertColumnHasColor(allPrintedLines, 2, CYAN);
- }
-
- /**
- * The output contains two tables. Both columns are PKs
- */
- @Test
- public void testColoredWithMultipleTablesWithPrimaryKeys() throws
SQLException {
- setupMockData();
- mockBeeline.getOpts().setColor(true);
-
- when(mockResultSetMetaData.getTableName(eq(1))).thenReturn("Table");
- when(mockResultSetMetaData.getTableName(eq(2))).thenReturn("Table2");
-
- DatabaseMetaData databaseMetaData2 = mock(DatabaseMetaData.class);
- ResultSet resultSet2 = mock(ResultSet.class);
- when(resultSet2.next()).thenReturn(true).thenReturn(false);
- when(resultSet2.getString(eq("COLUMN_NAME"))).thenReturn("Value");
- when(databaseMetaData2.getPrimaryKeys(anyString(), isNull(),
eq("Table2"))).thenReturn(resultSet2);
- when(databaseMetaData2.getConnection()).thenReturn(mockSqlConnection);
- when(dbConnection.getDatabaseMetaData()).thenReturn(mockDatabaseMetaData,
databaseMetaData2);
-
- BufferedRows bfRows = new BufferedRows(mockBeeline, mockResultSet);
- TableOutputFormat instance = new TableOutputFormat(mockBeeline);
-
- instance.print(bfRows);
-
- List<String> allPrintedLines = mockBeeline.getAllPrintedLines();
-
- // PK column are CYAN
- assertColumnHasColor(allPrintedLines, 1, CYAN);
- assertColumnHasColor(allPrintedLines, 2, CYAN);
- }
private boolean cellHasColor(List<String> table, int row, int col, String
color) {
String rowText = table.get(row);
@@ -227,31 +124,12 @@ private void setupMockData() throws SQLException {
mockBeeline.getOpts().setColor(true);
mockResultSet = mock(ResultSet.class);
- mockSqlConnection = mock(Connection.class);
- when(mockSqlConnection.getCatalog()).thenReturn("Catalog");
-
mockResultSetMetaData = mock(ResultSetMetaData.class);
when(mockResultSetMetaData.getColumnCount()).thenReturn(2);
when(mockResultSetMetaData.getColumnLabel(1)).thenReturn("Key");
when(mockResultSetMetaData.getColumnLabel(2)).thenReturn("Value");
- when(mockResultSetMetaData.getTableName(anyInt())).thenReturn("Table");
- when(mockResultSetMetaData.getColumnName(eq(1))).thenReturn("Key");
- when(mockResultSetMetaData.getColumnName(eq(2))).thenReturn("Value");
when(mockResultSet.getMetaData()).thenReturn(mockResultSetMetaData);
- mockMetadataResultSet = mock(ResultSet.class);
- when(mockMetadataResultSet.next()).thenReturn(true).thenReturn(false);
- when(mockMetadataResultSet.getString(eq("COLUMN_NAME"))).thenReturn("Key",
"Value");
-
- mockDatabaseMetaData = mock(DatabaseMetaData.class);
- when(mockDatabaseMetaData.getPrimaryKeys(anyString(), isNull(),
anyString())).thenReturn(mockMetadataResultSet);
-
- dbConnection = mock(DatabaseConnection.class);
- when(dbConnection.getDatabaseMetaData()).thenReturn(mockDatabaseMetaData);
- when(mockDatabaseMetaData.getConnection()).thenReturn(mockSqlConnection);
-
- mockBeeline.getDatabaseConnections().setConnection(dbConnection);
-
mockRow = new TestBufferedRows.MockRow();
// returns true as long as there is more data in mockResultData array
when(mockResultSet.next()).thenAnswer(new Answer<Boolean>() {