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