Refine search
Statement statement = null; try { connection = DriverManager.getConnection("jdbc:calcite:", properties); final CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class); final SchemaPlus rootSchema = calciteConnection.getRootSchema(); rootSchema.add("FLOWFILE", flowFileTable); rootSchema.setCacheEnabled(false); statement = connection.createStatement(); resultSet = statement.executeQuery(sql); } catch (final Throwable t) { flowFileTable.close();
@Override public Iterable<Object[]> apply(final String schemaName) final SchemaPlus subSchema = rootSchema.getSubSchema(schemaName);
private Set<String> getAuthorizedTableNamesFromSubSchema( final SchemaPlus subSchema, final AuthenticationResult authenticationResult ) { if (DruidSchema.NAME.equals(subSchema.getName())) { // The "druid" schema's tables represent Druid datasources which require authorization return ImmutableSet.copyOf( AuthorizationUtils.filterAuthorizedResources( authenticationResult, subSchema.getTableNames(), DRUID_TABLE_RA_GENERATOR, authorizerMapper ) ); } else { // for non "druid" schema, we don't filter anything return subSchema.getTableNames(); } }
@Override public Object[] apply(final String schemaName) { final SchemaPlus subSchema = rootSchema.getSubSchema(schemaName); return new Object[]{ CATALOG_NAME, // CATALOG_NAME subSchema.getName(), // SCHEMA_NAME null, // SCHEMA_OWNER null, // DEFAULT_CHARACTER_SET_CATALOG null, // DEFAULT_CHARACTER_SET_SCHEMA null, // DEFAULT_CHARACTER_SET_NAME null // SQL_PATH }; } }
private CachedStatement buildCachedStatement(final String sql, final Supplier<CalciteConnection> connectionSupplier, final ProcessSession session, final FlowFile flowFile, final RecordReaderFactory recordReaderFactory) throws SQLException { final CalciteConnection connection = connectionSupplier.get(); final SchemaPlus rootSchema = connection.getRootSchema(); final FlowFileTable<?, ?> flowFileTable = new FlowFileTable<>(session, flowFile, recordReaderFactory, getLogger()); rootSchema.add("FLOWFILE", flowFileTable); rootSchema.setCacheEnabled(false); final PreparedStatement stmt = connection.prepareStatement(sql); return new CachedStatement(stmt, flowFileTable, connection); }
/** * Tests a table function with literal arguments. */ @Test public void testTableFunction() throws SQLException { try (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(Smalls.GENERATE_STRINGS_METHOD); schema.add("GenerateStrings", table); final String sql = "select *\n" + "from table(\"s\".\"GenerateStrings\"(5)) as t(n, c)\n" + "where char_length(c) > 3"; ResultSet resultSet = connection.createStatement().executeQuery(sql); assertThat(CalciteAssert.toString(resultSet), equalTo("N=4; C=abcd\n")); } }
/** * Tests a table function that implements {@link ScannableTable} and returns * a single column. */ @Test public void testScannableTableFunction() 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(Smalls.MAZE_METHOD); schema.add("Maze", table); final String sql = "select *\n" + "from table(\"s\".\"Maze\"(5, 3, 1))"; ResultSet resultSet = connection.createStatement().executeQuery(sql); final String result = "S=abcde\n" + "S=xyz\n" + "S=generate(w=5, h=3, s=1)\n"; assertThat(CalciteAssert.toString(resultSet), is(result)); }
/** * The example in the README. */ @Test public void testReadme() throws ClassNotFoundException, SQLException { Properties info = new Properties(); info.setProperty("lex", "JAVA"); Connection connection = DriverManager.getConnection("jdbc:calcite:", info); CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class); final SchemaPlus rootSchema = calciteConnection.getRootSchema(); rootSchema.add("hr", new ReflectiveSchema(new HrSchema())); Statement statement = calciteConnection.createStatement(); ResultSet resultSet = statement.executeQuery("select d.deptno, min(e.empid)\n" + "from hr.emps as e\n" + "join hr.depts as d\n" + " on e.deptno = d.deptno\n" + "group by d.deptno\n" + "having count(*) > 1"); final String s = CalciteAssert.toString(resultSet); assertThat(s, notNullValue()); resultSet.close(); statement.close(); connection.close(); }
@Test public void testCloneSchema() throws ClassNotFoundException, SQLException { final Connection connection = CalciteAssert.that(CalciteAssert.Config.JDBC_FOODMART).connect(); final CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class); final SchemaPlus rootSchema = calciteConnection.getRootSchema(); final SchemaPlus foodmart = rootSchema.getSubSchema("foodmart"); rootSchema.add("foodmart2", new CloneSchema(foodmart)); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery( "select count(*) from \"foodmart2\".\"time_by_day\""); assertTrue(resultSet.next()); assertEquals(730, resultSet.getInt(1)); resultSet.close(); connection.close(); }
Connection connection = DriverManager.getConnection("jdbc:calcite:"); CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class); calciteConnection.getRootSchema().add("SAMPLE", new SimpleTable()); Statement statement = calciteConnection.createStatement(); ResultSet resultSet = statement.executeQuery("select A, SUM(B) from SAMPLE group by A"); "A=foo; EXPR$1=8", "A=bar; EXPR$1=4"), equalTo(CalciteAssert.toSet(resultSet))); assertThat(resultSetMetaData.getColumnName(1), equalTo("A")); assertThat(resultSetMetaData.getTableName(1), equalTo("SAMPLE")); assertThat(resultSetMetaData.getSchemaName(1), nullValue()); assertThat(resultSetMetaData.getColumnClassName(1), equalTo("java.lang.Integer")); resultSet.close(); statement.close(); connection.close();
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)); assertThat(aSchema.getSubSchemaNames().size(), is(0)); 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()); assertThat(aSchema.getSubSchemaNames().size(), is(2)); aSchema.setCacheEnabled(true); assertThat(aSchema.getSubSchemaNames().size(), is(2)); aSchema.add("b3", new AbstractSchema()); // explicit aSubSchemaMap.put("b4", new AbstractSchema()); // implicit 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()); final SchemaPlus a2Schema = rootSchema.add("a", new AbstractSchema() { @Override protected Map<String, Schema> getSubSchemaMap() {
private Connection setupConnectionWithNestedAnyTypeTable() throws SQLException { Connection connection = DriverManager.getConnection("jdbc:calcite:"); CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class); SchemaPlus rootSchema = calciteConnection.getRootSchema(); SchemaPlus schema = rootSchema.add("s", new AbstractSchema()); schema.add("nested", new NestedCollectionWithAnyTypeTable()); return connection; }
/** Test case for * <a href="https://issues.apache.org/jira/browse/CALCITE-281">[CALCITE-1919] * NPE when target in ReflectiveSchema belongs to the unnamed package</a>. */ @Test public void testReflectiveSchemaInUnnamedPackage() throws Exception { final Driver driver = new Driver(); try (CalciteConnection connection = (CalciteConnection) driver.connect("jdbc:calcite:", new Properties())) { SchemaPlus rootSchema = connection.getRootSchema(); final Class<?> c = Class.forName("RootHr"); final Object o = c.getDeclaredConstructor().newInstance(); rootSchema.add("hr", new ReflectiveSchema(o)); connection.setSchema("hr"); final Statement statement = connection.createStatement(); final String sql = "select * from \"emps\""; final ResultSet resultSet = statement.executeQuery(sql); final String expected = "empid=100; name=Bill\n" + "empid=200; name=Eric\n" + "empid=150; name=Sebastian\n"; assertThat(CalciteAssert.toString(resultSet), is(expected)); } }
@Test public void testExplicitImplicitSchemaSameName() throws Exception { final SchemaPlus rootSchema = CalciteSchema.createRootSchema(false).plus(); // 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; } }); // add explicit schema "/a/b". aSchema.add("b", new AbstractSchema()); // add implicit schema "/a/b" aSubSchemaMap.put("b", new AbstractSchema()); aSchema.setCacheEnabled(true); // explicit should win implicit. assertThat(aSchema.getSubSchemaNames().size(), is(1)); }
public static SchemaPlus createRootSchema( final DruidSchema druidSchema, final SystemSchema systemSchema, final AuthorizerMapper authorizerMapper ) { final SchemaPlus rootSchema = CalciteSchema.createRootSchema(false, false).plus(); rootSchema.add(DruidSchema.NAME, druidSchema); rootSchema.add(InformationSchema.NAME, new InformationSchema(rootSchema, authorizerMapper)); rootSchema.add(SystemSchema.NAME, systemSchema); return rootSchema; }
checkRequiredAttributes(jsonMaterialization, "sql"); final SchemaPlus schema = currentSchema(); if (!schema.isMutable()) { throw new RuntimeException( "Cannot define materialization; parent schema '" viewName = "$" + schema.getTableNames().size(); existing = true; } else { schema.add(viewName, MaterializedViewTable.create(calciteSchema, jsonMaterialization.getSql(), jsonMaterialization.viewSchemaPath, viewPath,
/** Tests some of the properties of the {@link Lattice} data structure. */ @Test public void testLattice() throws Exception { modelWithLattice("star", "select 1 from \"foodmart\".\"sales_fact_1997\" as s\n" + "join \"foodmart\".\"product\" as p using (\"product_id\")\n" + "join \"foodmart\".\"time_by_day\" as t on t.\"time_id\" = s.\"time_id\"") .doWithConnection(c -> { final SchemaPlus schema = c.getRootSchema(); final SchemaPlus adhoc = schema.getSubSchema("adhoc"); assertThat(adhoc.getTableNames().contains("EMPLOYEES"), is(true)); final Map.Entry<String, CalciteSchema.LatticeEntry> entry = adhoc.unwrap(CalciteSchema.class).getLatticeMap().firstEntry(); final Lattice lattice = entry.getValue().getLattice(); assertThat(lattice.firstColumn("S"), is(10)); assertThat(lattice.firstColumn("P"), is(18)); assertThat(lattice.firstColumn("T"), is(0)); assertThat(lattice.firstColumn("PC"), is(-1)); assertThat(lattice.defaultMeasures.size(), is(1)); assertThat(lattice.rootNode.descendants.size(), is(3)); }); }
public static void registerCatalog( SchemaPlus parentSchema, String catalogName, ReadableCatalog catalog) { LOGGER.info("Register catalog '{}' to Calcite", catalogName); SchemaPlus catalogSchema = parentSchema.getSubSchema(catalogName); if (catalogSchema != null) { throw new CatalogAlreadyExistException(catalogName); } else { CatalogCalciteSchema newCatalog = new CatalogCalciteSchema(catalogName, catalog); SchemaPlus schemaPlusOfNewCatalog = parentSchema.add(catalogName, newCatalog); newCatalog.registerSubSchemas(schemaPlusOfNewCatalog); } }
private static void addSchemasToCloseList(final SchemaPlus tree, final List<AutoCloseable> toClose) { for(String subSchemaName : tree.getSubSchemaNames()) { addSchemasToCloseList(tree.getSubSchema(subSchemaName), toClose); } try { AbstractSchema drillSchemaImpl = tree.unwrap(AbstractSchema.class); toClose.add(drillSchemaImpl); } catch (ClassCastException e) { // Ignore as the SchemaPlus is not an implementation of Drill schema. } } }