use of org.apache.calcite.schema.impl.AbstractSchema in project calcite by apache.
the class TableFunctionTest method testScannableTableFunctionWithNamedParameters.
/**
* As {@link #testScannableTableFunction()} but with named parameters.
*/
@Test
public 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();
}
use of org.apache.calcite.schema.impl.AbstractSchema in project calcite by apache.
the class UdfTest method testArrayUserDefinedFunction.
/**
* Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-1834">[CALCITE-1834]
* User-defined function for Arrays</a>.
*/
@Test
public void testArrayUserDefinedFunction() throws Exception {
try (Connection connection = DriverManager.getConnection("jdbc:calcite:")) {
CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
SchemaPlus rootSchema = calciteConnection.getRootSchema();
rootSchema.add("hr", new ReflectiveSchema(new JdbcTest.HrSchema()));
SchemaPlus post = rootSchema.add("POST", new AbstractSchema());
post.add("ARRAY_APPEND", new ArrayAppendDoubleFunction());
post.add("ARRAY_APPEND", new ArrayAppendIntegerFunction());
final String sql = "select \"empid\" as EMPLOYEE_ID,\n" + " \"name\" || ' ' || \"name\" as EMPLOYEE_NAME,\n" + " \"salary\" as EMPLOYEE_SALARY,\n" + " POST.ARRAY_APPEND(ARRAY[1,2,3], \"deptno\") as DEPARTMENTS\n" + "from \"hr\".\"emps\"";
final String result = "" + "EMPLOYEE_ID=100; EMPLOYEE_NAME=Bill Bill;" + " EMPLOYEE_SALARY=10000.0; DEPARTMENTS=[1, 2, 3, 10]\n" + "EMPLOYEE_ID=200; EMPLOYEE_NAME=Eric Eric;" + " EMPLOYEE_SALARY=8000.0; DEPARTMENTS=[1, 2, 3, 20]\n" + "EMPLOYEE_ID=150; EMPLOYEE_NAME=Sebastian Sebastian;" + " EMPLOYEE_SALARY=7000.0; DEPARTMENTS=[1, 2, 3, 10]\n" + "EMPLOYEE_ID=110; EMPLOYEE_NAME=Theodore Theodore;" + " EMPLOYEE_SALARY=11500.0; DEPARTMENTS=[1, 2, 3, 10]\n";
try (Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql)) {
assertThat(CalciteAssert.toString(resultSet), is(result));
}
connection.close();
}
}
use of org.apache.calcite.schema.impl.AbstractSchema in project calcite by apache.
the class UdfTest method testUserDefinedFunctionInView.
/**
* Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-937">[CALCITE-937]
* User-defined function within view</a>.
*/
@Test
public void testUserDefinedFunctionInView() throws Exception {
Class.forName("org.apache.calcite.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:calcite:");
CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
SchemaPlus rootSchema = calciteConnection.getRootSchema();
rootSchema.add("hr", new ReflectiveSchema(new JdbcTest.HrSchema()));
SchemaPlus post = rootSchema.add("POST", new AbstractSchema());
post.add("MY_INCREMENT", ScalarFunctionImpl.create(Smalls.MyIncrement.class, "eval"));
final String viewSql = "select \"empid\" as EMPLOYEE_ID,\n" + " \"name\" || ' ' || \"name\" as EMPLOYEE_NAME,\n" + " \"salary\" as EMPLOYEE_SALARY,\n" + " POST.MY_INCREMENT(\"empid\", 10) as INCREMENTED_SALARY\n" + "from \"hr\".\"emps\"";
post.add("V_EMP", ViewTable.viewMacro(post, viewSql, ImmutableList.<String>of(), ImmutableList.of("POST", "V_EMP"), null));
final String result = "" + "EMPLOYEE_ID=100; EMPLOYEE_NAME=Bill Bill; EMPLOYEE_SALARY=10000.0; INCREMENTED_SALARY=110.0\n" + "EMPLOYEE_ID=200; EMPLOYEE_NAME=Eric Eric; EMPLOYEE_SALARY=8000.0; INCREMENTED_SALARY=220.0\n" + "EMPLOYEE_ID=150; EMPLOYEE_NAME=Sebastian Sebastian; EMPLOYEE_SALARY=7000.0; INCREMENTED_SALARY=165.0\n" + "EMPLOYEE_ID=110; EMPLOYEE_NAME=Theodore Theodore; EMPLOYEE_SALARY=11500.0; INCREMENTED_SALARY=121.0\n";
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(viewSql);
assertThat(CalciteAssert.toString(resultSet), is(result));
resultSet.close();
ResultSet viewResultSet = statement.executeQuery("select * from \"POST\".\"V_EMP\"");
assertThat(CalciteAssert.toString(viewResultSet), is(result));
statement.close();
connection.close();
}
Aggregations