org.apache.calcite.adapter.java.AbstractQueryableTable Java Examples

The following examples show how to use org.apache.calcite.adapter.java.AbstractQueryableTable. 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: CodesFunction.java    From calcite with Apache License 2.0 6 votes vote down vote up
public static QueryableTable getTable(String name) {

    return new AbstractQueryableTable(Object[].class) {
      @Override public RelDataType getRowType(RelDataTypeFactory typeFactory) {
        return typeFactory.builder()
            .add("TYPE", SqlTypeName.VARCHAR)
            .add("CODEVALUE", SqlTypeName.VARCHAR)
            .build();
      }

      @Override public Queryable<String[]> asQueryable(QueryProvider queryProvider,
                                                       SchemaPlus schema,
                                                       String tableName) {
        if (name == null) {
          return Linq4j.<String[]>emptyEnumerable().asQueryable();
        }
        return Linq4j.asEnumerable(new String[][]{
            new String[]{"HASHCODE", "" + name.hashCode()},
            new String[]{"BASE64",
                Base64.getEncoder().encodeToString(name.getBytes(StandardCharsets.UTF_8))}
        }).asQueryable();
      }
    };
  }
 
Example #2
Source File: PreferredGenresTableFactory.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 fetchPreferredGenres();
    }
  };
}
 
Example #3
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 #4
Source File: Smalls.java    From calcite with Apache License 2.0 6 votes vote down vote up
private static QueryableTable oneThreePlus(String s) {
  List<Integer> items;
  // Argument is null in case SQL contains function call with expression.
  // Then the engine calls a function with null arguments to get getRowType.
  if (s == null) {
    items = ImmutableList.of();
  } else {
    Integer latest = Integer.parseInt(s.substring(1, s.length() - 1));
    items = ImmutableList.of(1, 3, latest);
  }
  final Enumerable<Integer> enumerable = Linq4j.asEnumerable(items);
  return new AbstractQueryableTable(Integer.class) {
    public <E> Queryable<E> asQueryable(
        QueryProvider queryProvider, SchemaPlus schema, String tableName) {
      //noinspection unchecked
      return (Queryable<E>) enumerable.asQueryable();
    }

    public RelDataType getRowType(RelDataTypeFactory typeFactory) {
      return typeFactory.builder().add("c", SqlTypeName.INTEGER).build();
    }
  };
}
 
Example #5
Source File: Smalls.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * A function that adds a number to the first column of input cursor
 */
public static QueryableTable processCursor(final int offset,
    final Enumerable<Object[]> a) {
  return new AbstractQueryableTable(Object[].class) {
    public RelDataType getRowType(RelDataTypeFactory typeFactory) {
      return typeFactory.builder()
          .add("result", SqlTypeName.INTEGER)
          .build();
    }

    public <T> Queryable<T> asQueryable(QueryProvider queryProvider,
        SchemaPlus schema, String tableName) {
      final Enumerable<Integer> enumerable =
          a.select(a0 -> offset + ((Integer) a0[0]));
      //noinspection unchecked
      return (Queryable) enumerable.asQueryable();
    }
  };
}
 
Example #6
Source File: Smalls.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * A function that sums the second column of first input cursor, second
 * column of first input and the given int.
 */
public static QueryableTable processCursors(final int offset,
    final Enumerable<Object[]> a, final Enumerable<IntString> b) {
  return new AbstractQueryableTable(Object[].class) {
    public RelDataType getRowType(RelDataTypeFactory typeFactory) {
      return typeFactory.builder()
          .add("result", SqlTypeName.INTEGER)
          .build();
    }

    public <T> Queryable<T> asQueryable(QueryProvider queryProvider,
        SchemaPlus schema, String tableName) {
      final Enumerable<Integer> enumerable =
          a.zip(b, (v0, v1) -> ((Integer) v0[1]) + v1.n + offset);
      //noinspection unchecked
      return (Queryable) enumerable.asQueryable();
    }
  };
}
 
Example #7
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 #8
Source File: UDTFExplode.java    From marble with Apache License 2.0 4 votes vote down vote up
public static QueryableTable eval(List<String> params) {
  return new AbstractQueryableTable(String.class) {
    public RelDataType getRowType(RelDataTypeFactory typeFactory) {
      RelDataType dataType = typeFactory.createSqlType(
          SqlTypeName.VARCHAR);
      return typeFactory.createStructType(
          Lists.newArrayList(dataType), Lists.newArrayList("s"));
    }

    public <T> Queryable<T> asQueryable(QueryProvider queryProvider,
        SchemaPlus schema, String tableName) {
      BaseQueryable<String> queryable =
          new BaseQueryable<String>(null, String.class, null) {
            public Enumerator<String> enumerator() {
              return new Enumerator<String>() {
                int i = 0;
                int curI;
                String curS;

                public String current() {
                  return curS;
                }

                public boolean moveNext() {
                  if (params == null) {
                    return false;
                  }
                  if (i < params.size()) {
                    curI = i;
                    curS = params.get(i);
                    ++i;
                    return true;
                  } else {
                    return false;
                  }
                }

                public void reset() {
                  i = 0;
                }

                public void close() {
                }
              };
            }
          };
      //noinspection unchecked
      return (Queryable<T>) queryable;
    }
  };
}
 
Example #9
Source File: Smalls.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** A function that generates a table that generates a sequence of
 * {@link IntString} values. */
public static QueryableTable generateStrings(final Integer count) {
  return new AbstractQueryableTable(IntString.class) {
    public RelDataType getRowType(RelDataTypeFactory typeFactory) {
      return typeFactory.createJavaType(IntString.class);
    }

    public <T> Queryable<T> asQueryable(QueryProvider queryProvider,
        SchemaPlus schema, String tableName) {
      BaseQueryable<IntString> queryable =
          new BaseQueryable<IntString>(null, IntString.class, null) {
            public Enumerator<IntString> enumerator() {
              return new Enumerator<IntString>() {
                static final String Z = "abcdefghijklm";

                int i = 0;
                int curI;
                String curS;

                public IntString current() {
                  return new IntString(curI, curS);
                }

                public boolean moveNext() {
                  if (i < count) {
                    curI = i;
                    curS = Z.substring(0, i % Z.length());
                    ++i;
                    return true;
                  } else {
                    return false;
                  }
                }

                public void reset() {
                  i = 0;
                }

                public void close() {
                }
              };
            }
          };
      //noinspection unchecked
      return (Queryable<T>) queryable;
    }
  };
}