# Issue #60655: Throw Can't find column xxx in schema Column Error when execute SQL

## Metadata
- Issue: https://github.com/pingcap/tidb/issues/60655
- Status: closed
- Type: type/bug
- Created At: 2025-04-18T08:54:17Z
- Closed At: 2025-05-07T17:23:51Z
- Labels: affects-7.5, affects-8.1, affects-8.5, report/customer, severity/moderate, sig/planner, type/bug

## Customer-Facing Phenomenon
- 1. Minimal reproduce step (Required)

## Linked PRs
- Fix PR #60822: planner: fix wrong TopN's ByItem with expression.ScalarFunction when to PushDownTopN
  URL: https://github.com/pingcap/tidb/pull/60822
  State: closed
  Merged At: 2025-05-07T17:23:50Z
  Changed Files Count: 9
  Main Modules: pkg/planner/core, tests/integrationtest, pkg/executor
  Sample Changed Files:
  - pkg/executor/infoschema_reader_test.go
  - pkg/planner/core/casetest/vectorsearch/testdata/ann_index_suite_in.json
  - pkg/planner/core/casetest/vectorsearch/testdata/ann_index_suite_out.json
  - pkg/planner/core/integration_test.go
  - pkg/planner/core/operator/logicalop/logical_projection.go
  - pkg/planner/core/operator/logicalop/logicalop_test/BUILD.bazel
  - pkg/planner/core/operator/logicalop/logicalop_test/logical_operator_test.go
  - tests/integrationtest/r/planner/core/plan.result
  - tests/integrationtest/t/planner/core/plan.test
  PR Summary: What problem does this PR solve? Problem Summary: In the There is a column in topN.ByItems is generated by proj, when topN is pushed down below proj, topN cannot obtain the column from datasource, to fix this, we can check whether topN.ByItems contains a column(with ID=0) generated by proj, if so, proj will prevent the optimizer from pushing topN down. 1. A column with ID=0 indicates that the column cannot be resolved by data source.
- Fix PR #61004: planner: fix wrong TopN's ByItem with expression.ScalarFunction when to PushDownTopN (#60822)
  URL: https://github.com/pingcap/tidb/pull/61004
  State: closed
  Merged At: 2025-07-09T01:43:09Z
  Changed Files Count: 4
  Main Modules: pkg/planner/core, tests/integrationtest
  Sample Changed Files:
  - pkg/planner/core/integration_test.go
  - pkg/planner/core/rule_topn_push_down.go
  - tests/integrationtest/r/planner/core/plan.result
  - tests/integrationtest/t/planner/core/plan.test
  PR Summary: This is an automated cherry-pick of #60822 What problem does this PR solve? Problem Summary: In the There is a column in topN.ByItems is generated by proj, when topN is pushed down below proj, topN cannot obtain the column from datasource, to fix this, we can check whether topN.ByItems contains a column(with ID=0) generated by proj, if so, proj will prevent the optimizer from pushing topN down.
- Fix PR #61005: planner: fix wrong TopN's ByItem with expression.ScalarFunction when to PushDownTopN (#60822)
  URL: https://github.com/pingcap/tidb/pull/61005
  State: open
  Merged At: not merged
  Changed Files Count: 9
  Main Modules: pkg/planner/core, tests/integrationtest, pkg/executor
  Sample Changed Files:
  - pkg/executor/infoschema_reader_test.go
  - pkg/planner/core/casetest/vectorsearch/testdata/ann_index_suite_in.json
  - pkg/planner/core/casetest/vectorsearch/testdata/ann_index_suite_out.json
  - pkg/planner/core/integration_test.go
  - pkg/planner/core/operator/logicalop/logical_projection.go
  - pkg/planner/core/operator/logicalop/logicalop_test/BUILD.bazel
  - pkg/planner/core/operator/logicalop/logicalop_test/logical_operator_test.go
  - tests/integrationtest/r/planner/core/plan.result
  - tests/integrationtest/t/planner/core/plan.test
  PR Summary: This is an automated cherry-pick of #60822 What problem does this PR solve? Problem Summary: In the There is a column in topN.ByItems is generated by proj, when topN is pushed down below proj, topN cannot obtain the column from datasource, to fix this, we can check whether topN.ByItems contains a column(with ID=0) generated by proj, if so, proj will prevent the optimizer from pushing topN down.
- Fix PR #61006: planner: fix wrong TopN's ByItem with expression.ScalarFunction when to PushDownTopN (#60822)
  URL: https://github.com/pingcap/tidb/pull/61006
  State: closed
  Merged At: 2025-07-08T03:13:09Z
  Changed Files Count: 7
  Main Modules: pkg/planner/core, tests/integrationtest, pkg/executor
  Sample Changed Files:
  - pkg/executor/infoschema_reader_test.go
  - pkg/planner/core/integration_test.go
  - pkg/planner/core/operator/logicalop/logical_projection.go
  - pkg/planner/core/operator/logicalop/logicalop_test/BUILD.bazel
  - pkg/planner/core/operator/logicalop/logicalop_test/logical_operator_test.go
  - tests/integrationtest/r/planner/core/plan.result
  - tests/integrationtest/t/planner/core/plan.test
  PR Summary: This is an automated cherry-pick of #60822 What problem does this PR solve? Problem Summary: In the There is a column in topN.ByItems is generated by proj, when topN is pushed down below proj, topN cannot obtain the column from datasource, to fix this, we can check whether topN.ByItems contains a column(with ID=0) generated by proj, if so, proj will prevent the optimizer from pushing topN down.

## Notes
- At least one merged PR was found. The merge timestamp above can be used as the fix landing time in the main branch.
