apache / apache/datafusion

RelationPlanner TABLESAMPLE example applies relation aliases twice

Open
#24,753 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
9.3k
Forks
2.4k
Avg merge
3d 7h
Merged PRs (30d)
344

Description

## Describe the bug

The official `RelationPlanner` `TABLESAMPLE` example recursively plans the underlying table with its alias still attached, then returns the same alias in `PlannedRelation`.

Those two APIs both apply aliases:

1. `RelationPlannerContext::plan(...)` applies the alias on the inner relation.
2. DataFusion applies `PlannedRelation::alias` around the completed extension plan.

As a result, an alias with a column list is represented twice in the logical plan. Besides making the plan noisy, the duplicate projections make the example a risky pattern for downstream implementations to copy.

That has already happened: [VGI's sampling planner follows the example's recursive-planning pattern](https://github.com/Query-farm/vgi-datafusion/blob/c6e3a6cfb05202a61233aa535912d8e2faae3e6e/src/sampling.rs#L32-L65) and [returns the alias again with the completed plan](https://github.com/Query-farm/vgi-datafusion/blob/c6e3a6cfb05202a61233aa535912d8e2faae3e6e/src/sampling.rs#L131-L133).

## To reproduce

Run the example planner with an alias that renames columns:

```sql
SELECT *
FROM sample_data AS sampled(first, second)
TABLESAMPLE (3 ROWS)
```

The logical plan contains two `SubqueryAlias: sampled` nodes and two alias projections.

## Expected behavior

The example should remove the outer alias before recursively planning the underlying table, then return that alias with the completed sampled plan. The logical plan should contain one relation alias and one column-renaming projection.

The public API docs and user guide should state this ownership rule clearly so extension authors know which layer applies the alias.

## Additional context

This is an example and documentation repair; the core alias application behavior does not need to change.

Contributor guide

Open the contributing guide

Research direction

Find the official RelationPlanner TABLESAMPLE example and reproduce the issue with SELECT * FROM sample_data AS sampled(first, second) TABLESAMPLE (3 ROWS). Inspect how RelationPlannerContext::plan and PlannedRelation::alias handle aliases; done means the example produces one alias and one column-renaming projection, with the public API docs and user guide explaining alias ownership.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust, sql
Domain
databases, documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.