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);
}
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));
}
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;
}
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());
}
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);
}
}
Aggregations