This repository was archived by the owner on May 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 110
TAJO-2160: Implement string_agg function. #1029
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
tajo-core/src/main/java/org/apache/tajo/engine/function/builtin/StringAgg.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.