SQL Rewrite Test

Target

Facing logic databases and tables cannot be executed directly in actual databases. SQL rewrite is used to rewrite logic SQL into rightly executable ones in actual databases, including two parts, correctness rewrite and optimization rewrite. rewrite tests are for these targets.

Test

The rewrite tests are in the test folder under sharding-core/sharding-core-rewrite . Followings are the main part for rewrite tests:

  • test engine
  • environment configuration
  • assert data

Test engine is the entrance of rewrite tests, just like other test engines, through Junit Parameterized, read every and each data in the xml file under the target test type in test\resources, and then assert by the engine one by one

Environment configuration is the yaml file under test type under test\resources\yaml. The configuration file contains dataSources,shardingRule,encryptRule and other info. for example:

  1. dataSources:
  2. db: !!com.zaxxer.hikari.HikariDataSource
  3. driverClassName: org.h2.Driver
  4. jdbcUrl: jdbc:h2:mem:db;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
  5. username: sa
  6. password:
  7. ## sharding Rules
  8. rules:
  9. - !SHARDING
  10. tables:
  11. t_account:
  12. actualDataNodes: db.t_account_${0..1}
  13. tableStrategy:
  14. standard:
  15. shardingColumn: account_id
  16. shardingAlgorithmName: account_table_inline
  17. keyGenerateStrategy:
  18. column: account_id
  19. keyGeneratorName: snowflake
  20. t_account_detail:
  21. actualDataNodes: db.t_account_detail_${0..1}
  22. tableStrategy:
  23. standard:
  24. shardingColumn: order_id
  25. shardingAlgorithmName: account_detail_table_inline
  26. bindingTables:
  27. - t_account, t_account_detail
  28. shardingAlgorithms:
  29. account_table_inline:
  30. type: INLINE
  31. props:
  32. algorithm-expression: t_account_${account_id % 2}
  33. account_detail_table_inline:
  34. type: INLINE
  35. props:
  36. algorithm-expression: t_account_detail_${account_id % 2}
  37. keyGenerators:
  38. snowflake:
  39. type: SNOWFLAKE
  40. props:
  41. worker-id: 123

Assert data are in the xml under test type in test\resources. In the xml file, yaml-rule means the environment configuration file path, input contains the target SQL and parameters, output contains the expected SQL and parameters. The db-type described the type for SQL parse, default is SQL92. For example:

  1. <rewrite-assertions yaml-rule="yaml/sharding/sharding-rule.yaml">
  2. <!-- to change SQL parse type, change db-type -->
  3. <rewrite-assertion id="create_index_for_mysql" db-type="MySQL">
  4. <input sql="CREATE INDEX index_name ON t_account ('status')" />
  5. <output sql="CREATE INDEX index_name ON t_account_0 ('status')" />
  6. <output sql="CREATE INDEX index_name ON t_account_1 ('status')" />
  7. </rewrite-assertion>
  8. </rewrite-assertions>

After set up the assert data and environment configuration, rewrite test engine will assert the corresponding SQL without any Java code modification.