apache / shardingsphere

Distributed SQL transaction & query engine for data sharding, scaling, encryption, and more - on any database.
Apache License 2.0
19.79k stars 6.7k forks source link

SQL query result does not use alias error:Can not get index from column label #21823

Closed 794086163 closed 1 year ago

794086163 commented 1 year ago

Caused by: java.sql.SQLFeatureNotSupportedException: Can not get index from column label split_part. at org.apache.shardingsphere.driver.jdbc.core.resultset.ShardingSphereResultSet.lambda$getIndexFromColumnLabelAndIndexMap$0(ShardingSphereResultSet.java:393) ~[shardingsphere-jdbc-core-5.2.1.jar:5.2.1] at org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions.checkState(ShardingSpherePreconditions.java:41) ~[shardingsphere-infra-util-5.2.1.jar:5.2.1] at org.apache.shardingsphere.driver.jdbc.core.resultset.ShardingSphereResultSet.getIndexFromColumnLabelAndIndexMap(ShardingSphereResultSet.java:393) ~[shardingsphere-jdbc-core-5.2.1.jar:5.2.1] at org.apache.shardingsphere.driver.jdbc.core.resultset.ShardingSphereResultSet.getString(ShardingSphereResultSet.java:165) ~[shardingsphere-jdbc-core-5.2.1.jar:5.2.1] at sun.reflect.GeneratedMethodAccessor194.invoke(Unknown Source) ~[na:na] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_331] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_331] at org.apache.ibatis.logging.jdbc.ResultSetLogger.invoke(ResultSetLogger.java:69) ~[mybatis-3.5.10.jar:3.5.10] at com.sun.proxy.$Proxy300.getString(Unknown Source) ~[na:na] at org.apache.ibatis.type.StringTypeHandler.getNullableResult(StringTypeHandler.java:37) ~[mybatis-3.5.10.jar:3.5.10] at org.apache.ibatis.type.StringTypeHandler.getNullableResult(StringTypeHandler.java:26) ~[mybatis-3.5.10.jar:3.5.10] at org.apache.ibatis.type.BaseTypeHandler.getResult(BaseTypeHandler.java:85) ~[mybatis-3.5.10.jar:3.5.10] at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createPrimitiveResultObject(DefaultResultSetHandler.java:830) ~[mybatis-3.5.10.jar:3.5.10] at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createResultObject(DefaultResultSetHandler.java:663) ~[mybatis-3.5.10.jar:3.5.10] at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createResultObject(DefaultResultSetHandler.java:642) ~[mybatis-3.5.10.jar:3.5.10] at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.getRowValue(DefaultResultSetHandler.java:404) ~[mybatis-3.5.10.jar:3.5.10] at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValuesForSimpleResultMap(DefaultResultSetHandler.java:361) ~[mybatis-3.5.10.jar:3.5.10] at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValues(DefaultResultSetHandler.java:335) ~[mybatis-3.5.10.jar:3.5.10] at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSet(DefaultResultSetHandler.java:308) ~[mybatis-3.5.10.jar:3.5.10] at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSets(DefaultResultSetHandler.java:201) ~[mybatis-3.5.10.jar:3.5.10] at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:65) ~[mybatis-3.5.10.jar:3.5.10] at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:79) ~[mybatis-3.5.10.jar:3.5.10] at com.baomidou.mybatisplus.core.executor.MybatisSimpleExecutor.doQuery(MybatisSimpleExecutor.java:69) ~[mybatis-plus-core-3.4.0.jar:3.4.0] at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:325) ~[mybatis-3.5.10.jar:3.5.10] at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156) ~[mybatis-3.5.10.jar:3.5.10] at com.baomidou.mybatisplus.core.executor.MybatisCachingExecutor.query(MybatisCachingExecutor.java:165) ~[mybatis-plus-core-3.4.0.jar:3.4.0] at com.github.pagehelper.PageInterceptor.intercept(PageInterceptor.java:111) ~[pagehelper-5.2.0.jar:na] at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:62) ~[mybatis-3.5.10.jar:3.5.10] at com.sun.proxy.$Proxy298.query(Unknown Source) ~[na:na] at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:151) ~[mybatis-3.5.10.jar:3.5.10] at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:145) ~[mybatis-3.5.10.jar:3.5.10] at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140) ~[mybatis-3.5.10.jar:3.5.10] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_331] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_331] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_331] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_331] at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:426) ~[mybatis-spring-2.0.5.jar:2.0.5] ... 148 common frames omitted

image

Can this BUG be fixed? Thanks

794086163 commented 1 year ago

SQL: select split_part("t1".grid_name,'-',1) from ${tableName} AS "t1" where

    AND "t1".${fieldNameTv} is not null
    AND "t1".grid_name is not NULL
    group by "split_part"("t1".grid_name,'-',1)
RaigorJiang commented 1 year ago

Hi @794086163, Thanks for your feedback, can you provide your table structure, just like a CREATE TABLE sql statement.

794086163 commented 1 year ago

Hi @794086163, Thanks for your feedback, can you provide your table structure, just like a CREATE TABLE sql statement.

DROP TABLE IF EXISTS "public"."logical_tb1"; CREATE TABLE "public"."logical_tb1" ( "id" int4 NOT NULL, "name" varchar(20) COLLATE "pg_catalog"."default" ) ;


-- Records of logical_tb1


INSERT INTO "public"."logical_tb1" VALUES (1, 'a'); INSERT INTO "public"."logical_tb1" VALUES (2, 'bca'); INSERT INTO "public"."logical_tb1" VALUES (3, 'liuer'); INSERT INTO "public"."logical_tb1" VALUES (4, 'ergou'); INSERT INTO "public"."logical_tb1" VALUES (8, 'p'); INSERT INTO "public"."logical_tb1" VALUES (9, 'ooo'); INSERT INTO "public"."logical_tb1" VALUES (33, 'p'); INSERT INTO "public"."logical_tb1" VALUES (44, 'ooo');


-- Primary Key structure for table logical_tb1


ALTER TABLE "public"."logical_tb1" ADD CONSTRAINT "logical_tb1_pkey" PRIMARY KEY ("id");

select split_part("t1".name,'-',1) from logical_tb1 AS "t1"

wj-li commented 1 year ago

I'm also having the same issue. When SQL incoming parameters use aliases.

RaigorJiang commented 1 year ago

Hi @794086163 Version 5.3.0 has solved this problem, please refer to #22614

flydoging commented 5 months ago

When I use the HINT strategy and the query column is an aggregate function, there is still this issue. I am using version 5.3.2

strongduanmu commented 5 months ago

@flydoging You can try the latest 5.4.1, we introduced SQLBinder to handle the analysis of columnLabel.

flydoging commented 4 months ago

@flydoging You can try the latest 5.4.1, we introduced SQLBinder to handle the analysis of columnLabel.

Thank you for your reply. Currently, I use data table alias in all queries