You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by mo...@apache.org on 2026/04/04 16:04:38 UTC

(doris) branch master updated: [fix](jdbc) Preserve query tvf column aliases across JDBC catalogs (#61939)

This is an automated email from the ASF dual-hosted git repository.

morningman pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new a8c890545ec [fix](jdbc) Preserve query tvf column aliases across JDBC catalogs (#61939)
a8c890545ec is described below

commit a8c890545ecb86fcf0516e4ff6b7c53f2635aed0
Author: daidai <ch...@selectdb.com>
AuthorDate: Sun Apr 5 00:04:29 2026 +0800

    [fix](jdbc) Preserve query tvf column aliases across JDBC catalogs (#61939)
    
    ### What problem does this PR solve?
    
    Related PR: #34516
    
    Problem Summary:
    
    Fix bug:
    `query()` passes the SQL through to the JDBC catalog directly, so the
    returned column names should follow the aliases defined in that SQL.
    
    For example, for:
    ```sql
    select * from query(
      'catalog' = 'jdbc_catalog',
      'query' = 'select a as x from test.t'
    );
    ```
    the output column name should be `x`, not the original source column
    name `a`.
---
 .../datasource/jdbc/util/JdbcFieldSchema.java      |  5 +-
 .../datasource/jdbc/util/JdbcFieldSchemaTest.java  | 74 ++++++++++++++++++++++
 .../external_table_p0/jdbc/test_jdbc_query_tvf.out |  6 ++
 .../jdbc/test_jdbc_query_tvf.groovy                |  5 +-
 4 files changed, 88 insertions(+), 2 deletions(-)

diff --git a/fe/fe-core/src/main/java/org/apache/doris/datasource/jdbc/util/JdbcFieldSchema.java b/fe/fe-core/src/main/java/org/apache/doris/datasource/jdbc/util/JdbcFieldSchema.java
index d91b06b46c9..5e1f0a87bba 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/datasource/jdbc/util/JdbcFieldSchema.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/datasource/jdbc/util/JdbcFieldSchema.java
@@ -99,7 +99,10 @@ public class JdbcFieldSchema {
     }
 
     public JdbcFieldSchema(ResultSetMetaData metaData, int columnIndex) throws SQLException {
-        this.columnName = metaData.getColumnName(columnIndex);
+        String columnLabel = metaData.getColumnLabel(columnIndex);
+        this.columnName = columnLabel == null || columnLabel.isEmpty()
+                ? metaData.getColumnName(columnIndex)
+                : columnLabel;
         this.dataType = metaData.getColumnType(columnIndex);
         this.dataTypeName = Optional.ofNullable(metaData.getColumnTypeName(columnIndex));
         this.columnSize = Optional.of(metaData.getPrecision(columnIndex));
diff --git a/fe/fe-core/src/test/java/org/apache/doris/datasource/jdbc/util/JdbcFieldSchemaTest.java b/fe/fe-core/src/test/java/org/apache/doris/datasource/jdbc/util/JdbcFieldSchemaTest.java
new file mode 100644
index 00000000000..5eda7393f99
--- /dev/null
+++ b/fe/fe-core/src/test/java/org/apache/doris/datasource/jdbc/util/JdbcFieldSchemaTest.java
@@ -0,0 +1,74 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.datasource.jdbc.util;
+
+import mockit.Expectations;
+import mockit.Mocked;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.sql.ResultSetMetaData;
+import java.sql.Types;
+
+public class JdbcFieldSchemaTest {
+
+    @Mocked
+    private ResultSetMetaData metaData;
+
+    @Test
+    public void testUseColumnLabelForQueryAlias() throws Exception {
+        new Expectations() {{
+                metaData.getColumnLabel(1);
+                result = "t1";
+                metaData.getColumnType(1);
+                result = Types.VARCHAR;
+                metaData.getColumnTypeName(1);
+                result = "VARCHAR";
+                metaData.getPrecision(1);
+                result = 64;
+                metaData.getScale(1);
+                result = 0;
+            }};
+
+        JdbcFieldSchema schema = new JdbcFieldSchema(metaData, 1);
+
+        Assert.assertEquals("t1", schema.getColumnName());
+    }
+
+    @Test
+    public void testFallbackToColumnNameWhenLabelMissing() throws Exception {
+        new Expectations() {{
+                metaData.getColumnLabel(1);
+                result = "";
+                metaData.getColumnName(1);
+                result = "username";
+                metaData.getColumnType(1);
+                result = Types.VARCHAR;
+                metaData.getColumnTypeName(1);
+                result = "VARCHAR";
+                metaData.getPrecision(1);
+                result = 64;
+                metaData.getScale(1);
+                result = 0;
+            }};
+
+        JdbcFieldSchema schema = new JdbcFieldSchema(metaData, 1);
+
+        Assert.assertEquals("username", schema.getColumnName());
+    }
+}
diff --git a/regression-test/data/external_table_p0/jdbc/test_jdbc_query_tvf.out b/regression-test/data/external_table_p0/jdbc/test_jdbc_query_tvf.out
index 76911221065..8299d4e9678 100644
--- a/regression-test/data/external_table_p0/jdbc/test_jdbc_query_tvf.out
+++ b/regression-test/data/external_table_p0/jdbc/test_jdbc_query_tvf.out
@@ -42,3 +42,9 @@ year	smallint	Yes	true	\N	NONE
 -- !sql --
 4
 
+-- !sql --
+alias_smallint_u	int	Yes	true	\N	NONE
+alias_tinyint_u	smallint	Yes	true	\N	NONE
+
+-- !sql --
+203	303
diff --git a/regression-test/suites/external_table_p0/jdbc/test_jdbc_query_tvf.groovy b/regression-test/suites/external_table_p0/jdbc/test_jdbc_query_tvf.groovy
index 4798f0a429f..c3903f325d8 100644
--- a/regression-test/suites/external_table_p0/jdbc/test_jdbc_query_tvf.groovy
+++ b/regression-test/suites/external_table_p0/jdbc/test_jdbc_query_tvf.groovy
@@ -43,8 +43,11 @@ suite("test_jdbc_query_tvf", "p0,external") {
         order_qt_sql """desc function query('catalog' = '${catalog_name}', 'query' = 'select * from doris_test.all_types') """
         order_qt_sql """select * from query('catalog' = '${catalog_name}', 'query' = 'select * from doris_test.all_types') """
         order_qt_sql """select * from query('catalog' = '${catalog_name}', 'query' = 'select count(*) as cnt from doris_test.all_types') """
+        order_qt_sql """desc function query('catalog' = '${catalog_name}',
+                'query' = 'select tinyint_u as alias_tinyint_u, smallint_u as alias_smallint_u from doris_test.all_types where tinyint_u is not null') """
+        order_qt_sql """select * from query('catalog' = '${catalog_name}',
+                'query' = 'select tinyint_u as alias_tinyint_u, smallint_u as alias_smallint_u from doris_test.all_types where tinyint_u is not null') where alias_tinyint_u = 203 """
 
 //         sql """drop catalog if exists ${catalog_name} """
     }
 }
-


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org