Search in sources :

Example 1 with CalciteServerStatement

use of org.apache.calcite.server.CalciteServerStatement in project calcite by apache.

the class CalciteMetaImpl method fetch.

@Override
public Frame fetch(StatementHandle h, long offset, int fetchMaxRowCount) throws NoSuchStatementException {
    final CalciteConnectionImpl calciteConnection = getConnection();
    CalciteServerStatement stmt = calciteConnection.server.getStatement(h);
    final Signature signature = stmt.getSignature();
    final Iterator<Object> iterator;
    if (stmt.getResultSet() == null) {
        final Iterable<Object> iterable = _createIterable(h, signature, null, null);
        iterator = iterable.iterator();
        stmt.setResultSet(iterator);
    } else {
        iterator = stmt.getResultSet();
    }
    final List rows = MetaImpl.collect(signature.cursorFactory, LimitIterator.of(iterator, fetchMaxRowCount), new ArrayList<List<Object>>());
    boolean done = fetchMaxRowCount == 0 || rows.size() < fetchMaxRowCount;
    @SuppressWarnings("unchecked") List<Object> rows1 = (List<Object>) rows;
    return new Meta.Frame(offset, done, rows1);
}
Also used : CalciteServerStatement(org.apache.calcite.server.CalciteServerStatement) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList)

Example 2 with CalciteServerStatement

use of org.apache.calcite.server.CalciteServerStatement in project calcite by apache.

the class CalciteMetaImpl method execute.

@Override
public ExecuteResult execute(StatementHandle h, List<TypedValue> parameterValues, int maxRowsInFirstFrame) throws NoSuchStatementException {
    final CalciteConnectionImpl calciteConnection = getConnection();
    CalciteServerStatement stmt = calciteConnection.server.getStatement(h);
    final Signature signature = stmt.getSignature();
    MetaResultSet metaResultSet;
    if (signature.statementType.canUpdate()) {
        final Iterable<Object> iterable = _createIterable(h, signature, parameterValues, null);
        final Iterator<Object> iterator = iterable.iterator();
        stmt.setResultSet(iterator);
        metaResultSet = MetaResultSet.count(h.connectionId, h.id, ((Number) iterator.next()).intValue());
    } else {
        // Don't populate the first frame.
        // It's not worth saving a round-trip, since we're local.
        final Meta.Frame frame = new Meta.Frame(0, false, Collections.emptyList());
        metaResultSet = MetaResultSet.create(h.connectionId, h.id, false, signature, frame);
    }
    return new ExecuteResult(ImmutableList.of(metaResultSet));
}
Also used : Meta(org.apache.calcite.avatica.Meta) CalciteServerStatement(org.apache.calcite.server.CalciteServerStatement)

Example 3 with CalciteServerStatement

use of org.apache.calcite.server.CalciteServerStatement in project calcite by apache.

the class CalciteMetaImpl method prepare.

@Override
public StatementHandle prepare(ConnectionHandle ch, String sql, long maxRowCount) {
    final StatementHandle h = createStatement(ch);
    final CalciteConnectionImpl calciteConnection = getConnection();
    final CalciteServerStatement statement;
    try {
        statement = calciteConnection.server.getStatement(h);
    } catch (NoSuchStatementException e) {
        // Not possible. We just created a statement.
        throw new AssertionError("missing statement", e);
    }
    final Context context = statement.createPrepareContext();
    final CalcitePrepare.Query<Object> query = toQuery(context, sql);
    h.signature = calciteConnection.parseQuery(query, context, maxRowCount);
    statement.setSignature(h.signature);
    return h;
}
Also used : DataContext(org.apache.calcite.DataContext) Context(org.apache.calcite.jdbc.CalcitePrepare.Context) CalciteServerStatement(org.apache.calcite.server.CalciteServerStatement) NoSuchStatementException(org.apache.calcite.avatica.NoSuchStatementException)

Example 4 with CalciteServerStatement

use of org.apache.calcite.server.CalciteServerStatement in project calcite by apache.

the class TraitPropagationTest method run.

// Created so that we can control when the TraitDefs are defined (e.g.
// before the cluster is created).
private static RelNode run(PropAction action, RuleSet rules) throws Exception {
    FrameworkConfig config = Frameworks.newConfigBuilder().ruleSets(rules).build();
    final Properties info = new Properties();
    final Connection connection = DriverManager.getConnection("jdbc:calcite:", info);
    final CalciteServerStatement statement = connection.createStatement().unwrap(CalciteServerStatement.class);
    final CalcitePrepare.Context prepareContext = statement.createPrepareContext();
    final JavaTypeFactory typeFactory = prepareContext.getTypeFactory();
    CalciteCatalogReader catalogReader = new CalciteCatalogReader(prepareContext.getRootSchema(), prepareContext.getDefaultSchemaPath(), typeFactory, prepareContext.config());
    final RexBuilder rexBuilder = new RexBuilder(typeFactory);
    final RelOptPlanner planner = new VolcanoPlanner(config.getCostFactory(), config.getContext());
    // set up rules before we generate cluster
    planner.clearRelTraitDefs();
    planner.addRelTraitDef(RelCollationTraitDef.INSTANCE);
    planner.addRelTraitDef(ConventionTraitDef.INSTANCE);
    planner.clear();
    for (RelOptRule r : rules) {
        planner.addRule(r);
    }
    final RelOptCluster cluster = RelOptCluster.create(planner, rexBuilder);
    return action.apply(cluster, catalogReader, prepareContext.getRootSchema().plus());
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) Connection(java.sql.Connection) CalcitePrepare(org.apache.calcite.jdbc.CalcitePrepare) Properties(java.util.Properties) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) RelOptRule(org.apache.calcite.plan.RelOptRule) CalciteCatalogReader(org.apache.calcite.prepare.CalciteCatalogReader) CalciteServerStatement(org.apache.calcite.server.CalciteServerStatement) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) RexBuilder(org.apache.calcite.rex.RexBuilder) FrameworkConfig(org.apache.calcite.tools.FrameworkConfig)

Example 5 with CalciteServerStatement

use of org.apache.calcite.server.CalciteServerStatement 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)

Aggregations

CalciteServerStatement (org.apache.calcite.server.CalciteServerStatement)10 Connection (java.sql.Connection)3 ArrayList (java.util.ArrayList)3 NoSuchStatementException (org.apache.calcite.avatica.NoSuchStatementException)3 Properties (java.util.Properties)2 DataContext (org.apache.calcite.DataContext)2 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)2 Meta (org.apache.calcite.avatica.Meta)2 CalcitePrepare (org.apache.calcite.jdbc.CalcitePrepare)2 Context (org.apache.calcite.jdbc.CalcitePrepare.Context)2 ImmutableList (com.google.common.collect.ImmutableList)1 SQLException (java.sql.SQLException)1 List (java.util.List)1 CalciteConnection (org.apache.calcite.jdbc.CalciteConnection)1 RelOptCluster (org.apache.calcite.plan.RelOptCluster)1 RelOptPlanner (org.apache.calcite.plan.RelOptPlanner)1 RelOptRule (org.apache.calcite.plan.RelOptRule)1 CalciteCatalogReader (org.apache.calcite.prepare.CalciteCatalogReader)1 CalcitePrepareImpl (org.apache.calcite.prepare.CalcitePrepareImpl)1 RexBuilder (org.apache.calcite.rex.RexBuilder)1