[BugFix]Fix table_type (#59368)

Signed-off-by: Zac-saodiseng <3253345336@qq.com>
This commit is contained in:
Zach 2025-05-27 14:31:21 +08:00 committed by GitHub
parent 2f1ec5e920
commit d2620451d4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 7 additions and 119 deletions

View File

@ -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<ColumnMapping> 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);

View File

@ -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);

View File

@ -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.
* <p>
* MySQL 5.x uses "BASE TABLE" for normal tables.
* MySQL 8.x and later use "TABLE" for normal tables.
* <p>
* This function ensures compatibility with different MySQL versions
* and prevents issues with BI tools like Tableau that expect the correct table type.
* <p>
* 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";
}
}

View File

@ -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());

View File

@ -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());
}

View File

@ -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: