From 0ea667aed4072ebb5625a02c7330ac9b6b48a8ea Mon Sep 17 00:00:00 2001 From: ZhangCheng Date: Thu, 21 Nov 2024 17:28:24 +0800 Subject: [PATCH] Add shorthand projection isNeedRewrite judgement (#33758) --- .../projection/EncryptProjectionTokenGenerator.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/projection/EncryptProjectionTokenGenerator.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/projection/EncryptProjectionTokenGenerator.java index f1b23f9ed65ea..dc2dc31711302 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/projection/EncryptProjectionTokenGenerator.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/projection/EncryptProjectionTokenGenerator.java @@ -123,7 +123,7 @@ private SubstitutableColumnNameToken generateSQLToken(final ShorthandProjectionS ColumnProjection columnProjection = (ColumnProjection) each; boolean newAddedColumn = existColumnNames.add(columnProjection.getOriginalTable().getValue() + "." + columnProjection.getOriginalColumn().getValue()); Optional encryptTable = encryptRule.findEncryptTable(columnProjection.getOriginalTable().getValue()); - if (encryptTable.isPresent() && encryptTable.get().isEncryptColumn(columnProjection.getOriginalColumn().getValue()) && !selectStatementContext.containsTableSubquery()) { + if (encryptTable.isPresent() && encryptTable.get().isEncryptColumn(columnProjection.getOriginalColumn().getValue()) && isNeedRewrite(selectStatementContext, subqueryType)) { EncryptColumn encryptColumn = encryptTable.get().getEncryptColumn(columnProjection.getOriginalColumn().getValue()); projections.addAll(generateProjections(encryptColumn, columnProjection, subqueryType, newAddedColumn)); continue; @@ -137,6 +137,13 @@ private SubstitutableColumnNameToken generateSQLToken(final ShorthandProjectionS return new SubstitutableColumnNameToken(startIndex, segment.getStopIndex(), projections, selectStatementContext.getDatabaseType()); } + private boolean isNeedRewrite(final SelectStatementContext selectStatementContext, final SubqueryType subqueryType) { + if (SubqueryType.TABLE == subqueryType) { + return true; + } + return !selectStatementContext.containsTableSubquery(); + } + private boolean isNeedRewrite(final SelectStatementContext selectStatementContext, final ColumnProjectionSegment columnSegment) { SelectStatement sqlStatement = selectStatementContext.getSqlStatement(); if (sqlStatement.getWithSegment().isPresent() && !(sqlStatement.getFrom().isPresent() && sqlStatement.getFrom().get() instanceof SubqueryTableSegment)