Skip to content
This repository was archived by the owner on May 12, 2021. It is now read-only.
Closed
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 @@ -748,4 +748,33 @@ public void testCorr() throws Exception {
executeString("DROP TABLE testbuiltin11 PURGE");
}
}

@Test
public void testStringAgg() throws Exception {
Schema schema = SchemaBuilder.builder()
.add("id", TajoDataTypes.Type.INT4)
.add("value", TajoDataTypes.Type.TEXT)
.build();
String[] data = new String[]{
"1|\\N",
"1|a",
"1|b",
"1|c",
"2|d",
"2|f"};
TajoTestingCluster.createTable(conf, "testbuiltin11", schema, data, 1);

try {
ResultSet res = executeString("select string_agg(value, '|') as value from testbuiltin11 group by id order by id");
String ascExpected = "value\n" +
"-------------------------------\n" +
"null|a|b|c\n" +
"d|f\n";

assertEquals(ascExpected, resultSetToString(res));
res.close();
} finally {
executeString("DROP TABLE testbuiltin11 PURGE");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/**
* 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.tajo.engine.function.builtin;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.tajo.InternalTypes.StringAggProto;
import org.apache.tajo.catalog.CatalogUtil;
import org.apache.tajo.catalog.Column;
import org.apache.tajo.common.TajoDataTypes.DataType;
import org.apache.tajo.common.TajoDataTypes.Type;
import org.apache.tajo.datum.Datum;
import org.apache.tajo.datum.DatumFactory;
import org.apache.tajo.datum.NullDatum;
import org.apache.tajo.datum.ProtobufDatum;
import org.apache.tajo.engine.function.annotation.Description;
import org.apache.tajo.engine.function.annotation.ParamTypes;
import org.apache.tajo.plan.function.AggFunction;
import org.apache.tajo.plan.function.FunctionContext;
import org.apache.tajo.storage.Tuple;

/**
* Function definition
*
* TEXT string_agg(expr TEXT, delimiter TEXT)
*/
@Description(
functionName = "string_agg",
description = "input values concatenated into a string, separated by delimiter",
example = "> SELECT string_agg(expr, delimiter);",
returnType = Type.TEXT,
paramTypes = { @ParamTypes(paramTypes = { Type.TEXT, Type.TEXT }) }
)
public class StringAgg extends AggFunction<Datum> {

public StringAgg() {
super(new Column[] {
new Column("expr", Type.TEXT),
new Column("delimiter", Type.TEXT)
});
}

@Override
public FunctionContext newContext() {
return new ConcatContext();
}

@Override
public void eval(FunctionContext ctx, Tuple params) {
ConcatContext concatCtx = (ConcatContext) ctx;

String concatData = null;
if (!params.isBlankOrNull(0)) {
concatData = params.getText(0);
}

if (concatCtx.concatData.length() > 0) {
concatCtx.concatData.append(concatCtx.delimiter);
} else {
// When first time, set the delimiter.
concatCtx.delimiter = StringEscapeUtils.unescapeJava(params.getText(1));
}
concatCtx.concatData.append(concatData);
}

@Override
public void merge(FunctionContext context, Tuple part) {
ConcatContext concatCtx = (ConcatContext) context;
if (part.isBlankOrNull(0)) {
return;
}

ProtobufDatum datum = (ProtobufDatum) part.getProtobufDatum(0);
StringAggProto proto = (StringAggProto) datum.get();

String delimiter = proto.getDelimiter();
String concatData = proto.getValue();

if (concatCtx.concatData.length() > 0) {
concatCtx.concatData.append(delimiter);
}
concatCtx.concatData.append(concatData);
}

@Override
public Datum getPartialResult(FunctionContext ctx) {
ConcatContext concatCtx = (ConcatContext) ctx;
if (concatCtx.concatData.length() == 0) {
return NullDatum.get();
}

StringAggProto.Builder builder = StringAggProto.newBuilder();
builder.setDelimiter(concatCtx.delimiter);
builder.setValue(concatCtx.concatData.toString());
return new ProtobufDatum(builder.build());
}

@Override
public DataType getPartialResultType() {
return CatalogUtil.newDataType(Type.PROTOBUF, StringAggProto.class.getName());
}

@Override
public Datum terminate(FunctionContext ctx) {
ConcatContext concatCtx = (ConcatContext) ctx;
if (concatCtx.concatData.length() == 0) {
return NullDatum.get();
} else {
return DatumFactory.createText(concatCtx.concatData.toString());
}
}

protected static class ConcatContext implements FunctionContext {
String delimiter;
StringBuilder concatData = new StringBuilder(128);
}
}
5 changes: 5 additions & 0 deletions tajo-core/src/main/proto/InternalTypes.proto
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,8 @@ message CorrProto {
required double yvar = 5;
required double covar = 6;
}

message StringAggProto {
required string value = 1;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, this proto is unnecessary define. the StringAgg is only to aggregate a value.
What do you think about that?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops. I’m wrong. delimiter should send to context for merge phase.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It needs to keep the second parameter, delimiter, in the merge method of StringAgg when the second stages execute to merge the result of fisrt stage of StringAgg.

required string delimiter = 2;
}