use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection in project sidewinder by srotya.
the class SqlApi method initCalcite.
public void initCalcite() throws SQLException, ClassNotFoundException {
Class.forName("org.apache.calcite.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:calcite:");
CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
ModelHandler.create(calciteConnection.getRootSchema(), "now", Arrays.asList(""), NowFunction.class.getName(), "apply");
ModelHandler.create(calciteConnection.getRootSchema(), "milli", Arrays.asList(""), ToMilliseconds.class.getName(), "apply");
ModelHandler.create(calciteConnection.getRootSchema(), "ts", Arrays.asList(""), ToTimestamp.class.getName(), "eval");
ModelHandler.create(calciteConnection.getRootSchema(), "datediff", Arrays.asList(""), DateDiffFunction.class.getName(), "apply");
for (Function function : calciteConnection.getRootSchema().getFunctions("ts")) {
System.out.println(function.getParameters().get(0).getName());
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection in project calcite by apache.
the class ViewTableMacro method apply.
public TranslatableTable apply(List<Object> arguments) {
final CalciteConnection connection = MaterializedViewTable.MATERIALIZATION_CONNECTION;
CalcitePrepare.AnalyzeViewResult parsed = Schemas.analyzeView(connection, schema, schemaPath, viewSql, viewPath, modifiable != null && modifiable);
final List<String> schemaPath1 = schemaPath != null ? schemaPath : schema.path(null);
if ((modifiable == null || modifiable) && parsed.modifiable && parsed.table != null) {
return modifiableViewTable(parsed, viewSql, schemaPath1, viewPath, schema);
} else {
return viewTable(parsed, viewSql, schemaPath1, viewPath);
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection in project calcite by apache.
the class JdbcTest method testSchemaCaching.
@Test
public void testSchemaCaching() throws Exception {
final Connection connection = CalciteAssert.that(CalciteAssert.Config.JDBC_FOODMART).connect();
final CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
final SchemaPlus rootSchema = calciteConnection.getRootSchema();
// create schema "/a"
final Map<String, Schema> aSubSchemaMap = new HashMap<>();
final SchemaPlus aSchema = rootSchema.add("a", new AbstractSchema() {
@Override
protected Map<String, Schema> getSubSchemaMap() {
return aSubSchemaMap;
}
});
aSchema.setCacheEnabled(true);
assertThat(aSchema.getSubSchemaNames().size(), is(0));
// first call, to populate the cache
assertThat(aSchema.getSubSchemaNames().size(), is(0));
// create schema "/a/b1". Appears only when we disable caching.
aSubSchemaMap.put("b1", new AbstractSchema());
assertThat(aSchema.getSubSchemaNames().size(), is(0));
assertThat(aSchema.getSubSchema("b1"), nullValue());
aSchema.setCacheEnabled(false);
assertThat(aSchema.getSubSchemaNames().size(), is(1));
assertThat(aSchema.getSubSchema("b1"), notNullValue());
// create schema "/a/b2". Appears immediately, because caching is disabled.
aSubSchemaMap.put("b2", new AbstractSchema());
assertThat(aSchema.getSubSchemaNames().size(), is(2));
// an explicit sub-schema appears immediately, even if caching is enabled
aSchema.setCacheEnabled(true);
assertThat(aSchema.getSubSchemaNames().size(), is(2));
// explicit
aSchema.add("b3", new AbstractSchema());
// implicit
aSubSchemaMap.put("b4", new AbstractSchema());
assertThat(aSchema.getSubSchemaNames().size(), is(3));
aSchema.setCacheEnabled(false);
assertThat(aSchema.getSubSchemaNames().size(), is(4));
for (String name : aSchema.getSubSchemaNames()) {
assertThat(aSchema.getSubSchema(name), notNullValue());
}
// create schema "/a2"
final Map<String, Schema> a2SubSchemaMap = new HashMap<>();
final SchemaPlus a2Schema = rootSchema.add("a", new AbstractSchema() {
@Override
protected Map<String, Schema> getSubSchemaMap() {
return a2SubSchemaMap;
}
});
a2Schema.setCacheEnabled(true);
assertThat(a2Schema.getSubSchemaNames().size(), is(0));
// create schema "/a2/b3". Change not visible since caching is enabled.
a2SubSchemaMap.put("b3", new AbstractSchema());
assertThat(a2Schema.getSubSchemaNames().size(), is(0));
Thread.sleep(1);
assertThat(a2Schema.getSubSchemaNames().size(), is(0));
// Change visible after we turn off caching.
a2Schema.setCacheEnabled(false);
assertThat(a2Schema.getSubSchemaNames().size(), is(1));
a2SubSchemaMap.put("b4", new AbstractSchema());
assertThat(a2Schema.getSubSchemaNames().size(), is(2));
for (String name : aSchema.getSubSchemaNames()) {
assertThat(aSchema.getSubSchema(name), notNullValue());
}
// add tables and retrieve with various case sensitivities
final TableInRootSchemaTest.SimpleTable table = new TableInRootSchemaTest.SimpleTable();
a2Schema.add("table1", table);
a2Schema.add("TABLE1", table);
a2Schema.add("tabLe1", table);
a2Schema.add("tabLe2", table);
assertThat(a2Schema.getTableNames().size(), equalTo(4));
final CalciteSchema a2CalciteSchema = CalciteSchema.from(a2Schema);
assertThat(a2CalciteSchema.getTable("table1", true), notNullValue());
assertThat(a2CalciteSchema.getTable("table1", false), notNullValue());
assertThat(a2CalciteSchema.getTable("taBle1", true), nullValue());
assertThat(a2CalciteSchema.getTable("taBle1", false), notNullValue());
final TableMacro function = ViewTable.viewMacro(a2Schema, "values 1", null, null, null);
Util.discard(function);
connection.close();
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection in project calcite by apache.
the class JdbcTest method testVersion.
/**
* Make sure that the properties look sane.
*/
@Test
public void testVersion() throws ClassNotFoundException, SQLException {
Connection connection = DriverManager.getConnection("jdbc:calcite:");
CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
final DatabaseMetaData metaData = calciteConnection.getMetaData();
assertEquals("Calcite JDBC Driver", metaData.getDriverName());
final String driverVersion = metaData.getDriverVersion();
final int driverMajor = metaData.getDriverMajorVersion();
final int driverMinor = metaData.getDriverMinorVersion();
assertEquals(1, driverMajor);
assertTrue(driverMinor >= 0 && driverMinor < 20);
assertEquals("Calcite", metaData.getDatabaseProductName());
final String databaseVersion = metaData.getDatabaseProductVersion();
final int databaseMajor = metaData.getDatabaseMajorVersion();
assertEquals(driverMajor, databaseMajor);
final int databaseMinor = metaData.getDatabaseMinorVersion();
assertEquals(driverMinor, databaseMinor);
// Check how version is composed of major and minor version. Note that
// version is stored in pom.xml; major and minor version are
// stored in org-apache-calcite-jdbc.properties, but derived from
// version.major and version.minor in pom.xml.
//
// We are more permissive for snapshots.
// For instance, we allow 1.4.0-SNAPSHOT to match {major=1, minor=3}.
// Previously, this test would break the first build after a release.
assertTrue(driverVersion.startsWith(driverMajor + "."));
assertTrue(driverVersion.split("\\.").length >= 2);
assertTrue(driverVersion.equals(mm(driverMajor, driverMinor)) || driverVersion.startsWith(mm(driverMajor, driverMinor) + ".") || driverVersion.startsWith(mm(driverMajor, driverMinor) + "-") || driverVersion.endsWith("-SNAPSHOT") && driverVersion.startsWith(mm(driverMajor, driverMinor + 1)));
assertTrue(databaseVersion.startsWith("1."));
assertTrue(databaseVersion.split("\\.").length >= 2);
assertTrue(databaseVersion.equals(mm(databaseMajor, databaseMinor)) || databaseVersion.startsWith(mm(databaseMajor, databaseMinor) + ".") || databaseVersion.startsWith(mm(databaseMajor, databaseMinor) + "-") || databaseVersion.endsWith("-SNAPSHOT") && databaseVersion.startsWith(mm(driverMajor, driverMinor + 1)));
connection.close();
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection in project calcite by apache.
the class JdbcTest method testAutomaticTemporaryTable.
/**
* Tests saving query results into temporary tables, per
* {@link org.apache.calcite.avatica.Handler.ResultSink}.
*/
@Test
public void testAutomaticTemporaryTable() throws Exception {
final List<Object> objects = new ArrayList<>();
CalciteAssert.that().with(new CalciteAssert.ConnectionFactory() {
public CalciteConnection createConnection() throws SQLException {
CalciteConnection connection = (CalciteConnection) new AutoTempDriver(objects).connect("jdbc:calcite:", new Properties());
final SchemaPlus rootSchema = connection.getRootSchema();
rootSchema.add("hr", new ReflectiveSchema(new HrSchema()));
connection.setSchema("hr");
return connection;
}
}).doWithConnection(new Function<CalciteConnection, Object>() {
public Object apply(CalciteConnection a0) {
try {
a0.createStatement().executeQuery("select * from \"hr\".\"emps\" " + "where \"deptno\" = 10");
assertEquals(1, objects.size());
return null;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
});
}
Aggregations