Skip to content

Conversation

@fengttt
Copy link
Contributor

@fengttt fengttt commented Nov 12, 2025

User description

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue #3335

What this PR does / why we need it:

Adding :: cast.


PR Type

Enhancement


Description

  • Add support for PostgreSQL-style :: cast operator in MySQL dialect

  • Convert :: cast syntax to standard SQL CAST() function during parsing

  • Implement scanner token COLON_COLON for double colon recognition

  • Add comprehensive test cases for various cast scenarios


Diagram Walkthrough

flowchart LR
  A["Input: expr::type"] -- "Scanner tokenizes ::" --> B["COLON_COLON token"]
  B -- "Parser processes" --> C["simple_expr COLON_COLON mo_cast_type"]
  C -- "Converts to" --> D["CAST(expr AS type)"]
Loading

File Walkthrough

Relevant files
Enhancement
scanner.go
Add COLON_COLON token recognition                                               

pkg/sql/parsers/dialect/mysql/scanner.go

  • Add detection for :: operator in scanner
  • Return new COLON_COLON token when double colon is encountered
  • Check for second colon after first colon character
+4/-0     
mysql_sql.y
Add :: cast operator grammar rule                                               

pkg/sql/parsers/dialect/mysql/mysql_sql.y

  • Add COLON_COLON token to grammar with right associativity
  • Add new grammar rule for simple_expr COLON_COLON mo_cast_type
  • Convert :: cast syntax to tree.NewCastExpr() function call
+5/-0     
Tests
mysql_sql_test.go
Add comprehensive :: cast operator tests                                 

pkg/sql/parsers/dialect/mysql/mysql_sql_test.go

  • Add 14 test cases covering various :: cast scenarios
  • Test cases include literals, columns, expressions, and different data
    types
  • Cover edge cases like null values and complex expressions
  • Verify conversion to standard CAST() syntax
+39/-0   
Miscellaneous
mysql_sql.go
Update generated parser code                                                         

pkg/sql/parsers/dialect/mysql/mysql_sql.go

  • Auto-generated parser code from grammar changes
  • Updated to handle new COLON_COLON token and cast rule
+8637/-8583

@qodo-merge-pro
Copy link

You are nearing your monthly Qodo Merge usage quota. For more information, please visit here.

PR Compliance Guide 🔍

Below is a summary of compliance checks for this PR:

Security Compliance
Literal quote stripping

Description: Test expects parser to strip quotes from string literal in comparison (right side becomes
unquoted 'test'), which could cause SQL injection or semantic errors if implemented
similarly in production parsing.
mysql_sql_test.go [481-483]

Referred Code
	input:  "select count(*) from t where value::varchar = 'test'",
	output: "select count(*) from t where cast(value as varchar) = test",
}, {
Ticket Compliance
🟢
🎫 #3335
🟢 Support PostgreSQL-style cast operator using the :: syntax (e.g., expr::type) in SQL.
Parser should rewrite expr::type into cast(expr as type).
Ensure :: works in common contexts such as literals, identifiers/columns, expressions,
predicates, and with type modifiers (e.g., varchar(50)).
Codebase Duplication Compliance
Codebase context is not defined

Follow the guide to enable codebase context checks.

Custom Compliance
🟢
Generic: Meaningful Naming and Self-Documenting Code

Objective: Ensure all identifiers clearly express their purpose and intent, making code
self-documenting

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Secure Error Handling

Objective: To prevent the leakage of sensitive system information through error messages while
providing sufficient detail for internal debugging.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Secure Logging Practices

Objective: To ensure logs are useful for debugging and auditing without exposing sensitive
information like PII, PHI, or cardholder data.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Comprehensive Audit Trails

Objective: To create a detailed and reliable record of critical system actions for security analysis
and compliance.

Status:
No audit logs: The added parsing and casting features introduce new behaviors (handling :: cast) without
any observable logging of critical actions, though this test-focused change may not
require auditing.

Referred Code
	input:  "select '123'::int",
	output: "select cast(123 as int)",
}, {
	input:  "select '123'::varchar",
	output: "select cast(123 as varchar)",
}, {
	input:  "select a::int from t1",
	output: "select cast(a as int) from t1",
}, {
	input:  "select column1::timestamp from t2",
	output: "select cast(column1 as timestamp) from t2",
}, {
	input:  "select '2022-01-30'::date",
	output: "select cast(2022-01-30 as date)",
}, {
	input:  "select 123::varchar(50)",
	output: "select cast(123 as varchar(50))",
}, {
	input:  "select a + b::int",
	output: "select a + cast(b as int)",
}, {


 ... (clipped 18 lines)

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Robust Error Handling and Edge Case Management

Objective: Ensure comprehensive error handling that provides meaningful context and graceful
degradation

Status:
Missing error handling: The new tokenization for '::' returns COLON_COLON without validating malformed
sequences or providing error context, which may rely on upstream parser handling.

Referred Code
if s.peek(1) == ':' {
	s.incN(2)
	return COLON_COLON, ""
}

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Security-First Input Validation and Data Handling

Objective: Ensure all data inputs are validated, sanitized, and handled securely to prevent
vulnerabilities

Status:
Input casting parse: The new grammar accepts 'simple_expr :: type' without visible validation of
allowed types or safeguards, which may be enforced elsewhere in the type system.

Referred Code
|   simple_expr COLON_COLON mo_cast_type
    {
        $$ = tree.NewCastExpr($1, $3)
    }

Learn more about managing compliance generic rules or creating your own custom rules

Compliance status legend 🟢 - Fully Compliant
🟡 - Partial Compliant
🔴 - Not Compliant
⚪ - Requires Further Human Verification
🏷️ - Compliance label

Copy link
Contributor

@iamlinjunhong iamlinjunhong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有语法冲突
➜ parsers git:(fengttt-greatest) ✗ make
go run github.com/matrixorigin/matrixone/pkg/sql/parsers/goyacc -o mysql_sql.go mysql_sql.y

conflicts: 14 shift/reduce
gofmt -w mysql_sql.go
rm -f y.output
go run github.com/matrixorigin/matrixone/pkg/sql/parsers/goyacc -o postgresql_sql.go postgresql_sql.y
gofmt -w postgresql_sql.go
rm -f y.output

@fengttt
Copy link
Contributor Author

fengttt commented Nov 14, 2025

有语法冲突 ➜ parsers git:(fengttt-greatest) ✗ make go run github.com/matrixorigin/matrixone/pkg/sql/parsers/goyacc -o mysql_sql.go mysql_sql.y

conflicts: 14 shift/reduce gofmt -w mysql_sql.go rm -f y.output go run github.com/matrixorigin/matrixone/pkg/sql/parsers/goyacc -o postgresql_sql.go postgresql_sql.y gofmt -w postgresql_sql.go rm -f y.output

We will leave all shift/reduce to later work.

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

Labels

kind/feature Review effort 2/5 size/XXL Denotes a PR that changes 2000+ lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants