All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.dinky.shaded.paimon.mergetree.compact.aggregate.AggregateMergeFunction Maven / Gradle / Ivy

The newest version!
/*
 * 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.dinky.shaded.paimon.mergetree.compact.aggregate;

import org.dinky.shaded.paimon.CoreOptions;
import org.dinky.shaded.paimon.KeyValue;
import org.dinky.shaded.paimon.data.GenericRow;
import org.dinky.shaded.paimon.data.InternalRow;
import org.dinky.shaded.paimon.mergetree.compact.MergeFunction;
import org.dinky.shaded.paimon.mergetree.compact.MergeFunctionFactory;
import org.dinky.shaded.paimon.options.Options;
import org.dinky.shaded.paimon.types.DataType;
import org.dinky.shaded.paimon.types.RowKind;
import org.dinky.shaded.paimon.utils.Projection;

import javax.annotation.Nullable;

import java.util.Arrays;
import java.util.List;

import static org.dinky.shaded.paimon.utils.InternalRowUtils.createFieldGetters;
import static org.dinky.shaded.paimon.utils.Preconditions.checkNotNull;

/**
 * A {@link MergeFunction} where key is primary key (unique) and value is the partial record,
 * pre-aggregate non-null fields on merge.
 */
public class AggregateMergeFunction implements MergeFunction {

    private final InternalRow.FieldGetter[] getters;
    private final FieldAggregator[] aggregators;

    private KeyValue latestKv;
    private GenericRow row;
    private KeyValue reused;

    public AggregateMergeFunction(
            InternalRow.FieldGetter[] getters, FieldAggregator[] aggregators) {
        this.getters = getters;
        this.aggregators = aggregators;
    }

    @Override
    public void reset() {
        this.latestKv = null;
        this.row = new GenericRow(getters.length);
        Arrays.stream(aggregators).forEach(FieldAggregator::reset);
    }

    @Override
    public void add(KeyValue kv) {
        latestKv = kv;
        boolean isRetract =
                kv.valueKind() != RowKind.INSERT && kv.valueKind() != RowKind.UPDATE_AFTER;
        for (int i = 0; i < getters.length; i++) {
            FieldAggregator fieldAggregator = aggregators[i];
            Object accumulator = getters[i].getFieldOrNull(row);
            Object inputField = getters[i].getFieldOrNull(kv.value());
            Object mergedField =
                    isRetract
                            ? fieldAggregator.retract(accumulator, inputField)
                            : fieldAggregator.agg(accumulator, inputField);
            row.setField(i, mergedField);
        }
    }

    @Nullable
    @Override
    public KeyValue getResult() {
        checkNotNull(
                latestKv,
                "Trying to get result from merge function without any input. This is unexpected.");

        if (reused == null) {
            reused = new KeyValue();
        }
        return reused.replace(latestKv.key(), latestKv.sequenceNumber(), RowKind.INSERT, row);
    }

    public static MergeFunctionFactory factory(
            Options conf,
            List tableNames,
            List tableTypes,
            List primaryKeys) {
        return new Factory(conf, tableNames, tableTypes, primaryKeys);
    }

    private static class Factory implements MergeFunctionFactory {

        private static final long serialVersionUID = 1L;

        private final CoreOptions options;
        private final List tableNames;
        private final List tableTypes;
        private final List primaryKeys;

        private Factory(
                Options conf,
                List tableNames,
                List tableTypes,
                List primaryKeys) {
            this.options = new CoreOptions(conf);
            this.tableNames = tableNames;
            this.tableTypes = tableTypes;
            this.primaryKeys = primaryKeys;
        }

        @Override
        public MergeFunction create(@Nullable int[][] projection) {
            List fieldNames = tableNames;
            List fieldTypes = tableTypes;
            if (projection != null) {
                Projection project = Projection.of(projection);
                fieldNames = project.project(tableNames);
                fieldTypes = project.project(tableTypes);
            }

            FieldAggregator[] fieldAggregators = new FieldAggregator[fieldNames.size()];
            for (int i = 0; i < fieldNames.size(); i++) {
                String fieldName = fieldNames.get(i);
                DataType fieldType = fieldTypes.get(i);
                // aggregate by primary keys, so they do not aggregate
                boolean isPrimaryKey = primaryKeys.contains(fieldName);
                String strAggFunc = options.fieldAggFunc(fieldName);
                boolean ignoreRetract = options.fieldAggIgnoreRetract(fieldName);

                fieldAggregators[i] =
                        FieldAggregator.createFieldAggregator(
                                fieldType, strAggFunc, ignoreRetract, isPrimaryKey);
            }

            return new AggregateMergeFunction(createFieldGetters(fieldTypes), fieldAggregators);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy