zabetak commented on a change in pull request #1020: [CALCITE-2812] Add 
algebraic operators to allow expressing recursive queries (Ruben Quesada Lopez)
URL: https://github.com/apache/calcite/pull/1020#discussion_r276286371
 
 

 ##########
 File path: 
core/src/main/java/org/apache/calcite/schema/impl/TransientTable.java
 ##########
 @@ -0,0 +1,142 @@
+/*
+ * 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.calcite.schema.impl;
+
+import org.apache.calcite.DataContext;
+import org.apache.calcite.adapter.java.AbstractQueryableTable;
+import org.apache.calcite.linq4j.AbstractEnumerable;
+import org.apache.calcite.linq4j.Enumerable;
+import org.apache.calcite.linq4j.Enumerator;
+import org.apache.calcite.linq4j.Linq4j;
+import org.apache.calcite.linq4j.QueryProvider;
+import org.apache.calcite.linq4j.Queryable;
+import org.apache.calcite.linq4j.function.Experimental;
+import org.apache.calcite.linq4j.tree.Expression;
+import org.apache.calcite.plan.RelOptCluster;
+import org.apache.calcite.plan.RelOptTable;
+import org.apache.calcite.prepare.Prepare;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.TableModify;
+import org.apache.calcite.rel.logical.LogicalTableModify;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.schema.ModifiableTable;
+import org.apache.calcite.schema.ScannableTable;
+import org.apache.calcite.schema.SchemaPlus;
+import org.apache.calcite.schema.Schemas;
+
+import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * Simple modifiable table backed by a Java list.
+ * It can be used, for example, in a repeat union to accumulate all 
intermediate results.
+ *
+ * <p>NOTE: The current API is experimental and subject to change without 
notice.</p>
+ */
+@Experimental
+public class TransientTable extends AbstractQueryableTable
+    implements ModifiableTable, ScannableTable {
+  private static final Type TYPE = Object[].class;
+  private final List rows = new ArrayList();
+  private final String name;
+  private final RelDataType rowType;
+
+  public TransientTable(String name, RelDataType rowType) {
+    super(TYPE);
+    this.name = name;
+    this.rowType = rowType;
+  }
+
+  @Override public TableModify toModificationRel(
+      RelOptCluster cluster,
+      RelOptTable table,
+      Prepare.CatalogReader catalogReader,
+      RelNode child,
+      TableModify.Operation operation,
+      List<String> updateColumnList,
+      List<RexNode> sourceExpressionList,
+      boolean flattened) {
+    return LogicalTableModify.create(table, catalogReader, child, operation,
+        updateColumnList, sourceExpressionList, flattened);
+  }
+
+  @Override public Collection getModifiableCollection() {
+    return rows;
+  }
+
+  @Override public Enumerable<Object[]> scan(DataContext root) {
+    // add the table into the schema, so that it is accessible by any 
potential operator
+    root.getRootSchema().add(name, this);
+
+    return new AbstractEnumerable<Object[]>() {
+      public Enumerator<Object[]> enumerator() {
+        return new Enumerator<Object[]>() {
+          private final List list = rows;
+          private int i = -1;
+
+          // TODO cleaner way to handle non-array objects?
+          @Override public Object[] current() {
+            Object current = list.get(i);
+            return current.getClass().isArray()
+                ? (Object[]) current
+                : new Object[]{current};
+          }
+
+          @Override public boolean moveNext() {
+            return ++i < list.size();
+          }
+
+          @Override public void reset() {
+            i = -1;
+          }
+
+          @Override public void close() {
+          }
+        };
+      }
+    };
+  }
+
+  public Expression getExpression(SchemaPlus schema, String tableName,
+                                  Class clazz) {
+    return Schemas.tableExpression(schema, elementType, tableName, clazz);
+  }
+
+  @Override public <T> Queryable<T> asQueryable(QueryProvider queryProvider,
+                                                SchemaPlus schema, String 
tableName) {
+    return new AbstractTableQueryable<T>(queryProvider, schema, this, 
tableName) {
+      public Enumerator<T> enumerator() {
+        //noinspection unchecked
+        return (Enumerator<T>) Linq4j.enumerator(rows);
+      }
+    };
+  }
+
+  @Override public RelDataType getRowType(RelDataTypeFactory typeFactory) {
+    return rowType;
 
 Review comment:
   Replace with `return typeFactory.copy(rowType);`

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to