Skip to content

Commit

Permalink
Updating String sql aggregator as per comments and handling an issue …
Browse files Browse the repository at this point in the history
…with separators
  • Loading branch information
somu-imply committed Aug 3, 2023
1 parent 52f6003 commit c16d926
Showing 1 changed file with 30 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.rex.RexBuilder;
import org.apache.calcite.rex.RexLiteral;
import org.apache.calcite.rex.RexNode;
import org.apache.calcite.sql.SqlAggFunction;
import org.apache.calcite.sql.SqlCallBinding;
import org.apache.calcite.sql.SqlFunctionCategory;
Expand Down Expand Up @@ -109,36 +110,40 @@ public Aggregation toDruidAggregation(
return null;
}

RexNode separatorNode = Expressions.fromFieldAccess(
rexBuilder.getTypeFactory(),
rowSignature,
project,
aggregateCall.getArgList().get(1)
);
if (!separatorNode.isA(SqlKind.LITERAL)) {
// separator must be a literal
return null;
}

final String separator;

if (arguments.size() > 1) {
separator = RexLiteral.stringValue(
Expressions.fromFieldAccess(
rexBuilder.getTypeFactory(),
rowSignature,
project,
aggregateCall.getArgList().get(1)
)
);
} else {
separator = "";
separator = RexLiteral.stringValue(separatorNode);

if (separator == null) {
// separator must not be null
return null;
}

final HumanReadableBytes maxSizeBytes;
Integer maxSizeBytes = null;

if (arguments.size() > 2) {
maxSizeBytes = HumanReadableBytes.valueOf(
RexLiteral.intValue(
Expressions.fromFieldAccess(
rexBuilder.getTypeFactory(),
rowSignature,
project,
aggregateCall.getArgList().get(2)
)
)
RexNode maxBytes = Expressions.fromFieldAccess(
rexBuilder.getTypeFactory(),
rowSignature,
project,
aggregateCall.getArgList().get(2)
);
} else {
maxSizeBytes = null;
if (!maxBytes.isA(SqlKind.LITERAL)) {
// maxBytes must be a literal
return null;
}
maxSizeBytes = ((Number) RexLiteral.value(maxBytes)).intValue();
}

final DruidExpression arg = arguments.get(0);
Expand Down Expand Up @@ -176,7 +181,7 @@ public Aggregation toDruidAggregation(
StringUtils.format("array_set_add_all(\"__acc\", \"%s\")", name),
null,
finalizer,
maxSizeBytes != null ? new HumanReadableBytes(maxSizeBytes.getBytes()) : null,
maxSizeBytes != null ? new HumanReadableBytes(maxSizeBytes) : null,
macroTable
),
dimFilter
Expand All @@ -199,7 +204,7 @@ public Aggregation toDruidAggregation(
StringUtils.format("array_concat(\"__acc\", \"%s\")", name),
null,
finalizer,
maxSizeBytes != null ? new HumanReadableBytes(maxSizeBytes.getBytes()) : null,
maxSizeBytes != null ? new HumanReadableBytes(maxSizeBytes) : null,
macroTable
),
dimFilter
Expand Down

0 comments on commit c16d926

Please sign in to comment.