From bc3c1e646be44ed510cffcf06d78051f7dc5bca1 Mon Sep 17 00:00:00 2001 From: Prashant Sharma Date: Wed, 2 Jul 2025 16:17:37 +0530 Subject: [PATCH] Fixed singlestore varchar type mapping. Currently varchar(len) is mapped to either tinytext, text, mediumtext or longtext of singlestore types. However now singlestore supports varchar with length upto varchar(21844). So we need not convert it into tiny text or text. --- .../src/main/sphinx/connector/singlestore.rst | 44 +++++++++++++++++++ .../plugin/singlestore/SingleStoreClient.java | 35 ++++++++++++--- .../TestSingleStoreDistributedQueries.java | 10 ++--- .../TestSingleStoreIntegrationSmokeTest.java | 9 ++-- .../TestSingleStoreTypeMapping.java | 12 ++--- 5 files changed, 86 insertions(+), 24 deletions(-) diff --git a/presto-docs/src/main/sphinx/connector/singlestore.rst b/presto-docs/src/main/sphinx/connector/singlestore.rst index 3545bd3d99ee1..f55999410da90 100644 --- a/presto-docs/src/main/sphinx/connector/singlestore.rst +++ b/presto-docs/src/main/sphinx/connector/singlestore.rst @@ -75,6 +75,50 @@ For :doc:`/sql/create-table` statement, the default table type is ``columnstore` The table type can be configured by setting the ``default_table_type`` engine variable, see `Creating a Columnstore Table `_. +SingleStore to PrestoDB type mapping +------------------------------------ + +Map of SingleStore types to the relevant PrestoDB types: + +.. list-table:: SingleStore to PrestoDB type mapping + :widths: 50, 50 + :header-rows: 1 + + * - SingleStore type + - PrestoDB type + * - ``BOOLEAN`` + - ``BOOLEAN`` + * - ``INTEGER`` + - ``INTEGER`` + * - ``FLOAT`` + - ``REAL`` + * - ``DOUBLE`` + - ``DOUBLE`` + * - ``DECIMAL`` + - ``DECIMAL`` + * - ``LARGETEXT`` + - ``VARCHAR (unbounded)`` + * - ``VARCHAR(len)`` + - ``VARCHAR(len) len < 21845`` + * - ``CHAR(len)`` + - ``CHAR(len)`` + * - ``MEDIUMTEXT`` + - ``VARCHAR(len) 21845 <= len < 5592405`` + * - ``LARGETEXT`` + - ``VARCHAR(len) 5592405 <= len < 1431655765`` + * - ``MEDIUMBLOB`` + - ``VARBINARY`` + * - ``UUID`` + - ``UUID`` + * - ``DATE`` + - ``DATE`` + * - ``TIME`` + - ``TIME`` + * - ``DATETIME`` + - ``TIMESTAMP`` + +No other types are supported. + The following SQL statements are not supported: * :doc:`/sql/alter-schema` diff --git a/presto-singlestore/src/main/java/com/facebook/presto/plugin/singlestore/SingleStoreClient.java b/presto-singlestore/src/main/java/com/facebook/presto/plugin/singlestore/SingleStoreClient.java index 2e8c4d2290973..a4228026ff214 100644 --- a/presto-singlestore/src/main/java/com/facebook/presto/plugin/singlestore/SingleStoreClient.java +++ b/presto-singlestore/src/main/java/com/facebook/presto/plugin/singlestore/SingleStoreClient.java @@ -22,6 +22,8 @@ import com.facebook.presto.plugin.jdbc.JdbcConnectorId; import com.facebook.presto.plugin.jdbc.JdbcIdentity; import com.facebook.presto.plugin.jdbc.JdbcTableHandle; +import com.facebook.presto.plugin.jdbc.JdbcTypeHandle; +import com.facebook.presto.plugin.jdbc.mapping.ReadMapping; import com.facebook.presto.spi.ConnectorSession; import com.facebook.presto.spi.ConnectorTableMetadata; import com.facebook.presto.spi.PrestoException; @@ -34,6 +36,7 @@ import java.sql.DatabaseMetaData; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Types; import java.util.Collection; import java.util.Optional; import java.util.Properties; @@ -44,8 +47,11 @@ import static com.facebook.presto.common.type.TimestampWithTimeZoneType.TIMESTAMP_WITH_TIME_ZONE; import static com.facebook.presto.common.type.UuidType.UUID; import static com.facebook.presto.common.type.VarbinaryType.VARBINARY; +import static com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType; import static com.facebook.presto.common.type.Varchars.isVarcharType; import static com.facebook.presto.plugin.jdbc.JdbcErrorCode.JDBC_ERROR; +import static com.facebook.presto.plugin.jdbc.mapping.StandardColumnMappings.varbinaryReadMapping; +import static com.facebook.presto.plugin.jdbc.mapping.StandardColumnMappings.varcharReadMapping; import static com.facebook.presto.spi.StandardErrorCode.ALREADY_EXISTS; import static com.facebook.presto.spi.StandardErrorCode.NOT_SUPPORTED; import static com.google.common.util.concurrent.MoreExecutors.directExecutor; @@ -143,21 +149,36 @@ protected String toSqlType(Type type) if (varcharType.isUnbounded()) { return "longtext"; } - if (varcharType.getLengthSafe() <= 255) { - return "tinytext"; + if (varcharType.getLengthSafe() <= 21844) { + // 21844 is the maximum length a singlestore varchar supports. + return super.toSqlType(type); } - if (varcharType.getLengthSafe() <= 65535) { - return "text"; - } - if (varcharType.getLengthSafe() <= 16777215) { + if (varcharType.getLengthSafe() <= 5592405) { // 16MB return "mediumtext"; } - return "longtext"; + if (varcharType.getLengthSafe() <= 1431655765) { // 100MB to 1GB + return "longtext"; // max = 1431655765 + } + else { + throw new PrestoException(NOT_SUPPORTED, "Unsupported column width: " + varcharType.getLengthSafe()); + } } return super.toSqlType(type); } + @Override + public Optional toPrestoType(ConnectorSession session, JdbcTypeHandle typeHandle) + { + switch (typeHandle.getJdbcType()) { + case Types.CLOB: + return Optional.of(varcharReadMapping(createUnboundedVarcharType())); + case Types.BLOB: + return Optional.of(varbinaryReadMapping()); + } + return super.toPrestoType(session, typeHandle); + } + @Override public void createTable(ConnectorSession session, ConnectorTableMetadata tableMetadata) { diff --git a/presto-singlestore/src/test/java/com/facebook/presto/plugin/singlestore/TestSingleStoreDistributedQueries.java b/presto-singlestore/src/test/java/com/facebook/presto/plugin/singlestore/TestSingleStoreDistributedQueries.java index dbe8acb45a5bb..c5baeecb4a52e 100644 --- a/presto-singlestore/src/test/java/com/facebook/presto/plugin/singlestore/TestSingleStoreDistributedQueries.java +++ b/presto-singlestore/src/test/java/com/facebook/presto/plugin/singlestore/TestSingleStoreDistributedQueries.java @@ -65,19 +65,17 @@ public final void destroy() public void testShowColumns(@Optional("PARQUET") String storageFormat) { MaterializedResult actual = computeActual("SHOW COLUMNS FROM orders"); - MaterializedResult expectedParametrizedVarchar = resultBuilder(getSession(), VARCHAR, VARCHAR, VARCHAR, VARCHAR, BIGINT, BIGINT, BIGINT) .row("orderkey", "bigint", "", "", 19L, null, null) .row("custkey", "bigint", "", "", 19L, null, null) - .row("orderstatus", "varchar(85)", "", "", null, null, 85L)//utf8 + .row("orderstatus", "varchar(1)", "", "", null, null, 1L)//utf8 .row("totalprice", "double", "", "", 53L, null, null) .row("orderdate", "date", "", "", null, null, null) - .row("orderpriority", "varchar(85)", "", "", null, null, 85L) - .row("clerk", "varchar(85)", "", "", null, null, 85L) + .row("orderpriority", "varchar(15)", "", "", null, null, 15L) + .row("clerk", "varchar(15)", "", "", null, null, 15L) .row("shippriority", "integer", "", "", 10L, null, null) - .row("comment", "varchar(85)", "", "", null, null, 85L) + .row("comment", "varchar(79)", "", "", null, null, 79L) .build(); - assertEquals(actual, expectedParametrizedVarchar); } diff --git a/presto-singlestore/src/test/java/com/facebook/presto/plugin/singlestore/TestSingleStoreIntegrationSmokeTest.java b/presto-singlestore/src/test/java/com/facebook/presto/plugin/singlestore/TestSingleStoreIntegrationSmokeTest.java index d0b2a486d5709..d2f9c8c329707 100644 --- a/presto-singlestore/src/test/java/com/facebook/presto/plugin/singlestore/TestSingleStoreIntegrationSmokeTest.java +++ b/presto-singlestore/src/test/java/com/facebook/presto/plugin/singlestore/TestSingleStoreIntegrationSmokeTest.java @@ -65,17 +65,16 @@ public void testDescribeTable() { // we need specific implementation of this tests due to specific Presto<->SingleStore varchar length mapping. MaterializedResult actualColumns = computeActual("DESC ORDERS").toTestTypes(); - MaterializedResult expectedColumns = MaterializedResult.resultBuilder(getQueryRunner().getDefaultSession(), VARCHAR, VARCHAR, VARCHAR, VARCHAR, BIGINT, BIGINT, BIGINT) .row("orderkey", "bigint", "", "", 19L, null, null) .row("custkey", "bigint", "", "", 19L, null, null) - .row("orderstatus", "varchar(85)", "", "", null, null, 85L)//utf-8 + .row("orderstatus", "varchar(1)", "", "", null, null, 1L)//utf-8 .row("totalprice", "double", "", "", 53L, null, null) .row("orderdate", "date", "", "", null, null, null) - .row("orderpriority", "varchar(85)", "", "", null, null, 85L) - .row("clerk", "varchar(85)", "", "", null, null, 85L) + .row("orderpriority", "varchar(15)", "", "", null, null, 15L) + .row("clerk", "varchar(15)", "", "", null, null, 15L) .row("shippriority", "integer", "", "", 10L, null, null) - .row("comment", "varchar(85)", "", "", null, null, 85L) + .row("comment", "varchar(79)", "", "", null, null, 79L) .build(); assertEquals(actualColumns, expectedColumns); } diff --git a/presto-singlestore/src/test/java/com/facebook/presto/plugin/singlestore/TestSingleStoreTypeMapping.java b/presto-singlestore/src/test/java/com/facebook/presto/plugin/singlestore/TestSingleStoreTypeMapping.java index 11730c87b3684..7f91aafe26f9c 100644 --- a/presto-singlestore/src/test/java/com/facebook/presto/plugin/singlestore/TestSingleStoreTypeMapping.java +++ b/presto-singlestore/src/test/java/com/facebook/presto/plugin/singlestore/TestSingleStoreTypeMapping.java @@ -95,12 +95,12 @@ public void testBasicTypes() public void testPrestoCreatedParameterizedVarchar() { DataTypeTest.create() - .addRoundTrip(stringDataType("varchar(10)", createVarcharType(255 / 3)), "text_a")//utf-8 - .addRoundTrip(stringDataType("varchar(255)", createVarcharType(255 / 3)), "text_b") - .addRoundTrip(stringDataType("varchar(256)", createVarcharType(65535 / 3)), "text_c") - .addRoundTrip(stringDataType("varchar(65535)", createVarcharType(65535 / 3)), "text_d") - .addRoundTrip(stringDataType("varchar(65536)", createVarcharType(16777215 / 3)), "text_e") - .addRoundTrip(stringDataType("varchar(16777215)", createVarcharType(16777215 / 3)), "text_f") + .addRoundTrip(stringDataType("varchar(10)", createVarcharType(10)), "text_a")//utf-8 + .addRoundTrip(stringDataType("varchar(255)", createVarcharType(255)), "text_b") + .addRoundTrip(stringDataType("varchar(21844)", createVarcharType(21844)), "text_c") + .addRoundTrip(stringDataType("varchar(21846)", createVarcharType(5592405)), "text_d") + .addRoundTrip(stringDataType("varchar(65536)", createVarcharType(5592405)), "text_e") + .addRoundTrip(stringDataType("varchar(16777215)", createVarcharType(1431655765)), "text_f") .execute(getQueryRunner(), prestoCreateAsSelect("presto_test_parameterized_varchar")); }