org.apache.calcite.schema.impl.TableFunctionImpl Java Examples

The following examples show how to use org.apache.calcite.schema.impl.TableFunctionImpl. 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: ExampleFunctionTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
public void checkMazeTableFunction(Boolean solution, String maze)
    throws SQLException, ClassNotFoundException {
  Connection connection = DriverManager.getConnection("jdbc:calcite:");
  CalciteConnection calciteConnection =
      connection.unwrap(CalciteConnection.class);
  SchemaPlus rootSchema = calciteConnection.getRootSchema();
  SchemaPlus schema = rootSchema.add("s", new AbstractSchema());
  final TableFunction table = TableFunctionImpl.create(MAZE_METHOD);
  schema.add("Maze", table);
  final TableFunction table2 = TableFunctionImpl.create(SOLVE_METHOD);
  schema.add("Solve", table2);
  final String sql;
  if (solution) {
    sql = "select *\n"
        + "from table(\"s\".\"Solve\"(5, 3, 1)) as t(s)";
  } else {
    sql = "select *\n"
        + "from table(\"s\".\"Maze\"(5, 3, 1)) as t(s)";
  }
  ResultSet resultSet = connection.createStatement().executeQuery(sql);
  final StringBuilder b = new StringBuilder();
  while (resultSet.next()) {
    b.append(resultSet.getString(1)).append("\n");
  }
  assertThat(b.toString(), is(maze));
}
 
Example #2
Source File: EnumerableTableFunctionScan.java    From calcite with Apache License 2.0 6 votes vote down vote up
private boolean isQueryable() {
  if (!(getCall() instanceof RexCall)) {
    return false;
  }
  final RexCall call = (RexCall) getCall();
  if (!(call.getOperator() instanceof SqlUserDefinedTableFunction)) {
    return false;
  }
  final SqlUserDefinedTableFunction udtf =
      (SqlUserDefinedTableFunction) call.getOperator();
  if (!(udtf.getFunction() instanceof TableFunctionImpl)) {
    return false;
  }
  final TableFunctionImpl tableFunction =
      (TableFunctionImpl) udtf.getFunction();
  final Method method = tableFunction.method;
  return QueryableTable.class.isAssignableFrom(method.getReturnType());
}
 
Example #3
Source File: TableFunctionTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Tests a table function with literal arguments.
 */
@Test void testTableFunction() throws SQLException {
  try (Connection connection = DriverManager.getConnection("jdbc:calcite:")) {
    CalciteConnection calciteConnection =
        connection.unwrap(CalciteConnection.class);
    SchemaPlus rootSchema = calciteConnection.getRootSchema();
    SchemaPlus schema = rootSchema.add("s", new AbstractSchema());
    final TableFunction table =
        TableFunctionImpl.create(Smalls.GENERATE_STRINGS_METHOD);
    schema.add("GenerateStrings", table);
    final String sql = "select *\n"
        + "from table(\"s\".\"GenerateStrings\"(5)) as t(n, c)\n"
        + "where char_length(c) > 3";
    ResultSet resultSet = connection.createStatement().executeQuery(sql);
    assertThat(CalciteAssert.toString(resultSet),
        equalTo("N=4; C=abcd\n"));
  }
}
 
Example #4
Source File: TableFunctionTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testTableFunctionWithArrayParameter() throws SQLException {
  try (Connection connection = DriverManager.getConnection("jdbc:calcite:")) {
    CalciteConnection calciteConnection =
        connection.unwrap(CalciteConnection.class);
    SchemaPlus rootSchema = calciteConnection.getRootSchema();
    SchemaPlus schema = rootSchema.add("s", new AbstractSchema());
    final TableFunction table =
        TableFunctionImpl.create(Smalls.GENERATE_STRINGS_OF_INPUT_SIZE_METHOD);
    schema.add("GenerateStringsOfInputSize", table);
    final String sql = "select *\n"
        + "from table(\"s\".\"GenerateStringsOfInputSize\"(ARRAY[5,4,3,1,2])) as t(n, c)\n"
        + "where char_length(c) > 3";
    ResultSet resultSet = connection.createStatement().executeQuery(sql);
    assertThat(CalciteAssert.toString(resultSet),
        equalTo("N=4; C=abcd\n"));
  }
}
 
Example #5
Source File: TableFunctionTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testTableFunctionWithMapParameter() throws SQLException {
  try (Connection connection = DriverManager.getConnection("jdbc:calcite:")) {
    CalciteConnection calciteConnection =
        connection.unwrap(CalciteConnection.class);
    SchemaPlus rootSchema = calciteConnection.getRootSchema();
    SchemaPlus schema = rootSchema.add("s", new AbstractSchema());
    final TableFunction table =
        TableFunctionImpl.create(Smalls.GENERATE_STRINGS_OF_INPUT_MAP_SIZE_METHOD);
    schema.add("GenerateStringsOfInputMapSize", table);
    final String sql = "select *\n"
        + "from table(\"s\".\"GenerateStringsOfInputMapSize\"(Map[5,4,3,1])) as t(n, c)\n"
        + "where char_length(c) > 0";
    ResultSet resultSet = connection.createStatement().executeQuery(sql);
    assertThat(CalciteAssert.toString(resultSet),
        equalTo("N=1; C=a\n"));
  }
}
 
Example #6
Source File: TableFunctionTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Tests a table function that implements {@link ScannableTable} and returns
 * a single column.
 */
@Test void testScannableTableFunction()
    throws SQLException, ClassNotFoundException {
  Connection connection = DriverManager.getConnection("jdbc:calcite:");
  CalciteConnection calciteConnection =
      connection.unwrap(CalciteConnection.class);
  SchemaPlus rootSchema = calciteConnection.getRootSchema();
  SchemaPlus schema = rootSchema.add("s", new AbstractSchema());
  final TableFunction table = TableFunctionImpl.create(Smalls.MAZE_METHOD);
  schema.add("Maze", table);
  final String sql = "select *\n"
      + "from table(\"s\".\"Maze\"(5, 3, 1))";
  ResultSet resultSet = connection.createStatement().executeQuery(sql);
  final String result = "S=abcde\n"
      + "S=xyz\n"
      + "S=generate(w=5, h=3, s=1)\n";
  assertThat(CalciteAssert.toString(resultSet), is(result));
}
 
Example #7
Source File: TableFunctionTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-2382">[CALCITE-2382]
 * Sub-query lateral joined to table function</a>. */
@Test void testInlineViewLateralTableFunction() throws SQLException {
  try (Connection connection = DriverManager.getConnection("jdbc:calcite:")) {
    CalciteConnection calciteConnection =
        connection.unwrap(CalciteConnection.class);
    SchemaPlus rootSchema = calciteConnection.getRootSchema();
    SchemaPlus schema = rootSchema.add("s", new AbstractSchema());
    final TableFunction table =
        TableFunctionImpl.create(Smalls.GENERATE_STRINGS_METHOD);
    schema.add("GenerateStrings", table);
    Table tbl = new ScannableTableTest.SimpleTable();
    schema.add("t", tbl);

    final String sql = "select *\n"
        + "from (select 5 as f0 from \"s\".\"t\") \"a\",\n"
        + "  lateral table(\"s\".\"GenerateStrings\"(f0)) as t(n, c)\n"
        + "where char_length(c) > 3";
    ResultSet resultSet = connection.createStatement().executeQuery(sql);
    final String expected = "F0=5; N=4; C=abcd\n"
        + "F0=5; N=4; C=abcd\n"
        + "F0=5; N=4; C=abcd\n"
        + "F0=5; N=4; C=abcd\n";
    assertThat(CalciteAssert.toString(resultSet), equalTo(expected));
  }
}
 
Example #8
Source File: TableFunctions.java    From mat-calcite-plugin with Apache License 2.0 5 votes vote down vote up
public static Multimap<String, TableFunction> createAll() {
    ImmutableMultimap.Builder<String, TableFunction> builder = ImmutableMultimap.builder();
    builder.put("getValues", TableFunctionImpl.create(TableFunctions.class, "getValues"));
    builder.put("getMapEntries", TableFunctionImpl.create(TableFunctions.class, "getMapEntries"));
    builder.put("getRetainedSet", TableFunctionImpl.create(TableFunctions.class, "getRetainedSet"));
    builder.put("getOutboundReferences", TableFunctionImpl.create(TableFunctions.class, "getOutboundReferences"));
    builder.put("getInboundReferences", TableFunctionImpl.create(TableFunctions.class, "getInboundReferences"));
    return builder.build();
}
 
Example #9
Source File: TableFunctionTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** As {@link #testScannableTableFunction()} but with named parameters. */
@Test void testScannableTableFunctionWithNamedParameters()
    throws SQLException, ClassNotFoundException {
  Connection connection = DriverManager.getConnection("jdbc:calcite:");
  CalciteConnection calciteConnection =
      connection.unwrap(CalciteConnection.class);
  SchemaPlus rootSchema = calciteConnection.getRootSchema();
  SchemaPlus schema = rootSchema.add("s", new AbstractSchema());
  final TableFunction table = TableFunctionImpl.create(Smalls.MAZE2_METHOD);
  schema.add("Maze", table);
  final String sql = "select *\n"
      + "from table(\"s\".\"Maze\"(5, 3, 1))";
  final Statement statement = connection.createStatement();
  ResultSet resultSet = statement.executeQuery(sql);
  final String result = "S=abcde\n"
      + "S=xyz\n";
  assertThat(CalciteAssert.toString(resultSet),
      is(result + "S=generate2(w=5, h=3, s=1)\n"));

  final String sql2 = "select *\n"
      + "from table(\"s\".\"Maze\"(WIDTH => 5, HEIGHT => 3, SEED => 1))";
  resultSet = statement.executeQuery(sql2);
  assertThat(CalciteAssert.toString(resultSet),
      is(result + "S=generate2(w=5, h=3, s=1)\n"));

  final String sql3 = "select *\n"
      + "from table(\"s\".\"Maze\"(HEIGHT => 3, WIDTH => 5))";
  resultSet = statement.executeQuery(sql3);
  assertThat(CalciteAssert.toString(resultSet),
      is(result + "S=generate2(w=5, h=3, s=null)\n"));
  connection.close();
}
 
Example #10
Source File: TableFunctionTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
private Connection getConnectionWithMultiplyFunction() throws SQLException {
  Connection connection =
      DriverManager.getConnection("jdbc:calcite:");
  CalciteConnection calciteConnection =
      connection.unwrap(CalciteConnection.class);
  SchemaPlus rootSchema = calciteConnection.getRootSchema();
  SchemaPlus schema = rootSchema.add("s", new AbstractSchema());
  final TableFunction table =
      TableFunctionImpl.create(Smalls.MULTIPLICATION_TABLE_METHOD);
  schema.add("multiplication", table);
  return connection;
}
 
Example #11
Source File: LookupOperatorOverloadsTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
private void checkInternal(boolean caseSensitive) throws SQLException {
  final SqlNameMatcher nameMatcher =
      SqlNameMatchers.withCaseSensitive(caseSensitive);
  final String schemaName = "MySchema";
  final String funcName = "MyFUNC";
  final String anotherName = "AnotherFunc";

  try (Connection connection = DriverManager.getConnection("jdbc:calcite:")) {
    CalciteConnection calciteConnection =
        connection.unwrap(CalciteConnection.class);
    SchemaPlus rootSchema = calciteConnection.getRootSchema();
    SchemaPlus schema = rootSchema.add(schemaName, new AbstractSchema());
    final TableFunction table = TableFunctionImpl.create(Smalls.MAZE_METHOD);
    schema.add(funcName, table);
    schema.add(anotherName, table);
    final TableFunction table2 =
        TableFunctionImpl.create(Smalls.MAZE3_METHOD);
    schema.add(funcName, table2);

    final CalciteServerStatement statement =
        connection.createStatement().unwrap(CalciteServerStatement.class);
    final CalcitePrepare.Context prepareContext =
        statement.createPrepareContext();
    final JavaTypeFactory typeFactory = prepareContext.getTypeFactory();
    CalciteCatalogReader reader =
        new CalciteCatalogReader(prepareContext.getRootSchema(),
            ImmutableList.of(), typeFactory, prepareContext.config());

    final List<SqlOperator> operatorList = new ArrayList<>();
    SqlIdentifier myFuncIdentifier =
        new SqlIdentifier(Lists.newArrayList(schemaName, funcName), null,
            SqlParserPos.ZERO, null);
    reader.lookupOperatorOverloads(myFuncIdentifier,
        SqlFunctionCategory.USER_DEFINED_TABLE_FUNCTION, SqlSyntax.FUNCTION,
        operatorList, nameMatcher);
    checkFunctionType(2, funcName, operatorList);

    operatorList.clear();
    reader.lookupOperatorOverloads(myFuncIdentifier,
        SqlFunctionCategory.USER_DEFINED_FUNCTION, SqlSyntax.FUNCTION,
        operatorList, nameMatcher);
    checkFunctionType(0, null, operatorList);

    operatorList.clear();
    SqlIdentifier anotherFuncIdentifier =
        new SqlIdentifier(Lists.newArrayList(schemaName, anotherName), null,
            SqlParserPos.ZERO, null);
    reader.lookupOperatorOverloads(anotherFuncIdentifier,
        SqlFunctionCategory.USER_DEFINED_TABLE_FUNCTION, SqlSyntax.FUNCTION,
        operatorList, nameMatcher);
    checkFunctionType(1, anotherName, operatorList);
  }
}
 
Example #12
Source File: TableFunctionTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** As {@link #testScannableTableFunction()} but with named parameters. */
@Test void testMultipleScannableTableFunctionWithNamedParameters()
    throws SQLException, ClassNotFoundException {
  try (Connection connection = DriverManager.getConnection("jdbc:calcite:");
       Statement statement = connection.createStatement()) {
    CalciteConnection calciteConnection =
        connection.unwrap(CalciteConnection.class);
    SchemaPlus rootSchema = calciteConnection.getRootSchema();
    SchemaPlus schema = rootSchema.add("s", new AbstractSchema());
    final TableFunction table1 = TableFunctionImpl.create(Smalls.MAZE_METHOD);
    schema.add("Maze", table1);
    final TableFunction table2 = TableFunctionImpl.create(Smalls.MAZE2_METHOD);
    schema.add("Maze", table2);
    final TableFunction table3 = TableFunctionImpl.create(Smalls.MAZE3_METHOD);
    schema.add("Maze", table3);
    final String sql = "select *\n"
        + "from table(\"s\".\"Maze\"(5, 3, 1))";
    ResultSet resultSet = statement.executeQuery(sql);
    final String result = "S=abcde\n"
        + "S=xyz\n";
    assertThat(CalciteAssert.toString(resultSet),
        is(result + "S=generate(w=5, h=3, s=1)\n"));

    final String sql2 = "select *\n"
        + "from table(\"s\".\"Maze\"(WIDTH => 5, HEIGHT => 3, SEED => 1))";
    resultSet = statement.executeQuery(sql2);
    assertThat(CalciteAssert.toString(resultSet),
        is(result + "S=generate2(w=5, h=3, s=1)\n"));

    final String sql3 = "select *\n"
        + "from table(\"s\".\"Maze\"(HEIGHT => 3, WIDTH => 5))";
    resultSet = statement.executeQuery(sql3);
    assertThat(CalciteAssert.toString(resultSet),
        is(result + "S=generate2(w=5, h=3, s=null)\n"));

    final String sql4 = "select *\n"
        + "from table(\"s\".\"Maze\"(FOO => 'a'))";
    resultSet = statement.executeQuery(sql4);
    assertThat(CalciteAssert.toString(resultSet),
        is(result + "S=generate3(foo=a)\n"));
  }
}