/*
 * Licensed to Crate under one or more contributor license agreements.
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.  Crate 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.
 *
 * However, if you have executed another commercial license agreement
 * with Crate these terms will supersede the license and you may use the
 * software solely pursuant to the terms of the relevant commercial
 * agreement.
 */

package io.crate.expression.tablefunctions;

import io.crate.data.Input;
import io.crate.data.Row;
import io.crate.metadata.BaseFunctionResolver;
import io.crate.metadata.FunctionIdent;
import io.crate.metadata.FunctionImplementation;
import io.crate.metadata.FunctionInfo;
import io.crate.metadata.TransactionContext;
import io.crate.metadata.functions.params.FuncParams;
import io.crate.metadata.tablefunctions.TableFunctionImplementation;
import io.crate.types.DataType;
import io.crate.types.ObjectType;
import io.crate.types.RowType;

import java.util.List;

/**
 * Generates a one row, no column, empty table.
 */
public class EmptyRowTableFunction {

    private static final String NAME = "empty_row";

    static class EmptyRowTableFunctionImplementation extends TableFunctionImplementation<Object> {

        private final FunctionInfo info;

        private EmptyRowTableFunctionImplementation(FunctionInfo info) {
            this.info = info;
        }

        @Override
        public FunctionInfo info() {
            return info;
        }

        @Override
        @SafeVarargs
        public final Iterable<Row> evaluate(TransactionContext txnCtx, Input<Object>... args) {
            return List.of(Row.EMPTY);
        }

        @Override
        public RowType returnType() {
            return RowType.EMPTY;
        }

        @Override
        public boolean hasLazyResultSet() {
            return false;
        }
    }

    public static void register(TableFunctionModule module) {
        module.register(NAME, new BaseFunctionResolver(FuncParams.NONE) {

            @Override
            public FunctionImplementation getForTypes(List<DataType> dataTypes) throws IllegalArgumentException {
                return new EmptyRowTableFunctionImplementation(
                    new FunctionInfo(new FunctionIdent(NAME, dataTypes), ObjectType.untyped(), FunctionInfo.Type.TABLE));
            }
        });
    }
}

@@ -1120,117 +1120,21 @@
 ata.
-BaseFunctionResolver;%0Aimport io.crate.metadata.FunctionIdent;%0Aimport io.crate.metadata.FunctionImplementation
+FunctionIdent
 ;%0Aim
@@ -1254,25 +1254,17 @@
 ons.
-params.FuncParams
+Signature
 ;%0Aim
@@ -1356,84 +1356,52 @@
 pes.
-Data
+Row
 Type;%0A
+%0A
 import 
-io.crate.types.ObjectType;%0Aimport io.crate.types.RowTyp
+javax.annotation.Nullabl
 e;%0A
-%0A
 impo
@@ -1567,16 +1567,478 @@
 _row%22;%0A%0A
+    public static void register(TableFunctionModule module) %7B%0A        module.register(%0A            Signature.table(NAME, RowType.EMPTY.getTypeSignature()),%0A            (signature, argTypes) -%3E new EmptyRowTableFunctionImplementation(%0A                signature,%0A                new FunctionInfo(%0A                    new FunctionIdent(NAME, argTypes),%0A                    RowType.EMPTY,%0A                    FunctionInfo.Type.TABLE)%0A            )%0A        );%0A    %7D%0A%0A
     stat
@@ -2169,16 +2169,59 @@
 fo info;
+%0A        private final Signature signature;
 %0A%0A      
@@ -2266,16 +2266,37 @@
 ntation(
+Signature signature, 
 Function
@@ -2334,24 +2334,64 @@
 nfo = info;%0A
+            this.signature = signature;%0A
         %7D%0A%0A 
@@ -2472,32 +2472,148 @@
 nfo;%0A        %7D%0A%0A
+        @Nullable%0A        @Override%0A        public Signature signature() %7B%0A            return signature;%0A        %7D%0A%0A
         @Overrid
@@ -2989,499 +2989,7 @@
    %7D
-%0A%0A    public static void register(TableFunctionModule module) %7B%0A        module.register(NAME, new BaseFunctionResolver(FuncParams.NONE) %7B%0A%0A            @Override%0A            public FunctionImplementation getForTypes(List%3CDataType%3E dataTypes) throws IllegalArgumentException %7B%0A                return new EmptyRowTableFunctionImplementation(%0A                    new FunctionInfo(new FunctionIdent(NAME, dataTypes), ObjectType.untyped(), FunctionInfo.Type.TABLE));%0A            %7D%0A        %7D);%0A    %7D
 %0A%7D%0A

Use new func registry for the empty row table function.