use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection in project calcite by apache.
the class LinqFrontJdbcBackTest method testTableWhere.
@Test
public void testTableWhere() throws SQLException, ClassNotFoundException {
final Connection connection = CalciteAssert.that(CalciteAssert.Config.JDBC_FOODMART).connect();
final CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
final SchemaPlus rootSchema = calciteConnection.getRootSchema();
ParameterExpression c = Expressions.parameter(JdbcTest.Customer.class, "c");
String s = Schemas.queryable(Schemas.createDataContext(connection, rootSchema), rootSchema.getSubSchema("foodmart"), JdbcTest.Customer.class, "customer").where(Expressions.<Predicate1<JdbcTest.Customer>>lambda(Expressions.lessThan(Expressions.field(c, "customer_id"), Expressions.constant(5)), c)).toList().toString();
Util.discard(s);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection in project calcite by apache.
the class MultiJdbcSchemaJoinTest method setup.
private Connection setup() throws SQLException {
// Create a jdbc database & table
final String db = TempDb.INSTANCE.getUrl();
Connection c1 = DriverManager.getConnection(db, "", "");
Statement stmt1 = c1.createStatement();
// This is a table we can join with the emps from the hr schema
stmt1.execute("create table table1(id integer not null primary key, " + "field1 varchar(10))");
stmt1.execute("insert into table1 values(100, 'foo')");
stmt1.execute("insert into table1 values(200, 'bar')");
c1.close();
// Make a Calcite schema with both a jdbc schema and a non-jdbc schema
Connection connection = DriverManager.getConnection("jdbc:calcite:");
CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
SchemaPlus rootSchema = calciteConnection.getRootSchema();
rootSchema.add("DB", JdbcSchema.create(rootSchema, "DB", JdbcSchema.dataSource(db, "org.hsqldb.jdbcDriver", "", ""), null, null));
rootSchema.add("hr", new ReflectiveSchema(new JdbcTest.HrSchema()));
return connection;
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection in project calcite by apache.
the class MultiJdbcSchemaJoinTest method testSchemaConsistency.
@Test
public void testSchemaConsistency() throws Exception {
// Create a database
final String db = TempDb.INSTANCE.getUrl();
Connection c1 = DriverManager.getConnection(db, "", "");
Statement stmt1 = c1.createStatement();
stmt1.execute("create table table1(id varchar(10) not null primary key, " + "field1 varchar(10))");
// Connect via calcite to these databases
Connection connection = DriverManager.getConnection("jdbc:calcite:");
CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
SchemaPlus rootSchema = calciteConnection.getRootSchema();
final DataSource ds = JdbcSchema.dataSource(db, "org.hsqldb.jdbcDriver", "", "");
rootSchema.add("DB", JdbcSchema.create(rootSchema, "DB", ds, null, null));
Statement stmt3 = connection.createStatement();
ResultSet rs;
// fails, table does not exist
try {
rs = stmt3.executeQuery("select * from db.table2");
fail("expected error, got " + rs);
} catch (SQLException e) {
assertThat(e.getCause().getCause().getMessage(), equalTo("Object 'TABLE2' not found within 'DB'"));
}
stmt1.execute("create table table2(id varchar(10) not null primary key, " + "field1 varchar(10))");
stmt1.execute("insert into table2 values('a', 'aaaa')");
PreparedStatement stmt2 = connection.prepareStatement("select * from db.table2");
stmt1.execute("alter table table2 add column field2 varchar(10)");
// "field2" not visible to stmt2
rs = stmt2.executeQuery();
assertThat(CalciteAssert.toString(rs), equalTo("ID=a; FIELD1=aaaa\n"));
// "field2" visible to a new query
rs = stmt3.executeQuery("select * from db.table2");
assertThat(CalciteAssert.toString(rs), equalTo("ID=a; FIELD1=aaaa; FIELD2=null\n"));
c1.close();
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection in project calcite by apache.
the class RelMdColumnOriginsTest method testQueryWithAggregateGroupingSets.
/**
* Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-542">[CALCITE-542]
* Support for Aggregate with grouping sets in RelMdColumnOrigins</a>.
*/
@Test
public void testQueryWithAggregateGroupingSets() throws Exception {
Connection connection = DriverManager.getConnection("jdbc:calcite:");
CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
calciteConnection.getRootSchema().add("T1", new TableInRootSchemaTest.SimpleTable());
Statement statement = calciteConnection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT TABLE1.ID, TABLE2.ID FROM " + "(SELECT GROUPING(A) AS ID FROM T1 " + "GROUP BY ROLLUP(A,B)) TABLE1 " + "JOIN " + "(SELECT GROUPING(A) AS ID FROM T1 " + "GROUP BY ROLLUP(A,B)) TABLE2 " + "ON TABLE1.ID = TABLE2.ID");
final String result1 = "ID=0; ID=0";
final String result2 = "ID=1; ID=1";
final ImmutableMultiset<String> expectedResult = ImmutableMultiset.<String>builder().addCopies(result1, 25).add(result2).build();
assertThat(CalciteAssert.toSet(resultSet), equalTo(expectedResult));
final ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
assertThat(resultSetMetaData.getColumnName(1), equalTo("ID"));
assertThat(resultSetMetaData.getTableName(1), nullValue());
assertThat(resultSetMetaData.getSchemaName(1), nullValue());
assertThat(resultSetMetaData.getColumnName(2), equalTo("ID"));
assertThat(resultSetMetaData.getTableName(2), nullValue());
assertThat(resultSetMetaData.getSchemaName(2), nullValue());
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 CalciteAssert method assertQuery.
static void assertQuery(Connection connection, String sql, int limit, boolean materializationsEnabled, List<Pair<Hook, Function>> hooks, Function<ResultSet, Void> resultChecker, Function<Integer, Void> updateChecker, Function<Throwable, Void> exceptionChecker) throws Exception {
final String message = "With materializationsEnabled=" + materializationsEnabled + ", limit=" + limit;
try (final Closer closer = new Closer()) {
if (connection instanceof CalciteConnection) {
CalciteConnection calciteConnection = (CalciteConnection) connection;
calciteConnection.getProperties().setProperty(CalciteConnectionProperty.MATERIALIZATIONS_ENABLED.camelName(), Boolean.toString(materializationsEnabled));
calciteConnection.getProperties().setProperty(CalciteConnectionProperty.CREATE_MATERIALIZATIONS.camelName(), Boolean.toString(materializationsEnabled));
if (!calciteConnection.getProperties().containsKey(CalciteConnectionProperty.TIME_ZONE.camelName())) {
// Do not override id some test has already set this property.
calciteConnection.getProperties().setProperty(CalciteConnectionProperty.TIME_ZONE.camelName(), DateTimeUtils.UTC_ZONE.getID());
}
}
for (Pair<Hook, Function> hook : hooks) {
closer.add(hook.left.addThread(hook.right));
}
Statement statement = connection.createStatement();
statement.setMaxRows(limit <= 0 ? limit : Math.max(limit, 1));
ResultSet resultSet = null;
Integer updateCount = null;
try {
if (updateChecker == null) {
resultSet = statement.executeQuery(sql);
} else {
updateCount = statement.executeUpdate(sql);
}
if (exceptionChecker != null) {
exceptionChecker.apply(null);
return;
}
} catch (Exception | Error e) {
if (exceptionChecker != null) {
exceptionChecker.apply(e);
return;
}
throw e;
}
if (resultChecker != null) {
resultChecker.apply(resultSet);
}
if (updateChecker != null) {
updateChecker.apply(updateCount);
}
if (resultSet != null) {
resultSet.close();
}
statement.close();
connection.close();
} catch (Error | RuntimeException e) {
// at the very top level of the exception stack.
throw e;
} catch (Throwable e) {
throw new RuntimeException(message, e);
}
}
Aggregations