apache / apache/shardingsphere

WITH clause RECURSIVE subquery Unknown column exception when shorhand projection are used.

Open
#35,164 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
20.8k
Forks
6.9k
Avg merge
11h 35m
Merged PRs (30d)
326

Description

## Bug Report

**For English only**, other languages will not accept.

Before report a bug, make sure you have:

- Searched open and closed [GitHub issues](https://github.com/apache/shardingsphere/issues).
- Read documentation: [ShardingSphere Doc](https://shardingsphere.apache.org/document/current/en/overview).

Please pay attention on issues you submitted, because we maybe need more details.
If no response anymore and we cannot reproduce it on current information, we will **close it**.

Please answer these questions before submitting your issue. Thanks!

### Which version of ShardingSphere did you use?

5.5.3-SNAPSHOT

### Which project did you use? ShardingSphere-JDBC or ShardingSphere-Proxy?

ShardingSphere-JDBC and ShardingSphere-Proxy

### Expected behavior

`WITH RECURSIVE cte AS ( SELECT * from t_user UNION ALL SELECT user_id+1, name FROM cte WHERE user_id < 5 ) SELECT * FROM cte`

Above query runs successfully without showing any Error

### Actual behavior

![Image](https://github.com/user-attachments/assets/935053e0-a445-4c97-bf43-ba86657ed178)

### Reason analyze (If you can)

```
if (recursive && segment.getAliasName().isPresent()) {
binderContext.getExternalTableBinderContexts().put(
new CaseInsensitiveString(segment.getAliasName().get()), new SimpleTableSegmentBinderContext(segment.getColumns().stream().map(ColumnProjectionSegment::new).collect(Collectors.toList()), TableSourceType.TEMPORARY_TABLE));
}

```
In CommonTableExpressionSegmentBinder when getExternalTableBinderContexts is created Shorthand Projections are not caught correctly as Subquery is not binded first but for subquery to be binded we need getExternalTableBinderContexts

### Steps to reproduce the behavior, such as: SQL to execute, sharding rule configuration, when exception occur etc.

### Example codes for reproduce this issue (such as a github link).

Contributor guide

Open the contributing guide

Research direction

Start at CommonTableExpressionSegmentBinder and inspect how external table binder contexts are created for recursive CTEs with shorthand projections. Reproduce the supplied WITH RECURSIVE query through ShardingSphere-JDBC or ShardingSphere-Proxy, then verify it completes without an Unknown column exception.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, sql
Domain
database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.