Today for AI

Three-Stage Evolution of Domestic Database Adaptation in the AI Era

Advanced level · Step-by-step guide · Cost: Free

Contents9 sections

Under the wave of Information Technology Application Innovation in China, migrating core systems from Oracle, SQL Server, or MySQL to domestic databases such as OceanBase, TiDB, or openGauss has become a necessity.

Database migration is far more than data replication. Adapting hundreds of SQL dialects embedded in applications remains a tough challenge. This guide summarizes the three stages of database dialect adaptation.

Stage 1: Traditional Dialect Resolution and Branching

In the early days, developers manually adjusted application code to bypass dialect issues.

Adaptation Strategy

Developers typically write conditional branches in the data access layer (DAL) or ORM configurations based on the database type.

  • Application Branching: In MyBatis XML configs, developers write numerous <if test="dbType == 'mysql'"> or <if test="dbType == 'oracle'"> blocks, maintaining multiple SQL variations for a single query.
  • Dynamic Parsing & Regex Rewrite: Middlewares or SQL gateways intercept SQL statements at runtime, using AST parsing or regex to rewrite source dialects into target syntaxes on the fly.
  • In-Kernel Emulation: Database vendors build compatibility layers inside their engine (e.g., enabling an Oracle mode) to natively parse legacy syntax.

Limitations

This stage carries high maintenance costs and high operational risks. Application-level branching introduces code duplication. Adding a new database engine requires retrofitting every SQL query, creating exponential code bloat. Moreover, dynamic parsing often misses edge cases like complex stored procedures or implicit behaviors (e.g., how Oracle treats empty strings as NULL while other engines do not), leading to silent data anomalies in production.

Stage 2: AI-Generated Test Cases with One-by-One Modification

With the rise of Large Language Models (LLMs), manual search has been replaced by automated SQL harvesting and verification.

Adaptation Strategy

This stage implements a test-driven migration workflow.

  1. Automated SQL Harvesting: Scripts extract queries from XML files, hardcoded code strings, and slow-query logs.
  2. AI-Driven Test Generation: The harvested queries are sent to an LLM. The model analyzes the SQL syntax, drafts schema definitions, generates mock datasets, and executes them against the source database to establish "Gold Standard" outputs.
  3. Automated Compatibility Execution: Mock schemas and datasets are initialized in the target database. The harvested queries are executed, and their outputs (error codes, row counts, cell values) are compared row-by-row against the Gold Standard.
  4. AI-Driven Code Patching: After compiling compatibility reports, the AI formulates rewriting plans for the failing queries. An autonomous agent then automatically locates the queries and patches the application code.

Limitations

This stage drastically improves test coverage. The majority of incompatibilities are caught before release. However, resolution still relies on editing application code. If 300 queries fail due to dialect issues, the AI will inject 300 patches directly into the application code. Although fully automated, the codebase still gets polluted with dialect-specific logic, and subsequent feature development continues to trigger compatibility loops.

Stage 3: AI-Generated Test Cases with Native Dialect Functions

Stage three preserves the automated harvesting and verification from Stage two but shifts the resolution logic from the application to the database engine level.

Adaptation Strategy

Instead of retrofitting application code, we focus on extending the target database's capabilities to match the source dialect.

  1. AI Compatibility Check: AI scripts harvest queries and run compatibility tests. When a dialect failure occurs (e.g., openGauss fails to execute an Oracle-specific function), the system does not prompt the developer to alter the code.
  2. AI-Generated Functions: The platform feeds the failing function (e.g., Oracle's DECODE, NVL, SYSDATE) and its usage context to the LLM. The LLM then generates equivalent PL/SQL, PL/pgSQL, or UDFs for the target database.
  3. Validation & Deployment: The generated functions are compiled and deployed directly to the target database. The compatibility test suite is re-run. If it passes, the database has successfully adapted to the source dialect.

Advantages

This approach shifts the paradigm. The application codebase requires zero modification; switching databases is reduced to changing the JDBC connection string. By simulating missing functions on the target database, dialect discrepancies are resolved at the database level. Developers can continue coding in their preferred syntax without polluting the core codebase with database-specific branches. This reduces the migration engineering effort by over 90%.

Next step

Continue with related topics

Continue along the same topic.

Browse latest news