Search in sources :

Example 16 with ReflectiveSchema

use of org.apache.calcite.adapter.java.ReflectiveSchema in project calcite by apache.

the class CalciteAssert method addSchema.

public static SchemaPlus addSchema(SchemaPlus rootSchema, SchemaSpec schema) {
    SchemaPlus foodmart;
    SchemaPlus jdbcScott;
    final ConnectionSpec cs;
    final DataSource dataSource;
    switch(schema) {
        case REFLECTIVE_FOODMART:
            return rootSchema.add("foodmart", new ReflectiveSchema(new JdbcTest.FoodmartSchema()));
        case JDBC_SCOTT:
            cs = DatabaseInstance.HSQLDB.scott;
            dataSource = JdbcSchema.dataSource(cs.url, cs.driver, cs.username, cs.password);
            return rootSchema.add("JDBC_SCOTT", JdbcSchema.create(rootSchema, "JDBC_SCOTT", dataSource, cs.catalog, cs.schema));
        case JDBC_FOODMART:
            cs = DB.foodmart;
            dataSource = JdbcSchema.dataSource(cs.url, cs.driver, cs.username, cs.password);
            return rootSchema.add("foodmart", JdbcSchema.create(rootSchema, "foodmart", dataSource, cs.catalog, cs.schema));
        case JDBC_FOODMART_WITH_LATTICE:
            foodmart = rootSchema.getSubSchema("foodmart");
            if (foodmart == null) {
                foodmart = CalciteAssert.addSchema(rootSchema, SchemaSpec.JDBC_FOODMART);
            }
            foodmart.add("lattice", Lattice.create(foodmart.unwrap(CalciteSchema.class), "select 1 from \"foodmart\".\"sales_fact_1997\" as s\n" + "join \"foodmart\".\"time_by_day\" as t using (\"time_id\")\n" + "join \"foodmart\".\"customer\" as c using (\"customer_id\")\n" + "join \"foodmart\".\"product\" as p using (\"product_id\")\n" + "join \"foodmart\".\"product_class\" as pc on p.\"product_class_id\" = pc.\"product_class_id\"", true));
            return foodmart;
        case SCOTT:
            jdbcScott = rootSchema.getSubSchema("jdbc_scott");
            if (jdbcScott == null) {
                jdbcScott = CalciteAssert.addSchema(rootSchema, SchemaSpec.JDBC_SCOTT);
            }
            return rootSchema.add("scott", new CloneSchema(jdbcScott));
        case CLONE_FOODMART:
            foodmart = rootSchema.getSubSchema("foodmart");
            if (foodmart == null) {
                foodmart = CalciteAssert.addSchema(rootSchema, SchemaSpec.JDBC_FOODMART);
            }
            return rootSchema.add("foodmart2", new CloneSchema(foodmart));
        case GEO:
            ModelHandler.addFunctions(rootSchema, null, ImmutableList.<String>of(), GeoFunctions.class.getName(), "*", true);
            final SchemaPlus s = rootSchema.add("GEO", new AbstractSchema());
            ModelHandler.addFunctions(s, "countries", ImmutableList.<String>of(), CountriesTableFunction.class.getName(), null, false);
            final String sql = "select * from table(\"countries\"(true))";
            final ViewTableMacro viewMacro = ViewTable.viewMacro(rootSchema, sql, ImmutableList.of("GEO"), ImmutableList.<String>of(), false);
            s.add("countries", viewMacro);
            return s;
        case HR:
            return rootSchema.add("hr", new ReflectiveSchema(new JdbcTest.HrSchema()));
        case LINGUAL:
            return rootSchema.add("SALES", new ReflectiveSchema(new JdbcTest.LingualSchema()));
        case BLANK:
            return rootSchema.add("BLANK", new AbstractSchema());
        case ORINOCO:
            final SchemaPlus orinoco = rootSchema.add("ORINOCO", new AbstractSchema());
            orinoco.add("ORDERS", new StreamTest.OrdersHistoryTable(StreamTest.OrdersStreamTableFactory.getRowList()));
            return orinoco;
        case POST:
            final SchemaPlus post = rootSchema.add("POST", new AbstractSchema());
            post.add("EMP", ViewTable.viewMacro(post, "select * from (values\n" + "    ('Jane', 10, 'F'),\n" + "    ('Bob', 10, 'M'),\n" + "    ('Eric', 20, 'M'),\n" + "    ('Susan', 30, 'F'),\n" + "    ('Alice', 30, 'F'),\n" + "    ('Adam', 50, 'M'),\n" + "    ('Eve', 50, 'F'),\n" + "    ('Grace', 60, 'F'),\n" + "    ('Wilma', cast(null as integer), 'F'))\n" + "  as t(ename, deptno, gender)", ImmutableList.<String>of(), ImmutableList.of("POST", "EMP"), null));
            post.add("DEPT", ViewTable.viewMacro(post, "select * from (values\n" + "    (10, 'Sales'),\n" + "    (20, 'Marketing'),\n" + "    (30, 'Engineering'),\n" + "    (40, 'Empty')) as t(deptno, dname)", ImmutableList.<String>of(), ImmutableList.of("POST", "DEPT"), null));
            post.add("EMPS", ViewTable.viewMacro(post, "select * from (values\n" + "    (100, 'Fred',  10, CAST(NULL AS CHAR(1)), CAST(NULL AS VARCHAR(20)), 40,               25, TRUE,    FALSE, DATE '1996-08-03'),\n" + "    (110, 'Eric',  20, 'M',                   'San Francisco',           3,                80, UNKNOWN, FALSE, DATE '2001-01-01'),\n" + "    (110, 'John',  40, 'M',                   'Vancouver',               2, CAST(NULL AS INT), FALSE,   TRUE,  DATE '2002-05-03'),\n" + "    (120, 'Wilma', 20, 'F',                   CAST(NULL AS VARCHAR(20)), 1,                 5, UNKNOWN, TRUE,  DATE '2005-09-07'),\n" + "    (130, 'Alice', 40, 'F',                   'Vancouver',               2, CAST(NULL AS INT), FALSE,   TRUE,  DATE '2007-01-01'))\n" + " as t(empno, name, deptno, gender, city, empid, age, slacker, manager, joinedat)", ImmutableList.<String>of(), ImmutableList.of("POST", "EMPS"), null));
            return post;
        default:
            throw new AssertionError("unknown schema " + schema);
    }
}
Also used : CloneSchema(org.apache.calcite.adapter.clone.CloneSchema) GeoFunctions(org.apache.calcite.runtime.GeoFunctions) ViewTableMacro(org.apache.calcite.schema.impl.ViewTableMacro) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ReflectiveSchema(org.apache.calcite.adapter.java.ReflectiveSchema) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) DataSource(javax.sql.DataSource) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema)

Example 17 with ReflectiveSchema

use of org.apache.calcite.adapter.java.ReflectiveSchema in project calcite by apache.

the class ExceptionMessageTest method setUp.

@Before
public void setUp() throws SQLException {
    Connection connection = DriverManager.getConnection("jdbc:calcite:");
    CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
    SchemaPlus rootSchema = calciteConnection.getRootSchema();
    rootSchema.add("test", new ReflectiveSchema(new TestSchema()));
    calciteConnection.setSchema("test");
    this.conn = calciteConnection;
}
Also used : Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ReflectiveSchema(org.apache.calcite.adapter.java.ReflectiveSchema) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Before(org.junit.Before)

Example 18 with ReflectiveSchema

use of org.apache.calcite.adapter.java.ReflectiveSchema 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();
    }
}
Also used : AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) Statement(java.sql.Statement) Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ResultSet(java.sql.ResultSet) ReflectiveSchema(org.apache.calcite.adapter.java.ReflectiveSchema) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Test(org.junit.Test)

Example 19 with ReflectiveSchema

use of org.apache.calcite.adapter.java.ReflectiveSchema 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();
}
Also used : AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) Statement(java.sql.Statement) Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ResultSet(java.sql.ResultSet) ReflectiveSchema(org.apache.calcite.adapter.java.ReflectiveSchema) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Test(org.junit.Test)

Example 20 with ReflectiveSchema

use of org.apache.calcite.adapter.java.ReflectiveSchema in project calcite by apache.

the class PlannerTest method checkTpchQuery.

public String checkTpchQuery(String tpchTestQuery) throws Exception {
    final SchemaPlus schema = Frameworks.createRootSchema(true).add("tpch", new ReflectiveSchema(new TpchSchema()));
    final FrameworkConfig config = Frameworks.newConfigBuilder().parserConfig(SqlParser.configBuilder().setLex(Lex.MYSQL).build()).defaultSchema(schema).programs(Programs.ofRules(Programs.RULE_SET)).build();
    String plan;
    try (Planner p = Frameworks.getPlanner(config)) {
        SqlNode n = p.parse(tpchTestQuery);
        n = p.validate(n);
        RelNode r = p.rel(n).project();
        plan = RelOptUtil.toString(r);
    }
    return plan;
}
Also used : RelNode(org.apache.calcite.rel.RelNode) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ReflectiveSchema(org.apache.calcite.adapter.java.ReflectiveSchema) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

ReflectiveSchema (org.apache.calcite.adapter.java.ReflectiveSchema)22 SchemaPlus (org.apache.calcite.schema.SchemaPlus)21 CalciteConnection (org.apache.calcite.jdbc.CalciteConnection)16 Test (org.junit.Test)16 ResultSet (java.sql.ResultSet)13 Connection (java.sql.Connection)11 Statement (java.sql.Statement)11 PreparedStatement (java.sql.PreparedStatement)7 Properties (java.util.Properties)7 AbstractSchema (org.apache.calcite.schema.impl.AbstractSchema)7 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)6 AvaticaStatement (org.apache.calcite.avatica.AvaticaStatement)5 AvaticaConnection (org.apache.calcite.avatica.AvaticaConnection)4 Driver (org.apache.calcite.jdbc.Driver)3 RelNode (org.apache.calcite.rel.RelNode)3 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 RelOptPlanner (org.apache.calcite.plan.RelOptPlanner)2 SqlNode (org.apache.calcite.sql.SqlNode)2 JdbcTest (org.apache.calcite.test.JdbcTest)2