You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2022/10/12 00:48:58 UTC

[GitHub] [iceberg] huaxingao opened a new pull request, #5961: add Aggregate Expressions

huaxingao opened a new pull request, #5961:
URL: https://github.com/apache/iceberg/pull/5961

   add Aggregate Expressions. These will be used for push down Max/Min/Count to Iceberg
   Here is the  original [PR](https://github.com/apache/iceberg/pull/5872/)


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on a diff in pull request #5961: add Aggregate Expressions

Posted by GitBox <gi...@apache.org>.
rdblue commented on code in PR #5961:
URL: https://github.com/apache/iceberg/pull/5961#discussion_r1002772765


##########
api/src/main/java/org/apache/iceberg/expressions/UnboundAggregate.java:
##########
@@ -0,0 +1,73 @@
+/*
+ * 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.iceberg.expressions;
+
+import org.apache.iceberg.exceptions.ValidationException;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.types.Types;
+
+public class UnboundAggregate<T> extends Aggregate<UnboundTerm<T>>
+    implements Unbound<T, Expression> {
+
+  UnboundAggregate(Operation op, UnboundTerm<T> term) {
+    super(op, term);
+  }
+
+  @Override
+  public NamedReference<?> ref() {
+    return term().ref();
+  }
+
+  /**
+   * Bind this UnboundAggregate.
+   *
+   * @param struct The {@link Types.StructType struct type} to resolve references by name.
+   * @param caseSensitive A boolean flag to control whether the bind should enforce case
+   *     sensitivity.
+   * @return an {@link Expression}
+   * @throws ValidationException if literals do not match bound references, or if comparison on
+   *     expression is invalid
+   */
+  @Override
+  public Expression bind(Types.StructType struct, boolean caseSensitive) {
+    if (op() == Operation.COUNT_STAR) {
+      return new BoundAggregate<>(op(), null);
+    } else {
+      Preconditions.checkArgument(term() != null, "aggregate term can't be null");
+      BoundTerm<T> bound = term().bind(struct, caseSensitive);
+      return new BoundAggregate<>(op(), bound);
+    }
+  }
+
+  @Override
+  public String toString() {

Review Comment:
   Should `BoundAggregate` also implement `toString`?



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on a diff in pull request #5961: add Aggregate Expressions

Posted by GitBox <gi...@apache.org>.
rdblue commented on code in PR #5961:
URL: https://github.com/apache/iceberg/pull/5961#discussion_r1002772845


##########
api/src/main/java/org/apache/iceberg/expressions/UnboundAggregate.java:
##########
@@ -0,0 +1,73 @@
+/*
+ * 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.iceberg.expressions;
+
+import org.apache.iceberg.exceptions.ValidationException;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.types.Types;
+
+public class UnboundAggregate<T> extends Aggregate<UnboundTerm<T>>
+    implements Unbound<T, Expression> {
+
+  UnboundAggregate(Operation op, UnboundTerm<T> term) {
+    super(op, term);
+  }
+
+  @Override
+  public NamedReference<?> ref() {
+    return term().ref();
+  }
+
+  /**
+   * Bind this UnboundAggregate.
+   *
+   * @param struct The {@link Types.StructType struct type} to resolve references by name.
+   * @param caseSensitive A boolean flag to control whether the bind should enforce case
+   *     sensitivity.
+   * @return an {@link Expression}
+   * @throws ValidationException if literals do not match bound references, or if comparison on
+   *     expression is invalid
+   */
+  @Override
+  public Expression bind(Types.StructType struct, boolean caseSensitive) {
+    if (op() == Operation.COUNT_STAR) {
+      return new BoundAggregate<>(op(), null);
+    } else {
+      Preconditions.checkArgument(term() != null, "aggregate term can't be null");

Review Comment:
   Style: this is not capitalized and it doesn't match the typical error style for this, which is "Invalid aggregate term: null".



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] nastra commented on a diff in pull request #5961: add Aggregate Expressions

Posted by GitBox <gi...@apache.org>.
nastra commented on code in PR #5961:
URL: https://github.com/apache/iceberg/pull/5961#discussion_r1005406682


##########
api/src/test/java/org/apache/iceberg/expressions/TestAggregateBinding.java:
##########
@@ -0,0 +1,116 @@
+/*
+ * 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.iceberg.expressions;
+
+import java.util.Arrays;
+import java.util.List;
+import org.apache.iceberg.exceptions.ValidationException;
+import org.apache.iceberg.types.Types;
+import org.apache.iceberg.types.Types.StructType;
+import org.assertj.core.api.Assertions;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestAggregateBinding {
+  private static final List<Expression.Operation> AGGREGATES =
+      Arrays.asList(Expression.Operation.COUNT, Expression.Operation.MAX, Expression.Operation.MIN);
+  private static final StructType struct =
+      StructType.of(Types.NestedField.required(10, "x", Types.IntegerType.get()));
+
+  @Test
+  public void testAggregateBinding() {
+    for (Expression.Operation op : AGGREGATES) {
+      UnboundAggregate unbound = null;
+      switch (op) {
+        case COUNT:
+          unbound = Expressions.count("x");
+          break;
+        case MAX:
+          unbound = Expressions.max("x");
+          break;
+        case MIN:
+          unbound = Expressions.min("x");
+          break;
+        default:
+          throw new UnsupportedOperationException("Invalid aggregate: " + op);
+      }
+
+      Expression expr = unbound.bind(struct, true);
+      BoundAggregate bound = assertAndUnwrapAggregate(expr);
+
+      Assert.assertEquals("Should reference correct field ID", 10, bound.ref().fieldId());
+      Assert.assertEquals("Should not change the comparison operation", op, bound.op());
+    }
+  }
+
+  @Test
+  public void testCountStarBinding() {
+    UnboundAggregate unbound = Expressions.countStar();
+    Expression expr = unbound.bind(null, false);
+    BoundAggregate bound = assertAndUnwrapAggregate(expr);
+
+    Assert.assertEquals(
+        "Should not change the comparison operation", Expression.Operation.COUNT_STAR, bound.op());
+  }
+
+  @Test
+  public void testBoundAggregateFails() {
+    Expression unbound = Expressions.count("x");
+    Assertions.assertThatThrownBy(() -> Binder.bind(struct, Binder.bind(struct, unbound)))
+        .isInstanceOf(IllegalStateException.class)
+        .hasMessageContaining("Found already bound aggregate");
+  }
+
+  @Test
+  public void testCaseInsensitiveReference() {
+    Expression expr = Expressions.max("X");
+    Expression boundExpr = Binder.bind(struct, expr, false);
+    BoundAggregate bound = assertAndUnwrapAggregate(boundExpr);
+    Assert.assertEquals("Should reference correct field ID", 10, bound.ref().fieldId());
+    Assert.assertEquals(
+        "Should not change the comparison operation", Expression.Operation.MAX, bound.op());
+  }
+
+  @Test
+  public void testCaseSensitiveReference() {
+    Expression expr = Expressions.max("X");
+    Assertions.assertThatThrownBy(() -> Binder.bind(struct, expr, true))
+        .isInstanceOf(ValidationException.class)
+        .hasMessageContaining("Cannot find field 'X' in struct");
+  }
+
+  @Test
+  public void testMissingField() {
+    UnboundAggregate unbound = Expressions.count("missing");
+    try {
+      unbound.bind(struct, false);
+      Assert.fail("Binding a missing field should fail");
+    } catch (ValidationException e) {

Review Comment:
   can we use `Assertions.assertThatThrownBy` here rather than a `try-catch` with `Assert.fail`?



##########
api/src/test/java/org/apache/iceberg/expressions/TestAggregateBinding.java:
##########
@@ -0,0 +1,116 @@
+/*
+ * 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.iceberg.expressions;
+
+import java.util.Arrays;
+import java.util.List;
+import org.apache.iceberg.exceptions.ValidationException;
+import org.apache.iceberg.types.Types;
+import org.apache.iceberg.types.Types.StructType;
+import org.assertj.core.api.Assertions;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestAggregateBinding {
+  private static final List<Expression.Operation> AGGREGATES =
+      Arrays.asList(Expression.Operation.COUNT, Expression.Operation.MAX, Expression.Operation.MIN);
+  private static final StructType struct =
+      StructType.of(Types.NestedField.required(10, "x", Types.IntegerType.get()));
+
+  @Test
+  public void testAggregateBinding() {
+    for (Expression.Operation op : AGGREGATES) {
+      UnboundAggregate unbound = null;
+      switch (op) {
+        case COUNT:
+          unbound = Expressions.count("x");
+          break;
+        case MAX:
+          unbound = Expressions.max("x");
+          break;
+        case MIN:
+          unbound = Expressions.min("x");
+          break;
+        default:
+          throw new UnsupportedOperationException("Invalid aggregate: " + op);
+      }
+
+      Expression expr = unbound.bind(struct, true);
+      BoundAggregate bound = assertAndUnwrapAggregate(expr);
+
+      Assert.assertEquals("Should reference correct field ID", 10, bound.ref().fieldId());
+      Assert.assertEquals("Should not change the comparison operation", op, bound.op());
+    }
+  }
+
+  @Test
+  public void testCountStarBinding() {
+    UnboundAggregate unbound = Expressions.countStar();
+    Expression expr = unbound.bind(null, false);
+    BoundAggregate bound = assertAndUnwrapAggregate(expr);
+
+    Assert.assertEquals(
+        "Should not change the comparison operation", Expression.Operation.COUNT_STAR, bound.op());
+  }
+
+  @Test
+  public void testBoundAggregateFails() {
+    Expression unbound = Expressions.count("x");
+    Assertions.assertThatThrownBy(() -> Binder.bind(struct, Binder.bind(struct, unbound)))
+        .isInstanceOf(IllegalStateException.class)
+        .hasMessageContaining("Found already bound aggregate");
+  }
+
+  @Test
+  public void testCaseInsensitiveReference() {
+    Expression expr = Expressions.max("X");
+    Expression boundExpr = Binder.bind(struct, expr, false);
+    BoundAggregate bound = assertAndUnwrapAggregate(boundExpr);
+    Assert.assertEquals("Should reference correct field ID", 10, bound.ref().fieldId());
+    Assert.assertEquals(
+        "Should not change the comparison operation", Expression.Operation.MAX, bound.op());
+  }
+
+  @Test
+  public void testCaseSensitiveReference() {
+    Expression expr = Expressions.max("X");
+    Assertions.assertThatThrownBy(() -> Binder.bind(struct, expr, true))
+        .isInstanceOf(ValidationException.class)
+        .hasMessageContaining("Cannot find field 'X' in struct");
+  }
+
+  @Test
+  public void testMissingField() {
+    UnboundAggregate unbound = Expressions.count("missing");
+    try {
+      unbound.bind(struct, false);
+      Assert.fail("Binding a missing field should fail");
+    } catch (ValidationException e) {
+      Assert.assertTrue(
+          "Validation should complain about missing field",
+          e.getMessage().contains("Cannot find field 'missing' in struct:"));
+    }
+  }
+
+  private static <T, C> BoundAggregate<T, C> assertAndUnwrapAggregate(Expression expr) {
+    Assert.assertTrue(

Review Comment:
   it might be worth to also update this to something like `Assertions.assertThat(expr).isInstanceOf(BoundAggregate.class)`



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] huaxingao commented on a diff in pull request #5961: add Aggregate Expressions

Posted by GitBox <gi...@apache.org>.
huaxingao commented on code in PR #5961:
URL: https://github.com/apache/iceberg/pull/5961#discussion_r1002801854


##########
api/src/main/java/org/apache/iceberg/expressions/UnboundAggregate.java:
##########
@@ -0,0 +1,73 @@
+/*
+ * 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.iceberg.expressions;
+
+import org.apache.iceberg.exceptions.ValidationException;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.types.Types;
+
+public class UnboundAggregate<T> extends Aggregate<UnboundTerm<T>>
+    implements Unbound<T, Expression> {
+
+  UnboundAggregate(Operation op, UnboundTerm<T> term) {
+    super(op, term);
+  }
+
+  @Override
+  public NamedReference<?> ref() {
+    return term().ref();
+  }
+
+  /**
+   * Bind this UnboundAggregate.
+   *
+   * @param struct The {@link Types.StructType struct type} to resolve references by name.
+   * @param caseSensitive A boolean flag to control whether the bind should enforce case
+   *     sensitivity.
+   * @return an {@link Expression}
+   * @throws ValidationException if literals do not match bound references, or if comparison on
+   *     expression is invalid
+   */
+  @Override
+  public Expression bind(Types.StructType struct, boolean caseSensitive) {
+    if (op() == Operation.COUNT_STAR) {
+      return new BoundAggregate<>(op(), null);
+    } else {
+      Preconditions.checkArgument(term() != null, "aggregate term can't be null");

Review Comment:
   Fixed. Thanks!



##########
api/src/main/java/org/apache/iceberg/expressions/UnboundAggregate.java:
##########
@@ -0,0 +1,73 @@
+/*
+ * 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.iceberg.expressions;
+
+import org.apache.iceberg.exceptions.ValidationException;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.types.Types;
+
+public class UnboundAggregate<T> extends Aggregate<UnboundTerm<T>>
+    implements Unbound<T, Expression> {
+
+  UnboundAggregate(Operation op, UnboundTerm<T> term) {
+    super(op, term);
+  }
+
+  @Override
+  public NamedReference<?> ref() {
+    return term().ref();
+  }
+
+  /**
+   * Bind this UnboundAggregate.
+   *
+   * @param struct The {@link Types.StructType struct type} to resolve references by name.
+   * @param caseSensitive A boolean flag to control whether the bind should enforce case
+   *     sensitivity.
+   * @return an {@link Expression}
+   * @throws ValidationException if literals do not match bound references, or if comparison on
+   *     expression is invalid
+   */
+  @Override
+  public Expression bind(Types.StructType struct, boolean caseSensitive) {
+    if (op() == Operation.COUNT_STAR) {
+      return new BoundAggregate<>(op(), null);
+    } else {
+      Preconditions.checkArgument(term() != null, "aggregate term can't be null");
+      BoundTerm<T> bound = term().bind(struct, caseSensitive);
+      return new BoundAggregate<>(op(), bound);
+    }
+  }
+
+  @Override
+  public String toString() {
+    switch (op()) {
+      case COUNT:
+        return "count(" + term() + ")";
+      case COUNT_STAR:
+        return "count(*)";
+      case MAX:
+        return "max(" + term() + ")";
+      case MIN:
+        return "min(" + term() + ")";
+      default:
+        return "Aggregate is not supported: operation = " + op();

Review Comment:
   Fixed. Thanks!



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on pull request #5961: add Aggregate Expressions

Posted by GitBox <gi...@apache.org>.
rdblue commented on PR #5961:
URL: https://github.com/apache/iceberg/pull/5961#issuecomment-1291158511

   Thanks, @huaxingao!


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on a diff in pull request #5961: add Aggregate Expressions

Posted by GitBox <gi...@apache.org>.
rdblue commented on code in PR #5961:
URL: https://github.com/apache/iceberg/pull/5961#discussion_r1004975423


##########
api/src/test/java/org/apache/iceberg/expressions/TestAggregateBinding.java:
##########
@@ -0,0 +1,116 @@
+/*
+ * 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.iceberg.expressions;
+
+import java.util.Arrays;
+import java.util.List;
+import org.apache.iceberg.exceptions.ValidationException;
+import org.apache.iceberg.types.Types;
+import org.apache.iceberg.types.Types.StructType;
+import org.assertj.core.api.Assertions;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestAggregateBinding {
+  private static final List<Expression.Operation> AGGREGATES =
+      Arrays.asList(Expression.Operation.COUNT, Expression.Operation.MAX, Expression.Operation.MIN);
+  private static final StructType struct =
+      StructType.of(Types.NestedField.required(10, "x", Types.IntegerType.get()));
+
+  @Test
+  public void testAggregateBinding() {
+    for (Expression.Operation op : AGGREGATES) {

Review Comment:
   Since `AGGREGATES` is only used here, I'd probably recommend refactoring so that you have a list of expressions for this loop:
   
   ```java
     private static final List<Expression> = ImmutableList.of(Expressions.count("x"), Expressions.max("x"), Expressions.min("x"));
     ...
     for (Expression unbound : AGGREGATES) {
       ...
     }
   ```



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] huaxingao commented on pull request #5961: add Aggregate Expressions

Posted by GitBox <gi...@apache.org>.
huaxingao commented on PR #5961:
URL: https://github.com/apache/iceberg/pull/5961#issuecomment-1291186808

   Thanks a lot! @rdblue I will refactor `AGGREGATES` to a list of expressions in my next PR.


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue merged pull request #5961: add Aggregate Expressions

Posted by GitBox <gi...@apache.org>.
rdblue merged PR #5961:
URL: https://github.com/apache/iceberg/pull/5961


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on a diff in pull request #5961: add Aggregate Expressions

Posted by GitBox <gi...@apache.org>.
rdblue commented on code in PR #5961:
URL: https://github.com/apache/iceberg/pull/5961#discussion_r1002772731


##########
api/src/main/java/org/apache/iceberg/expressions/BoundAggregate.java:
##########
@@ -0,0 +1,47 @@
+/*
+ * 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.iceberg.expressions;
+
+import org.apache.iceberg.StructLike;
+import org.apache.iceberg.types.Type;
+import org.apache.iceberg.types.Types;
+
+public class BoundAggregate<T, C> extends Aggregate<BoundTerm<T>> implements Bound<C> {
+  protected BoundAggregate(Operation op, BoundTerm<T> term) {
+    super(op, term);
+  }
+
+  @Override
+  public C eval(StructLike struct) {
+    throw new UnsupportedOperationException(this.getClass().getName() + " does not implement eval");
+  }
+
+  @Override
+  public BoundReference<?> ref() {
+    return term().ref();
+  }
+
+  public Type type() {

Review Comment:
   Oh, I see that there aren't subclasses. Nevermind, this is okay.



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on a diff in pull request #5961: add Aggregate Expressions

Posted by GitBox <gi...@apache.org>.
rdblue commented on code in PR #5961:
URL: https://github.com/apache/iceberg/pull/5961#discussion_r1002772465


##########
api/src/main/java/org/apache/iceberg/expressions/ExpressionVisitors.java:
##########
@@ -55,6 +55,14 @@ public <T> R predicate(BoundPredicate<T> pred) {
     public <T> R predicate(UnboundPredicate<T> pred) {
       return null;
     }
+
+    public <T, C> R aggregate(BoundAggregate<T, C> agg) {
+      throw new UnsupportedOperationException("aggregate is not supported");

Review Comment:
   Style: error messages must always use sentence capitalization; "Cannot visit aggregate expression".



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] huaxingao commented on pull request #5961: add Aggregate Expressions

Posted by GitBox <gi...@apache.org>.
huaxingao commented on PR #5961:
URL: https://github.com/apache/iceberg/pull/5961#issuecomment-1276824769

   cc @rdblue @aokolnychyi @flyrain 
   This PR is ready for review. Could you please take a look when you have a moment?


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on a diff in pull request #5961: add Aggregate Expressions

Posted by GitBox <gi...@apache.org>.
rdblue commented on code in PR #5961:
URL: https://github.com/apache/iceberg/pull/5961#discussion_r1002773496


##########
spark/v3.3/spark/src/main/java/org/apache/iceberg/spark/SparkAggregates.java:
##########
@@ -0,0 +1,79 @@
+/*
+ * 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.iceberg.spark;
+
+import java.util.Map;
+import org.apache.iceberg.expressions.Expression;
+import org.apache.iceberg.expressions.Expression.Operation;
+import org.apache.iceberg.expressions.Expressions;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.spark.sql.connector.expressions.NamedReference;
+import org.apache.spark.sql.connector.expressions.aggregate.AggregateFunc;
+import org.apache.spark.sql.connector.expressions.aggregate.Count;
+import org.apache.spark.sql.connector.expressions.aggregate.CountStar;
+import org.apache.spark.sql.connector.expressions.aggregate.Max;
+import org.apache.spark.sql.connector.expressions.aggregate.Min;
+
+public class SparkAggregates {
+  private SparkAggregates() {}
+
+  private static final Map<Class<? extends AggregateFunc>, Operation> AGGREGATES =
+      ImmutableMap.<Class<? extends AggregateFunc>, Operation>builder()
+          .put(Count.class, Operation.COUNT)
+          .put(CountStar.class, Operation.COUNT_STAR)
+          .put(Max.class, Operation.MAX)
+          .put(Min.class, Operation.MIN)
+          .build();
+
+  public static Expression convert(AggregateFunc aggregate) {
+    Operation op = AGGREGATES.get(aggregate.getClass());
+    if (op != null) {
+      switch (op) {
+        case COUNT:
+          Count countAgg = (Count) aggregate;
+          if (countAgg.isDistinct()) {
+            // manifest file doesn't have count distinct so this can't be converted to push down
+            return null;
+          }
+          if (countAgg.column() instanceof NamedReference) {

Review Comment:
   Style: Try to add empty lines between control flow blocks and the following statement or block.



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] huaxingao commented on a diff in pull request #5961: add Aggregate Expressions

Posted by GitBox <gi...@apache.org>.
huaxingao commented on code in PR #5961:
URL: https://github.com/apache/iceberg/pull/5961#discussion_r1002801800


##########
api/src/main/java/org/apache/iceberg/expressions/ExpressionVisitors.java:
##########
@@ -55,6 +55,14 @@ public <T> R predicate(BoundPredicate<T> pred) {
     public <T> R predicate(UnboundPredicate<T> pred) {
       return null;
     }
+
+    public <T, C> R aggregate(BoundAggregate<T, C> agg) {
+      throw new UnsupportedOperationException("aggregate is not supported");
+    }
+
+    public <T> R aggregate(UnboundAggregate<T> agg) {
+      return null;

Review Comment:
   Changed. Thanks!



##########
api/src/main/java/org/apache/iceberg/expressions/UnboundAggregate.java:
##########
@@ -0,0 +1,73 @@
+/*
+ * 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.iceberg.expressions;
+
+import org.apache.iceberg.exceptions.ValidationException;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.types.Types;
+
+public class UnboundAggregate<T> extends Aggregate<UnboundTerm<T>>
+    implements Unbound<T, Expression> {
+
+  UnboundAggregate(Operation op, UnboundTerm<T> term) {
+    super(op, term);
+  }
+
+  @Override
+  public NamedReference<?> ref() {
+    return term().ref();
+  }
+
+  /**
+   * Bind this UnboundAggregate.
+   *
+   * @param struct The {@link Types.StructType struct type} to resolve references by name.
+   * @param caseSensitive A boolean flag to control whether the bind should enforce case
+   *     sensitivity.
+   * @return an {@link Expression}
+   * @throws ValidationException if literals do not match bound references, or if comparison on
+   *     expression is invalid
+   */
+  @Override
+  public Expression bind(Types.StructType struct, boolean caseSensitive) {
+    if (op() == Operation.COUNT_STAR) {
+      return new BoundAggregate<>(op(), null);
+    } else {
+      Preconditions.checkArgument(term() != null, "aggregate term can't be null");
+      BoundTerm<T> bound = term().bind(struct, caseSensitive);
+      return new BoundAggregate<>(op(), bound);
+    }
+  }
+
+  @Override
+  public String toString() {

Review Comment:
   Right. I moved this method to `Aggregate`.



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on a diff in pull request #5961: add Aggregate Expressions

Posted by GitBox <gi...@apache.org>.
rdblue commented on code in PR #5961:
URL: https://github.com/apache/iceberg/pull/5961#discussion_r1002772485


##########
api/src/main/java/org/apache/iceberg/expressions/ExpressionVisitors.java:
##########
@@ -55,6 +55,14 @@ public <T> R predicate(BoundPredicate<T> pred) {
     public <T> R predicate(UnboundPredicate<T> pred) {
       return null;
     }
+
+    public <T, C> R aggregate(BoundAggregate<T, C> agg) {
+      throw new UnsupportedOperationException("aggregate is not supported");
+    }
+
+    public <T> R aggregate(UnboundAggregate<T> agg) {
+      return null;

Review Comment:
   I think this should also throw `UnsupportedOperationException`



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on a diff in pull request #5961: add Aggregate Expressions

Posted by GitBox <gi...@apache.org>.
rdblue commented on code in PR #5961:
URL: https://github.com/apache/iceberg/pull/5961#discussion_r1002772326


##########
api/src/main/java/org/apache/iceberg/expressions/BoundAggregate.java:
##########
@@ -0,0 +1,47 @@
+/*
+ * 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.iceberg.expressions;
+
+import org.apache.iceberg.StructLike;
+import org.apache.iceberg.types.Type;
+import org.apache.iceberg.types.Types;
+
+public class BoundAggregate<T, C> extends Aggregate<BoundTerm<T>> implements Bound<C> {
+  protected BoundAggregate(Operation op, BoundTerm<T> term) {
+    super(op, term);
+  }
+
+  @Override
+  public C eval(StructLike struct) {
+    throw new UnsupportedOperationException(this.getClass().getName() + " does not implement eval");
+  }
+
+  @Override
+  public BoundReference<?> ref() {
+    return term().ref();
+  }
+
+  public Type type() {

Review Comment:
   I'm a little uncomfortable having this implementation here. What happens if another aggregate is added and someone forgets to update this method? Then the aggregate will produce `term().type()` whether or not that's correct.
   
   It would be better to put this in the individual implementations.



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on a diff in pull request #5961: add Aggregate Expressions

Posted by GitBox <gi...@apache.org>.
rdblue commented on code in PR #5961:
URL: https://github.com/apache/iceberg/pull/5961#discussion_r1002773463


##########
spark/v3.3/spark/src/main/java/org/apache/iceberg/spark/SparkAggregates.java:
##########
@@ -0,0 +1,79 @@
+/*
+ * 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.iceberg.spark;
+
+import java.util.Map;
+import org.apache.iceberg.expressions.Expression;
+import org.apache.iceberg.expressions.Expression.Operation;
+import org.apache.iceberg.expressions.Expressions;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.spark.sql.connector.expressions.NamedReference;
+import org.apache.spark.sql.connector.expressions.aggregate.AggregateFunc;
+import org.apache.spark.sql.connector.expressions.aggregate.Count;
+import org.apache.spark.sql.connector.expressions.aggregate.CountStar;
+import org.apache.spark.sql.connector.expressions.aggregate.Max;
+import org.apache.spark.sql.connector.expressions.aggregate.Min;
+
+public class SparkAggregates {
+  private SparkAggregates() {}
+
+  private static final Map<Class<? extends AggregateFunc>, Operation> AGGREGATES =
+      ImmutableMap.<Class<? extends AggregateFunc>, Operation>builder()
+          .put(Count.class, Operation.COUNT)
+          .put(CountStar.class, Operation.COUNT_STAR)
+          .put(Max.class, Operation.MAX)
+          .put(Min.class, Operation.MIN)
+          .build();
+
+  public static Expression convert(AggregateFunc aggregate) {
+    Operation op = AGGREGATES.get(aggregate.getClass());
+    if (op != null) {
+      switch (op) {
+        case COUNT:
+          Count countAgg = (Count) aggregate;
+          if (countAgg.isDistinct()) {
+            // manifest file doesn't have count distinct so this can't be converted to push down
+            return null;

Review Comment:
   This isn't in the Java API, but the spec does allow setting a map of distinct counts if we want to start pushing this down.



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on a diff in pull request #5961: add Aggregate Expressions

Posted by GitBox <gi...@apache.org>.
rdblue commented on code in PR #5961:
URL: https://github.com/apache/iceberg/pull/5961#discussion_r1002773289


##########
api/src/test/java/org/apache/iceberg/expressions/TestAggregateBinding.java:
##########
@@ -0,0 +1,110 @@
+/*
+ * 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.iceberg.expressions;
+
+import static org.apache.iceberg.expressions.Expression.Operation.COUNT;
+import static org.apache.iceberg.expressions.Expression.Operation.COUNT_STAR;
+import static org.apache.iceberg.expressions.Expression.Operation.MAX;
+import static org.apache.iceberg.expressions.Expression.Operation.MIN;
+import static org.apache.iceberg.expressions.Expressions.max;
+import static org.apache.iceberg.expressions.Expressions.ref;

Review Comment:
   We typically avoid static imports. It's better to use `Expressions.max`.



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] huaxingao commented on a diff in pull request #5961: add Aggregate Expressions

Posted by GitBox <gi...@apache.org>.
huaxingao commented on code in PR #5961:
URL: https://github.com/apache/iceberg/pull/5961#discussion_r1002801896


##########
api/src/test/java/org/apache/iceberg/expressions/TestAggregateBinding.java:
##########
@@ -0,0 +1,110 @@
+/*
+ * 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.iceberg.expressions;
+
+import static org.apache.iceberg.expressions.Expression.Operation.COUNT;
+import static org.apache.iceberg.expressions.Expression.Operation.COUNT_STAR;
+import static org.apache.iceberg.expressions.Expression.Operation.MAX;
+import static org.apache.iceberg.expressions.Expression.Operation.MIN;
+import static org.apache.iceberg.expressions.Expressions.max;
+import static org.apache.iceberg.expressions.Expressions.ref;
+import static org.apache.iceberg.types.Types.NestedField.required;
+
+import java.util.Arrays;
+import java.util.List;
+import org.apache.iceberg.exceptions.ValidationException;
+import org.apache.iceberg.types.Types;
+import org.apache.iceberg.types.Types.StructType;
+import org.assertj.core.api.Assertions;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestAggregateBinding {
+  private static final List<Expression.Operation> AGGREGATES = Arrays.asList(COUNT, MAX, MIN);
+  private static final StructType struct =
+      StructType.of(required(10, "x", Types.IntegerType.get()));
+
+  @Test
+  @SuppressWarnings("unchecked")
+  public void testAggregateBinding() {
+    for (Expression.Operation op : AGGREGATES) {
+      UnboundAggregate unbound = new UnboundAggregate(op, ref("x"));

Review Comment:
   Fixed. Thanks!



##########
api/src/test/java/org/apache/iceberg/expressions/TestAggregateBinding.java:
##########
@@ -0,0 +1,110 @@
+/*
+ * 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.iceberg.expressions;
+
+import static org.apache.iceberg.expressions.Expression.Operation.COUNT;
+import static org.apache.iceberg.expressions.Expression.Operation.COUNT_STAR;
+import static org.apache.iceberg.expressions.Expression.Operation.MAX;
+import static org.apache.iceberg.expressions.Expression.Operation.MIN;
+import static org.apache.iceberg.expressions.Expressions.max;
+import static org.apache.iceberg.expressions.Expressions.ref;

Review Comment:
   Removed static imports. Thanks!



##########
spark/v3.3/spark/src/main/java/org/apache/iceberg/spark/SparkAggregates.java:
##########
@@ -0,0 +1,79 @@
+/*
+ * 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.iceberg.spark;
+
+import java.util.Map;
+import org.apache.iceberg.expressions.Expression;
+import org.apache.iceberg.expressions.Expression.Operation;
+import org.apache.iceberg.expressions.Expressions;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.spark.sql.connector.expressions.NamedReference;
+import org.apache.spark.sql.connector.expressions.aggregate.AggregateFunc;
+import org.apache.spark.sql.connector.expressions.aggregate.Count;
+import org.apache.spark.sql.connector.expressions.aggregate.CountStar;
+import org.apache.spark.sql.connector.expressions.aggregate.Max;
+import org.apache.spark.sql.connector.expressions.aggregate.Min;
+
+public class SparkAggregates {
+  private SparkAggregates() {}
+
+  private static final Map<Class<? extends AggregateFunc>, Operation> AGGREGATES =
+      ImmutableMap.<Class<? extends AggregateFunc>, Operation>builder()
+          .put(Count.class, Operation.COUNT)
+          .put(CountStar.class, Operation.COUNT_STAR)
+          .put(Max.class, Operation.MAX)
+          .put(Min.class, Operation.MIN)
+          .build();
+
+  public static Expression convert(AggregateFunc aggregate) {
+    Operation op = AGGREGATES.get(aggregate.getClass());
+    if (op != null) {
+      switch (op) {
+        case COUNT:
+          Count countAgg = (Count) aggregate;
+          if (countAgg.isDistinct()) {
+            // manifest file doesn't have count distinct so this can't be converted to push down
+            return null;
+          }
+          if (countAgg.column() instanceof NamedReference) {

Review Comment:
   Sorry I forgot this again. Fixed.



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] huaxingao commented on a diff in pull request #5961: add Aggregate Expressions

Posted by GitBox <gi...@apache.org>.
huaxingao commented on code in PR #5961:
URL: https://github.com/apache/iceberg/pull/5961#discussion_r1002801963


##########
spark/v3.3/spark/src/test/java/org/apache/iceberg/spark/source/TestSparkAggregates.java:
##########
@@ -0,0 +1,77 @@
+/*
+ * 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.iceberg.spark.source;
+
+import java.util.Map;
+import org.apache.iceberg.expressions.Expression;
+import org.apache.iceberg.expressions.Expressions;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.apache.iceberg.spark.SparkAggregates;
+import org.apache.spark.sql.connector.expressions.FieldReference;
+import org.apache.spark.sql.connector.expressions.NamedReference;
+import org.apache.spark.sql.connector.expressions.aggregate.Count;
+import org.apache.spark.sql.connector.expressions.aggregate.CountStar;
+import org.apache.spark.sql.connector.expressions.aggregate.Max;
+import org.apache.spark.sql.connector.expressions.aggregate.Min;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestSparkAggregates {
+
+  @Test
+  public void testAggregates() {
+    Map<String, String> attrMap = Maps.newHashMap();
+    attrMap.put("id", "id");
+    attrMap.put("`i.d`", "i.d");
+    attrMap.put("`i``d`", "i`d");
+    attrMap.put("`d`.b.`dd```", "d.b.dd`");
+    attrMap.put("a.`aa```.c", "a.aa`.c");
+
+    attrMap.forEach(
+        (quoted, unquoted) -> {
+          NamedReference namedReference = FieldReference.apply(quoted);
+
+          Max max = new Max(namedReference);
+          Expression expectedMax = Expressions.max(unquoted);
+          Expression acturalMax = SparkAggregates.convert(max);

Review Comment:
   Fixed. Thanks!



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] huaxingao commented on pull request #5961: add Aggregate Expressions

Posted by GitBox <gi...@apache.org>.
huaxingao commented on PR #5961:
URL: https://github.com/apache/iceberg/pull/5961#issuecomment-1292998499

   @nastra Thanks for your review! I have a follow-up [PR](https://github.com/apache/iceberg/pull/6065) to address your comments.


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on a diff in pull request #5961: add Aggregate Expressions

Posted by GitBox <gi...@apache.org>.
rdblue commented on code in PR #5961:
URL: https://github.com/apache/iceberg/pull/5961#discussion_r1002772983


##########
api/src/main/java/org/apache/iceberg/expressions/UnboundAggregate.java:
##########
@@ -0,0 +1,73 @@
+/*
+ * 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.iceberg.expressions;
+
+import org.apache.iceberg.exceptions.ValidationException;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.types.Types;
+
+public class UnboundAggregate<T> extends Aggregate<UnboundTerm<T>>
+    implements Unbound<T, Expression> {
+
+  UnboundAggregate(Operation op, UnboundTerm<T> term) {
+    super(op, term);
+  }
+
+  @Override
+  public NamedReference<?> ref() {
+    return term().ref();
+  }
+
+  /**
+   * Bind this UnboundAggregate.
+   *
+   * @param struct The {@link Types.StructType struct type} to resolve references by name.
+   * @param caseSensitive A boolean flag to control whether the bind should enforce case
+   *     sensitivity.
+   * @return an {@link Expression}
+   * @throws ValidationException if literals do not match bound references, or if comparison on
+   *     expression is invalid
+   */
+  @Override
+  public Expression bind(Types.StructType struct, boolean caseSensitive) {
+    if (op() == Operation.COUNT_STAR) {
+      return new BoundAggregate<>(op(), null);
+    } else {
+      Preconditions.checkArgument(term() != null, "aggregate term can't be null");
+      BoundTerm<T> bound = term().bind(struct, caseSensitive);
+      return new BoundAggregate<>(op(), bound);
+    }
+  }
+
+  @Override
+  public String toString() {
+    switch (op()) {
+      case COUNT:
+        return "count(" + term() + ")";
+      case COUNT_STAR:
+        return "count(*)";
+      case MAX:
+        return "max(" + term() + ")";
+      case MIN:
+        return "min(" + term() + ")";
+      default:
+        return "Aggregate is not supported: operation = " + op();

Review Comment:
   Throw `UnsupportedOperationException` instead. It isn't correct to return a string like this in an expression.



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on pull request #5961: add Aggregate Expressions

Posted by GitBox <gi...@apache.org>.
rdblue commented on PR #5961:
URL: https://github.com/apache/iceberg/pull/5961#issuecomment-1288203252

   Thanks, @huaxingao! This is very close. Just a few minor things to fix.


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] huaxingao commented on a diff in pull request #5961: add Aggregate Expressions

Posted by GitBox <gi...@apache.org>.
huaxingao commented on code in PR #5961:
URL: https://github.com/apache/iceberg/pull/5961#discussion_r1002801749


##########
api/src/main/java/org/apache/iceberg/expressions/ExpressionVisitors.java:
##########
@@ -55,6 +55,14 @@ public <T> R predicate(BoundPredicate<T> pred) {
     public <T> R predicate(UnboundPredicate<T> pred) {
       return null;
     }
+
+    public <T, C> R aggregate(BoundAggregate<T, C> agg) {
+      throw new UnsupportedOperationException("aggregate is not supported");

Review Comment:
   Fixed. Thanks!



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] huaxingao commented on pull request #5961: add Aggregate Expressions

Posted by GitBox <gi...@apache.org>.
huaxingao commented on PR #5961:
URL: https://github.com/apache/iceberg/pull/5961#issuecomment-1289199629

   @rdblue Thank you very much for your review! I have addressed the comments. Could you please take one more look when you have time? Thanks!


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on a diff in pull request #5961: add Aggregate Expressions

Posted by GitBox <gi...@apache.org>.
rdblue commented on code in PR #5961:
URL: https://github.com/apache/iceberg/pull/5961#discussion_r1002773167


##########
api/src/test/java/org/apache/iceberg/expressions/TestAggregateBinding.java:
##########
@@ -0,0 +1,110 @@
+/*
+ * 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.iceberg.expressions;
+
+import static org.apache.iceberg.expressions.Expression.Operation.COUNT;
+import static org.apache.iceberg.expressions.Expression.Operation.COUNT_STAR;
+import static org.apache.iceberg.expressions.Expression.Operation.MAX;
+import static org.apache.iceberg.expressions.Expression.Operation.MIN;
+import static org.apache.iceberg.expressions.Expressions.max;
+import static org.apache.iceberg.expressions.Expressions.ref;
+import static org.apache.iceberg.types.Types.NestedField.required;
+
+import java.util.Arrays;
+import java.util.List;
+import org.apache.iceberg.exceptions.ValidationException;
+import org.apache.iceberg.types.Types;
+import org.apache.iceberg.types.Types.StructType;
+import org.assertj.core.api.Assertions;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestAggregateBinding {
+  private static final List<Expression.Operation> AGGREGATES = Arrays.asList(COUNT, MAX, MIN);
+  private static final StructType struct =
+      StructType.of(required(10, "x", Types.IntegerType.get()));
+
+  @Test
+  @SuppressWarnings("unchecked")
+  public void testAggregateBinding() {
+    for (Expression.Operation op : AGGREGATES) {
+      UnboundAggregate unbound = new UnboundAggregate(op, ref("x"));

Review Comment:
   Why not use the `Expressions` factory methods to create unbound expressions?



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on a diff in pull request #5961: add Aggregate Expressions

Posted by GitBox <gi...@apache.org>.
rdblue commented on code in PR #5961:
URL: https://github.com/apache/iceberg/pull/5961#discussion_r1002772845


##########
api/src/main/java/org/apache/iceberg/expressions/UnboundAggregate.java:
##########
@@ -0,0 +1,73 @@
+/*
+ * 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.iceberg.expressions;
+
+import org.apache.iceberg.exceptions.ValidationException;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.types.Types;
+
+public class UnboundAggregate<T> extends Aggregate<UnboundTerm<T>>
+    implements Unbound<T, Expression> {
+
+  UnboundAggregate(Operation op, UnboundTerm<T> term) {
+    super(op, term);
+  }
+
+  @Override
+  public NamedReference<?> ref() {
+    return term().ref();
+  }
+
+  /**
+   * Bind this UnboundAggregate.
+   *
+   * @param struct The {@link Types.StructType struct type} to resolve references by name.
+   * @param caseSensitive A boolean flag to control whether the bind should enforce case
+   *     sensitivity.
+   * @return an {@link Expression}
+   * @throws ValidationException if literals do not match bound references, or if comparison on
+   *     expression is invalid
+   */
+  @Override
+  public Expression bind(Types.StructType struct, boolean caseSensitive) {
+    if (op() == Operation.COUNT_STAR) {
+      return new BoundAggregate<>(op(), null);
+    } else {
+      Preconditions.checkArgument(term() != null, "aggregate term can't be null");

Review Comment:
   Capitalization.



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on a diff in pull request #5961: add Aggregate Expressions

Posted by GitBox <gi...@apache.org>.
rdblue commented on code in PR #5961:
URL: https://github.com/apache/iceberg/pull/5961#discussion_r1002773705


##########
spark/v3.3/spark/src/test/java/org/apache/iceberg/spark/source/TestSparkAggregates.java:
##########
@@ -0,0 +1,77 @@
+/*
+ * 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.iceberg.spark.source;
+
+import java.util.Map;
+import org.apache.iceberg.expressions.Expression;
+import org.apache.iceberg.expressions.Expressions;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.apache.iceberg.spark.SparkAggregates;
+import org.apache.spark.sql.connector.expressions.FieldReference;
+import org.apache.spark.sql.connector.expressions.NamedReference;
+import org.apache.spark.sql.connector.expressions.aggregate.Count;
+import org.apache.spark.sql.connector.expressions.aggregate.CountStar;
+import org.apache.spark.sql.connector.expressions.aggregate.Max;
+import org.apache.spark.sql.connector.expressions.aggregate.Min;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestSparkAggregates {
+
+  @Test
+  public void testAggregates() {
+    Map<String, String> attrMap = Maps.newHashMap();
+    attrMap.put("id", "id");
+    attrMap.put("`i.d`", "i.d");
+    attrMap.put("`i``d`", "i`d");
+    attrMap.put("`d`.b.`dd```", "d.b.dd`");
+    attrMap.put("a.`aa```.c", "a.aa`.c");
+
+    attrMap.forEach(
+        (quoted, unquoted) -> {
+          NamedReference namedReference = FieldReference.apply(quoted);
+
+          Max max = new Max(namedReference);
+          Expression expectedMax = Expressions.max(unquoted);
+          Expression acturalMax = SparkAggregates.convert(max);

Review Comment:
   Typo: "actural" -> "actual"



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org