question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

shardingJDBC(4.0.0-RC2) sqlStatement parsing wrong, don't have where conditions

See original GitHub issue

I’m using shardingJDBC(version: 4.0.0-RC2) but when i upgrade to version 4.0.0-RC2 , i realize my localConfig about RangeShardingAlgorithm cease to be effective。I want to solve this problem。

when i debug in source code , here is my logicSQL

select id,comment_id,product_id,shop_id,customer_id,sub_cust_id,nick_name,customer_ip_addr,order_id,order_creation_date, product_category,product_medium,score,source_type,content,content_len,is_check,check_user,check_creation_date,is_anonymous, is_top,top_reason,top_user,top_creation_date,is_wonderful,wonderful_reason,wonderful_user,wonderful_creation_date,customer_attributes, is_sync,creation_date,main_product_id,delivery_confirm_date,total_helpful_num,total_useless_num,total_reply_num,factor_change_date, last_change_date,activity_type,activity_id,failed_reason,emotion_score,comment_type,ebook_is_delete from archive_comment where 1=1 and LEFT(product_category, 2) in ('01','98') and source_type = ? and creation_date = ? and (creation_date BETWEEN ? and ?) and ebook_is_delete = ? and source_type <> 14 order by creation_date desc limit ?, ? but in ShardingSelectOptimizeEngine.class , sqlStatement doesn’t found whereSegment. so it can’t route to myRangeAlgorithm ‘archiveCommentTableRangeShardingAlgorithm’

image


here is my code and config.

here is my spring config `

<bean id="archiveCommentTablePreciseShardingAlgorithm"
	  class="com.seth.jcomment.sharding.table.ArchiveCommentTablePreciseShardingAlgorithm" />
<!-- archive_comment 分表策略  范围分片算法-->
<bean id="archiveCommentTableRangeShardingAlgorithm"
	  class="com.seth.jcomment.sharding.table.ArchiveCommentTableRangeShardingAlgorithm" />

<sharding:standard-strategy id=“archiveCommentTableStrategy” sharding-column=“creation_date” precise-algorithm-ref=“archiveCommentTablePreciseShardingAlgorithm” range-algorithm-ref=“archiveCommentTableRangeShardingAlgorithm” />

			<sharding:table-rule
					logic-table="archive_comment"
					actual-data-nodes="archiveCommentMasterSlaveDataSource.comment_$->{2014..2029}"
					database-strategy-ref="archiveCommentDatabaseStrategy"
					table-strategy-ref="archiveCommentTableStrategy" />

`

here is my java code

` public class ArchiveCommentTableRangeShardingAlgorithm implements RangeShardingAlgorithm<String> {

@Override
public Collection<String> doSharding(Collection<String> availableTargetNames, RangeShardingValue<String> shardingValue) {
    // 校验分片值
    Range<String> valueRange = shardingValue.getValueRange();
    if (null == valueRange || valueRange.isEmpty()) {
        log.error("archive_comment库,范围分片算法,获取表失败,候选表:{},分片信息:{}", availableTargetNames, shardingValue);
        throw new JcommentException(JcommentExceptionEnum.SHARDING_VALUE_NAME_ERROR_EXCEPTION.getErrorCode(),
                JcommentExceptionEnum.SHARDING_VALUE_NAME_ERROR_EXCEPTION.getErrorMsg());
    }
    if (StringUtils.isBlank(valueRange.lowerEndpoint()) || StringUtils.isBlank(valueRange.upperEndpoint())){
        log.error("archive_comment库,范围分片算法,获取表失败,候选表:{},分片信息:{}", availableTargetNames, shardingValue);
        throw new JcommentException(JcommentExceptionEnum.SHARDING_VALUE_NAME_ERROR_EXCEPTION.getErrorCode(),
                JcommentExceptionEnum.SHARDING_VALUE_NAME_ERROR_EXCEPTION.getErrorMsg());
    }

    LocalDateTime lowerLocalDateTime = DateUtil.stringToLocalDateTime(valueRange.lowerEndpoint(), DateUtil.TIME_PATTERN);
    LocalDateTime upperLocalDateTime = DateUtil.stringToLocalDateTime(valueRange.upperEndpoint(), DateUtil.TIME_PATTERN);
    Set<String> resultSet = new HashSet<String>();
    for (String availableTargetName : availableTargetNames) {
        int suffix = Integer.valueOf(availableTargetName.substring(availableTargetName.length() - 4));
        if (suffix >= lowerLocalDateTime.getYear()
                && suffix <= upperLocalDateTime.getYear()) {
            resultSet.add(availableTargetName);
        }
    }
    log.info("archive_comment库,范围分片算法,获取表结果:{},分片信息:{}", resultSet, shardingValue);
    if (resultSet.isEmpty()) {
        throw new RuntimeException("archive_comment库,范围分片算法,获取表失败,请检查逻辑表名、分片键是否配置正确");
    }
    return resultSet;
}

}

`

here is mybatis mapper sql

<select id="queryEntityCount4CommentCenter" resultType="integer"> select count(*) from ${tableName} where 1=1 <if test="shardingCommentRequestDTO.id != null"> and id = #{shardingCommentRequestDTO.id,jdbcType=INTEGER} </if> <if test="shardingCommentRequestDTO.commentId != null"> and comment_id = #{shardingCommentRequestDTO.commentId,jdbcType=INTEGER} </if> <if test="shardingCommentRequestDTO.mainProductIds != null"> and main_product_id in <foreach collection="shardingCommentRequestDTO.mainProductIds" item="id" index="index" open="(" close=")" separator=","> #{id} </foreach> </if> <if test="shardingCommentRequestDTO.mainProductId != null"> and main_product_id = #{shardingCommentRequestDTO.mainProductId,jdbcType=BIGINT} </if> <if test="shardingCommentRequestDTO.shopId != null"> and shop_id = #{shardingCommentRequestDTO.shopId,jdbcType=INTEGER} </if> <if test="shardingCommentRequestDTO.customerId != null"> and customer_id = #{shardingCommentRequestDTO.customerId,jdbcType=INTEGER} </if> <if test="shardingCommentRequestDTO.subCustId != null"> and sub_cust_id = #{shardingCommentRequestDTO.subCustId,jdbcType=INTEGER} </if> <if test="shardingCommentRequestDTO.nickName != null"> and nick_name = #{shardingCommentRequestDTO.nickName,jdbcType=VARCHAR} </if> <if test="shardingCommentRequestDTO.customerIpAddr != null"> and customer_ip_addr = #{shardingCommentRequestDTO.customerIpAddr,jdbcType=VARCHAR} </if> <if test="shardingCommentRequestDTO.orderId != null"> and order_id = #{shardingCommentRequestDTO.orderId,jdbcType=BIGINT} </if> <if test="shardingCommentRequestDTO.orderCreationDate != null"> and order_creation_date = #{shardingCommentRequestDTO.orderCreationDate,jdbcType=TIMESTAMP} </if> <if test="shardingCommentRequestDTO.productCategory != null"> and product_category = #{shardingCommentRequestDTO.productCategory,jdbcType=VARCHAR} </if> <if test="shardingCommentRequestDTO.productMedium != null"> and product_medium = #{shardingCommentRequestDTO.productMedium,jdbcType=INTEGER} </if> <if test="shardingCommentRequestDTO.score != null"> and score = #{shardingCommentRequestDTO.score,jdbcType=REAL} </if> <if test="shardingCommentRequestDTO.sourceType != null"> and source_type = #{shardingCommentRequestDTO.sourceType,jdbcType=BIT} </if> <if test="shardingCommentRequestDTO.content != null"> and content like CONCAT('%',#{shardingCommentRequestDTO.content,jdbcType=VARCHAR},'%') </if> <if test="shardingCommentRequestDTO.totalHelpfulNum != null"> and total_helpful_num = #{shardingCommentRequestDTO.totalHelpfulNum,jdbcType=INTEGER} </if> <if test="shardingCommentRequestDTO.isCheck != null"> and is_check = #{shardingCommentRequestDTO.isCheck,jdbcType=BIT} </if> <if test="shardingCommentRequestDTO.checkCreationDate != null"> and check_creation_date = #{shardingCommentRequestDTO.checkCreationDate,jdbcType=TIMESTAMP} </if> <if test="shardingCommentRequestDTO.isAnonymous != null"> and is_anonymous = #{shardingCommentRequestDTO.isAnonymous,jdbcType=BIT} </if> <if test="shardingCommentRequestDTO.isTop != null"> and is_top = #{shardingCommentRequestDTO.isTop,jdbcType=BIT} </if> <if test="shardingCommentRequestDTO.topCreationDate != null"> and top_creation_date = #{shardingCommentRequestDTO.topCreationDate,jdbcType=TIMESTAMP} </if> <if test="shardingCommentRequestDTO.isWonderful != null"> and is_wonderful = #{shardingCommentRequestDTO.isWonderful,jdbcType=BIT} </if> <if test="shardingCommentRequestDTO.wonderfulCreationDate != null"> and wonderful_creation_date = #{shardingCommentRequestDTO.wonderfulCreationDate,jdbcType=TIMESTAMP} </if> <if test="shardingCommentRequestDTO.customerAttributes != null"> and customer_attributes = #{shardingCommentRequestDTO.customerAttributes,jdbcType=VARCHAR} </if> <if test="shardingCommentRequestDTO.creationDate != null"> and creation_date = #{shardingCommentRequestDTO.creationDate,jdbcType=TIMESTAMP} </if> <if test="shardingCommentRequestDTO.startCreationDate != null and shardingCommentRequestDTO.endCreationDate != null"> and (creation_date BETWEEN #{shardingCommentRequestDTO.startCreationDate,jdbcType=VARCHAR} and #{shardingCommentRequestDTO.endCreationDate,jdbcType=VARCHAR}) </if> <if test="shardingCommentRequestDTO.productId != null"> and product_id = #{shardingCommentRequestDTO.productId,jdbcType=BIGINT} </if> <if test="shardingCommentRequestDTO.totalUselessNum != null"> and total_useless_num = #{shardingCommentRequestDTO.totalUselessNum,jdbcType=INTEGER} </if> <if test="shardingCommentRequestDTO.totalReplyNum != null"> and total_reply_num = #{shardingCommentRequestDTO.totalReplyNum,jdbcType=INTEGER} </if> <if test="shardingCommentRequestDTO.deliveryConfirmDate != null"> and delivery_confirm_date = #{shardingCommentRequestDTO.deliveryConfirmDate,jdbcType=TIMESTAMP} </if> <if test="shardingCommentRequestDTO.activityType != null"> and activity_type = #{shardingCommentRequestDTO.activityType,jdbcType=BIT} </if> <if test="shardingCommentRequestDTO.activityId != null"> and activity_id = #{shardingCommentRequestDTO.activityId,jdbcType=INTEGER} </if> <if test="shardingCommentRequestDTO.lastChangeDate != null"> and last_change_date <![CDATA[ >= ]]> #{shardingCommentRequestDTO.lastChangeDate,jdbcType=TIMESTAMP} </if> <if test="shardingCommentRequestDTO.commentType != null"> and comment_type = #{shardingCommentRequestDTO.commentType,jdbcType=TINYINT} </if> <if test="shardingCommentRequestDTO.emotionScore != null"> and emotion_score = #{shardingCommentRequestDTO.emotionScore,jdbcType=INTEGER} </if> <if test="shardingCommentRequestDTO.noCommentId != null"> <foreach collection="shardingCommentRequestDTO.noCommentId" item="id" index="index"> and comment_id <![CDATA[ <> ]]> #{id} </foreach> </if> <if test="shardingCommentRequestDTO.isShoppingComment != 0"> and order_id > 10000 </if> and source_type <![CDATA[ <> ]]> 14 </select>

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sunbufucommented, Oct 19, 2019

Can you create a demo to proof your issue?

0reactions
SethMessengercommented, Oct 25, 2019

maybe it is my fault.

last week, i have time to resolve this problem , i can’t proof this issue neither. But I’m pretty sure at that time , it might initiate some Performance Issue.

i will still focus this problem. I will tell you when i proof this issue. really sorry to bother you guys.

Read more comments on GitHub >

github_iconTop Results From Across the Web

shardingJDBC(4.0.0-RC2) sqlStatement parsing wrong, don't ...
I'm using shardingJDBC(version: 4.0.0-RC2) but when i upgrade to version 4.0.0-RC2 , i realize my localConfig about RangeShardingAlgorithm ...
Read more >
shardingsphere/Lobby - Gitter
when execute it i got this error. Caused by: java.sql.SQLException: The MySQL server is running with the --read-only option so it cannot execute...
Read more >
EQL syntax reference | Elasticsearch Guide [8.5] | Elastic
Basic syntaxedit. EQL queries require an event category and a matching condition. The where keyword connects them. event_category where condition.
Read more >
org.apache.shardingsphere » sharding-jdbc » 4.0.0-RC2
Category/License Group / Artifact Version Updates Logging EPL 1.0LGPL 2.1 ch.qos.logback » logback‑classic 1.2.0 1.4.4 JDBC Driver Apache 2.0 com.alibaba » druid 1.1.11 1.2.15 com.atomikos »...
Read more >
ShardingSphere 4.x User Manual-Sharding-JDBC-Hint ...
Sharding fields are not in SQL or table structure, but in external business logic. Some operations forced to do in the master database....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found