org.apache.calcite.linq4j.Queryable Java Examples

The following examples show how to use org.apache.calcite.linq4j.Queryable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ListTable.java    From calcite with Apache License 2.0 6 votes vote down vote up
public <T> Queryable<T> asQueryable(final QueryProvider queryProvider,
    SchemaPlus schema, String tableName) {
  return new AbstractQueryable<T>() {
    public Type getElementType() {
      return elementType;
    }

    public Expression getExpression() {
      return expression;
    }

    public QueryProvider getProvider() {
      return queryProvider;
    }

    public Iterator<T> iterator() {
      //noinspection unchecked
      return list.iterator();
    }

    public Enumerator<T> enumerator() {
      //noinspection unchecked
      return Linq4j.enumerator(list);
    }
  };
}
 
Example #2
Source File: EnumerableTableScan.java    From calcite with Apache License 2.0 6 votes vote down vote up
private Expression toEnumerable(Expression expression) {
  final Type type = expression.getType();
  if (Types.isArray(type)) {
    if (Types.toClass(type).getComponentType().isPrimitive()) {
      expression =
          Expressions.call(BuiltInMethod.AS_LIST.method, expression);
    }
    return Expressions.call(BuiltInMethod.AS_ENUMERABLE.method, expression);
  } else if (Types.isAssignableFrom(Iterable.class, type)
      && !Types.isAssignableFrom(Enumerable.class, type)) {
    return Expressions.call(BuiltInMethod.AS_ENUMERABLE2.method,
        expression);
  } else if (Types.isAssignableFrom(Queryable.class, type)) {
    // Queryable extends Enumerable, but it's too "clever", so we call
    // Queryable.asEnumerable so that operations such as take(int) will be
    // evaluated directly.
    return Expressions.call(expression,
        BuiltInMethod.QUERYABLE_AS_ENUMERABLE.method);
  }
  return expression;
}
 
Example #3
Source File: OutboundReferencesTable.java    From mat-calcite-plugin with Apache License 2.0 6 votes vote down vote up
@Override
public Queryable<Object[]> asQueryable(QueryProvider queryProvider, SchemaPlus schemaPlus, String tableName) {
    return new AbstractTableQueryable<Object[]>(queryProvider, schemaPlus, this, tableName) {
        @Override
        public Enumerator<Object[]> enumerator() {
            FluentIterable<Object[]> it = FluentIterable
                    .from(references)
                    .transform(new Function<NamedReference, Object[]>() {
                        @Nullable
                        @Override
                        public Object[] apply(@Nullable NamedReference namedReference) {
                            HeapReference ref = null;
                            try {
                                ref = HeapReference.valueOf(namedReference.getObject());
                            } catch (SnapshotException e) {
                                e.printStackTrace();
                            }
                            return new Object[]{namedReference.getName(), ref};
                        }
                    });

            return Linq4j.iterableEnumerator(it);
        }
    };
}
 
Example #4
Source File: PreferredAlbumsTableFactory.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public AbstractQueryableTable create(
    SchemaPlus schema,
    String name,
    Map<String, Object> operand,
    RelDataType rowType) {
  return new AbstractQueryableTable(Integer.class) {
    @Override public RelDataType getRowType(RelDataTypeFactory typeFactory) {
      return typeFactory.builder().add("ID", SqlTypeName.INTEGER).build();
    }

    @Override public Queryable<Integer> asQueryable(
        QueryProvider qp,
        SchemaPlus sp,
        String string) {
      return fetchPreferredAlbums();
    }
  };
}
 
Example #5
Source File: CalciteConnectionImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
public <T> Enumerator<T> executeQuery(Queryable<T> queryable) {
  try {
    CalciteStatement statement = (CalciteStatement) createStatement();
    CalcitePrepare.CalciteSignature<T> signature =
        statement.prepare(queryable);
    return enumerable(statement.handle, signature).enumerator();
  } catch (SQLException e) {
    throw new RuntimeException(e);
  }
}
 
Example #6
Source File: ReflectiveSchema.java    From calcite with Apache License 2.0 5 votes vote down vote up
public <T> Queryable<T> asQueryable(QueryProvider queryProvider,
    SchemaPlus schema, String tableName) {
  return new AbstractTableQueryable<T>(queryProvider, schema, this,
      tableName) {
    @SuppressWarnings("unchecked")
    public Enumerator<T> enumerator() {
      return (Enumerator<T>) enumerable.enumerator();
    }
  };
}
 
Example #7
Source File: CloneSchema.java    From calcite with Apache License 2.0 5 votes vote down vote up
private Table createCloneTable(QueryProvider queryProvider,
    QueryableTable sourceTable, String name) {
  final Queryable<Object> queryable =
      sourceTable.asQueryable(queryProvider, sourceSchema, name);
  final JavaTypeFactory typeFactory =
      ((CalciteConnection) queryProvider).getTypeFactory();
  return createCloneTable(typeFactory, Schemas.proto(sourceTable),
      ImmutableList.of(), null, queryable);
}
 
Example #8
Source File: QueryableRelBuilder.java    From calcite with Apache License 2.0 5 votes vote down vote up
public <TCollection, TResult> Queryable<TResult> selectManyN(
    Queryable<T> source,
    FunctionExpression<Function1<T, Enumerable<TCollection>>>
        collectionSelector,
    FunctionExpression<Function2<T, TCollection, TResult>> resultSelector) {
  throw new UnsupportedOperationException();
}
 
Example #9
Source File: QueryableRelBuilder.java    From calcite with Apache License 2.0 5 votes vote down vote up
public <TKey, TResult> Queryable<TResult> groupByK(
    Queryable<T> source,
    FunctionExpression<Function1<T, TKey>> keySelector,
    FunctionExpression<Function2<TKey, Enumerable<T>, TResult>>
        resultSelector) {
  throw new UnsupportedOperationException();
}
 
Example #10
Source File: Table.java    From kareldb with Apache License 2.0 5 votes vote down vote up
@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.iterableEnumerator(
                () -> Iterators.transform(getModifiableCollection().iterator(), Table::toArray));
        }
    };
}
 
Example #11
Source File: ExtensionDdlExecutor.java    From calcite with Apache License 2.0 5 votes vote down vote up
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(list);
    }
  };
}
 
Example #12
Source File: PreferredGenresTableFactory.java    From calcite with Apache License 2.0 5 votes vote down vote up
private Queryable<Integer> fetchPreferredGenres() {
  if (EnvironmentFairy.getUser() == EnvironmentFairy.User.SPECIFIC_USER) {
    return Linq4j.asEnumerable(SPECIFIC_USER_PREFERRED_GENRES).asQueryable();
  } else {
    final ContiguousSet<Integer> set =
        ContiguousSet.create(Range.closed(FIRST_ID, LAST_ID),
            DiscreteDomain.integers());
    return Linq4j.asEnumerable(set).asQueryable();
  }
}
 
Example #13
Source File: QuarkViewTable.java    From quark with Apache License 2.0 5 votes vote down vote up
public <T> Queryable<T> asQueryable(final QueryProvider queryProvider,
                                    SchemaPlus schema, String tableName) {
  return new AbstractTableQueryable<T>(queryProvider, schema, this,
      tableName) {
    @SuppressWarnings("unchecked")
    public Enumerator<T> enumerator() {
      return null;
    }
  };
}
 
Example #14
Source File: QueryableRelBuilder.java    From calcite with Apache License 2.0 5 votes vote down vote up
public <TKey, TResult> Queryable<TResult> groupByK(
    Queryable<T> source,
    FunctionExpression<Function1<T, TKey>> keySelector,
    FunctionExpression<Function2<TKey, Enumerable<T>, TResult>>
        elementSelector,
    EqualityComparer<TKey> comparer) {
  throw new UnsupportedOperationException();
}
 
Example #15
Source File: QueryableRelBuilder.java    From calcite with Apache License 2.0 5 votes vote down vote up
public <TKey, TElement, TResult> Queryable<TResult> groupBy(
    Queryable<T> source,
    FunctionExpression<Function1<T, TKey>> keySelector,
    FunctionExpression<Function1<T, TElement>> elementSelector,
    FunctionExpression<Function2<TKey, Enumerable<TElement>, TResult>>
        resultSelector,
    EqualityComparer<TKey> comparer) {
  throw new UnsupportedOperationException();
}
 
Example #16
Source File: TableInRootSchemaTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
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>) enumeratorImpl(null);
    }
  };
}
 
Example #17
Source File: TpcdsSchema.java    From calcite with Apache License 2.0 5 votes vote down vote up
public <T> Queryable<T> asQueryable(final QueryProvider queryProvider,
    final SchemaPlus schema, final String tableName) {
  //noinspection unchecked
  return (Queryable) new AbstractTableQueryable<Object[]>(queryProvider,
      schema, this, tableName) {
    public Enumerator<Object[]> enumerator() {
      final Session session =
          Session.getDefaultSession()
              .withTable(tpcdsTable)
              .withScale(scaleFactor);
      final Results results = Results.constructResults(tpcdsTable, session);
      return Linq4j.asEnumerable(results)
          .selectMany(
              new Function1<List<List<String>>, Enumerable<Object[]>>() {
                final Column[] columns = tpcdsTable.getColumns();

                public Enumerable<Object[]> apply(
                    List<List<String>> inRows) {
                  final List<Object[]> rows = new ArrayList<>();
                  for (List<String> strings : inRows) {
                    final Object[] values = new Object[columns.length];
                    for (int i = 0; i < strings.size(); i++) {
                      values[i] = convert(strings.get(i), columns[i]);
                    }
                    rows.add(values);
                  }
                  return Linq4j.asEnumerable(rows);
                }

              })
          .enumerator();
    }
  };
}
 
Example #18
Source File: QueryableRelBuilder.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Queryable<T> except(
    Queryable<T> source,
    Enumerable<T> enumerable,
    EqualityComparer<T> tEqualityComparer,
    boolean all) {
  throw new UnsupportedOperationException();
}
 
Example #19
Source File: QueryableRelBuilder.java    From calcite with Apache License 2.0 5 votes vote down vote up
public <TKey, TElement> Queryable<Grouping<TKey, TElement>> groupBy(
    Queryable<T> source,
    FunctionExpression<Function1<T, TKey>> keySelector,
    FunctionExpression<Function1<T, TElement>> elementSelector,
    EqualityComparer<TKey> comparer) {
  throw new UnsupportedOperationException();
}
 
Example #20
Source File: QueryableRelBuilder.java    From calcite with Apache License 2.0 5 votes vote down vote up
public <TInner, TKey, TResult> Queryable<TResult> groupJoin(
    Queryable<T> source,
    Enumerable<TInner> inner,
    FunctionExpression<Function1<T, TKey>> outerKeySelector,
    FunctionExpression<Function1<TInner, TKey>> innerKeySelector,
    FunctionExpression<Function2<T, Enumerable<TInner>, TResult>>
        resultSelector,
    EqualityComparer<TKey> comparer) {
  throw new UnsupportedOperationException();
}
 
Example #21
Source File: QueryableRelBuilder.java    From calcite with Apache License 2.0 5 votes vote down vote up
public <TInner, TKey, TResult> Queryable<TResult> join(
    Queryable<T> source,
    Enumerable<TInner> inner,
    FunctionExpression<Function1<T, TKey>> outerKeySelector,
    FunctionExpression<Function1<TInner, TKey>> innerKeySelector,
    FunctionExpression<Function2<T, TInner, TResult>> resultSelector) {
  throw new UnsupportedOperationException();
}
 
Example #22
Source File: Linq4jTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testTakeQueryable() {
  final Queryable<Department> querableDepts =
      Linq4j.asEnumerable(depts).asQueryable();
  final List<Department> queryableResult =
      QueryableDefaults.take(querableDepts, 2).toList();

  assertEquals(2, queryableResult.size());
  assertEquals(depts[0], queryableResult.get(0));
  assertEquals(depts[1], queryableResult.get(1));
}
 
Example #23
Source File: QueryableRelBuilder.java    From calcite with Apache License 2.0 5 votes vote down vote up
public <TAccumulate, TResult> TResult aggregate(
    Queryable<T> source,
    TAccumulate seed,
    FunctionExpression<Function2<TAccumulate, T, TAccumulate>> func,
    FunctionExpression<Function1<TAccumulate, TResult>> selector) {
  throw new UnsupportedOperationException();
}
 
Example #24
Source File: Linq4jTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testTakeWhileNNoMatch() {
  final Queryable<Department> queryableDepts =
      Linq4j.asEnumerable(depts).asQueryable();
  Predicate2<Department, Integer> function2 = Functions.falsePredicate2();
  final List<Department> deptList =
      QueryableDefaults.takeWhileN(
          queryableDepts,
          Expressions.lambda(function2))
          .toList();

  assertEquals(0, deptList.size());
}
 
Example #25
Source File: CalciteMetaImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
public <T> Queryable<T> asQueryable(QueryProvider queryProvider,
    SchemaPlus schema, String tableName) {
  return new AbstractTableQueryable<T>(queryProvider, schema, this,
      tableName) {
    @SuppressWarnings("unchecked")
    public Enumerator<T> enumerator() {
      return (Enumerator<T>) MetadataTable.this.enumerator(
          ((CalciteConnectionImpl) queryProvider).meta());
    }
  };
}
 
Example #26
Source File: CalcitePrepareImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
public PreparedResult prepareQueryable(
    final Queryable queryable,
    RelDataType resultType) {
  return prepare_(() -> {
    final RelOptCluster cluster =
        prepare.createCluster(planner, rexBuilder);
    return new LixToRelTranslator(cluster, CalcitePreparingStmt.this)
        .translate(queryable);
  }, resultType);
}
 
Example #27
Source File: ArrayTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
public <T> Queryable<T> asQueryable(final QueryProvider queryProvider,
    SchemaPlus schema, String tableName) {
  return new AbstractTableQueryable<T>(queryProvider, schema, this,
      tableName) {
    @SuppressWarnings("unchecked")
    public Enumerator<T> enumerator() {
      final Content content = supplier.get();
      return content.enumerator();
    }
  };
}
 
Example #28
Source File: Smalls.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** A function that generates multiplication table of {@code ncol} columns x
 * {@code nrow} rows. */
public static QueryableTable multiplicationTable(final int ncol,
    final int nrow, Integer offset) {
  final int offs = offset == null ? 0 : offset;
  return new AbstractQueryableTable(Object[].class) {
    public RelDataType getRowType(RelDataTypeFactory typeFactory) {
      final RelDataTypeFactory.Builder builder = typeFactory.builder();
      builder.add("row_name", typeFactory.createJavaType(String.class));
      final RelDataType int_ = typeFactory.createJavaType(int.class);
      for (int i = 1; i <= ncol; i++) {
        builder.add("c" + i, int_);
      }
      return builder.build();
    }

    public Queryable<Object[]> asQueryable(QueryProvider queryProvider,
        SchemaPlus schema, String tableName) {
      final List<Object[]> table = new AbstractList<Object[]>() {
        @Override public Object[] get(int index) {
          Object[] cur = new Object[ncol + 1];
          cur[0] = "row " + index;
          for (int j = 1; j <= ncol; j++) {
            cur[j] = j * (index + 1) + offs;
          }
          return cur;
        }

        @Override public int size() {
          return nrow;
        }
      };
      return Linq4j.asEnumerable(table).asQueryable();
    }
  };
}
 
Example #29
Source File: QueryableRelBuilder.java    From calcite with Apache License 2.0 4 votes vote down vote up
public <TKey, TElement> Queryable<Grouping<TKey, TElement>> groupBy(
    Queryable<T> source,
    FunctionExpression<Function1<T, TKey>> keySelector,
    FunctionExpression<Function1<T, TElement>> elementSelector) {
  throw new UnsupportedOperationException();
}
 
Example #30
Source File: QueryableRelBuilder.java    From calcite with Apache License 2.0 4 votes vote down vote up
public boolean contains(
    Queryable<T> source, T element) {
  throw new UnsupportedOperationException();
}