Skip to content

[Bug] [JDBC Catalog] SQL Server user-defined alias data types map to UNSUPPORTED_TYPE #67793

Closed

Description

@ptrosek

Search before asking

  • I had searched in the issues and found no similar issues.

Version

Apache Doris: 4.1.0
SQL Server: 2019
JDBC Driver: both mssql-jdbc-13.4.0.jre8.jar and mssql-jdbc-12.10.2.jre8.jar have the same issue

What's Wrong?

In a SQL Server 2019 database (specifically observed in an InsERT Subiekt GT schema), columns defined using SQL Server user-defined alias types (CREATE TYPE ... FROM <base_type>) synchronize into the JDBC Catalog as UNSUPPORTED_TYPE.

This causes queries referencing the affected table via SELECT * to fail:

SQL Error [1105] [HY000]: errCode = 2, detailMessage = type UNSUPPORTED is unsupported for Nereids

Currently, querying these tables requires explicitly enumerating only non-aliased columns or bypassing catalog table abstraction via EXECUTE_STMT().

What You Expected?

Columns declared with a SQL Server alias type should resolve to the Doris type corresponding to their underlying primitive base type (for example, customtexttype FROM varchar(50) resolving to TEXT/STRING), allowing SELECT * and catalog queries to work normally.

How to Reproduce?

On SQL Server:

CREATE TYPE dbo.customtexttype FROM varchar(50) NOT NULL;
GO

CREATE TABLE dbo.test_alias (
    id int PRIMARY KEY,
    plain_col varchar(50) NULL,
    alias_col dbo.customtexttype NULL
);
GO

In Doris:

CREATE CATALOG sqlserver_test PROPERTIES (
    'type' = 'jdbc',
    'user' = '<user>',
    'password' = '<password>',
    'jdbc_url' = 'jdbc:sqlserver://<host>:1433;databaseName=<db>;encrypt=false',
    'driver_url' = 'https://repo1.maven.org/maven2/com/microsoft/sqlserver/mssql-jdbc/13.4.0.jre8/mssql-jdbc-13.4.0.jre8.jar',
    'driver_class' = 'com.microsoft.sqlserver.jdbc.SQLServerDriver'
);
DESC sqlserver_test.dbo.test_alias;

Result:

Field Type Null Key Default Extra
id int No true null
plain_col text Yes false null
alias_col unknown type: UNSUPPORTED_TYPE Yes false null
SELECT * FROM sqlserver_test.dbo.test_alias;

Will result in:

SQL Error [1105] [HY000]: errCode = 2, detailMessage = type UNSUPPORTED is unsupported for Nereids

Anything Else?

Root Cause

In JdbcSQLServerClient.jdbcTypeToDoris(JdbcFieldSchema fieldSchema) (fe/fe-core/src/main/java/org/apache/doris/datasource/jdbc/client/JdbcSQLServerClient.java):

@Override
protected Type jdbcTypeToDoris(JdbcFieldSchema fieldSchema) {
    String sqlserverType = fieldSchema.getDataTypeName().toLowerCase();
    switch (sqlserverType) {
        case "bit": return Type.BOOLEAN;
        case "tinyint":
        case "smallint": return Type.SMALLINT;
        // ...
        case "char":
        case "varchar": return ScalarType.createStringType();
        default:
            return Type.UNSUPPORTED; // <-- Alias types fall through here
    }
}
  1. Doris inspects fieldSchema.getDataTypeName(), which is populated from JDBC's DatabaseMetaData.getColumns() result column TYPE_NAME.
  2. For an alias type, SQL Server returns the declared alias name ("customtexttype"), not the underlying type name ("varchar").
  3. Because the alias name does not match any literal case, execution falls through to default: return Type.UNSUPPORTED.
  4. However, DatabaseMetaData.getColumns() simultaneously populates DATA_TYPE with the standard JDBC integer code for the base type (java.sql.Types.VARCHAR = 12). Doris already extracts and stores this in memory via fieldSchema.setDataType(rs.getInt("DATA_TYPE")).

Evidence from SQL Server Catalogs

Inspecting dbo.test_alias directly in SQL Server system views:

SELECT c.name AS column_name,
       t.name AS declared_type,
       bt.name AS base_type,
       t.is_user_defined,
       t.is_assembly_type
FROM sys.columns c
JOIN sys.types t ON c.user_type_id = t.user_type_id
LEFT JOIN sys.types bt ON t.system_type_id = bt.user_type_id AND bt.is_user_defined = 0
WHERE c.object_id = OBJECT_ID('dbo.test_alias') AND t.is_user_defined = 1;

Observed rows:

column_name declared_type base_type is_user_defined is_assembly_type
alias_col customtexttype varchar 1 0

is_assembly_type = 0 confirms this is a plain alias type over a base type, not a CLR UDT.

Metadata values returned by mssql-jdbc for alias_col:

  • fieldSchema.getDataTypeName(): "customtexttype"
  • fieldSchema.getDataType(): 12 (java.sql.Types.VARCHAR)
  • fieldSchema.getColumnSize(): 50
  • fieldSchema.getDecimalDigits(): 0

Proposed Solution (Hybrid Fallback)

Retain the primary name-based switch (sqlserverType) to preserve dialect-specific overrides (such as SQL Server tinyint mapping to SMALLINT or money mapping to DOUBLE).

In the default: branch, inspect fieldSchema.getDataType() against java.sql.Types before returning Type.UNSUPPORTED:

@Override
protected Type jdbcTypeToDoris(JdbcFieldSchema fieldSchema) {
    String sqlserverType = fieldSchema.getDataTypeName().toLowerCase();
    switch (sqlserverType) {
        case "bit":
            return Type.BOOLEAN;
        case "tinyint":
        case "smallint":
            return Type.SMALLINT;
        case "int":
            return Type.INT;
        case "bigint":
            return Type.BIGINT;
        case "real":
            return Type.FLOAT;
        case "float":
        case "money":
        case "smallmoney":
            return Type.DOUBLE;
        case "decimal":
        case "numeric":
            return ScalarType.createDecimalV3Type(fieldSchema.getColumnSize(), fieldSchema.getDecimalDigits());
        case "date":
            return ScalarType.getDefaultDateType(Type.DATE);
        case "datetime":
        case "datetime2":
        case "smalldatetime":
            return ScalarType.createDatetimeV2Type(6);
        case "char":
        case "varchar":
        case "nchar":
        case "nvarchar":
        case "text":
        case "ntext":
        case "time":
        case "datetimeoffset":
            return ScalarType.createStringType();
        case "image":
        case "binary":
        case "varbinary":
            return Type.UNSUPPORTED;
        default:
            // Fallback for alias types resolving to standard JDBC base types
            return mapJdbcTypeFallback(fieldSchema);
    }
}

private Type mapJdbcTypeFallback(JdbcFieldSchema fieldSchema) {
    switch (fieldSchema.getDataType()) {
        case java.sql.Types.BIT:
        case java.sql.Types.BOOLEAN:
            return Type.BOOLEAN;
        case java.sql.Types.TINYINT:
        case java.sql.Types.SMALLINT:
            return Type.SMALLINT;
        case java.sql.Types.INTEGER:
            return Type.INT;
        case java.sql.Types.BIGINT:
            return Type.BIGINT;
        case java.sql.Types.REAL:
            return Type.FLOAT;
        case java.sql.Types.FLOAT:
        case java.sql.Types.DOUBLE:
            return Type.DOUBLE;
        case java.sql.Types.NUMERIC:
        case java.sql.Types.DECIMAL:
            return ScalarType.createDecimalV3Type(fieldSchema.getColumnSize(), fieldSchema.getDecimalDigits());
        case java.sql.Types.DATE:
            return ScalarType.getDefaultDateType(Type.DATE);
        case java.sql.Types.TIMESTAMP:
            return ScalarType.createDatetimeV2Type(6);
        case java.sql.Types.CHAR:
        case java.sql.Types.VARCHAR:
        case java.sql.Types.LONGVARCHAR:
        case java.sql.Types.NCHAR:
        case java.sql.Types.NVARCHAR:
        case java.sql.Types.LONGNVARCHAR:
            return ScalarType.createStringType();
        default:
            return Type.UNSUPPORTED;
    }
}

Are you willing to submit PR?

  • Yes I am willing to submit a PR!

Code of Conduct

Activity

  1. Doris-Breakwater commented on Sep 10, 2026

    @Doris-Breakwater
    Collaborator

    Breakwater-GitHub-Analysis-Slot: slot_b4595a22787b

    Initial triage

    Assessment: confirmed, actionable JDBC Catalog bug. The report contains a minimal reproduction and the metadata values needed to establish the varchar-alias failure. No FE log or Doris profile is needed for that exact case. The issue currently has no labels; kind/fix, kind/datasource, and area/catalog would fit the scope.

    Verified facts

    1. In the 4.1.0 tag, JdbcClient.getJdbcColumnsInfo() calls DatabaseMetaData.getColumns() and builds a JdbcFieldSchema for every result row.
    2. JdbcFieldSchema(ResultSet) stores both DATA_TYPE and TYPE_NAME (the issue text's setDataType(...) wording is not the exact 4.1.0 implementation, but the data flow is equivalent).
    3. JdbcSQLServerClient.jdbcTypeToDoris() dispatches only on the normalized TYPE_NAME and returns Type.UNSUPPORTED from the default branch. It does not consult DATA_TYPE.
    4. Therefore, with the reported metadata tuple TYPE_NAME=customtexttype, DATA_TYPE=Types.VARCHAR, COLUMN_SIZE=50, the result is deterministically UNSUPPORTED, and the later Nereids error is a consequence rather than the root cause.
    5. The latest master source still has the same default behavior, so this is not already fixed there.

    I did not independently run a SQL Server 2019 instance; the runtime metadata tuple is reporter-supplied. However, it is consistent with SQL Server alias-type semantics and the JDBC contract: CREATE TYPE ... FROM ... defines an alias over a native base type, while DatabaseMetaData.getColumns() exposes a JDBC DATA_TYPE separately from the database-dependent TYPE_NAME (Microsoft CREATE TYPE documentation, JDBC DatabaseMetaData documentation).

    Review of the proposed fix

    Using the JDBC type code only as a fallback from the name-based default branch is a reasonable direction: it preserves SQL Server-specific name mappings for ordinary columns and fixes the supplied VARCHAR alias case.

    The pasted patch should not be applied verbatim because it is based on a different/older implementation shape. In 4.1.0, the change must preserve:

    • Optional access for dataTypeName, columnSize, and decimalDigits;
    • identity/parameter stripping already performed before the switch;
    • money -> DECIMAL(19,4) and smallmoney -> DECIMAL(10,4), not DOUBLE;
    • createDecimalOrStringType() for precision validation/fallback;
    • the reported datetime scale capped at 6, rather than forcing scale 6;
    • existing image/binary/varbinary behavior, including enable.mapping.varbinary.

    The shown fallback fixes the demonstrated string alias but does not yet justify the broader claim that all SQL Server alias types resolve like their base types. Legal alias bases also include time, binary types, datetimeoffset, uniqueidentifier, money, and others. Some are represented by driver-specific codes, and some codes are ambiguous: Microsoft documents geometry, geography, and CLR udt as JDBC VARBINARY, so blindly treating every unknown TYPE_NAME plus Types.VARBINARY as a binary alias could incorrectly make genuinely unsupported types queryable (Microsoft JDBC type mappings). Unknown CLR/special types must remain unsupported unless Doris has positively identified a non-assembly alias and its base type.

    Recommended next steps

    1. Accept a narrowly scoped PR for the safe JDBC-code fallback, starting with the demonstrated standard scalar mappings, while retaining the existing name-first switch and Type.UNSUPPORTED for unrecognized/vendor-specific codes.
    2. Add a focused FE test with TYPE_NAME=customtexttype, DATA_TYPE=Types.VARCHAR, and COLUMN_SIZE=50, asserting a Doris string type; include a control proving an unknown/CLR-like type still remains unsupported.
    3. Add an SQL Server external-catalog regression fixture using the issue's CREATE TYPE/CREATE TABLE, then assert both DESC and SELECT *. This validates schema discovery and the BE scanner, whose string path uses ResultSet.getObject() followed by string conversion.
    4. If the PR claims general alias-type support, first capture a metadata matrix from both reported driver versions for aliases over at least integer, decimal/money, date/datetime/time/datetimeoffset, character, uniqueidentifier, and binary bases. Include DATA_TYPE, TYPE_NAME, COLUMN_SIZE, DECIMAL_DIGITS, and any available source/UDT metadata, plus CLR/geometry controls. That evidence should determine whether a code-only fallback is sufficient or whether SQL Server-specific base-type resolution is required.

    No additional information blocks fixing the reported varchar(50) case. The broader alias-type scope needs the metadata matrix above; it should not be inferred from the single VARCHAR observation.

  2. added a commit that references this issue on Sep 14, 2026
    ee17a0a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions