Search in sources :

Example 31 with CalciteConnection

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

the class JdbcDriverTest method testInternalConnect_bounded_limit.

@Test
public void testInternalConnect_bounded_limit() throws Exception {
    ReadOnlyTableProvider tableProvider = new ReadOnlyTableProvider("test", ImmutableMap.of("test", TestBoundedTable.of(Schema.FieldType.INT32, "id", Schema.FieldType.STRING, "name").addRows(1, "first").addRows(1, "second first").addRows(2, "second")));
    CalciteConnection connection = JdbcDriver.connect(tableProvider, PipelineOptionsFactory.create());
    Statement statement = connection.createStatement();
    ResultSet resultSet1 = statement.executeQuery("SELECT * FROM test LIMIT 5");
    assertTrue(resultSet1.next());
    assertTrue(resultSet1.next());
    assertTrue(resultSet1.next());
    assertFalse(resultSet1.next());
    assertFalse(resultSet1.next());
    ResultSet resultSet2 = statement.executeQuery("SELECT * FROM test LIMIT 1");
    assertTrue(resultSet2.next());
    assertFalse(resultSet2.next());
    ResultSet resultSet3 = statement.executeQuery("SELECT * FROM test LIMIT 2");
    assertTrue(resultSet3.next());
    assertTrue(resultSet3.next());
    assertFalse(resultSet3.next());
    ResultSet resultSet4 = statement.executeQuery("SELECT * FROM test LIMIT 3");
    assertTrue(resultSet4.next());
    assertTrue(resultSet4.next());
    assertTrue(resultSet4.next());
    assertFalse(resultSet4.next());
}
Also used : Statement(java.sql.Statement) ReadOnlyTableProvider(org.apache.beam.sdk.extensions.sql.meta.provider.ReadOnlyTableProvider) ResultSet(java.sql.ResultSet) CalciteConnection(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection) Test(org.junit.Test)

Example 32 with CalciteConnection

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

the class JdbcDriverTest method testInternalConnect_boundedTable.

@Test
public void testInternalConnect_boundedTable() throws Exception {
    CalciteConnection connection = JdbcDriver.connect(BOUNDED_TABLE, PipelineOptionsFactory.create());
    Statement statement = connection.createStatement();
    ResultSet resultSet = statement.executeQuery("SELECT * FROM test");
    assertTrue(resultSet.next());
    assertEquals(1, resultSet.getInt("id"));
    assertEquals("first", resultSet.getString("name"));
    assertFalse(resultSet.next());
}
Also used : Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) CalciteConnection(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection) Test(org.junit.Test)

Example 33 with CalciteConnection

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

the class SQLExecEnvironment method withModel.

/**
 * Use given model file to initialize {@link SQLExecEnvironment}.
 * The model file contains definitions of endpoints and data formats.
 * Example of file format is like following:
 * <pre>
 *   {
 *     version: '1.0',
 *     defaultSchema: 'APEX',
 *     schemas: [{
 *       name: 'APEX',
 *       tables: [
 *         {
 *            name: 'ORDERS',
 *            type: 'custom',
 *            factory: 'org.apache.apex.malhar.sql.schema.ApexSQLTableFactory',
 *            stream: {
 *              stream: true
 *            },
 *            operand: {
 *              endpoint: 'file',
 *              messageFormat: 'csv',
 *              endpointOperands: {
 *                directory: '/tmp/input'
 *              },
 *              messageFormatOperands: {
 *                schema: '{"separator":",","quoteChar":"\\"","fields":[{"name":"RowTime","type":"Date","constraints":{"format":"dd/MM/yyyy hh:mm:ss"}},{"name":"id","type":"Integer"},{"name":"Product","type":"String"},{"name":"units","type":"Integer"}]}'
 *            }
 *            }
 *         }
 *       ]
 *     }]
 *   }
 * </pre>
 *
 * @param model String content of model file.
 * @return Returns this {@link SQLExecEnvironment}
 */
public SQLExecEnvironment withModel(String model) {
    if (model == null) {
        return this;
    }
    Properties p = new Properties();
    p.put("model", "inline:" + model);
    try (Connection connection = DriverManager.getConnection("jdbc:calcite:", p)) {
        CalciteConnection conn = connection.unwrap(CalciteConnection.class);
        this.schema = conn.getRootSchema().getSubSchema(connection.getSchema());
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
    return this;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Properties(java.util.Properties) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection)

Example 34 with CalciteConnection

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

the class CalciteSolrDriver method connect.

@Override
public Connection connect(String url, Properties info) throws SQLException {
    if (!this.acceptsURL(url)) {
        return null;
    }
    Connection connection = super.connect(url, info);
    CalciteConnection calciteConnection = (CalciteConnection) connection;
    final SchemaPlus rootSchema = calciteConnection.getRootSchema();
    String schemaName = info.getProperty("zk");
    if (schemaName == null) {
        throw new SQLException("zk must be set");
    }
    rootSchema.add(schemaName, new SolrSchema(info));
    // Set the default schema
    calciteConnection.setSchema(schemaName);
    return connection;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) SchemaPlus(org.apache.calcite.schema.SchemaPlus) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection)

Example 35 with CalciteConnection

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

the class QueryRecord method queryWithCache.

protected QueryResult queryWithCache(final ProcessSession session, final FlowFile flowFile, final String sql, final ProcessContext context, final RecordReaderFactory recordParserFactory) throws SQLException {
    final Supplier<CalciteConnection> connectionSupplier = () -> {
        final Properties properties = new Properties();
        properties.put(CalciteConnectionProperty.LEX.camelName(), Lex.MYSQL_ANSI.name());
        try {
            final Connection connection = DriverManager.getConnection("jdbc:calcite:", properties);
            final CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
            return calciteConnection;
        } catch (final Exception e) {
            throw new ProcessException(e);
        }
    };
    final CachedStatement cachedStatement = getStatement(sql, connectionSupplier, session, flowFile, recordParserFactory);
    final PreparedStatement stmt = cachedStatement.getStatement();
    final FlowFileTable<?, ?> table = cachedStatement.getTable();
    table.setFlowFile(session, flowFile);
    final ResultSet rs = stmt.executeQuery();
    return new QueryResult() {

        @Override
        public void close() throws IOException {
            table.close();
            final BlockingQueue<CachedStatement> statementQueue = statementQueues.get(sql);
            if (statementQueue == null || !statementQueue.offer(cachedStatement)) {
                try {
                    cachedStatement.getConnection().close();
                } catch (SQLException e) {
                    throw new IOException("Failed to close statement", e);
                }
            }
        }

        @Override
        public ResultSet getResultSet() {
            return rs;
        }

        @Override
        public int getRecordsRead() {
            return table.getRecordsRead();
        }
    };
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) Properties(java.util.Properties) SchemaNotFoundException(org.apache.nifi.schema.access.SchemaNotFoundException) ProcessException(org.apache.nifi.processor.exception.ProcessException) SQLException(java.sql.SQLException) IOException(java.io.IOException) ProcessException(org.apache.nifi.processor.exception.ProcessException) ResultSet(java.sql.ResultSet) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection)

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