LakshSingla commented on code in PR #15700:
URL: https://github.com/apache/druid/pull/15700#discussion_r1475661119


##########
processing/src/main/java/org/apache/druid/query/aggregation/SingleValueAggregatorFactory.java:
##########
@@ -0,0 +1,206 @@
+/*
+ * 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 org.apache.druid.segment.column.ValueType;
+
+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(JsonInclude.Include.NON_NULL)
+  private final String name;
+
+  @JsonProperty
+  @JsonInclude(JsonInclude.Include.NON_NULL)
+  private final String fieldName;
+
+  @JsonProperty
+  @JsonInclude(JsonInclude.Include.NON_NULL)
+  private final ColumnType columnType;
+
+  public static final int DEFAULT_MAX_STRING_SIZE = 1024;
+
+  @JsonCreator
+  public SingleValueAggregatorFactory(
+      @JsonProperty("name") String name,
+      @JsonProperty("fieldName") final String fieldName,
+      @JsonProperty("columnType") final ColumnType columnType
+  )
+  {
+    Preconditions.checkNotNull(name, "Must have a valid, non-null aggregator 
name");
+    Preconditions.checkNotNull(fieldName, "Must have a valid, non-null 
fieldName");
+
+    this.name = name;
+    this.fieldName = fieldName;
+    this.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);
+    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");
+  }
+
+  @Override
+  @Nullable
+  public Object combine(@Nullable Object lhs, @Nullable Object rhs)

Review Comment:
   What would happen if `lhs` represents something empty (i.e. hasn't 
encountered a single row), while `rhs` is something that has encountered a 
single row. In that case `rhs` should be returned. Similarly, the cases for 
when `lhs` represents something which means a single row, while `rhs` is empty; 
or when both are empty). 
   
   I am not sure if this will ever be called if one of the side represents 
"something empty", hence the confusion.
   This also is at odds with `getCombiningFactory`, since that doesn't throw 
upon being called, and returns a correct implementation. I assumed that both 
should have the same semantics. The calling patterns might be such that it's 
okay to not implement the .combine() and throw an exception here, and if so, I 
think there should be some comment explaining why. 



-- 
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]

Reply via email to