Skip to content

[Bug]: Prepared CTAS rejects parameter markers in SELECT expression #25425

Closed

Description

@Ariznawlll

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Searched issues for:

  • "CREATE TABLE" "SELECT ?" "prepare"
  • "CTAS" "prepare"
  • "only prepare statement can use ? expr"
  • "create table" "as select" "prepared"

No exact duplicate was found.

Branch Name

main

Commit ID

51656deb6003

Other Environment Information

  • MatrixOne: select version() returns 8.0.30-MatrixOne-v1.3.0
  • MySQL comparison: 8.0.45
  • SQL-level PREPARE / EXECUTE through mysql client.

Actual Behavior

MatrixOne rejects parameter markers inside CTAS when the CTAS statement is prepared:

ERROR 20301 (HY000): invalid input: only prepare statement can use ? expr
ERROR 20400 (HY000): invalid state prepared statement 's_ctas_param' does not exist

Control checks on the same MatrixOne build:

  • prepare s_select from 'select ? as c' works.
  • prepare s_ctas_no_param from 'create table t_no_param as select 1 as c' works.
  • Only prepare s_ctas_param from 'create table t_param as select ? as c' fails.

MySQL supports the parameterized CTAS prepared statement and creates the table successfully.

Expected Behavior

Same as MySQL: parameter markers in the query expression of a prepared CTAS should be accepted and bound at EXECUTE time.

Steps to Reproduce

drop database if exists mysql_compat_prepare_ctas_min;
create database mysql_compat_prepare_ctas_min;
use mysql_compat_prepare_ctas_min;

select 'select_param' as q;
prepare s_select from 'select ? as c';
set @p=1;
execute s_select using @p;
deallocate prepare s_select;

select 'ctas_no_param' as q;
prepare s_ctas_no_param from 'create table t_no_param as select 1 as c';
execute s_ctas_no_param;
select * from t_no_param;
deallocate prepare s_ctas_no_param;

select 'ctas_param' as q;
prepare s_ctas_param from 'create table t_param as select ? as c';
set @p=1;
execute s_ctas_param using @p;
select * from t_param;
deallocate prepare s_ctas_param;

MySQL result:

q
select_param
c
1
q
ctas_no_param
c
1
q
ctas_param
c
1

MatrixOne fails at the prepare s_ctas_param ... statement.

Additional information

A broader CTAS repeated-execute probe also showed MySQL can re-execute a prepared CTAS after dropping the target table and infer the CTAS column metadata from each execution parameter values. MatrixOne currently cannot reach that stage because the parameterized CTAS cannot be prepared.

Activity

  1. added this to the MO-202607 milestone on Jul 3, 2026
  2. added
    kind/bug
    on Jul 3, 2026
  3. added theissue type on Jul 3, 2026
  4. Ariznawlll commented on Jul 3, 2026

    @Ariznawlll
    CollaboratorAuthor

    Additional coverage on latest main 49842fb04: the same prepared CTAS limitation also reproduces when the parameter marker is in the CTAS query predicate, not only in the SELECT expression.

    Repro:

    drop database if exists compat_ddl_view_probe;
    create database compat_ddl_view_probe;
    use compat_ddl_view_probe;
    
    create table src (g int, i int, v varchar(12));
    insert into src values (1,10,'aa'),(1,20,'bbb'),(2,null,null);
    
    prepare s_ctas from 'create table ctas_prepared as select g,i,v from src where g = ?';
    set @g=1;
    execute s_ctas using @g;

    MySQL 8.0.45 creates ctas_prepared with the two g=1 rows. MatrixOne latest main rejects the PREPARE statement with:

    ERROR 20301 (HY000): invalid input: only prepare statement can use ? expr
    

    So this issue covers CTAS parameter markers in the prepared SELECT subtree more broadly, including WHERE predicates.

  5. Ariznawlll commented on Jul 6, 2026

    @Ariznawlll
    CollaboratorAuthor

    Additional prepared CTAS coverage on latest main 781161fab still reproduces this issue with multiple parameter markers in the CTAS select list.

    Repro shape:

    set @a = 7;
    set @b = 'hello';
    prepare s from 'create table ctas_p as select ? as a, ? as b';
    execute s using @a, @b;

    MySQL 8.0.45 creates ctas_p and returns 7 | hello.
    MatrixOne latest main rejects the prepared statement with:

    ERROR 20301 (HY000): invalid input: only prepare statement can use ? expr
    

    No separate issue opened; this is another shape of the same prepared CTAS parameter-marker limitation tracked here.

  6. Ariznawlll commented on Jul 8, 2026

    @Ariznawlll
    CollaboratorAuthor

    Revalidated as part of the remaining CTAS/view exploration on latest main at ec01c62e4.

    The prepared CTAS limitation still reproduces with multiple parameter markers and mixed return types:

    drop database if exists repro_prepare_ctas;
    create database repro_prepare_ctas;
    use repro_prepare_ctas;
    
    set @p_i = 7;
    set @p_s = 'prep';
    prepare stmt_ctas from 'create table ct_prepared as select ? as p_i, ? as p_s, coalesce(?, ?) as p_co';
    execute stmt_ctas using @p_i, @p_s, @p_s, @p_i;

    MySQL creates the table and returns:

    p_i  p_s   p_co
    7    prep  prep
    

    MatrixOne still fails with:

    ERROR 20301 (HY000): invalid input: only prepare statement can use ? expr
    
  7. Ariznawlll commented on Jul 22, 2026

    @Ariznawlll
    CollaboratorAuthor

    最新 main 回归结果(2026-07-22):问题仍可复现。

    • 测试基线:c0486a2446c84c6dd6bf142494fe85243877ddc5
    • MatrixOne:8.0.30-MatrixOne-v1.3.0
    • MySQL 对照:8.0.45
    • 普通 CTAS 对照:PREPARE ... FROM 'CREATE TABLE ... AS SELECT ... WHERE n >= 20' 可正常创建并返回预期数据。
    • 带参数 CTAS:PREPARE ... FROM 'CREATE TABLE ... AS SELECT ... WHERE n >= ?' 在 MatrixOne 中返回 ERROR 20301 (HY000): invalid input: only prepare statement can use ? expr,MySQL 可正常执行并返回预期数据。
    • 稳定性:相同用例重复 3 次,MatrixOne 3/3 复现。

    结论:最新 main 尚未修复。

  8. jiangxinmeng1 commented on Jul 23, 2026

    @jiangxinmeng1
    Contributor

    fixed by #26003

  9. added
    ai-easy
    on Jul 23, 2026
  10. Ariznawlll commented on Jul 23, 2026

    @Ariznawlll
    CollaboratorAuthor

    最新 main 验证结果:通过

    测试版本:MatrixOne main@ebd05ba52f0d08afb4225e7b4b2e97e8030c057d

    测试步骤

    1. 使用参数占位符准备 CREATE TABLE ... AS SELECT ...。
    2. 设置参数并执行 prepared CTAS,检查创建结果。
    3. 扩展验证多个参数、COALESCE 表达式以及删除后重新创建。

    实际结果

    • 带 ? 参数的 CTAS 可以成功 PREPARE 和 EXECUTE。
    • 基础场景创建后的值为 7。
    • 多参数、COALESCE 和第二次重建均得到预期数据。
    • 手工连续执行 3 轮通过;motr 回归连续执行 10 轮通过。

    回归用例

    • motr PR #61
    • 14_issue_regression.issue_25425_prepared_ctas_params
    • motr commit:5c7c1e5bbfa9f5f03c339a34fb931a4a2e2a946d

    结论:prepared CTAS 参数处理在最新 main 上正常,验证通过。

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

Metadata

Metadata

Assignees

Labels

ai-easykind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions