From d2620451d4f4ab8db31cdf96b52b420b54ae1c89 Mon Sep 17 00:00:00 2001 From: Zach <3253345336@qq.com> Date: Tue, 27 May 2025 14:31:21 +0800 Subject: [PATCH] [BugFix]Fix table_type (#59368) Signed-off-by: Zac-saodiseng <3253345336@qq.com> --- .../plugin/starrocks/StarRocksClient.java | 26 +-------- .../ExternalCatalogTableBasicInfo.java | 3 +- .../java/com/starrocks/catalog/Table.java | 38 +------------ .../java/com/starrocks/catalog/TableTest.java | 54 +------------------ .../java/com/starrocks/qe/ShowTablesTest.java | 3 +- .../test_temporary_table/R/temporary_table | 2 +- 6 files changed, 7 insertions(+), 119 deletions(-) diff --git a/contrib/trino-connector/src/main/java/io/trino/plugin/starrocks/StarRocksClient.java b/contrib/trino-connector/src/main/java/io/trino/plugin/starrocks/StarRocksClient.java index 81deb977d3f..507a7a42103 100644 --- a/contrib/trino-connector/src/main/java/io/trino/plugin/starrocks/StarRocksClient.java +++ b/contrib/trino-connector/src/main/java/io/trino/plugin/starrocks/StarRocksClient.java @@ -355,28 +355,6 @@ public class StarRocksClient return "'" + value.replace("'", "''").replace("\\", "\\\\") + "'"; } - private String getMysqlServerVersion() - { - return handle.createQuery("SELECT VERSION()") - .mapTo(String.class) - .findOne() - .orElse("5.7.0"); - } - - private String getTableTypeForMysql() - { - String version = getMysqlServerVersion(); - if (version == null || version.isEmpty()) { - return "BASE TABLE"; - } - if (version.startsWith("5")) { - return "BASE TABLE"; - } else if (version.startsWith("8")) { - return "TABLE"; - } - return "BASE TABLE"; - } - @Override public Optional toColumnMapping(ConnectorSession session, Connection connection, JdbcTypeHandle typeHandle) { @@ -1031,14 +1009,12 @@ public class StarRocksClient Long getRowCount(JdbcTableHandle table) { RemoteTableName remoteTableName = table.getRequiredNamedRelation().getRemoteTableName(); - String tableType = getTableTypeForMysql(); return handle.createQuery("" + "SELECT TABLE_ROWS FROM INFORMATION_SCHEMA.TABLES " + "WHERE TABLE_SCHEMA = :schema AND TABLE_NAME = :table_name " + - "AND TABLE_TYPE = :table_type ") + "AND TABLE_TYPE = 'BASE TABLE' ") .bind("schema", remoteTableName.getCatalogName().orElse(null)) .bind("table_name", remoteTableName.getTableName()) - .bind("table_type", tableType) .mapTo(Long.class) .findOne() .orElse(null); diff --git a/fe/fe-core/src/main/java/com/starrocks/catalog/ExternalCatalogTableBasicInfo.java b/fe/fe-core/src/main/java/com/starrocks/catalog/ExternalCatalogTableBasicInfo.java index 270045e7259..da1dac7edb4 100644 --- a/fe/fe-core/src/main/java/com/starrocks/catalog/ExternalCatalogTableBasicInfo.java +++ b/fe/fe-core/src/main/java/com/starrocks/catalog/ExternalCatalogTableBasicInfo.java @@ -52,8 +52,9 @@ public class ExternalCatalogTableBasicInfo implements BasicTable { @Override public String getMysqlType() { - return Table.getTableTypeForMysql(); + return "BASE TABLE"; } + @Override public String getEngine() { return Table.TableType.serialize(this.tableType); diff --git a/fe/fe-core/src/main/java/com/starrocks/catalog/Table.java b/fe/fe-core/src/main/java/com/starrocks/catalog/Table.java index 225941df429..b6054fd6bcb 100644 --- a/fe/fe-core/src/main/java/com/starrocks/catalog/Table.java +++ b/fe/fe-core/src/main/java/com/starrocks/catalog/Table.java @@ -47,7 +47,6 @@ import com.starrocks.catalog.constraint.UniqueConstraint; import com.starrocks.catalog.system.SystemTable; import com.starrocks.common.io.Writable; import com.starrocks.persist.gson.GsonPostProcessable; -import com.starrocks.qe.GlobalVariable; import com.starrocks.server.GlobalStateMgr; import com.starrocks.thrift.TTableDescriptor; import org.apache.commons.lang.NotImplementedException; @@ -604,43 +603,8 @@ public class Table extends MetaObject implements Writable, GsonPostProcessable, case SCHEMA: return "SYSTEM VIEW"; default: - // external table also returns "BASE TABLE" or "TABLE" for BI compatibility - return getTableTypeForMysql(); - } - } - - /** - * Determines the MySQL table type based on the MySQL server version. - *

- * MySQL 5.x uses "BASE TABLE" for normal tables. - * MySQL 8.x and later use "TABLE" for normal tables. - *

- * This function ensures compatibility with different MySQL versions - * and prevents issues with BI tools like Tableau that expect the correct table type. - *

- * If the version is invalid or cannot be parsed, the function defaults to "BASE TABLE". - * - * @return "BASE TABLE" for MySQL 5.x or invalid versions, "TABLE" for MySQL 8.x and later. - */ - public static String getTableTypeForMysql() { - String version = GlobalVariable.version; - if (version == null || version.isEmpty()) { - return "BASE TABLE"; // Default to "BASE TABLE" if version is missing - } - - try { - // Extract the major version number (e.g., "8" from "8.0.33") - String[] versionParts = version.split("\\."); - int majorVersion = Integer.parseInt(versionParts[0]); - - if (majorVersion >= 8) { - return "TABLE"; - } else { + // external table also returns "BASE TABLE" for BI compatibility return "BASE TABLE"; - } - } catch (NumberFormatException e) { - // If the version is invalid (e.g., "invalid.version"), return "BASE TABLE" as a fallback - return "BASE TABLE"; } } diff --git a/fe/fe-core/src/test/java/com/starrocks/catalog/TableTest.java b/fe/fe-core/src/test/java/com/starrocks/catalog/TableTest.java index faa9eada079..03991d621e4 100644 --- a/fe/fe-core/src/test/java/com/starrocks/catalog/TableTest.java +++ b/fe/fe-core/src/test/java/com/starrocks/catalog/TableTest.java @@ -38,7 +38,6 @@ import com.starrocks.catalog.Table.TableType; import com.starrocks.common.FeConstants; import com.starrocks.common.jmockit.Deencapsulation; import com.starrocks.persist.gson.GsonUtils; -import com.starrocks.qe.GlobalVariable; import com.starrocks.server.GlobalStateMgr; import org.junit.Assert; import org.junit.Before; @@ -59,58 +58,7 @@ public class TableTest { } @Test - public void testGetMysqlType_Version5() { - GlobalVariable.version = "5.7.0"; - Assert.assertEquals("BASE TABLE", new Table(TableType.OLAP).getMysqlType()); - Assert.assertEquals("BASE TABLE", new Table(TableType.OLAP_EXTERNAL).getMysqlType()); - Assert.assertEquals("BASE TABLE", new Table(TableType.CLOUD_NATIVE).getMysqlType()); - - Assert.assertEquals("BASE TABLE", new Table(TableType.MYSQL).getMysqlType()); - Assert.assertEquals("BASE TABLE", new Table(TableType.BROKER).getMysqlType()); - Assert.assertEquals("BASE TABLE", new Table(TableType.ELASTICSEARCH).getMysqlType()); - Assert.assertEquals("BASE TABLE", new Table(TableType.HIVE).getMysqlType()); - Assert.assertEquals("BASE TABLE", new Table(TableType.ICEBERG).getMysqlType()); - Assert.assertEquals("BASE TABLE", new Table(TableType.HUDI).getMysqlType()); - Assert.assertEquals("BASE TABLE", new Table(TableType.JDBC).getMysqlType()); - Assert.assertEquals("BASE TABLE", new Table(TableType.DELTALAKE).getMysqlType()); - Assert.assertEquals("BASE TABLE", new Table(TableType.FILE).getMysqlType()); - - Assert.assertEquals("VIEW", new Table(TableType.INLINE_VIEW).getMysqlType()); - Assert.assertEquals("VIEW", new Table(TableType.VIEW).getMysqlType()); - Assert.assertEquals("VIEW", new Table(TableType.MATERIALIZED_VIEW).getMysqlType()); - Assert.assertEquals("VIEW", new Table(TableType.CLOUD_NATIVE_MATERIALIZED_VIEW).getMysqlType()); - - Assert.assertEquals("SYSTEM VIEW", new Table(TableType.SCHEMA).getMysqlType()); - } - - @Test - public void testGetMysqlType_Version8() { - GlobalVariable.version = "8.0.33"; - Assert.assertEquals("TABLE", new Table(TableType.OLAP).getMysqlType()); - Assert.assertEquals("TABLE", new Table(TableType.OLAP_EXTERNAL).getMysqlType()); - Assert.assertEquals("TABLE", new Table(TableType.CLOUD_NATIVE).getMysqlType()); - - Assert.assertEquals("TABLE", new Table(TableType.MYSQL).getMysqlType()); - Assert.assertEquals("TABLE", new Table(TableType.BROKER).getMysqlType()); - Assert.assertEquals("TABLE", new Table(TableType.ELASTICSEARCH).getMysqlType()); - Assert.assertEquals("TABLE", new Table(TableType.HIVE).getMysqlType()); - Assert.assertEquals("TABLE", new Table(TableType.ICEBERG).getMysqlType()); - Assert.assertEquals("TABLE", new Table(TableType.HUDI).getMysqlType()); - Assert.assertEquals("TABLE", new Table(TableType.JDBC).getMysqlType()); - Assert.assertEquals("TABLE", new Table(TableType.DELTALAKE).getMysqlType()); - Assert.assertEquals("TABLE", new Table(TableType.FILE).getMysqlType()); - - Assert.assertEquals("VIEW", new Table(TableType.INLINE_VIEW).getMysqlType()); - Assert.assertEquals("VIEW", new Table(TableType.VIEW).getMysqlType()); - Assert.assertEquals("VIEW", new Table(TableType.MATERIALIZED_VIEW).getMysqlType()); - Assert.assertEquals("VIEW", new Table(TableType.CLOUD_NATIVE_MATERIALIZED_VIEW).getMysqlType()); - - Assert.assertEquals("SYSTEM VIEW", new Table(TableType.SCHEMA).getMysqlType()); - } - - @Test - public void testGetMysqlType_InvalidVersion() { - GlobalVariable.version = "invalid.version"; + public void testGetMysqlType() { Assert.assertEquals("BASE TABLE", new Table(TableType.OLAP).getMysqlType()); Assert.assertEquals("BASE TABLE", new Table(TableType.OLAP_EXTERNAL).getMysqlType()); Assert.assertEquals("BASE TABLE", new Table(TableType.CLOUD_NATIVE).getMysqlType()); diff --git a/fe/fe-core/src/test/java/com/starrocks/qe/ShowTablesTest.java b/fe/fe-core/src/test/java/com/starrocks/qe/ShowTablesTest.java index 458a78b4255..87a214633f3 100644 --- a/fe/fe-core/src/test/java/com/starrocks/qe/ShowTablesTest.java +++ b/fe/fe-core/src/test/java/com/starrocks/qe/ShowTablesTest.java @@ -96,7 +96,6 @@ public class ShowTablesTest { @Test public void testShowTableVerbose() throws Exception { - String mysqlTableType = Table.getTableTypeForMysql(); ctx.setCurrentUserIdentity(UserIdentity.ROOT); ctx.setCurrentRoleIds(Sets.newHashSet(PrivilegeBuiltinConstants.ROOT_ROLE_ID)); @@ -108,7 +107,7 @@ public class ShowTablesTest { Assert.assertEquals("VIEW", resultSet.getString(1)); Assert.assertTrue(resultSet.next()); Assert.assertEquals("testTbl", resultSet.getString(0)); - Assert.assertEquals(mysqlTableType, resultSet.getString(1)); + Assert.assertEquals("BASE TABLE", resultSet.getString(1)); Assert.assertFalse(resultSet.next()); } diff --git a/test/sql/test_temporary_table/R/temporary_table b/test/sql/test_temporary_table/R/temporary_table index 5db29653f55..2d6b04566b2 100644 --- a/test/sql/test_temporary_table/R/temporary_table +++ b/test/sql/test_temporary_table/R/temporary_table @@ -431,7 +431,7 @@ TABLE_ID bigint YES false None -- !result select TABLE_CATALOG,TABLE_NAME,TABLE_TYPE,ENGINE,TABLE_ROWS,AVG_ROW_LENGTH,DATA_LENGTH,MAX_DATA_LENGTH,INDEX_LENGTH,DATA_FREE,AUTO_INCREMENT,TABLE_COMMENT from information_schema.temp_tables where `session`=(select session_id()); -- result: -def t TABLE StarRocks 0 0 0 None None None None +def t BASE TABLE StarRocks 0 0 0 None None None None -- !result drop temporary table `t`; -- result: