strongduanmu commented on code in PR #33808:
URL: https://github.com/apache/shardingsphere/pull/33808#discussion_r1866862153
##########
features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/dql/groupby/aggregation/AggregationUnitFactory.java:
##########
@@ -37,6 +37,19 @@ public final class AggregationUnitFactory {
* @throws UnsupportedSQLOperationException unsupported SQL operation
exception
*/
public static AggregationUnit create(final AggregationType type, final
boolean isDistinct) {
Review Comment:
Please merge this method with next create method.
##########
features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/dql/groupby/aggregation/DistinctGroupConcatAggregationUnit.java:
##########
@@ -0,0 +1,52 @@
+/*
+ * 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 java.util.Collection;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class DistinctGroupConcatAggregationUnit implements AggregationUnit {
+
+ private static final String DEFAULT_SEPARATOR = ",";
+
+ private final Collection<Comparable<?>> values = new LinkedHashSet<>();
+
+ private String separator;
+
+ public DistinctGroupConcatAggregationUnit(final String separator) {
+ this.separator = separator;
+ }
+
+ @Override
+ public void merge(final List<Comparable<?>> values) {
+ if (null == values || null == values.get(0)) {
+ return;
+ }
+ this.values.add(values.get(0));
+ }
+
+ @Override
+ public Comparable<?> getResult() {
+ if (null == separator) {
+ separator = DEFAULT_SEPARATOR;
+ }
+ return
values.stream().map(Object::toString).collect(Collectors.joining(separator));
Review Comment:
Can you cast value to string when execute merge method? This way we can
reduce one loop call.
##########
features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/dql/groupby/aggregation/GroupConcatAggregationUnit.java:
##########
@@ -0,0 +1,55 @@
+/*
+ * 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 lombok.NoArgsConstructor;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.stream.Collectors;
+
+@NoArgsConstructor
+public class GroupConcatAggregationUnit implements AggregationUnit {
+
+ private static final String DEFAULT_SEPARATOR = ",";
+
+ private final Collection<Comparable<?>> values = new ArrayList<>();
+
+ private String separator;
+
+ public GroupConcatAggregationUnit(final String separator) {
+ this.separator = separator;
+ }
+
+ @Override
+ public void merge(final List<Comparable<?>> values) {
+ if (null == values || null == values.get(0)) {
+ return;
+ }
+ this.values.add(values.get(0));
+ }
+
+ @Override
+ public Comparable<?> getResult() {
+ if (null == separator) {
+ separator = DEFAULT_SEPARATOR;
+ }
+ return
values.stream().map(Object::toString).collect(Collectors.joining(separator));
Review Comment:
Can you cast value to string when execute merge method? This way we can
reduce one loop call.
##########
features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/dql/groupby/aggregation/DistinctGroupConcatAggregationUnit.java:
##########
@@ -0,0 +1,52 @@
+/*
+ * 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 java.util.Collection;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class DistinctGroupConcatAggregationUnit implements AggregationUnit {
+
+ private static final String DEFAULT_SEPARATOR = ",";
+
+ private final Collection<Comparable<?>> values = new LinkedHashSet<>();
+
+ private String separator;
+
+ public DistinctGroupConcatAggregationUnit(final String separator) {
+ this.separator = separator;
+ }
+
+ @Override
+ public void merge(final List<Comparable<?>> values) {
+ if (null == values || null == values.get(0)) {
+ return;
+ }
+ this.values.add(values.get(0));
+ }
+
+ @Override
+ public Comparable<?> getResult() {
+ if (null == separator) {
Review Comment:
Can we move this logic to DistinctGroupConcatAggregationUnit constructor?
##########
parser/sql/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/MySQLStatementVisitor.java:
##########
@@ -916,6 +917,11 @@ public final ASTNode visitAggregationFunction(final
AggregationFunctionContext c
: new
ExpressionProjectionSegment(ctx.getStart().getStartIndex(),
ctx.getStop().getStopIndex(), getOriginalText(ctx));
}
+ @Override
+ public ASTNode visitSeparatorName(final
MySQLStatementParser.SeparatorNameContext ctx) {
Review Comment:
Please use import static for SeparatorNameContext, and remove
`MySQLStatementParser.`.
##########
features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/dql/groupby/aggregation/DistinctGroupConcatAggregationUnit.java:
##########
@@ -0,0 +1,52 @@
+/*
+ * 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 java.util.Collection;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class DistinctGroupConcatAggregationUnit implements AggregationUnit {
+
+ private static final String DEFAULT_SEPARATOR = ",";
+
+ private final Collection<Comparable<?>> values = new LinkedHashSet<>();
+
+ private String separator;
Review Comment:
Please add final for separator.
##########
features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/aggregation/AggregationUnitFactoryTest.java:
##########
@@ -61,4 +61,10 @@ void assertCreateDistinctAverageAggregationUnit() {
void assertCreateBitXorAggregationUnit() {
assertThat(AggregationUnitFactory.create(AggregationType.BIT_XOR,
false), instanceOf(BitXorAggregationUnit.class));
}
+
+ @Test
+ void assertGroupConcatAggregationUnit() {
+ assertThat(AggregationUnitFactory.create(AggregationType.GROUP_CONCAT,
true), instanceOf(DistinctGroupConcatAggregationUnit.class));
+ assertThat(AggregationUnitFactory.create(AggregationType.GROUP_CONCAT,
true, " "), instanceOf(DistinctGroupConcatAggregationUnit.class));
Review Comment:
Please add unit test for GroupConcatAggregationUnit
##########
features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/dql/groupby/aggregation/GroupConcatAggregationUnit.java:
##########
@@ -0,0 +1,55 @@
+/*
+ * 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 lombok.NoArgsConstructor;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.stream.Collectors;
+
+@NoArgsConstructor
+public class GroupConcatAggregationUnit implements AggregationUnit {
Review Comment:
Please add final for GroupConcatAggregationUnit, and add javadoc.
##########
features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/dql/groupby/aggregation/DistinctGroupConcatAggregationUnit.java:
##########
@@ -0,0 +1,52 @@
+/*
+ * 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 java.util.Collection;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class DistinctGroupConcatAggregationUnit implements AggregationUnit {
Review Comment:
Please add final for DistinctGroupConcatAggregationUnit, and add javadoc.
##########
features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/dql/groupby/aggregation/GroupConcatAggregationUnit.java:
##########
@@ -0,0 +1,55 @@
+/*
+ * 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 lombok.NoArgsConstructor;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.stream.Collectors;
+
+@NoArgsConstructor
+public class GroupConcatAggregationUnit implements AggregationUnit {
+
+ private static final String DEFAULT_SEPARATOR = ",";
+
+ private final Collection<Comparable<?>> values = new ArrayList<>();
+
+ private String separator;
+
+ public GroupConcatAggregationUnit(final String separator) {
+ this.separator = separator;
+ }
+
+ @Override
+ public void merge(final List<Comparable<?>> values) {
+ if (null == values || null == values.get(0)) {
+ return;
+ }
+ this.values.add(values.get(0));
+ }
+
+ @Override
+ public Comparable<?> getResult() {
+ if (null == separator) {
Review Comment:
Can we move this logic to DistinctGroupConcatAggregationUnit constructor?
##########
features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/dql/groupby/aggregation/DistinctGroupConcatAggregationUnit.java:
##########
@@ -0,0 +1,52 @@
+/*
+ * 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 java.util.Collection;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class DistinctGroupConcatAggregationUnit implements AggregationUnit {
+
+ private static final String DEFAULT_SEPARATOR = ",";
+
+ private final Collection<Comparable<?>> values = new LinkedHashSet<>();
+
+ private String separator;
+
+ public DistinctGroupConcatAggregationUnit(final String separator) {
+ this.separator = separator;
+ }
+
+ @Override
+ public void merge(final List<Comparable<?>> values) {
+ if (null == values || null == values.get(0)) {
Review Comment:
What is the behavior of GROUP_CONCAT if values contains null? Ignore NULL
values?
##########
parser/sql/dialect/mysql/src/main/antlr4/imports/mysql/BaseRule.g4:
##########
@@ -962,8 +962,16 @@ udfFunction
: functionName LP_ (expr? | expr (COMMA_ expr)*) RP_
;
+separatorName
Review Comment:
Can you add a sql parse test case for this change?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]