wzq1022 opened a new issue, #31587:
URL: https://github.com/apache/shardingsphere/issues/31587

   I have looked up issues with 'table not existing' and found no useful 
information.
   
   i write this demo I wrote this demo with reference 
   [offical 
doc](https://shardingsphere.apache.org/document/current/cn/user-manual/shardingsphere-jdbc/java-api/rules/sharding/)
 and some blog.
   
   this is my demo code
   ```xml
           <dependency>
               <groupId>com.baomidou</groupId>
               <artifactId>mybatis-plus-boot-starter</artifactId>
               <version>3.5.6</version>
           </dependency>
           <dependency>
               <groupId>com.mysql</groupId>
               <artifactId>mysql-connector-j</artifactId>
               <version>8.3.0</version>
           </dependency>
   ```
   
   ```java
   public class DateTimeShardingAlgorithm implements 
StandardShardingAlgorithm<String> {
       
       @Override
       public String doSharding(Collection<String> availableTargetNames, 
PreciseShardingValue<String> preciseShardingValue) {
   
           //time-pattern must be yyyy-MM-dd HH:mm:ss
           String suffix = preciseShardingValue.getValue().substring(0, 7);
   
           for (String availableTargetName : availableTargetNames) {
               if (availableTargetName.endsWith(suffix)) {
                   return availableTargetName;
               }
           }
           return null;
       }
   
       @Override
       public Collection<String> doSharding(Collection<String> 
availableTargetNames, RangeShardingValue<String> rangeShardingValue) {
           return availableTargetNames;
       }
   }
   
   ```
   
   
   ```java
   public class DataSourceConfig {
   
       public DataSource getDataSource() throws SQLException {
           Map<String, DataSource> dataSourceMap = createDataSources();
   
           Properties properties = new Properties();
           properties.put(ConfigurationPropertyKey.SQL_SHOW.getKey(), true);
   
           return ShardingSphereDataSourceFactory.createDataSource("ds", new 
ModeConfiguration("Standalone", null), dataSourceMap,
                   Arrays.asList(createShardingRuleConfiguration(), 
createBroadcastRuleConfiguration()), properties);
       }
   
   
       private Map<String, DataSource> createDataSources() {
           Map<String, DataSource> dataSourceMap = new HashMap<>();
           HikariDataSource dataSource1 = new HikariDataSource();
           dataSource1.setDriverClassName("com.mysql.jdbc.Driver");
           dataSource1.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/ds");
           dataSource1.setUsername("root");
           dataSource1.setPassword("123456");
           dataSourceMap.put("ds", dataSource1);
           return dataSourceMap;
       }
   
   
       private BroadcastRuleConfiguration createBroadcastRuleConfiguration() {
           return new BroadcastRuleConfiguration(Collections.emptyList());
       }
   
       private ShardingRuleConfiguration createShardingRuleConfiguration() {
   
           ShardingRuleConfiguration shardingRuleConfiguration = new 
ShardingRuleConfiguration();
   
           //key generator
           shardingRuleConfiguration.getKeyGenerators().put("snowflake", new 
AlgorithmConfiguration("SNOWFLAKE", new Properties()));
   
           ShardingTableRuleConfiguration productTableRuleConfiguration = new 
ShardingTableRuleConfiguration("product", 
"ds.product_$->{2019..2099}-${(1..12).collect{t 
->t.toString().padLeft(2,'0')}}");
           productTableRuleConfiguration.setKeyGenerateStrategy(new 
KeyGenerateStrategyConfiguration("id", "snowflake"));
           productTableRuleConfiguration.setTableShardingStrategy(new 
StandardShardingStrategyConfiguration("begin_time", "begin_time_interval"));
           Properties properties = new Properties();
           properties.setProperty("strategy", "STANDARD");
           properties.setProperty("algorithmClassName", 
"org.wzq.config.DateTimeShardingAlgorithm");
           
shardingRuleConfiguration.getShardingAlgorithms().put("begin_time_interval", 
new AlgorithmConfiguration("CLASS_BASED", properties));
           
shardingRuleConfiguration.getTables().add(productTableRuleConfiguration);
   
           return shardingRuleConfiguration;
       }
   
   
       public static void main(String[] args) throws SQLException {
           DataSourceConfig dataSourceConfig = new DataSourceConfig();
           DataSource dataSource = dataSourceConfig.getDataSource();
   
           String sql = "select * from product where begin_time > '2024-06-01 
10:10:10'";
           try (Connection conn = dataSource.getConnection(); PreparedStatement 
ps = conn.prepareStatement(sql)) {
               ResultSet resultSet = ps.executeQuery();
               while (resultSet.next()) {
                   String logId = resultSet.getString("id");
                   System.out.println("logId = " + logId);
               }
           }
       }
   }
   ```
   
   then error occur:
   ```java
   Exception in thread "main" java.sql.SQLException: Table 'product' doesn't 
exist
        at 
org.apache.shardingsphere.dialect.mysql.mapper.MySQLDialectExceptionMapper.toSQLException(MySQLDialectExceptionMapper.java:120)
        at 
org.apache.shardingsphere.dialect.mysql.mapper.MySQLDialectExceptionMapper.convert(MySQLDialectExceptionMapper.java:72)
        at 
org.apache.shardingsphere.dialect.SQLExceptionTransformEngine.toSQLException(SQLExceptionTransformEngine.java:59)
        at 
org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSpherePreparedStatement.executeQuery(ShardingSpherePreparedStatement.java:249)
        at org.wzq.config.DataSourceConfig.main(DataSourceConfig.java:84)
   ```
   
   with debug, i find 
   
   in `org.apache.shardingsphere.infra.connection.kernel.KernelProcessor`
   
   ```java
       public ExecutionContext generateExecutionContext(QueryContext 
queryContext, ShardingSphereDatabase database, ShardingSphereRuleMetaData 
globalRuleMetaData, ConfigurationProperties props, ConnectionContext 
connectionContext) {
           RouteContext routeContext = this.route(queryContext, database, 
globalRuleMetaData, props, connectionContext);
           SQLRewriteResult rewriteResult = this.rewrite(queryContext, 
database, globalRuleMetaData, props, routeContext, connectionContext);
           ExecutionContext result = this.createExecutionContext(queryContext, 
database, routeContext, rewriteResult);
           this.logSQL(queryContext, props, result);
           return result;
       }
   ```
   
   <img width="537" alt="image" 
src="https://github.com/apache/shardingsphere/assets/169504532/e65fe858-47d2-4d8c-8401-8c2768e11b6e";>
   
   
   
   is database tables is empty casue this error?
   
   has any other `doc `or `demo example code`can i refer to ? or has any solve 
solution?
   
   looking for your replay.
   
   
   
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: 
[email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to