Search in sources :

Example 26 with CalciteConnection

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection in project calcite by apache.

the class LookupOperatorOverloadsTest method test.

@Test
public void test() throws SQLException {
    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.<String>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);
        checkFunctionType(2, funcName, operatorList);
        operatorList.clear();
        reader.lookupOperatorOverloads(myFuncIdentifier, SqlFunctionCategory.USER_DEFINED_FUNCTION, SqlSyntax.FUNCTION, operatorList);
        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);
        checkFunctionType(1, anotherName, operatorList);
    }
}
Also used : SqlOperator(org.apache.calcite.sql.SqlOperator) Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ArrayList(java.util.ArrayList) CalcitePrepare(org.apache.calcite.jdbc.CalcitePrepare) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) CalciteServerStatement(org.apache.calcite.server.CalciteServerStatement) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) TableFunction(org.apache.calcite.schema.TableFunction) SqlUserDefinedTableFunction(org.apache.calcite.sql.validate.SqlUserDefinedTableFunction) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Test(org.junit.Test)

Example 27 with CalciteConnection

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection in project calcite by apache.

the class JdbcExample method run.

public void run() throws ClassNotFoundException, SQLException {
    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 Hr()));
    rootSchema.add("foodmart", new ReflectiveSchema(new Foodmart()));
    Statement statement = connection.createStatement();
    ResultSet resultSet = statement.executeQuery("select *\n" + "from \"foodmart\".\"sales_fact_1997\" as s\n" + "join \"hr\".\"emps\" as e\n" + "on e.\"empid\" = s.\"cust_id\"");
    final StringBuilder buf = new StringBuilder();
    while (resultSet.next()) {
        int n = resultSet.getMetaData().getColumnCount();
        for (int i = 1; i <= n; i++) {
            buf.append(i > 1 ? "; " : "").append(resultSet.getMetaData().getColumnLabel(i)).append("=").append(resultSet.getObject(i));
        }
        System.out.println(buf.toString());
        buf.setLength(0);
    }
    resultSet.close();
    statement.close();
    connection.close();
}
Also used : 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)

Example 28 with CalciteConnection

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection in project calcite by apache.

the class CsvTest method testCsvStream.

@Test(timeout = 10000)
public void testCsvStream() throws Exception {
    final File file = File.createTempFile("stream", "csv");
    final String model = "{\n" + "  version: '1.0',\n" + "  defaultSchema: 'STREAM',\n" + "  schemas: [\n" + "    {\n" + "      name: 'SS',\n" + "      tables: [\n" + "        {\n" + "          name: 'DEPTS',\n" + "          type: 'custom',\n" + "          factory: '" + CsvStreamTableFactory.class.getName() + "',\n" + "          stream: {\n" + "            stream: true\n" + "          },\n" + "          operand: {\n" + "            file: " + escapeString(file.getAbsolutePath()) + ",\n" + "            flavor: \"scannable\"\n" + "          }\n" + "        }\n" + "      ]\n" + "    }\n" + "  ]\n" + "}\n";
    final String[] strings = { "DEPTNO:int,NAME:string", "10,\"Sales\"", "20,\"Marketing\"", "30,\"Engineering\"" };
    try (final Connection connection = DriverManager.getConnection("jdbc:calcite:model=inline:" + model);
        final PrintWriter pw = Util.printWriter(file);
        final Worker<Void> worker = new Worker<>()) {
        final Thread thread = new Thread(worker);
        thread.start();
        // Add some rows so that the table can deduce its row type.
        final Iterator<String> lines = Arrays.asList(strings).iterator();
        // header
        pw.println(lines.next());
        pw.flush();
        // first row
        worker.queue.put(writeLine(pw, lines.next()));
        // second row
        worker.queue.put(writeLine(pw, lines.next()));
        final CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
        final String sql = "select stream * from \"SS\".\"DEPTS\"";
        final PreparedStatement statement = calciteConnection.prepareStatement(sql);
        final ResultSet resultSet = statement.executeQuery();
        int count = 0;
        try {
            while (resultSet.next()) {
                ++count;
                if (lines.hasNext()) {
                    worker.queue.put(sleep(10));
                    worker.queue.put(writeLine(pw, lines.next()));
                } else {
                    worker.queue.put(cancel(statement));
                }
            }
            fail("expected exception, got end of data");
        } catch (SQLException e) {
            assertThat(e.getMessage(), is("Statement canceled"));
        }
        assertThat(count, anyOf(is(strings.length - 2), is(strings.length - 1)));
        assertThat(worker.e, nullValue());
        assertThat(worker.v, nullValue());
    } finally {
        Util.discard(file.delete());
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) PreparedStatement(java.sql.PreparedStatement) ResultSet(java.sql.ResultSet) File(java.io.File) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 29 with CalciteConnection

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection in project calcite by apache.

the class ExampleFunctionTest method checkMazeTableFunction.

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));
}
Also used : AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ResultSet(java.sql.ResultSet) TableFunction(org.apache.calcite.schema.TableFunction) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection)

Example 30 with CalciteConnection

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection in project samza by apache.

the class SamzaSqlQueryParser method createPlanner.

private static Planner createPlanner() {
    Connection connection;
    SchemaPlus rootSchema;
    try {
        JavaTypeFactory typeFactory = new SamzaSqlJavaTypeFactoryImpl();
        SamzaSqlDriver driver = new SamzaSqlDriver(typeFactory);
        DriverManager.deregisterDriver(DriverManager.getDriver("jdbc:calcite:"));
        DriverManager.registerDriver(driver);
        connection = driver.connect("jdbc:calcite:", new Properties());
        CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
        rootSchema = calciteConnection.getRootSchema();
    } catch (SQLException e) {
        throw new SamzaException(e);
    }
    final List<RelTraitDef> traitDefs = new ArrayList<>();
    traitDefs.add(ConventionTraitDef.INSTANCE);
    traitDefs.add(RelCollationTraitDef.INSTANCE);
    FrameworkConfig frameworkConfig = Frameworks.newConfigBuilder().parserConfig(SqlParser.configBuilder().setLex(Lex.JAVA).build()).defaultSchema(rootSchema).operatorTable(SqlStdOperatorTable.instance()).traitDefs(traitDefs).context(Contexts.EMPTY_CONTEXT).costFactory(null).build();
    return Frameworks.getPlanner(frameworkConfig);
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ArrayList(java.util.ArrayList) SamzaSqlDriver(org.apache.samza.sql.interfaces.SamzaSqlDriver) Properties(java.util.Properties) SamzaException(org.apache.samza.SamzaException) RelTraitDef(org.apache.calcite.plan.RelTraitDef) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) FrameworkConfig(org.apache.calcite.tools.FrameworkConfig) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) SamzaSqlJavaTypeFactoryImpl(org.apache.samza.sql.interfaces.SamzaSqlJavaTypeFactoryImpl)

Aggregations

CalciteConnection (org.apache.calcite.jdbc.CalciteConnection)65 Test (org.junit.Test)52 Connection (java.sql.Connection)51 ResultSet (java.sql.ResultSet)42 SchemaPlus (org.apache.calcite.schema.SchemaPlus)42 Statement (java.sql.Statement)32 PreparedStatement (java.sql.PreparedStatement)24 AbstractSchema (org.apache.calcite.schema.impl.AbstractSchema)22 Properties (java.util.Properties)17 ReflectiveSchema (org.apache.calcite.adapter.java.ReflectiveSchema)17 SQLException (java.sql.SQLException)16 AvaticaConnection (org.apache.calcite.avatica.AvaticaConnection)12 CalciteConnection (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection)11 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)11 TableFunction (org.apache.calcite.schema.TableFunction)10 AvaticaStatement (org.apache.calcite.avatica.AvaticaStatement)9 org.hsqldb.jdbcDriver (org.hsqldb.jdbcDriver)5 AssertThat (org.apache.calcite.test.CalciteAssert.AssertThat)4 IOException (java.io.IOException)3 ResultSetMetaData (java.sql.ResultSetMetaData)3