Skip to content

[Bug]: CTAS cannot reconstruct TRIM and POSITION special syntax #29330

Closed

Description

@Ariznawlll

Branch Name

main

Commit ID

95b1d2b

Other Environment Information

  • MatrixOne was built from the commit above and started locally with etc/launch/launch.toml.
  • The minimal BOTH case was reproduced in three fresh databases.
  • MySQL comparison version: 8.0.45.

Actual Behavior

TRIM with a row-dependent removal expression and POSITION(... IN ...) work in normal SELECTs and views, but CTAS cannot materialize the same expressions.

CREATE TABLE s(a VARCHAR(8), b VARCHAR(32));
INSERT INTO s VALUES ('ab', 'ababXabab');

SELECT TRIM(BOTH a FROM b) FROM s;
-- X

CREATE VIEW v AS SELECT TRIM(BOTH a FROM b) AS t FROM s;
SELECT t FROM v;
-- X

CREATE TABLE c AS SELECT TRIM(BOTH a FROM b) AS t FROM s;

The CTAS statement returns a parser error from the generated internal query:

ERROR 1105 (HY000): You have an error in your SQL syntax ...
syntax error ... near " `s`.`a` from `s`.`b`) as `t` from `s`) as __mo_ctas_source"

The same failure occurs for all three explicit directions:

CREATE TABLE c1 AS SELECT TRIM(BOTH a FROM b) AS t FROM s;
CREATE TABLE c2 AS SELECT TRIM(LEADING a FROM b) AS t FROM s;
CREATE TABLE c3 AS SELECT TRIM(TRAILING a FROM b) AS t FROM s;

Plain TRIM(b) CTAS succeeds, and the connection remains usable after each error.

The same CTAS reconstruction problem affects POSITION with either a column or a literal search value:

CREATE TABLE p1 AS SELECT POSITION(a IN b) AS p FROM s;
CREATE TABLE p2 AS SELECT POSITION('ab' IN b) AS p FROM s;

Both fail because the internal SELECT is regenerated as the unparseable POSITION(a, b) form. Equivalent LOCATE(a,b) and INSTR(b,a) CTAS statements succeed and materialize 3.

Expected Behavior

CTAS should preserve the valid TRIM([BOTH|LEADING|TRAILING] remstr FROM str) syntax and materialize the query results:

BOTH     -> X
LEADING  -> Xabab
TRAILING -> ababX

MySQL 8.0.45 successfully creates all three CTAS tables with those values.
It also materializes POSITION(a IN b) as 3.

Steps to Reproduce

DROP DATABASE IF EXISTS trim_ctas_repro;
CREATE DATABASE trim_ctas_repro;
USE trim_ctas_repro;

CREATE TABLE s(a VARCHAR(8), b VARCHAR(32));
INSERT INTO s VALUES ('ab', 'ababXabab');

CREATE TABLE c1 AS SELECT TRIM(BOTH a FROM b) AS t FROM s;
CREATE TABLE c2 AS SELECT TRIM(LEADING a FROM b) AS t FROM s;
CREATE TABLE c3 AS SELECT TRIM(TRAILING a FROM b) AS t FROM s;
CREATE TABLE p1 AS SELECT POSITION(a IN b) AS p FROM s;

Additional Information

The function value implementations are not the failing component. The error text shows that CTAS's generated SELECT does not reconstruct special function syntax into a parseable form. The original parser/AST formatter has special handling for TRIM ... FROM ... and POSITION ... IN ... in pkg/sql/parsers/tree/expr.go, while the CTAS path regenerates SQL from the bound plan expression. As controls, SUBSTRING(b FROM 3 FOR 4), EXTRACT(YEAR_MONTH FROM d), and CONVERT(b USING utf8mb4) all survive the same CTAS path.

Activity

  1. added this to the MO-202609 milestone on Sep 24, 2026
  2. added
    kind/bug
    on Sep 24, 2026
  3. added theissue type on Sep 24, 2026
  4. changed the title [-][Bug]: CTAS cannot materialize TRIM with an explicit removal expression[/-] [+][Bug]: CTAS cannot reconstruct TRIM and POSITION special syntax[/+] on Sep 24, 2026
  5. self-assigned this
    on Sep 24, 2026
  6. added a commit that references this issue on Sep 25, 2026
    36c3fbc
  7. Ariznawlll commented on Sep 27, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:5be6cd90b501da225be591ffffe6000bf01f515e
    • 部署方式:本地单机(1 CN / 1 TN / 1 LogService)
    • 测试日期:2026-09-27

    原问题验证

    在上述最新 main 上按 issue 原始复现步骤执行,并独立重复 3 次;3/3 均满足修复后的预期。当前 main 已包含修复提交 36c3fbce2c99410d83bd084d1eb9f09044284012。

    Happy Path 覆盖

    1. CTAS 能重建 TRIM special syntax 与 POSITION syntax,结果表值正确。

    Unhappy Path 覆盖

    1. 覆盖 BOTH/LEADING/TRAILING 语义和不存在子串,未再发生计划序列化失败。

    白盒验证

    确认修复提交 36c3fbce2c99410d83bd084d1eb9f09044284012 是当前 main 的祖先,并核对对应 MatrixOne BVT/实现路径;本次另以公开 SQL 协议执行黑盒验收。

    回归测试

    已加入 MOTR PR #204 的 issue_29323_29325_29327_29328_29329_29330_29331_sql_contracts.test,MOTR 提交 2b4462f2;本地基线比对连续 3 次通过。

    测试证据

    测试结论

    测试通过。

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