You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2021/10/21 09:33:16 UTC

[shardingsphere] branch master updated: support bit_xor function. (#13196)

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

duanzhengqiang 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 a83190c  support bit_xor function. (#13196)
a83190c is described below

commit a83190c89069a72a0be76364ef9a920628e731a1
Author: tuichenchuxin <86...@users.noreply.github.com>
AuthorDate: Thu Oct 21 17:32:37 2021 +0800

    support bit_xor function. (#13196)
    
    * support bit_xor function.
    
    * support bit_xor function.
---
 .../aggregation/AggregationUnitFactory.java        |  2 ++
 .../groupby/aggregation/BitXorAggregationUnit.java | 35 ++++++++++++-------
 .../aggregation/AggregationUnitFactoryTest.java    |  5 +++
 .../aggregation/BitXorAggregationUnitTest.java     | 40 ++++++++++++++++++++++
 .../src/main/antlr4/imports/mysql/BaseRule.g4      |  2 +-
 .../src/main/antlr4/imports/mysql/MySQLKeyword.g4  |  4 +++
 .../sql/common/constant/AggregationType.java       |  2 +-
 .../main/resources/case/dml/select-aggregate.xml   |  9 +++++
 .../sql/supported/dml/select-aggregate.xml         |  1 +
 9 files changed, 86 insertions(+), 14 deletions(-)

diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/merge/dql/groupby/aggregation/AggregationUnitFactory.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/merge/dql/groupby/aggregation/AggregationUnitFactory.java
index e42e8c2..2c50915 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/merge/dql/groupby/aggregation/AggregationUnitFactory.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/merge/dql/groupby/aggregation/AggregationUnitFactory.java
@@ -46,6 +46,8 @@ public final class AggregationUnitFactory {
                 return isDistinct ? new DistinctCountAggregationUnit() : new AccumulationAggregationUnit();
             case AVG:
                 return isDistinct ? new DistinctAverageAggregationUnit() : new AverageAggregationUnit();
+            case BIT_XOR:
+                return new BitXorAggregationUnit();
             default:
                 throw new UnsupportedOperationException(type.name());
         }
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/constant/AggregationType.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/merge/dql/groupby/aggregation/BitXorAggregationUnit.java
similarity index 53%
copy from shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/constant/AggregationType.java
copy to shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/merge/dql/groupby/aggregation/BitXorAggregationUnit.java
index d67e713..1536f90 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/constant/AggregationType.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/merge/dql/groupby/aggregation/BitXorAggregationUnit.java
@@ -15,23 +15,34 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.sql.parser.sql.common.constant;
+package org.apache.shardingsphere.sharding.merge.dql.groupby.aggregation;
 
-import java.util.Arrays;
+import lombok.RequiredArgsConstructor;
+
+import java.math.BigInteger;
+import java.util.List;
 
 /**
- * Aggregation function enum.
+ * BIT_XOR aggregation unit.
  */
-public enum AggregationType {
+@RequiredArgsConstructor
+public final class BitXorAggregationUnit implements AggregationUnit {
+    
+    private BigInteger result;
     
-    MAX, MIN, SUM, COUNT, AVG;
+    @Override
+    public void merge(final List<Comparable<?>> values) {
+        if (null == values || null == values.get(0)) {
+            return;
+        }
+        if (null == result) {
+            result = BigInteger.ZERO;
+        }
+        result = result.xor(new BigInteger(values.get(0).toString()));
+    }
     
-    /**
-     * Is aggregation type.
-     * @param aggregationType aggregation type
-     * @return is aggregation type or not
-     */
-    public static boolean isAggregationType(final String aggregationType) {
-        return Arrays.stream(values()).anyMatch(each -> aggregationType.equalsIgnoreCase(each.name()));
+    @Override
+    public Comparable<?> getResult() {
+        return result;
     }
 }
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/aggregation/AggregationUnitFactoryTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/aggregation/AggregationUnitFactoryTest.java
index a8102e1..f6b7157 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/aggregation/AggregationUnitFactoryTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/aggregation/AggregationUnitFactoryTest.java
@@ -56,4 +56,9 @@ public final class AggregationUnitFactoryTest {
     public void assertCreateDistinctAverageAggregationUnit() {
         assertThat(AggregationUnitFactory.create(AggregationType.AVG, true), instanceOf(DistinctAverageAggregationUnit.class));
     }
+    
+    @Test
+    public void assertCreateBitXorAggregationUnit() {
+        assertThat(AggregationUnitFactory.create(AggregationType.BIT_XOR, false), instanceOf(BitXorAggregationUnit.class));
+    }
 }
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/aggregation/BitXorAggregationUnitTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/aggregation/BitXorAggregationUnitTest.java
new file mode 100644
index 0000000..4b4ad62
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/aggregation/BitXorAggregationUnitTest.java
@@ -0,0 +1,40 @@
+/*
+ * 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.sharding.merge.dql.groupby.aggregation;
+
+import org.junit.Test;
+
+import java.math.BigInteger;
+import java.util.Collections;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public final class BitXorAggregationUnitTest {
+    
+    @Test
+    public void assertBitXorAggregation() {
+        BitXorAggregationUnit bitXorAggregationUnit = new BitXorAggregationUnit();
+        bitXorAggregationUnit.merge(null);
+        bitXorAggregationUnit.merge(Collections.singletonList(null));
+        bitXorAggregationUnit.merge(Collections.singletonList(new BigInteger("1")));
+        bitXorAggregationUnit.merge(Collections.singletonList(new BigInteger("2")));
+        bitXorAggregationUnit.merge(Collections.singletonList(new BigInteger("10")));
+        assertThat(((Number) bitXorAggregationUnit.getResult()).intValue(), is(9));
+    }
+}
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/BaseRule.g4 b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/BaseRule.g4
index c29c3ab..fd06049 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/BaseRule.g4
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/BaseRule.g4
@@ -888,7 +888,7 @@ aggregationFunction
     ;
     
 aggregationFunctionName
-    : MAX | MIN | SUM | COUNT | AVG
+    : MAX | MIN | SUM | COUNT | AVG | BIT_XOR
     ;
     
 distinct
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/MySQLKeyword.g4 b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/MySQLKeyword.g4
index 9d4cd6c..e79181d 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/MySQLKeyword.g4
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/MySQLKeyword.g4
@@ -123,6 +123,10 @@ AVG
     : A V G
     ;
 
+BIT_XOR
+    : B I T UL_ X O R
+    ;
+
 AVG_ROW_LENGTH
     : A V G UL_ R O W UL_ L E N G T H
     ;
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/constant/AggregationType.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/constant/AggregationType.java
index d67e713..8f080a6 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/constant/AggregationType.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/constant/AggregationType.java
@@ -24,7 +24,7 @@ import java.util.Arrays;
  */
 public enum AggregationType {
     
-    MAX, MIN, SUM, COUNT, AVG;
+    MAX, MIN, SUM, COUNT, AVG, BIT_XOR;
     
     /**
      * Is aggregation type.
diff --git a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/dml/select-aggregate.xml b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/dml/select-aggregate.xml
index 6348fdf..ebc4aa3 100644
--- a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/dml/select-aggregate.xml
+++ b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/dml/select-aggregate.xml
@@ -438,4 +438,13 @@
             </expr>
         </where>
     </select>
+
+    <select sql-case-id="select_bit_xor">
+        <from>
+            <simple-table name="t_order" start-index="29" stop-index="35" />
+        </from>
+        <projections start-index="7" stop-index="22">
+            <aggregation-projection type="BIT_XOR" inner-expression="(user_id)" start-index="7" stop-index="22" />
+        </projections>
+    </select>
 </sql-parser-test-cases>
diff --git a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/dml/select-aggregate.xml b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/dml/select-aggregate.xml
index 8a5704a..28ba320 100644
--- a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/dml/select-aggregate.xml
+++ b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/dml/select-aggregate.xml
@@ -31,4 +31,5 @@
     <sql-case id="select_count_with_sample_seed_clause" value="SELECT COUNT(*) * 10 FROM orders SAMPLE(10) SEED (1)" db-types="Oracle" />
     <sql-case id="select_count_with_in_clause" value="SELECT COUNT(*) FROM t_order WHERE last_value IN (?, ?)" db-types="MySQL"/>
     <sql-case id="select_count_with_not_in_clause" value="SELECT COUNT(*) FROM t_order WHERE category IN (?, ?) AND last_value NOT IN (?, ?)" db-types="MySQL"/>
+    <sql-case id="select_bit_xor" value="SELECT BIT_XOR(user_id) FROM t_order" db-types="MySQL" />
 </sql-cases>