Repository navigation
Description
Description
CREATE TABLE ... AS SELECT 错误拒绝 APPROX_PERCENTILE 的小数常量 percentile。相同表达式在普通 SELECT、VIEW、派生表和 INSERT ... SELECT 中可以执行;CTAS 却将 0.5 或显式 CAST(0.5 AS DOUBLE) 判断为非常量。
Environment
- Branch:
main - Commit:
0c3a04f390adaf6281fd592a49778ea5b1155e67 - Deployment: local single-CN cluster with isolated Log/TN/CN services and a fresh data directory
Steps to reproduce
create database approx_percentile_ctas;
use approx_percentile_ctas;
create table src(x int);
insert into src values (1),(2),(3),(4),(5),(6),(7),(8),(9);
-- Returns 5.
select approx_percentile(x,0.5) from src;
-- Fails.
create table c_fractional as
select approx_percentile(x,0.5) p from src;
-- The explicit-cast form fails in the same way.
create table c_cast as
select approx_percentile(x,cast(0.5 as double)) p from src;Actual behavior
Both CTAS statements return:
ERROR 20301 (HY000): invalid input: percentile argument of approx_percentile must be a constant
Neither target table remains after the rejected statement. The source table remains unchanged.
Expected behavior
0.5 and CAST(0.5 AS DOUBLE) are constant percentile expressions. CTAS should create the table and persist the same result (5) returned by the direct query.
Stability and controls
- Fractional literal
0.5: 3/3 reproductions. - Explicit
CAST(0.5 AS DOUBLE): 3/3 reproductions. - Direct
SELECT,VIEW, derived-table query and pre-created-tableINSERT ... SELECT: return5. - CTAS with
p=0returns1; CTAS withp=1returns9. - CTAS with the semantically equivalent constant expression
p=1/2returns5. - Grouped and ungrouped CTAS are both affected.
AVG,PERCENTILE_CONT(0.5)andPERCENTILE_DISC(0.5)CTAS controls succeed.
Code analysis
CTAS executes a generated internal INSERT ... SELECT from a derived query. The service log shows the fractional constant reaches this follow-up SQL unchanged, but constructAggregateConfig rejects its plan expression in validateApproxPercentileExpr.
The closely related exact-percentile issue #28631 was fixed by #28728. That change added normalizeAggregateConfigExpr only to the PERCENTILE_CONT / PERCENTILE_DISC branch. The adjacent APPROX_PERCENTILE branch still validates/evaluates its configuration without the CTAS normalization used by the exact variants. The literal-form boundary (0/1 and 1/2 work, 0.5/explicit DOUBLE cast fail) is consistent with a missing normalization step in the CTAS recompile path.
Regression coverage
After the product fix, add CTAS coverage for grouped and ungrouped APPROX_PERCENTILE with integer endpoints, fractional numeric literals, explicit numeric casts, constant arithmetic, NULL groups, and rejection cleanup for genuinely nonconstant/out-of-range arguments.
Related
- [Bug]: CTAS rejects constant percentile_cont and percentile_disc arguments #28631 / fix: allow constant percentiles in CTAS #28728: exact
PERCENTILE_CONTandPERCENTILE_DISCCTAS constant normalization (fixed) - [Bug]: invalid percentile parameters expose compile stack traces to SQL clients #28870: invalid percentile errors expose compile stack traces; this issue is the separate supported-query rejection
Activity
fix: preserve expression result metadata and coercion contracts (#28941)
Ariznawlll commentedon Sep 17, 2026
当前 main 复测未通过,转回 @XuPeng-SH 继续处理。
验证环境:本地单机 MatrixOne,由远端 main
0370bb4d6b118da29e164c54aa34ee010ed897bc全新构建;PR #28941 merge commitf6f8c0c885f999cbde3b0d394194b0a4a74176ae已包含。独立执行 3/3,结果一致。复现:源表
1..9上普通SELECT APPROX_PERCENTILE(x,0.5)正确返回 5;但CREATE TABLE ... AS SELECT APPROX_PERCENTILE(x,0.5)和显式CAST(0.5 AS DOUBLE)两种 CTAS 形式均稳定报ERROR 20301: percentile argument of approx_percentile must be a constant,目标表不会创建。0.5及其显式 DOUBLE cast 都是常量表达式;CTAS 应与普通 SELECT 一样成功并持久化结果 5。当前验收未通过,issue 保持 Open。fix: preserve expression result metadata and coercion contracts (matr…
fix: preserve expression result metadata and coercion contracts (matr…
XuPeng-SH commentedon Sep 17, 2026
fixed by #28540
Ariznawlll commentedon Sep 18, 2026
测试版本
main780ef933f2479aa1679faf13d08850ce6c8f7c2f原问题验证
以
1..9源表复测APPROX_PERCENTILE(x, 0.5)。普通 SELECT、字面量 CTAS、显式CAST(0.5 AS DOUBLE)CTAS、常量表达式1/2CTAS 与 View 查询都成功并返回5.0,不再出现“必须为常量”的错误。Happy Path 覆盖
5.0。5.0。0、1/2、1分别返回1.0、5.0、9.0。Unhappy Path 覆盖
APPROX_PERCENTILE(x, x/10)正确被拒绝,错误码20301。dynamic_fraction目标表数量为0;源表仍为 9 行、sum(x)=45,后续SELECT 1正常,确认没有残留部分 CTAS 状态。白盒验证
不适用:本 issue 的验收面是 SQL 常量归一化及 CTAS 原子性;未使用内部注入。
回归测试
测试证据
在上述 commit 上执行
./motr --record --branch main 14_issue_regression.issue_28886_ctas_approx_percentile:23 条语句全部成功;生成的 期望结果 记录了所有成功值及动态参数拒绝后的原子性检查。测试结论
测试通过。