alex-plekhanov commented on code in PR #11832:
URL: https://github.com/apache/ignite/pull/11832#discussion_r1933324657


##########
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/ArrayRowHandler.java:
##########
@@ -72,6 +73,34 @@ private ArrayRowHandler() {}
 
                 return fields;
             }
+
+            /**
+             * Wraps a column collection to {@code Object[]} if requred using 
the knowladge of expected column number.
+             *
+             * @param rowContainer Row values holder. Must be an {@code 
Iteratable} or an {@code Object[]}.
+             */
+            @Override public Object[] createByRowContainer(Object 
rowContainer) {

Review Comment:
   This method is not relied to `RowHandler`, it should be in 
`TableFunctionScan`



##########
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/RowHandler.java:
##########
@@ -71,5 +73,34 @@ interface RowFactory<Row> {
 
         /** */
         Row create(Object... fields);
+
+        /**
+         * Wraps a column collection with {@code Object[]} if requred.
+         *
+         * @param rowContainer Row values holder. Must be an {@code Iterable} 
or an {@code Object[]}.
+         */
+        default Row createByRowContainer(Object rowContainer) {
+            assert rowContainer instanceof Iterable || rowContainer.getClass() 
== Object[].class;
+
+            if (rowContainer.getClass() == Object[].class)
+                return create((Object[])rowContainer);
+
+            Iterator<?> it = ((Iterable<?>)rowContainer).iterator();
+
+            if (!it.hasNext())
+                return create(new Object[0]);
+
+            ArrayList<Object> lst = new ArrayList<>();
+
+            while (it.hasNext())
+                lst.add(it.next());
+
+            Object[] arr = new Object[lst.size()];
+
+            for (int i = 0; i < lst.size(); ++i)
+                arr[i] = lst.get(i);
+
+            return create(arr);
+        }

Review Comment:
   Remove it



##########
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/IgniteTableFunction.java:
##########
@@ -0,0 +1,140 @@
+/*
+ * 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.ignite.internal.processors.query.calcite.exec.exp;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+import org.apache.calcite.adapter.enumerable.NullPolicy;
+import org.apache.calcite.adapter.java.JavaTypeFactory;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.schema.TableFunction;
+import org.apache.ignite.cache.query.annotations.QuerySqlFunction;
+import org.apache.ignite.internal.processors.query.IgniteSQLException;
+import org.apache.ignite.internal.util.typedef.F;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Holder of user-defined table function.
+ *
+ * @see QuerySqlFunction#tableColumnTypes()
+ */
+public class IgniteTableFunction extends IgniteReflectiveFunctionBase 
implements TableFunction {
+    /** Column types of the returned table representation. */
+    private final Class<?>[] colTypes;
+
+    /** Column names of the returned table representation. */
+    private final List<String> colNames;
+
+    /**
+     * Creates user-defined table function holder.
+     *
+     * @param method The implementatng method.
+     * @param colTypes Column types of the returned table representation.
+     * @param colNames Column names of the returned table representation. Of 
empty, the defaults are used ('COL_1', 'COL_2', etc.).
+     * @param implementor Call implementor.
+     */
+    private IgniteTableFunction(Method method, Class<?>[] colTypes, @Nullable 
String[] colNames, CallImplementor implementor) {
+        super(method, implementor);
+
+        validate(method, colTypes, colNames);
+
+        this.colTypes = colTypes;
+
+        this.colNames = F.isEmpty(colNames) ? new ArrayList<>(colTypes.length) 
: Arrays.asList(colNames);
+
+        if (F.isEmpty(colNames)) {
+            for (int i = 0; i < colTypes.length; ++i)
+                this.colNames.add("COL_" + (i + 1));
+        }

Review Comment:
   1. Not sure we should allow column names auto-generation
   2. If use auto-generation, we should do it in Calcite style (see 
SqlUtil#GENERATED_EXPR_ALIAS_PREFIX)



##########
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/ArrayRowHandler.java:
##########
@@ -72,6 +73,34 @@ private ArrayRowHandler() {}
 
                 return fields;
             }
+
+            /**
+             * Wraps a column collection to {@code Object[]} if requred using 
the knowladge of expected column number.
+             *
+             * @param rowContainer Row values holder. Must be an {@code 
Iteratable} or an {@code Object[]}.
+             */
+            @Override public Object[] createByRowContainer(Object 
rowContainer) {
+                assert rowContainer instanceof Iterable || 
rowContainer.getClass() == Object[].class;

Review Comment:
   1. Here should be correct user exception, not assert. Assert - for cases 
that can never happen. Here we depend on user input and everything is possible.
   2. `Iterable` is to strong, let's use at least `Collection`, In real cases, 
I think, it will always be `List`. 



##########
modules/core/src/main/java/org/apache/ignite/internal/processors/query/schema/AbstractSchemaChangeListener.java:
##########
@@ -107,6 +108,12 @@ public abstract class AbstractSchemaChangeListener 
implements SchemaChangeListen
         // No-op.
     }
 
+    /** {@inheritDoc} */
+    @Override public void onTableFunctionCreated(String schemaName, String 
name, Method method, Class<?>[] colTypes,
+        @Nullable String[] colNames) {

Review Comment:
   Each parameter should be on it's own line (codestyle)



##########
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/schema/SchemaHolderImpl.java:
##########
@@ -357,6 +358,16 @@ private static Object 
affinityIdentity(CacheConfiguration<?, ?> ccfg) {
         rebuild();
     }
 
+    /** {@inheritDoc} */
+    @Override public void onTableFunctionCreated(String schemaName, String 
name, Method method, Class<?>[] colTypes,
+        @Nullable String[] colNames) {

Review Comment:
   Each parameter should be on it's own line (codestyle)



##########
modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/H2SchemaManager.java:
##########
@@ -237,6 +237,12 @@ private void dropTable(H2TableDescriptor tbl) {
         }
     }
 
+    /** {@inheritDoc} */
+    @Override public void onTableFunctionCreated(String schemaName, String 
name, Method method, Class<?>[] colTypes,
+        @Nullable String[] colNames) {

Review Comment:
   Each parameter should be on it's own line (codestyle)



##########
modules/core/src/main/java/org/apache/ignite/internal/processors/query/schema/management/SchemaManager.java:
##########
@@ -1415,6 +1418,12 @@ private CompoundSchemaChangeListener(GridKernalContext 
ctx, List<SchemaChangeLis
             lsnrs.forEach(lsnr -> executeSafe(() -> 
lsnr.onFunctionCreated(schemaName, name, deterministic, method)));
         }
 
+        /** {@inheritDoc} */
+        @Override public void onTableFunctionCreated(String schemaName, String 
name, Method method, Class<?>[] colTypes,
+            @Nullable String[] colNames) {

Review Comment:
   Each parameter should be on it's own line (codestyle)



##########
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/ArrayRowHandler.java:
##########
@@ -72,6 +73,34 @@ private ArrayRowHandler() {}
 
                 return fields;
             }
+
+            /**
+             * Wraps a column collection to {@code Object[]} if requred using 
the knowladge of expected column number.
+             *
+             * @param rowContainer Row values holder. Must be an {@code 
Iteratable} or an {@code Object[]}.
+             */
+            @Override public Object[] createByRowContainer(Object 
rowContainer) {
+                assert rowContainer instanceof Iterable || 
rowContainer.getClass() == Object[].class;
+
+                if (rowContainer.getClass() == Object[].class)
+                    return create((Object[])rowContainer);
+
+                Iterator<?> it = ((Iterable<?>)rowContainer).iterator();
+
+                if (!it.hasNext())
+                    return create(new Object[0]);
+
+                Object[] arr = new Object[types.length];
+
+                int cnt = 0;
+
+                while (it.hasNext())
+                    arr[cnt++] = it.next();
+
+                assert cnt == types.length;

Review Comment:
   Correct exception instead of assert



##########
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/ArrayRowHandler.java:
##########
@@ -72,6 +73,34 @@ private ArrayRowHandler() {}
 
                 return fields;
             }
+
+            /**
+             * Wraps a column collection to {@code Object[]} if requred using 
the knowladge of expected column number.
+             *
+             * @param rowContainer Row values holder. Must be an {@code 
Iteratable} or an {@code Object[]}.
+             */
+            @Override public Object[] createByRowContainer(Object 
rowContainer) {
+                assert rowContainer instanceof Iterable || 
rowContainer.getClass() == Object[].class;
+
+                if (rowContainer.getClass() == Object[].class)
+                    return create((Object[])rowContainer);
+
+                Iterator<?> it = ((Iterable<?>)rowContainer).iterator();
+
+                if (!it.hasNext())
+                    return create(new Object[0]);
+
+                Object[] arr = new Object[types.length];
+
+                int cnt = 0;
+
+                while (it.hasNext())
+                    arr[cnt++] = it.next();

Review Comment:
   `Collection.toArray()`



##########
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/RowHandler.java:
##########
@@ -71,5 +73,34 @@ interface RowFactory<Row> {
 
         /** */
         Row create(Object... fields);
+
+        /**
+         * Wraps a column collection to {@code Object[]} if requred.
+         *
+         * @param rowContainer Row values holder. Must be an {@code 
Iteratable} or an {@code Object[]}.
+         */
+        default Row createByRowContainer(Object rowContainer) {
+            assert rowContainer instanceof Iterable || rowContainer.getClass() 
== Object[].class;
+
+            if (rowContainer.getClass() == Object[].class)
+                return create((Object[])rowContainer);
+
+            Iterator<?> it = ((Iterable<?>)rowContainer).iterator();
+
+            if (!it.hasNext())
+                return create(new Object[0]);
+
+            ArrayList<Object> lst = new ArrayList<>();
+
+            while (it.hasNext())
+                lst.add(it.next());
+
+            Object[] arr = new Object[lst.size()];
+
+            for (int i = 0; i < lst.size(); ++i)
+                arr[i] = lst.get(i);
+
+            return create(arr);
+        }

Review Comment:
   Remove it



##########
modules/core/src/main/java/org/apache/ignite/internal/processors/query/schema/SchemaChangeListener.java:
##########
@@ -140,6 +141,18 @@ public void onColumnsDropped(
      */
     public void onFunctionCreated(String schemaName, String name, boolean 
deterministic, Method method);
 
+    /**
+     * Callback on table function creation.
+     *
+     * @param schemaName Schema name.
+     * @param name Function name.
+     * @param colTypes Column types of the table representation.
+     * @param colNames Column names if the table representation. If {@code 
null} or empty, the default names are used instead.
+     * @param method Public static method, implementing this function.
+     */
+    public void onTableFunctionCreated(String schemaName, String name, Method 
method, Class<?>[] colTypes,
+        @Nullable String[] colNames);

Review Comment:
   Each parameter should be on it's own line (codestyle)



##########
modules/core/src/main/java/org/apache/ignite/cache/query/annotations/QuerySqlFunction.java:
##########
@@ -74,6 +104,25 @@
      */
     String alias() default "";
 
+    /**
+     * Makes the function a table function and defines column types of the 
returned SQL table representation.
+     *
+     * @return Column types of the table if this function is a table function. 
By default, is empty meaning the function
+     * is not a table function.
+     */
+    Class<?>[] tableColumnTypes() default {};

Review Comment:
   Perhaps new annotation and just `columnTypes`/`columnNames` (whithout 
`table`)



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to