org.apache.flink.table.runtime.functions.aggfunctions.CountDistinct Maven / Gradle / Ivy
/*
* 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.flink.table.runtime.functions.aggfunctions;
import org.apache.flink.table.api.dataview.MapView;
import org.apache.flink.table.dataformat.GenericRow;
import org.apache.flink.table.functions.AggregateFunction;
import org.apache.flink.table.types.DataType;
import org.apache.flink.table.types.DataTypes;
import org.apache.flink.table.types.DecimalType;
import org.apache.flink.table.types.InternalType;
import org.apache.flink.table.types.RowType;
import org.apache.flink.table.types.TypeConverters;
import org.apache.flink.table.types.TypeInfoWrappedDataType;
import org.apache.flink.table.typeutils.BinaryStringTypeInfo;
import org.apache.flink.table.typeutils.MapViewTypeInfo;
import java.util.Map;
/**
* Built-in count distinct aggregate function.
*/
public class CountDistinct {
/**
* Base function for count distinct aggregate.
*/
@SuppressWarnings("unchecked")
public abstract static class CountDistinctAggFunction extends AggregateFunction {
public abstract DataType getValueTypeInfo();
@Override
public GenericRow createAccumulator() {
GenericRow acc = new GenericRow(3);
// count
acc.setLong(0, 0L);
// map
acc.update(1, new MapView(getValueTypeInfo(), DataTypes.LONG));
return acc;
}
public void accumulate(GenericRow acc, Object input) throws Exception {
if (input != null) {
long count = acc.getLong(0); // never null
MapView