Skip to content

[Bug]: CTAS rejects fractional constant APPROX_PERCENTILE arguments #28886

Closed

Description

@Ariznawlll

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-table INSERT ... SELECT: return 5.
  • CTAS with p=0 returns 1; CTAS with p=1 returns 9.
  • CTAS with the semantically equivalent constant expression p=1/2 returns 5.
  • Grouped and ungrouped CTAS are both affected.
  • AVG, PERCENTILE_CONT(0.5) and PERCENTILE_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

Activity

  1. added this to the MO-202609 milestone on Sep 15, 2026
  2. added
    kind/bug
    on Sep 15, 2026
  3. added theissue type on Sep 15, 2026
  4. self-assigned this
    on Sep 15, 2026
  5. added a commit that references this issue on Sep 17, 2026
    f6f8c0c
  6. Ariznawlll commented on Sep 17, 2026

    @Ariznawlll
    CollaboratorAuthor

    当前 main 复测未通过,转回 @XuPeng-SH 继续处理。

    验证环境:本地单机 MatrixOne,由远端 main 0370bb4d6b118da29e164c54aa34ee010ed897bc 全新构建;PR #28941 merge commit f6f8c0c885f999cbde3b0d394194b0a4a74176ae 已包含。独立执行 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。

  7. added 2 commits that reference this issue on Sep 17, 2026
    385154b
    fe9c331
  8. XuPeng-SH commented on Sep 17, 2026

    @XuPeng-SH
    Contributor

    fixed by #28540

  9. Ariznawlll commented on Sep 18, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:780ef933f2479aa1679faf13d08850ce6c8f7c2f
    • 部署方式:本地独立 MatrixOne 集群。
    • 测试日期:2026-09-18

    原问题验证

    以 1..9 源表复测 APPROX_PERCENTILE(x, 0.5)。普通 SELECT、字面量 CTAS、显式 CAST(0.5 AS DOUBLE) CTAS、常量表达式 1/2 CTAS 与 View 查询都成功并返回 5.0,不再出现“必须为常量”的错误。

    Happy Path 覆盖

    1. 直接查询与三种 fractional constant CTAS 形式均返回 5.0。
    2. View 上的相同表达式返回 5.0。
    3. CTAS 边界常量 0、1/2、1 分别返回 1.0、5.0、9.0。

    Unhappy Path 覆盖

    1. 行相关参数 APPROX_PERCENTILE(x, x/10) 正确被拒绝,错误码 20301。
    2. 拒绝后 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 条语句全部成功;生成的 期望结果 记录了所有成功值及动态参数拒绝后的原子性检查。

    测试结论

    测试通过。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions