You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ji...@apache.org on 2024/08/26 12:06:50 UTC

(shardingsphere) branch master updated: Add InventoryDumperContextTest (#32688)

This is an automated email from the ASF dual-hosted git repository.

jianglongtao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new d96a0be0ae6 Add InventoryDumperContextTest (#32688)
d96a0be0ae6 is described below

commit d96a0be0ae6b26c10b92c333100227c710f8bab0
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Mon Aug 26 20:06:43 2024 +0800

    Add InventoryDumperContextTest (#32688)
---
 .../inventory/InventoryDumperContextTest.java      | 66 ++++++++++++++++++++++
 .../visitor/statement/MySQLStatementVisitor.java   | 11 +++-
 2 files changed, 74 insertions(+), 3 deletions(-)

diff --git a/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/ingest/dumper/inventory/InventoryDumperContextTest.java b/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/ingest/dumper/inventory/InventoryDumperContextTest.java
new file mode 100644
index 00000000000..056f18eaaf7
--- /dev/null
+++ b/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/ingest/dumper/inventory/InventoryDumperContextTest.java
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.data.pipeline.core.ingest.dumper.inventory;
+
+import org.apache.shardingsphere.data.pipeline.core.ingest.dumper.DumperCommonContext;
+import org.apache.shardingsphere.data.pipeline.core.metadata.model.PipelineColumnMetaData;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+
+class InventoryDumperContextTest {
+    
+    @Test
+    void assertHasUniqueKey() {
+        InventoryDumperContext inventoryDumperContext = new InventoryDumperContext(mock(DumperCommonContext.class));
+        inventoryDumperContext.setUniqueKeyColumns(Collections.singletonList(mock(PipelineColumnMetaData.class)));
+        assertTrue(inventoryDumperContext.hasUniqueKey());
+    }
+    
+    @Test
+    void assertHasNotUniqueKeyWhenNull() {
+        InventoryDumperContext inventoryDumperContext = new InventoryDumperContext(mock(DumperCommonContext.class));
+        assertFalse(inventoryDumperContext.hasUniqueKey());
+    }
+    
+    @Test
+    void assertHasNotUniqueKeyWhenEmpty() {
+        InventoryDumperContext inventoryDumperContext = new InventoryDumperContext(mock(DumperCommonContext.class));
+        inventoryDumperContext.setUniqueKeyColumns(Collections.emptyList());
+        assertFalse(inventoryDumperContext.hasUniqueKey());
+    }
+    
+    @Test
+    void assertGetQueryColumnNamesWithInsertColumnNames() {
+        InventoryDumperContext inventoryDumperContext = new InventoryDumperContext(mock(DumperCommonContext.class));
+        inventoryDumperContext.setInsertColumnNames(Collections.singletonList("id"));
+        assertThat(inventoryDumperContext.getQueryColumnNames(), is(Collections.singletonList("id")));
+    }
+    
+    @Test
+    void assertGetQueryColumnNamesWithoutInsertColumnNames() {
+        InventoryDumperContext inventoryDumperContext = new InventoryDumperContext(mock(DumperCommonContext.class));
+        assertThat(inventoryDumperContext.getQueryColumnNames(), is(Collections.singletonList("*")));
+    }
+}
diff --git a/parser/sql/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/MySQLStatementVisitor.java b/parser/sql/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/MySQLStatementVisitor.java
index 5bc545ea464..d12dd47cd0e 100644
--- a/parser/sql/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/MySQLStatementVisitor.java
+++ b/parser/sql/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/MySQLStatementVisitor.java
@@ -1098,9 +1098,7 @@ public abstract class MySQLStatementVisitor extends MySQLStatementBaseVisitor<AS
     
     @Override
     public final ASTNode visitSubstringFunction(final SubstringFunctionContext ctx) {
-        String substringFunctionName = null == ctx.SUBSTRING() ? ctx.MID().getText() : ctx.SUBSTRING().getText();
-        String functionName = null == ctx.SUBSTR() ? substringFunctionName : ctx.SUBSTR().getText();
-        FunctionSegment result = new FunctionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), functionName, getOriginalText(ctx));
+        FunctionSegment result = new FunctionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), getFunctionName(ctx), getOriginalText(ctx));
         result.getParameters().add((ExpressionSegment) visit(ctx.expr()));
         for (TerminalNode each : ctx.NUMBER_()) {
             result.getParameters().add(new LiteralExpressionSegment(each.getSymbol().getStartIndex(), each.getSymbol().getStopIndex(), new NumberLiteralValue(each.getText()).getValue()));
@@ -1995,6 +1993,13 @@ public abstract class MySQLStatementVisitor extends MySQLStatementBaseVisitor<AS
         return new EngineSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), SQLUtils.getExactlyValue(ctx.textOrIdentifier().getText()));
     }
     
+    private static String getFunctionName(final SubstringFunctionContext ctx) {
+        if (null == ctx.SUBSTR()) {
+            return null == ctx.SUBSTRING() ? ctx.MID().getText() : ctx.SUBSTRING().getText();
+        }
+        return ctx.SUBSTR().getText();
+    }
+    
     protected String getOriginalText(final ParserRuleContext ctx) {
         return ctx.start.getInputStream().getText(new Interval(ctx.start.getStartIndex(), ctx.stop.getStopIndex()));
     }