connection = DriverManager.getConnection("jdbc:calcite:", properties); final CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class); final SchemaPlus rootSchema = calciteConnection.getRootSchema();
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); }
CalciteConnection connection = (CalciteConnection) driver.connect("jdbc:calcite:", new Properties()); SchemaPlus rootSchema = connection.getRootSchema(); rootSchema.add("sale", new ReflectiveSchema(new Smalls.WideSaleSchema())); connection.setSchema("sale"); final Statement statement = connection.createStatement(); assertThat(resultSet.next(), is(false)); connection.close();
public void visit(JsonRoot jsonRoot) { checkRequiredAttributes(jsonRoot, "version"); final Pair<String, SchemaPlus> pair = Pair.of(null, connection.getRootSchema()); schemaStack.push(pair); for (JsonSchema schema : jsonRoot.schemas) { schema.accept(this); } final Pair<String, SchemaPlus> p = schemaStack.pop(); assert p == pair; if (jsonRoot.defaultSchema != null) { try { connection.setSchema(jsonRoot.defaultSchema); } catch (SQLException e) { throw new RuntimeException(e); } } }
/** Test case for bug where if two tables have different element classes * but those classes have identical fields, Calcite would generate code to use * the wrong element class; a {@link ClassCastException} would ensue. */ @Test public void testDifferentTypesSameFields() throws Exception { Connection connection = DriverManager.getConnection("jdbc:calcite:"); CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class); final SchemaPlus rootSchema = calciteConnection.getRootSchema(); rootSchema.add("TEST", new ReflectiveSchema(new MySchema())); Statement statement = calciteConnection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT \"myvalue\" from TEST.\"mytable2\""); assertEquals("myvalue=2\n", CalciteAssert.toString(resultSet)); resultSet.close(); statement.close(); connection.close(); }
/** 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)); } }
try { final PreparedStatement statement = connection.prepareStatement("VALUES CURRENT_TIMESTAMP"); ResultSet resultSet;
CalciteConnection calciteConnection = (CalciteConnection) connection; calciteConnection.getProperties().setProperty( CalciteConnectionProperty.MATERIALIZATIONS_ENABLED.camelName(), Boolean.toString(false)); calciteConnection.getProperties().setProperty( CalciteConnectionProperty.CREATE_MATERIALIZATIONS.camelName(), Boolean.toString(false)); if (! calciteConnection.getProperties().containsKey( CalciteConnectionProperty.TIME_ZONE.camelName())) { calciteConnection.getProperties().setProperty( CalciteConnectionProperty.TIME_ZONE.camelName(), DateTimeUtils.UTC_ZONE.getID());
with.doWithConnection(connection -> { try { final Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("explain plan for\n" connection.prepareStatement("explain plan for\n" + "insert into \"adhoc\".V (empno, \"salary\", \"name\")\n" + "values (56, 123.4, 'Fred')"); connection.prepareStatement("explain plan for\n" + "insert into \"adhoc\".V (e, name)\n" + "values (56, 'Fred')");
/** * Creates a context for the purposes of preparing a statement. * * @param connection Connection * @param schema Schema * @param schemaPath Path wherein to look for functions * @param objectPath Path of the object being analyzed (usually a view), * or null * @param propValues Connection properties * @return Context */ private static CalcitePrepare.Context makeContext( CalciteConnection connection, CalciteSchema schema, List<String> schemaPath, List<String> objectPath, final ImmutableMap<CalciteConnectionProperty, String> propValues) { if (connection == null) { final CalcitePrepare.Context context0 = CalcitePrepare.Dummy.peek(); final CalciteConnectionConfig config = mutate(context0.config(), propValues); return makeContext(config, context0.getTypeFactory(), context0.getDataContext(), schema, schemaPath, objectPath); } else { final CalciteConnectionConfig config = mutate(connection.config(), propValues); return makeContext(config, connection.getTypeFactory(), createDataContext(connection, schema.root().plus()), schema, schemaPath, objectPath); } }
public String getDefaultSchema() throws SQLException { if (defaultSchema == null) { if (connection == null) { return rootSchemaPlus.getName(); } else { return connection.getSchema(); } } else { return defaultSchema; } }
CalciteConnection connection = (CalciteConnection) driver.connect("jdbc:calcite:", new Properties()); SchemaPlus rootSchema = connection.getRootSchema(); rootSchema.add("sale", new ReflectiveSchema(new Smalls.WideSaleSchema())); connection.setSchema("sale"); final Statement statement = connection.createStatement(); assertThat(resultSet.next(), is(false)); connection.close();
public void visit(JsonRoot jsonRoot) { checkRequiredAttributes(jsonRoot, "version"); final Pair<String, SchemaPlus> pair = Pair.of(null, connection == null ? rootSchemaPlus : connection.getRootSchema()); schemaStack.push(pair); for (JsonSchema schema : jsonRoot.schemas) { schema.accept(this); } final Pair<String, SchemaPlus> p = schemaStack.pop(); assert p == pair; if (jsonRoot.defaultSchema != null) { try { if (connection != null) { connection.setSchema(jsonRoot.defaultSchema); } else { defaultSchema = jsonRoot.defaultSchema; } } catch (SQLException e) { throw new RuntimeException(e); } } }
/** Test case for bug where if two tables have different element classes * but those classes have identical fields, Calcite would generate code to use * the wrong element class; a {@link ClassCastException} would ensue. */ @Test public void testDifferentTypesSameFields() throws Exception { Connection connection = DriverManager.getConnection("jdbc:calcite:"); CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class); final SchemaPlus rootSchema = calciteConnection.getRootSchema(); rootSchema.add("TEST", new ReflectiveSchema(new MySchema())); Statement statement = calciteConnection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT \"myvalue\" from TEST.\"mytable2\""); assertEquals("myvalue=2\n", CalciteAssert.toString(resultSet)); resultSet.close(); statement.close(); connection.close(); }
/** 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)); } }
private void checkPreparedOffsetFetch(final int offset, final int fetch, final Matcher<? super ResultSet> matcher) throws Exception { CalciteAssert.hr() .doWithConnection(connection -> { final String sql = "select \"name\"\n" + "from \"hr\".\"emps\"\n" + "order by \"empid\" offset ? fetch next ? rows only"; try (PreparedStatement p = connection.prepareStatement(sql)) { final ParameterMetaData pmd = p.getParameterMetaData(); assertThat(pmd.getParameterCount(), is(2)); assertThat(pmd.getParameterType(1), is(Types.INTEGER)); assertThat(pmd.getParameterType(2), is(Types.INTEGER)); p.setInt(1, offset); p.setInt(2, fetch); try (ResultSet r = p.executeQuery()) { assertThat(r, matcher); } } catch (SQLException e) { throw new RuntimeException(e); } }); }
(Consumer<RelNode>) substitutionChecker::apply)); ((CalciteConnection) connection).getProperties().setProperty( CalciteConnectionProperty.MATERIALIZATIONS_ENABLED.camelName(), Boolean.toString(materializationsEnabled)); ((CalciteConnection) connection).getProperties().setProperty( CalciteConnectionProperty.CREATE_MATERIALIZATIONS.camelName(), Boolean.toString(materializationsEnabled));
with.doWithConnection(connection -> { try { final Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("explain plan for\n" connection.prepareStatement("explain plan for\n" + "insert into \"adhoc\".V (empno, \"salary\", \"name\")\n" + "values (56, 123.4, 'Fred')"); connection.prepareStatement("explain plan for\n" + "insert into \"adhoc\".V (e, name)\n" + "values (56, 'Fred')");
/** * Creates a context for the purposes of preparing a statement. * * @param connection Connection * @param schema Schema * @param schemaPath Path wherein to look for functions * @param objectPath Path of the object being analyzed (usually a view), * or null * @param propValues Connection properties * @return Context */ private static CalcitePrepare.Context makeContext( CalciteConnection connection, CalciteSchema schema, List<String> schemaPath, List<String> objectPath, final ImmutableMap<CalciteConnectionProperty, String> propValues) { if (connection == null) { final CalcitePrepare.Context context0 = CalcitePrepare.Dummy.peek(); final CalciteConnectionConfig config = mutate(context0.config(), propValues); return makeContext(config, context0.getTypeFactory(), context0.getDataContext(), schema, schemaPath, objectPath); } else { final CalciteConnectionConfig config = mutate(connection.config(), propValues); return makeContext(config, connection.getTypeFactory(), createDataContext(connection, schema.root().plus()), schema, schemaPath, objectPath); } }
assertEquals("adhoc", connection.getSchema()); } catch (SQLException e) { throw new RuntimeException(e);