Search in sources :

Example 6 with Enumerable

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.Enumerable in project calcite by apache.

the class DruidConnectionImpl method enumerable.

/**
 * Executes a request and returns the resulting rows as an
 * {@link Enumerable}, running the parser in a thread provided by
 * {@code service}.
 */
public Enumerable<Row> enumerable(final QueryType queryType, final String request, final List<String> fieldNames, final ExecutorService service) throws IOException {
    return new AbstractEnumerable<Row>() {

        public Enumerator<Row> enumerator() {
            final BlockingQueueEnumerator<Row> enumerator = new BlockingQueueEnumerator<>();
            final RunnableQueueSink sink = new RunnableQueueSink() {

                public void send(Row row) throws InterruptedException {
                    enumerator.queue.put(row);
                }

                public void end() {
                    enumerator.done.set(true);
                }

                @SuppressWarnings("deprecation")
                public void setSourceEnumerable(Enumerable<Row> enumerable) throws InterruptedException {
                    for (Row row : enumerable) {
                        send(row);
                    }
                    end();
                }

                public void run() {
                    try {
                        final Page page = new Page();
                        final List<ColumnMetaData.Rep> fieldTypes = Collections.nCopies(fieldNames.size(), null);
                        request(queryType, request, this, fieldNames, fieldTypes, page);
                        enumerator.done.set(true);
                    } catch (Throwable e) {
                        enumerator.throwableHolder.set(e);
                        enumerator.done.set(true);
                    }
                }
            };
            service.execute(sink);
            return enumerator;
        }
    };
}
Also used : AbstractEnumerable(org.apache.calcite.linq4j.AbstractEnumerable) Enumerable(org.apache.calcite.linq4j.Enumerable) AbstractEnumerable(org.apache.calcite.linq4j.AbstractEnumerable) Row(org.apache.calcite.interpreter.Row)

Example 7 with Enumerable

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.Enumerable in project calcite by apache.

the class ScannableTableTest method testPrepared2.

/**
 * Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-1031">[CALCITE-1031]
 * In prepared statement, CsvScannableTable.scan is called twice</a>.
 */
@Test
public void testPrepared2() throws SQLException {
    final Properties properties = new Properties();
    properties.setProperty("caseSensitive", "true");
    try (final Connection connection = DriverManager.getConnection("jdbc:calcite:", properties)) {
        final CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
        final AtomicInteger scanCount = new AtomicInteger();
        final AtomicInteger enumerateCount = new AtomicInteger();
        final Schema schema = new AbstractSchema() {

            @Override
            protected Map<String, Table> getTableMap() {
                return ImmutableMap.<String, Table>of("TENS", new SimpleTable() {

                    private Enumerable<Object[]> superScan(DataContext root) {
                        return super.scan(root);
                    }

                    @Override
                    public Enumerable<Object[]> scan(final DataContext root) {
                        scanCount.incrementAndGet();
                        return new AbstractEnumerable<Object[]>() {

                            public Enumerator<Object[]> enumerator() {
                                enumerateCount.incrementAndGet();
                                return superScan(root).enumerator();
                            }
                        };
                    }
                });
            }
        };
        calciteConnection.getRootSchema().add("TEST", schema);
        final String sql = "select * from \"TEST\".\"TENS\" where \"i\" < ?";
        final PreparedStatement statement = calciteConnection.prepareStatement(sql);
        assertThat(scanCount.get(), is(0));
        assertThat(enumerateCount.get(), is(0));
        // First execute
        statement.setInt(1, 20);
        assertThat(scanCount.get(), is(0));
        ResultSet resultSet = statement.executeQuery();
        assertThat(scanCount.get(), is(1));
        assertThat(enumerateCount.get(), is(1));
        assertThat(resultSet, Matchers.returnsUnordered("i=0", "i=10"));
        assertThat(scanCount.get(), is(1));
        assertThat(enumerateCount.get(), is(1));
        // Second execute
        resultSet = statement.executeQuery();
        assertThat(scanCount.get(), is(2));
        assertThat(resultSet, Matchers.returnsUnordered("i=0", "i=10"));
        assertThat(scanCount.get(), is(2));
        // Third execute
        statement.setInt(1, 30);
        resultSet = statement.executeQuery();
        assertThat(scanCount.get(), is(3));
        assertThat(resultSet, Matchers.returnsUnordered("i=0", "i=10", "i=20"));
        assertThat(scanCount.get(), is(3));
    }
}
Also used : ProjectableFilterableTable(org.apache.calcite.schema.ProjectableFilterableTable) ScannableTable(org.apache.calcite.schema.ScannableTable) Table(org.apache.calcite.schema.Table) FilterableTable(org.apache.calcite.schema.FilterableTable) AbstractTable(org.apache.calcite.schema.impl.AbstractTable) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) Schema(org.apache.calcite.schema.Schema) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) PreparedStatement(java.sql.PreparedStatement) Properties(java.util.Properties) DataContext(org.apache.calcite.DataContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) Enumerator(org.apache.calcite.linq4j.Enumerator) ResultSet(java.sql.ResultSet) Enumerable(org.apache.calcite.linq4j.Enumerable) AbstractEnumerable(org.apache.calcite.linq4j.AbstractEnumerable) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Test(org.junit.Test)

Aggregations

Enumerable (org.apache.calcite.linq4j.Enumerable)7 DataContext (org.apache.calcite.DataContext)4 Connection (java.sql.Connection)3 ArrayList (java.util.ArrayList)3 CalciteConnection (org.apache.calcite.jdbc.CalciteConnection)3 Test (org.junit.Test)3 BindableRel (org.apache.calcite.interpreter.BindableRel)2 Bindables (org.apache.calcite.interpreter.Bindables)2 AbstractEnumerable (org.apache.calcite.linq4j.AbstractEnumerable)2 Enumerator (org.apache.calcite.linq4j.Enumerator)2 QueryProvider (org.apache.calcite.linq4j.QueryProvider)2 Function1 (org.apache.calcite.linq4j.function.Function1)2 ParameterExpression (org.apache.calcite.linq4j.tree.ParameterExpression)2 RexBuilder (org.apache.calcite.rex.RexBuilder)2 RexNode (org.apache.calcite.rex.RexNode)2 Employee (org.apache.calcite.test.JdbcTest.Employee)2 Supplier (com.google.common.base.Supplier)1 ImmutableList (com.google.common.collect.ImmutableList)1 Sequence (io.druid.java.util.common.guava.Sequence)1 PreparedStatement (java.sql.PreparedStatement)1