use of org.apache.calcite.util.JsonBuilder in project calcite by apache.
the class JdbcTest method testSelfReferentialView2.
@Test
public void testSelfReferentialView2() throws Exception {
final String model = "{\n" + " version: '1.0',\n" + " defaultSchema: 'adhoc',\n" + " schemas: [ {\n" + " name: 'adhoc',\n" + " tables: [ {\n" + " name: 'A',\n" + " type: 'view',\n" + " sql: " + new JsonBuilder().toJsonString("select * from B") + "\n" + " }, {\n" + " name: 'B',\n" + " type: 'view',\n" + " sql: " + new JsonBuilder().toJsonString("select * from C") + "\n" + " }, {\n" + " name: 'C',\n" + " type: 'view',\n" + " sql: " + new JsonBuilder().toJsonString("select * from D, B") + "\n" + " }, {\n" + " name: 'D',\n" + " type: 'view',\n" + " sql: " + new JsonBuilder().toJsonString("select * from (values (1, 'a')) as t(x, y)") + "\n" + " } ]\n" + " } ]\n" + "}";
final CalciteAssert.AssertThat with = CalciteAssert.model(model);
//
// +-----+
// V |
// A --> B --> C --> D
//
// A is not in a cycle, but depends on cyclic views
// B is cyclic
// C is cyclic
// D is not cyclic
with.query("select x from \"adhoc\".a").throws_("Cannot resolve 'adhoc.A'; it references view 'adhoc.B', " + "whose definition is cyclic");
with.query("select x from \"adhoc\".b").throws_("Cannot resolve 'adhoc.B'; it references view 'adhoc.B', " + "whose definition is cyclic");
// as previous, but implicit schema
with.query("select x from b").throws_("Cannot resolve 'B'; it references view 'adhoc.B', " + "whose definition is cyclic");
with.query("select x from \"adhoc\".c").throws_("Cannot resolve 'adhoc.C'; it references view 'adhoc.C', " + "whose definition is cyclic");
with.query("select x from \"adhoc\".d").returns("X=1\n");
with.query("select x from \"adhoc\".d except select x from \"adhoc\".a").throws_("Cannot resolve 'adhoc.A'; it references view 'adhoc.B', " + "whose definition is cyclic");
}
use of org.apache.calcite.util.JsonBuilder in project calcite by apache.
the class MaterializationTest method testFilterQueryOnProjectView8.
/**
* Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-988">[CALCITE-988]
* FilterToProjectUnifyRule.invert(MutableRel, MutableRel, MutableProject)
* works incorrectly</a>.
*/
@Test
public void testFilterQueryOnProjectView8() {
try (final TryThreadLocal.Memo ignored = Prepare.THREAD_TRIM.push(true)) {
MaterializationService.setThreadLocal();
final String m = "select \"salary\", \"commission\",\n" + "\"deptno\", \"empid\", \"name\" from \"emps\"";
final String v = "select * from \"emps\" where \"name\" is null";
final String q = "select * from V where \"commission\" is null";
final JsonBuilder builder = new JsonBuilder();
final String model = "{\n" + " version: '1.0',\n" + " defaultSchema: 'hr',\n" + " schemas: [\n" + " {\n" + " materializations: [\n" + " {\n" + " table: 'm0',\n" + " view: 'm0v',\n" + " sql: " + builder.toJsonString(m) + " }\n" + " ],\n" + " tables: [\n" + " {\n" + " name: 'V',\n" + " type: 'view',\n" + " sql: " + builder.toJsonString(v) + "\n" + " }\n" + " ],\n" + " type: 'custom',\n" + " name: 'hr',\n" + " factory: 'org.apache.calcite.adapter.java.ReflectiveSchema$Factory',\n" + " operand: {\n" + " class: 'org.apache.calcite.test.JdbcTest$HrSchema'\n" + " }\n" + " }\n" + " ]\n" + "}\n";
CalciteAssert.that().withModel(model).query(q).enableMaterializations(true).explainMatches("", CONTAINS_M0).sameResultWithMaterializationsDisabled();
}
}
use of org.apache.calcite.util.JsonBuilder in project calcite by apache.
the class Driver method createHandler.
@Override
protected Handler createHandler() {
return new HandlerImpl() {
@Override
public void onConnectionInit(AvaticaConnection connection_) throws SQLException {
final CalciteConnectionImpl connection = (CalciteConnectionImpl) connection_;
super.onConnectionInit(connection);
final String model = model(connection);
if (model != null) {
try {
new ModelHandler(connection, model);
} catch (IOException e) {
throw new SQLException(e);
}
}
connection.init();
}
String model(CalciteConnectionImpl connection) {
String model = connection.config().model();
if (model != null) {
return model;
}
SchemaFactory schemaFactory = connection.config().schemaFactory(SchemaFactory.class, null);
final Properties info = connection.getProperties();
final String schemaName = Util.first(connection.config().schema(), "adhoc");
if (schemaFactory == null) {
final JsonSchema.Type schemaType = connection.config().schemaType();
if (schemaType != null) {
switch(schemaType) {
case JDBC:
schemaFactory = JdbcSchema.Factory.INSTANCE;
break;
case MAP:
schemaFactory = AbstractSchema.Factory.INSTANCE;
break;
}
}
}
if (schemaFactory != null) {
final JsonBuilder json = new JsonBuilder();
final Map<String, Object> root = json.map();
root.put("version", "1.0");
root.put("defaultSchema", schemaName);
final List<Object> schemaList = json.list();
root.put("schemas", schemaList);
final Map<String, Object> schema = json.map();
schemaList.add(schema);
schema.put("type", "custom");
schema.put("name", schemaName);
schema.put("factory", schemaFactory.getClass().getName());
final Map<String, Object> operandMap = json.map();
schema.put("operand", operandMap);
for (Map.Entry<String, String> entry : Util.toMap(info).entrySet()) {
if (entry.getKey().startsWith("schema.")) {
operandMap.put(entry.getKey().substring("schema.".length()), entry.getValue());
}
}
return "inline:" + json.toJsonString(root);
}
return null;
}
};
}
use of org.apache.calcite.util.JsonBuilder in project calcite by apache.
the class MaterializationTest method testViewSchemaPath.
@Test
public void testViewSchemaPath() {
try (final TryThreadLocal.Memo ignored = Prepare.THREAD_TRIM.push(true)) {
MaterializationService.setThreadLocal();
final String m = "select empno, deptno from emp";
final String q = "select deptno from scott.emp";
final List<String> path = ImmutableList.of("SCOTT");
final JsonBuilder builder = new JsonBuilder();
final String model = "{\n" + " version: '1.0',\n" + " defaultSchema: 'hr',\n" + " schemas: [\n" + JdbcTest.SCOTT_SCHEMA + " ,\n" + " {\n" + " materializations: [\n" + " {\n" + " table: 'm0',\n" + " view: 'm0v',\n" + " sql: " + builder.toJsonString(m) + ",\n" + " viewSchemaPath: " + builder.toJsonString(path) + " }\n" + " ],\n" + " type: 'custom',\n" + " name: 'hr',\n" + " factory: 'org.apache.calcite.adapter.java.ReflectiveSchema$Factory',\n" + " operand: {\n" + " class: 'org.apache.calcite.test.JdbcTest$HrSchema'\n" + " }\n" + " }\n" + " ]\n" + "}\n";
CalciteAssert.that().withModel(model).query(q).enableMaterializations(true).explainMatches("", CONTAINS_M0).sameResultWithMaterializationsDisabled();
}
}
Aggregations