Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] group_concat support distinct #9576

Merged
merged 5 commits into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ under the License.
### description
#### Syntax

`VARCHAR GROUP_CONCAT(VARCHAR str[, VARCHAR sep])`
`VARCHAR GROUP_CONCAT([DISTINCT] VARCHAR str[, VARCHAR sep])`


This function is an aggregation function similar to sum (), and group_concat links multiple rows of results in the result set to a string. The second parameter, sep, is a connection symbol between strings, which can be omitted. This function usually needs to be used with group by statements.
Expand All @@ -43,22 +43,30 @@ mysql> select value from test;
| a |
| b |
| c |
| c |
+-------+

mysql> select GROUP_CONCAT(value) from test;
+-----------------------+
| GROUP_CONCAT(`value`) |
+-----------------------+
| a, b, c |
| a, b, c, c |
+-----------------------+

mysql> select GROUP_CONCAT(value, " ") from test;
+----------------------------+
| GROUP_CONCAT(`value`, ' ') |
+----------------------------+
| a b c |
| a b c c |
+----------------------------+

mysql> select GROUP_CONCAT(DISTINCT value) from test;
+-----------------------+
| GROUP_CONCAT(`value`) |
+-----------------------+
| a, b, c |
+-----------------------+

mysql> select GROUP_CONCAT(value, NULL) from test;
+----------------------------+
| GROUP_CONCAT(`value`, NULL)|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ under the License.
### description
#### Syntax

`VARCHAR GROUP_CONCAT(VARCHAR str[, VARCHAR sep])`
`VARCHAR GROUP_CONCAT([DISTINCT] VARCHAR str[, VARCHAR sep])`


该函数是类似于 sum() 的聚合函数,group_concat 将结果集中的多行结果连接成一个字符串。第二个参数 sep 为字符串之间的连接符号,该参数可以省略。该函数通常需要和 group by 语句一起使用。
Expand All @@ -43,20 +43,28 @@ mysql> select value from test;
| a |
| b |
| c |
| c |
+-------+

mysql> select GROUP_CONCAT(value) from test;
+-----------------------+
| GROUP_CONCAT(`value`) |
+-----------------------+
| a, b, c, c |
+-----------------------+

mysql> select GROUP_CONCAT(DISTINCT value) from test;
+-----------------------+
| GROUP_CONCAT(`value`) |
+-----------------------+
| a, b, c |
+-----------------------+

mysql> select GROUP_CONCAT(value, " ") from test;
+----------------------------+
| GROUP_CONCAT(`value`, ' ') |
+----------------------------+
| a b c |
| a b c c |
+----------------------------+

mysql> select GROUP_CONCAT(value, NULL) from test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,10 +481,6 @@ private void analyzeBuiltinAggFunction(Analyzer analyzer) throws AnalysisExcepti
"group_concat requires one or two parameters: " + this.toSql());
}

if (fnParams.isDistinct()) {
throw new AnalysisException("group_concat does not support DISTINCT");
}

Expr arg0 = getChild(0);
if (!arg0.type.isStringType() && !arg0.type.isNull()) {
throw new AnalysisException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package org.apache.doris.common.util;

import org.apache.parquet.Strings;
import com.google.common.base.Strings;

public class SqlUtils {
public static String escapeUnquote(String ident) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -787,9 +787,12 @@ public void testWithUnionToSql() throws Exception {
@Test
public void testSelectOuterJoinSql() throws Exception {
ConnectContext ctx = UtFrameUtils.createDefaultCtx();
String sql1 = "select l.citycode, group_concat(r.username) from db1.table1 l left join db1.table2 r on l.citycode=r.citycode group by l.citycode";
String sql1 = "select l.citycode, group_concat(distinct r.username) from db1.table1 l "
+ "left join db1.table2 r on l.citycode=r.citycode group by l.citycode";
SelectStmt stmt1 = (SelectStmt) UtFrameUtils.parseAndAnalyzeStmt(sql1, ctx);
Assert.assertTrue(stmt1.getAnalyzer().getSlotDesc(new SlotId(2)).getIsNullable());
Assert.assertTrue(stmt1.getAnalyzer().getSlotDescriptor("r.username").getIsNullable());
FunctionCallExpr expr = (FunctionCallExpr) stmt1.getSelectList().getItems().get(1).getExpr();
Assert.assertTrue(expr.getFnParams().isDistinct());
}
}
7 changes: 7 additions & 0 deletions regression-test/data/query/group_concat/test_group_concat.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !select --
false, false

-- !select --
false

26 changes: 26 additions & 0 deletions regression-test/suites/query/group_concat/test_group_concat.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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.

suite("test_group_concat", "query") {
qt_select """
SELECT group_concat(k6) FROM test_query_db.test where k6='false'
"""

qt_select """
SELECT group_concat(DISTINCT k6) FROM test_query_db.test where k6='false'
"""
}