LakshSingla commented on code in PR #15700: URL: https://github.com/apache/druid/pull/15700#discussion_r1480885114
########## processing/src/main/java/org/apache/druid/query/aggregation/SingleValueAggregatorFactory.java: ########## @@ -0,0 +1,201 @@ +/* + * 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.druid.query.aggregation; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonTypeName; +import com.google.common.base.Preconditions; +import org.apache.druid.error.DruidException; +import org.apache.druid.segment.ColumnSelectorFactory; +import org.apache.druid.segment.ColumnValueSelector; +import org.apache.druid.segment.column.ColumnCapabilities; +import org.apache.druid.segment.column.ColumnType; + +import javax.annotation.Nullable; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.Objects; + +/** + * This AggregatorFactory is meant to wrap the subquery used as an expression into a single value + * and is expected to throw an exception when the subquery results in more than one row + * + * <p> + * This consumes columnType as well along with name and fieldName to pass it on to underlying + * {@link SingleValueBufferAggregator} to work with different ColumnTypes + */ +@JsonTypeName("singleValue") +public class SingleValueAggregatorFactory extends AggregatorFactory +{ + @JsonProperty + @JsonInclude + private final String name; + @JsonProperty + @JsonInclude + private final String fieldName; + @JsonProperty + @JsonInclude + private final ColumnType columnType; + public static final int DEFAULT_MAX_VALUE_SIZE = 1024; + + @JsonCreator + public SingleValueAggregatorFactory( + @JsonProperty("name") String name, + @JsonProperty("fieldName") final String fieldName, + @JsonProperty("columnType") final ColumnType columnType + ) + { + this.name = Preconditions.checkNotNull(name, "name"); + this.fieldName = Preconditions.checkNotNull(fieldName, "fieldName"); + this.columnType = Preconditions.checkNotNull(columnType, "columnType"); + } + + @Override + public Aggregator factorize(ColumnSelectorFactory metricFactory) + { + ColumnValueSelector selector = metricFactory.makeColumnValueSelector(fieldName); + return new SingleValueAggregator(selector); + } + + @Override + public BufferAggregator factorizeBuffered(ColumnSelectorFactory metricFactory) + { + ColumnValueSelector selector = metricFactory.makeColumnValueSelector(fieldName); + ColumnCapabilities columnCapabilities = metricFactory.getColumnCapabilities(fieldName); + Preconditions.checkNotNull(columnCapabilities, "Unable to get the capabilities of [%s]", fieldName); Review Comment: null checks are better done using an `if` clause. Preconditions is a shorthand, we mostly use in the constructor. It doesn't play nice with the DruidExcption system, because now we don't know the target persona for the error message. Therefore, let's throw a DruidException here, because, now it will force us to think of the persona of the error message, and word it accordingly. I think it should be aimed at devs only, because users/admins/operators won't know what to do if such an error occurs, and won't make sense for them. Also, check out https://github.com/apache/druid/blob/master/dev/style-conventions.md#message-formatting-for-logs-and-exceptions. The message should make sense if all the extrapolation ([%s]) is removed. Therefore, the message should be ```suggestion Preconditions.checkNotNull(columnCapabilities, "Unable to get the capabilities of field [%s]", fieldName); ``` ########## processing/src/main/java/org/apache/druid/query/aggregation/SingleValueAggregatorFactory.java: ########## @@ -0,0 +1,201 @@ +/* + * 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.druid.query.aggregation; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonTypeName; +import com.google.common.base.Preconditions; +import org.apache.druid.error.DruidException; +import org.apache.druid.segment.ColumnSelectorFactory; +import org.apache.druid.segment.ColumnValueSelector; +import org.apache.druid.segment.column.ColumnCapabilities; +import org.apache.druid.segment.column.ColumnType; + +import javax.annotation.Nullable; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.Objects; + +/** + * This AggregatorFactory is meant to wrap the subquery used as an expression into a single value + * and is expected to throw an exception when the subquery results in more than one row + * + * <p> + * This consumes columnType as well along with name and fieldName to pass it on to underlying + * {@link SingleValueBufferAggregator} to work with different ColumnTypes + */ +@JsonTypeName("singleValue") +public class SingleValueAggregatorFactory extends AggregatorFactory +{ + @JsonProperty + @JsonInclude + private final String name; + @JsonProperty + @JsonInclude + private final String fieldName; + @JsonProperty + @JsonInclude + private final ColumnType columnType; + public static final int DEFAULT_MAX_VALUE_SIZE = 1024; + + @JsonCreator + public SingleValueAggregatorFactory( + @JsonProperty("name") String name, + @JsonProperty("fieldName") final String fieldName, + @JsonProperty("columnType") final ColumnType columnType + ) + { + this.name = Preconditions.checkNotNull(name, "name"); + this.fieldName = Preconditions.checkNotNull(fieldName, "fieldName"); + this.columnType = Preconditions.checkNotNull(columnType, "columnType"); + } + + @Override + public Aggregator factorize(ColumnSelectorFactory metricFactory) + { + ColumnValueSelector selector = metricFactory.makeColumnValueSelector(fieldName); + return new SingleValueAggregator(selector); + } + + @Override + public BufferAggregator factorizeBuffered(ColumnSelectorFactory metricFactory) + { + ColumnValueSelector selector = metricFactory.makeColumnValueSelector(fieldName); + ColumnCapabilities columnCapabilities = metricFactory.getColumnCapabilities(fieldName); + Preconditions.checkNotNull(columnCapabilities, "Unable to get the capabilities of [%s]", fieldName); + ColumnType columnType = new ColumnType(columnCapabilities.getType(), null, null); + return new SingleValueBufferAggregator(selector, columnType); + } + + @Override + public Comparator getComparator() + { + throw DruidException.defensive("Single Value Aggregator would not have more than one row to compare"); + } + + /** + * Combine method would never be invoked as the broker sends the subquery to multiple segments + * and gather the results to a single value on which the single value aggregator is applied. + * Though getCombiningFactory would be invoked for understanding the fieldname. + */ Review Comment: :rocket: -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
