Skip to content

Commit

Permalink
Add parser.statement.core.extractor package (#33761)
Browse files Browse the repository at this point in the history
* Add parser.statement.core.extractor package

* Add parser.statement.core.extractor package
  • Loading branch information
terrymanu authored Nov 21, 2024
1 parent 522218c commit 03f22b1
Show file tree
Hide file tree
Showing 43 changed files with 146 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.predicate.AndPredicate;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.predicate.WhereSegment;
import org.apache.shardingsphere.sql.parser.statement.core.util.ExpressionExtractUtils;
import org.apache.shardingsphere.sql.parser.statement.core.extractor.ExpressionExtractor;

import java.util.Collection;
import java.util.Map;
Expand Down Expand Up @@ -77,7 +77,7 @@ private void check(final EncryptRule rule, final ShardingSphereSchema schema, fi

private boolean includesLike(final Collection<WhereSegment> whereSegments, final ColumnSegment targetColumnSegment) {
for (WhereSegment each : whereSegments) {
Collection<AndPredicate> andPredicates = ExpressionExtractUtils.getAndPredicates(each.getExpr());
Collection<AndPredicate> andPredicates = ExpressionExtractor.getAndPredicates(each.getExpr());
for (AndPredicate andPredicate : andPredicates) {
if (isLikeColumnSegment(andPredicate, targetColumnSegment)) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.subquery.SubqueryExpressionSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.predicate.AndPredicate;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.predicate.WhereSegment;
import org.apache.shardingsphere.sql.parser.statement.core.util.ColumnExtractUtils;
import org.apache.shardingsphere.sql.parser.statement.core.util.ExpressionExtractUtils;
import org.apache.shardingsphere.sql.parser.statement.core.extractor.ColumnExtractor;
import org.apache.shardingsphere.sql.parser.statement.core.extractor.ExpressionExtractor;

import java.util.Collection;
import java.util.HashSet;
Expand Down Expand Up @@ -99,7 +99,7 @@ public Collection<EncryptCondition> createEncryptConditions(final Collection<Whe
ShardingSphereSchema schema = ((TableAvailable) sqlStatementContext).getTablesContext().getSchemaName().map(schemas::get).orElseGet(() -> schemas.get(defaultSchema));
Map<String, String> expressionTableNames = ((TableAvailable) sqlStatementContext).getTablesContext().findTableNames(columnSegments, schema);
for (WhereSegment each : whereSegments) {
Collection<AndPredicate> andPredicates = ExpressionExtractUtils.getAndPredicates(each.getExpr());
Collection<AndPredicate> andPredicates = ExpressionExtractor.getAndPredicates(each.getExpr());
for (AndPredicate predicate : andPredicates) {
addEncryptConditions(result, predicate.getPredicates(), expressionTableNames);
}
Expand All @@ -120,7 +120,7 @@ private void addEncryptConditions(final Collection<EncryptCondition> encryptCond
if (!findNotContainsNullLiteralsExpression(expression).isPresent()) {
return;
}
for (ColumnSegment each : ColumnExtractUtils.extract(expression)) {
for (ColumnSegment each : ColumnExtractor.extract(expression)) {
String tableName = expressionTableNames.getOrDefault(each.getExpression(), "");
Optional<EncryptTable> encryptTable = encryptRule.findEncryptTable(tableName);
if (encryptTable.isPresent() && encryptTable.get().isEncryptColumn(each.getIdentifier().getValue())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.predicate.AndPredicate;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.predicate.WhereSegment;
import org.apache.shardingsphere.sql.parser.statement.core.util.ExpressionExtractUtils;
import org.apache.shardingsphere.sql.parser.statement.core.extractor.ExpressionExtractor;
import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;

import java.util.Collection;
Expand Down Expand Up @@ -113,7 +113,7 @@ private SubstitutableColumnNameToken buildSubstitutableColumnNameToken(final Enc

private boolean includesLike(final Collection<WhereSegment> whereSegments, final ColumnSegment targetColumnSegment) {
for (WhereSegment each : whereSegments) {
Collection<AndPredicate> andPredicates = ExpressionExtractUtils.getAndPredicates(each.getExpr());
Collection<AndPredicate> andPredicates = ExpressionExtractor.getAndPredicates(each.getExpr());
for (AndPredicate andPredicate : andPredicates) {
if (isLikeColumnSegment(andPredicate, targetColumnSegment)) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.predicate.AndPredicate;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.predicate.WhereSegment;
import org.apache.shardingsphere.sql.parser.statement.core.util.ExpressionExtractUtils;
import org.apache.shardingsphere.sql.parser.statement.core.extractor.ExpressionExtractor;

import java.util.Collection;
import java.util.LinkedList;
Expand Down Expand Up @@ -61,7 +61,7 @@ protected Collection<ShadowColumnCondition> getShadowColumnConditions(final Stri
private Collection<ExpressionSegment> getWhereSegment() {
Collection<ExpressionSegment> result = new LinkedList<>();
for (WhereSegment each : sqlStatementContext.getWhereSegments()) {
for (AndPredicate predicate : ExpressionExtractUtils.getAndPredicates(each.getExpr())) {
for (AndPredicate predicate : ExpressionExtractor.getAndPredicates(each.getExpr())) {
result.addAll(predicate.getPredicates());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.predicate.AndPredicate;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.predicate.WhereSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.OwnerSegment;
import org.apache.shardingsphere.sql.parser.statement.core.util.ColumnExtractUtils;
import org.apache.shardingsphere.sql.parser.statement.core.util.ExpressionExtractUtils;
import org.apache.shardingsphere.sql.parser.statement.core.extractor.ColumnExtractor;
import org.apache.shardingsphere.sql.parser.statement.core.extractor.ExpressionExtractor;

import java.util.Collection;
import java.util.LinkedList;
Expand All @@ -56,7 +56,7 @@ public ShadowSelectStatementDataSourceMappingsRetriever(final SelectStatementCon
protected Collection<ShadowColumnCondition> getShadowColumnConditions(final String shadowColumnName) {
Collection<ShadowColumnCondition> result = new LinkedList<>();
for (ExpressionSegment each : getWhereSegment()) {
Collection<ColumnSegment> columns = ColumnExtractUtils.extract(each);
Collection<ColumnSegment> columns = ColumnExtractor.extract(each);
if (1 != columns.size()) {
continue;
}
Expand All @@ -68,7 +68,7 @@ protected Collection<ShadowColumnCondition> getShadowColumnConditions(final Stri
private Collection<ExpressionSegment> getWhereSegment() {
Collection<ExpressionSegment> result = new LinkedList<>();
for (WhereSegment each : sqlStatementContext.getWhereSegments()) {
for (AndPredicate predicate : ExpressionExtractUtils.getAndPredicates(each.getExpr())) {
for (AndPredicate predicate : ExpressionExtractor.getAndPredicates(each.getExpr())) {
result.addAll(predicate.getPredicates());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.predicate.AndPredicate;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.predicate.WhereSegment;
import org.apache.shardingsphere.sql.parser.statement.core.util.ColumnExtractUtils;
import org.apache.shardingsphere.sql.parser.statement.core.util.ExpressionExtractUtils;
import org.apache.shardingsphere.sql.parser.statement.core.extractor.ColumnExtractor;
import org.apache.shardingsphere.sql.parser.statement.core.extractor.ExpressionExtractor;

import java.util.Collection;
import java.util.LinkedList;
Expand All @@ -53,7 +53,7 @@ public ShadowUpdateStatementDataSourceMappingsRetriever(final UpdateStatementCon
protected Collection<ShadowColumnCondition> getShadowColumnConditions(final String shadowColumnName) {
Collection<ShadowColumnCondition> result = new LinkedList<>();
for (ExpressionSegment each : getWhereSegment()) {
if (1 != ColumnExtractUtils.extract(each).size()) {
if (1 != ColumnExtractor.extract(each).size()) {
continue;
}
String tableName = sqlStatementContext.getTablesContext().getTableNames().iterator().next();
Expand All @@ -65,7 +65,7 @@ protected Collection<ShadowColumnCondition> getShadowColumnConditions(final Stri
private Collection<ExpressionSegment> getWhereSegment() {
Collection<ExpressionSegment> result = new LinkedList<>();
for (WhereSegment each : sqlStatementContext.getWhereSegments()) {
for (AndPredicate predicate : ExpressionExtractUtils.getAndPredicates(each.getExpr())) {
for (AndPredicate predicate : ExpressionExtractor.getAndPredicates(each.getExpr())) {
result.addAll(predicate.getPredicates());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.predicate.WhereSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.OwnerSegment;
import org.apache.shardingsphere.sql.parser.statement.core.util.ColumnExtractUtils;
import org.apache.shardingsphere.sql.parser.statement.core.extractor.ColumnExtractor;
import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
import org.apache.shardingsphere.test.mock.AutoMockExtension;
import org.apache.shardingsphere.test.mock.StaticMockSettings;
Expand All @@ -43,7 +43,7 @@
import static org.mockito.Mockito.when;

@ExtendWith(AutoMockExtension.class)
@StaticMockSettings({ColumnExtractUtils.class, ShadowExtractor.class})
@StaticMockSettings({ColumnExtractor.class, ShadowExtractor.class})
class ShadowSelectStatementDataSourceMappingsRetrieverTest {

@Test
Expand All @@ -55,7 +55,7 @@ void assertRetrieveWithColumnOwner() {
when(sqlStatementContext.getWhereSegments()).thenReturn(Arrays.asList(whereSegment, mock(WhereSegment.class, RETURNS_DEEP_STUBS)));
ColumnSegment columnSegment = mock(ColumnSegment.class);
when(columnSegment.getOwner()).thenReturn(Optional.of(new OwnerSegment(0, 0, new IdentifierValue("foo"))));
when(ColumnExtractUtils.extract(expressionSegment)).thenReturn(Collections.singleton(columnSegment));
when(ColumnExtractor.extract(expressionSegment)).thenReturn(Collections.singleton(columnSegment));
when(ShadowExtractor.extractValues(expressionSegment, Collections.singletonList("foo"))).thenReturn(Optional.of(Collections.singleton("foo")));
when(sqlStatementContext.getTablesContext().getTableNames()).thenReturn(Collections.singleton("foo_tbl"));
when(sqlStatementContext.getTablesContext().getTableAliasNameMap().get("foo")).thenReturn("foo_tbl");
Expand All @@ -74,7 +74,7 @@ void assertRetrieveWithoutColumnOwner() {
when(sqlStatementContext.getWhereSegments()).thenReturn(Arrays.asList(whereSegment, mock(WhereSegment.class, RETURNS_DEEP_STUBS)));
ColumnSegment columnSegment = mock(ColumnSegment.class);
when(columnSegment.getOwner()).thenReturn(Optional.empty());
when(ColumnExtractUtils.extract(expressionSegment)).thenReturn(Collections.singleton(columnSegment));
when(ColumnExtractor.extract(expressionSegment)).thenReturn(Collections.singleton(columnSegment));
when(ShadowExtractor.extractValues(expressionSegment, Collections.singletonList("foo"))).thenReturn(Optional.of(Collections.singleton("foo")));
when(sqlStatementContext.getTablesContext().getTableNames()).thenReturn(Collections.singleton("foo_tbl"));
ShadowSelectStatementDataSourceMappingsRetriever retriever = new ShadowSelectStatementDataSourceMappingsRetriever(sqlStatementContext, Collections.singletonList("foo"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.predicate.WhereSegment;
import org.apache.shardingsphere.sql.parser.statement.core.util.ColumnExtractUtils;
import org.apache.shardingsphere.sql.parser.statement.core.extractor.ColumnExtractor;
import org.apache.shardingsphere.test.mock.AutoMockExtension;
import org.apache.shardingsphere.test.mock.StaticMockSettings;
import org.junit.jupiter.api.Test;
Expand All @@ -41,7 +41,7 @@
import static org.mockito.Mockito.when;

@ExtendWith(AutoMockExtension.class)
@StaticMockSettings({ColumnExtractUtils.class, ShadowExtractor.class})
@StaticMockSettings({ColumnExtractor.class, ShadowExtractor.class})
class ShadowUpdateStatementDataSourceMappingsRetrieverTest {

@Test
Expand All @@ -51,7 +51,7 @@ void assertRetrieve() {
ExpressionSegment expressionSegment = mock(ExpressionSegment.class);
when(whereSegment.getExpr()).thenReturn(expressionSegment);
when(sqlStatementContext.getWhereSegments()).thenReturn(Arrays.asList(whereSegment, mock(WhereSegment.class, RETURNS_DEEP_STUBS)));
when(ColumnExtractUtils.extract(expressionSegment)).thenReturn(Collections.singleton(mock(ColumnSegment.class)));
when(ColumnExtractor.extract(expressionSegment)).thenReturn(Collections.singleton(mock(ColumnSegment.class)));
when(ShadowExtractor.extractValues(expressionSegment, Collections.singletonList("foo"))).thenReturn(Optional.of(Collections.singleton("foo")));
when(sqlStatementContext.getTablesContext().getTableNames()).thenReturn(Collections.singleton("foo_tbl"));
ShadowUpdateStatementDataSourceMappingsRetriever retriever = new ShadowUpdateStatementDataSourceMappingsRetriever(sqlStatementContext, Collections.singletonList("foo"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.order.item.ColumnOrderByItemSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.OwnerSegment;
import org.apache.shardingsphere.sql.parser.statement.core.util.TableExtractor;
import org.apache.shardingsphere.sql.parser.statement.core.extractor.TableExtractor;
import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;

import java.util.Collection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.predicate.AndPredicate;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.predicate.WhereSegment;
import org.apache.shardingsphere.sql.parser.statement.core.util.ColumnExtractUtils;
import org.apache.shardingsphere.sql.parser.statement.core.util.ExpressionExtractUtils;
import org.apache.shardingsphere.sql.parser.statement.core.extractor.ColumnExtractor;
import org.apache.shardingsphere.sql.parser.statement.core.extractor.ExpressionExtractor;
import org.apache.shardingsphere.sql.parser.statement.core.util.SafeNumberOperationUtils;
import org.apache.shardingsphere.timeservice.core.rule.TimestampServiceRule;

Expand Down Expand Up @@ -93,7 +93,7 @@ public List<ShardingCondition> createShardingConditions(final SQLStatementContex
}

private Collection<ShardingCondition> createShardingConditions(final ExpressionSegment expression, final List<Object> params, final Map<String, String> columnExpressionTableNames) {
Collection<AndPredicate> andPredicates = ExpressionExtractUtils.getAndPredicates(expression);
Collection<AndPredicate> andPredicates = ExpressionExtractor.getAndPredicates(expression);
Collection<ShardingCondition> result = new LinkedList<>();
for (AndPredicate each : andPredicates) {
Map<Column, Collection<ShardingConditionValue>> shardingConditionValues = createShardingConditionValueMap(each.getPredicates(), params, columnExpressionTableNames);
Expand All @@ -119,7 +119,7 @@ private Map<Column, Collection<ShardingConditionValue>> createShardingConditionV
final List<Object> params, final Map<String, String> columnTableNames) {
Map<Column, Collection<ShardingConditionValue>> result = new HashMap<>(predicates.size(), 1F);
for (ExpressionSegment each : predicates) {
for (ColumnSegment columnSegment : ColumnExtractUtils.extract(each)) {
for (ColumnSegment columnSegment : ColumnExtractor.extract(each)) {
Optional<String> tableName = Optional.ofNullable(columnTableNames.get(columnSegment.getExpression()));
Optional<String> shardingColumn = tableName.flatMap(optional -> shardingRule.findShardingColumn(columnSegment.getColumnBoundInfo().getOriginalColumn().getValue(), optional));
if (!tableName.isPresent() || !shardingColumn.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.AlterViewStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.SelectStatement;
import org.apache.shardingsphere.sql.parser.statement.core.util.TableExtractor;
import org.apache.shardingsphere.sql.parser.statement.core.extractor.TableExtractor;

import java.util.Arrays;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.routine.RoutineBodySegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateFunctionStatement;
import org.apache.shardingsphere.sql.parser.statement.core.util.TableExtractor;
import org.apache.shardingsphere.sql.parser.statement.core.extractor.TableExtractor;

import java.util.Collection;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.routine.RoutineBodySegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateProcedureStatement;
import org.apache.shardingsphere.sql.parser.statement.core.util.TableExtractor;
import org.apache.shardingsphere.sql.parser.statement.core.extractor.TableExtractor;

import java.util.Collection;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateViewStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.SelectStatement;
import org.apache.shardingsphere.sql.parser.statement.core.util.TableExtractor;
import org.apache.shardingsphere.sql.parser.statement.core.extractor.TableExtractor;

import java.util.Collection;
import java.util.List;
Expand Down
Loading

0 comments on commit 03f22b1

Please sign in to comment.