Java Code Examples for org.apache.calcite.sql.SqlSplittableAggFunction

The following examples show how to use org.apache.calcite.sql.SqlSplittableAggFunction. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Bats   Source File: AggregateJoinTransposeRule.java    License: Apache License 2.0 6 votes vote down vote up
private static boolean isAggregateSupported(Aggregate aggregate, boolean allowFunctions) {
  if (!allowFunctions && !aggregate.getAggCallList().isEmpty()) {
    return false;
  }
  if (aggregate.getGroupType() != Aggregate.Group.SIMPLE) {
    return false;
  }
  // If any aggregate functions do not support splitting, bail out
  // If any aggregate call has a filter or is distinct, bail out
  for (AggregateCall aggregateCall : aggregate.getAggCallList()) {
    if (aggregateCall.getAggregation().unwrap(SqlSplittableAggFunction.class)
        == null) {
      return false;
    }
    if (aggregateCall.filterArg >= 0 || aggregateCall.isDistinct()) {
      return false;
    }
  }
  return true;
}
 
Example 2
Source Project: calcite   Source File: AggregateJoinTransposeRule.java    License: Apache License 2.0 6 votes vote down vote up
private static boolean isAggregateSupported(Aggregate aggregate, boolean allowFunctions) {
  if (!allowFunctions && !aggregate.getAggCallList().isEmpty()) {
    return false;
  }
  if (aggregate.getGroupType() != Aggregate.Group.SIMPLE) {
    return false;
  }
  // If any aggregate functions do not support splitting, bail out
  // If any aggregate call has a filter or is distinct, bail out
  for (AggregateCall aggregateCall : aggregate.getAggCallList()) {
    if (aggregateCall.getAggregation().unwrap(SqlSplittableAggFunction.class)
        == null) {
      return false;
    }
    if (aggregateCall.filterArg >= 0 || aggregateCall.isDistinct()) {
      return false;
    }
  }
  return true;
}
 
Example 3
Source Project: Bats   Source File: AggregateJoinTransposeRule.java    License: Apache License 2.0 5 votes vote down vote up
/** Creates a {@link org.apache.calcite.sql.SqlSplittableAggFunction.Registry}
 * that is a view of a list. */
private static <E> SqlSplittableAggFunction.Registry<E> registry(
    final List<E> list) {
  return e -> {
    int i = list.indexOf(e);
    if (i < 0) {
      i = list.size();
      list.add(e);
    }
    return i;
  };
}
 
Example 4
Source Project: flink   Source File: FlinkAggregateJoinTransposeRule.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates a {@link org.apache.calcite.sql.SqlSplittableAggFunction.Registry}
 * that is a view of a list.
 */
private static <E> SqlSplittableAggFunction.Registry<E> registry(
		final List<E> list) {
	return new SqlSplittableAggFunction.Registry<E>() {
		public int register(E e) {
			int i = list.indexOf(e);
			if (i < 0) {
				i = list.size();
				list.add(e);
			}
			return i;
		}
	};
}
 
Example 5
Source Project: flink   Source File: FlinkAggregateJoinTransposeRule.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates a {@link org.apache.calcite.sql.SqlSplittableAggFunction.Registry}
 * that is a view of a list.
 */
private static <E> SqlSplittableAggFunction.Registry<E> registry(
		final List<E> list) {
	return new SqlSplittableAggFunction.Registry<E>() {
		public int register(E e) {
			int i = list.indexOf(e);
			if (i < 0) {
				i = list.size();
				list.add(e);
			}
			return i;
		}
	};
}
 
Example 6
Source Project: calcite   Source File: AggregateJoinTransposeRule.java    License: Apache License 2.0 5 votes vote down vote up
/** Creates a {@link org.apache.calcite.sql.SqlSplittableAggFunction.Registry}
 * that is a view of a list. */
private static <E> SqlSplittableAggFunction.Registry<E> registry(
    final List<E> list) {
  return e -> {
    int i = list.indexOf(e);
    if (i < 0) {
      i = list.size();
      list.add(e);
    }
    return i;
  };
}
 
Example 7
Source Project: calcite   Source File: AggregateMergeRule.java    License: Apache License 2.0 5 votes vote down vote up
private boolean isAggregateSupported(AggregateCall aggCall) {
  if (aggCall.isDistinct()
      || aggCall.hasFilter()
      || aggCall.isApproximate()
      || aggCall.getArgList().size() > 1) {
    return false;
  }
  SqlSplittableAggFunction splitter = aggCall.getAggregation()
      .unwrap(SqlSplittableAggFunction.class);
  return splitter != null;
}
 
Example 8
Source Project: calcite   Source File: AggregateRemoveRule.java    License: Apache License 2.0 5 votes vote down vote up
private static boolean isAggregateSupported(Aggregate aggregate) {
  if (aggregate.getGroupType() != Aggregate.Group.SIMPLE
      || aggregate.getGroupCount() == 0) {
    return false;
  }
  // If any aggregate functions do not support splitting, bail out.
  for (AggregateCall aggregateCall : aggregate.getAggCallList()) {
    if (aggregateCall.filterArg >= 0
        || aggregateCall.getAggregation()
            .unwrap(SqlSplittableAggFunction.class) == null) {
      return false;
    }
  }
  return true;
}
 
Example 9
Source Project: Bats   Source File: SqlMinMaxAggFunction.java    License: Apache License 2.0 4 votes vote down vote up
@Override public <T> T unwrap(Class<T> clazz) {
  if (clazz == SqlSplittableAggFunction.class) {
    return clazz.cast(SqlSplittableAggFunction.SelfSplitter.INSTANCE);
  }
  return super.unwrap(clazz);
}
 
Example 10
Source Project: Bats   Source File: SqlCountAggFunction.java    License: Apache License 2.0 4 votes vote down vote up
@Override public <T> T unwrap(Class<T> clazz) {
  if (clazz == SqlSplittableAggFunction.class) {
    return clazz.cast(SqlSplittableAggFunction.CountSplitter.INSTANCE);
  }
  return super.unwrap(clazz);
}
 
Example 11
Source Project: Bats   Source File: SqlBitOpAggFunction.java    License: Apache License 2.0 4 votes vote down vote up
@Override public <T> T unwrap(Class<T> clazz) {
  if (clazz == SqlSplittableAggFunction.class) {
    return clazz.cast(SqlSplittableAggFunction.SelfSplitter.INSTANCE);
  }
  return super.unwrap(clazz);
}
 
Example 12
Source Project: Bats   Source File: SqlSumEmptyIsZeroAggFunction.java    License: Apache License 2.0 4 votes vote down vote up
@Override public <T> T unwrap(Class<T> clazz) {
  if (clazz == SqlSplittableAggFunction.class) {
    return clazz.cast(SqlSplittableAggFunction.Sum0Splitter.INSTANCE);
  }
  return super.unwrap(clazz);
}
 
Example 13
Source Project: Bats   Source File: SqlSumAggFunction.java    License: Apache License 2.0 4 votes vote down vote up
@Override public <T> T unwrap(Class<T> clazz) {
  if (clazz == SqlSplittableAggFunction.class) {
    return clazz.cast(SqlSplittableAggFunction.SumSplitter.INSTANCE);
  }
  return super.unwrap(clazz);
}
 
Example 14
Source Project: calcite   Source File: AggregateMergeRule.java    License: Apache License 2.0 4 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final Aggregate topAgg = call.rel(0);
  final Aggregate bottomAgg = call.rel(1);
  if (topAgg.getGroupCount() > bottomAgg.getGroupCount()) {
    return;
  }

  final ImmutableBitSet bottomGroupSet = bottomAgg.getGroupSet();
  final Map<Integer, Integer> map = new HashMap<>();
  bottomGroupSet.forEach(v -> map.put(map.size(), v));
  for (int k : topAgg.getGroupSet()) {
    if (!map.containsKey(k)) {
      return;
    }
  }

  // top aggregate keys must be subset of lower aggregate keys
  final ImmutableBitSet topGroupSet = topAgg.getGroupSet().permute(map);
  if (!bottomGroupSet.contains(topGroupSet)) {
    return;
  }

  boolean hasEmptyGroup = topAgg.getGroupSets()
      .stream().anyMatch(n -> n.isEmpty());

  final List<AggregateCall> finalCalls = new ArrayList<>();
  for (AggregateCall topCall : topAgg.getAggCallList()) {
    if (!isAggregateSupported(topCall)
        || topCall.getArgList().size() == 0) {
      return;
    }
    // Make sure top aggregate argument refers to one of the aggregate
    int bottomIndex = topCall.getArgList().get(0) - bottomGroupSet.cardinality();
    if (bottomIndex >= bottomAgg.getAggCallList().size()
        || bottomIndex < 0) {
      return;
    }
    AggregateCall bottomCall = bottomAgg.getAggCallList().get(bottomIndex);
    // Should not merge if top agg with empty group keys and the lower agg
    // function is COUNT, because in case of empty input for lower agg,
    // the result is empty, if we merge them, we end up with 1 result with
    // 0, which is wrong.
    if (!isAggregateSupported(bottomCall)
        || (bottomCall.getAggregation() == SqlStdOperatorTable.COUNT
             && hasEmptyGroup)) {
      return;
    }
    SqlSplittableAggFunction splitter = Objects.requireNonNull(
        bottomCall.getAggregation().unwrap(SqlSplittableAggFunction.class));
    AggregateCall finalCall = splitter.merge(topCall, bottomCall);
    // fail to merge the aggregate call, bail out
    if (finalCall == null) {
      return;
    }
    finalCalls.add(finalCall);
  }

  // re-map grouping sets
  ImmutableList<ImmutableBitSet> newGroupingSets = null;
  if (topAgg.getGroupType() != Group.SIMPLE) {
    newGroupingSets =
        ImmutableBitSet.ORDERING.immutableSortedCopy(
            ImmutableBitSet.permute(topAgg.getGroupSets(), map));
  }

  final Aggregate finalAgg =
      topAgg.copy(topAgg.getTraitSet(), bottomAgg.getInput(), topGroupSet,
          newGroupingSets, finalCalls);
  call.transformTo(finalAgg);
}
 
Example 15
Source Project: calcite   Source File: AggregateRemoveRule.java    License: Apache License 2.0 4 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final Aggregate aggregate = call.rel(0);
  final RelNode input = aggregate.getInput();
  final RelMetadataQuery mq = call.getMetadataQuery();
  if (!SqlFunctions.isTrue(mq.areColumnsUnique(input, aggregate.getGroupSet()))) {
    return;
  }

  final RelBuilder relBuilder = call.builder();
  final RexBuilder rexBuilder = relBuilder.getRexBuilder();
  final List<RexNode> projects = new ArrayList<>();
  for (AggregateCall aggCall : aggregate.getAggCallList()) {
    final SqlAggFunction aggregation = aggCall.getAggregation();
    if (aggregation.getKind() == SqlKind.SUM0) {
      // Bail out for SUM0 to avoid potential infinite rule matching,
      // because it may be generated by transforming SUM aggregate
      // function to SUM0 and COUNT.
      return;
    }
    final SqlSplittableAggFunction splitter =
        Objects.requireNonNull(
            aggregation.unwrap(SqlSplittableAggFunction.class));
    final RexNode singleton = splitter.singleton(
        rexBuilder, input.getRowType(), aggCall);
    projects.add(singleton);
  }

  final RelNode newInput = convert(input, aggregate.getTraitSet().simplify());
  relBuilder.push(newInput);
  if (!projects.isEmpty()) {
    projects.addAll(0, relBuilder.fields(aggregate.getGroupSet()));
    relBuilder.project(projects);
  } else if (newInput.getRowType().getFieldCount()
      > aggregate.getRowType().getFieldCount()) {
    // If aggregate was projecting a subset of columns, and there were no
    // aggregate functions, add a project for the same effect.
    relBuilder.project(relBuilder.fields(aggregate.getGroupSet()));
  }
  call.getPlanner().prune(aggregate);
  call.transformTo(relBuilder.build());
}
 
Example 16
Source Project: calcite   Source File: SqlMinMaxAggFunction.java    License: Apache License 2.0 4 votes vote down vote up
@Override public <T> T unwrap(Class<T> clazz) {
  if (clazz == SqlSplittableAggFunction.class) {
    return clazz.cast(SqlSplittableAggFunction.SelfSplitter.INSTANCE);
  }
  return super.unwrap(clazz);
}
 
Example 17
Source Project: calcite   Source File: SqlCountAggFunction.java    License: Apache License 2.0 4 votes vote down vote up
@Override public <T> T unwrap(Class<T> clazz) {
  if (clazz == SqlSplittableAggFunction.class) {
    return clazz.cast(SqlSplittableAggFunction.CountSplitter.INSTANCE);
  }
  return super.unwrap(clazz);
}
 
Example 18
Source Project: calcite   Source File: SqlBitOpAggFunction.java    License: Apache License 2.0 4 votes vote down vote up
@Override public <T> T unwrap(Class<T> clazz) {
  if (clazz == SqlSplittableAggFunction.class) {
    return clazz.cast(SqlSplittableAggFunction.SelfSplitter.INSTANCE);
  }
  return super.unwrap(clazz);
}
 
Example 19
Source Project: calcite   Source File: SqlSumEmptyIsZeroAggFunction.java    License: Apache License 2.0 4 votes vote down vote up
@Override public <T> T unwrap(Class<T> clazz) {
  if (clazz == SqlSplittableAggFunction.class) {
    return clazz.cast(SqlSplittableAggFunction.Sum0Splitter.INSTANCE);
  }
  return super.unwrap(clazz);
}
 
Example 20
Source Project: calcite   Source File: SqlSumAggFunction.java    License: Apache License 2.0 4 votes vote down vote up
@Override public <T> T unwrap(Class<T> clazz) {
  if (clazz == SqlSplittableAggFunction.class) {
    return clazz.cast(SqlSplittableAggFunction.SumSplitter.INSTANCE);
  }
  return super.unwrap(clazz);
}