Skip to content

Commit

Permalink
Test function consistency by comparing Calcite's vs raw ResultSet
Browse files Browse the repository at this point in the history
  • Loading branch information
zabetak committed Feb 26, 2024
1 parent 6ba3130 commit f18d884
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 5 deletions.
5 changes: 4 additions & 1 deletion bom/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,10 @@ dependencies {
// Eventually we should get rid of slf4j-log4j12 dependency but currently it is not possible
// since certain modules (Pig, Piglet) have dependencies using directly Log4j 1.x APIs
runtimev("org.slf4j:slf4j-log4j12", "slf4j")
apiv("org.testcontainers:testcontainers")
apiv("org.testcontainers:testcontainers", "testcontainers")
apiv("org.testcontainers:oracle-xe", "testcontainers")
apiv("org.testcontainers:mysql", "testcontainers")
apiv("org.testcontainers:postgresql", "testcontainers")
apiv("redis.clients:jedis")
apiv("sqlline:sqlline")
runtimev("org.openjdk.jmh:jmh-core", "jmh")
Expand Down
11 changes: 7 additions & 4 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ dependencies {
testCompileOnly("org.immutables:value-annotations")
testCompileOnly("com.google.code.findbugs:jsr305")

testH2("com.h2database:h2")
testMysql("mysql:mysql-connector-java")
testOracle("com.oracle.ojdbc:ojdbc8")
testPostgresql("org.postgresql:postgresql")
testRuntimeOnly("com.h2database:h2")
testRuntimeOnly("mysql:mysql-connector-java")
testRuntimeOnly("com.oracle.ojdbc:ojdbc8")
testRuntimeOnly("org.postgresql:postgresql")

testImplementation(project(":testkit"))
testImplementation("commons-lang:commons-lang")
Expand All @@ -94,6 +94,9 @@ dependencies {
testImplementation("net.hydromatic:quidem")
testImplementation("org.apache.calcite.avatica:avatica-server")
testImplementation("org.apache.commons:commons-pool2")
testImplementation("org.testcontainers:oracle-xe")
testImplementation("org.testcontainers:mysql")
testImplementation("org.testcontainers:postgresql")
testImplementation("org.hsqldb:hsqldb::jdk8")
testImplementation("sqlline:sqlline")
testImplementation(kotlin("stdlib-jdk8"))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.calcite.test;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.JdbcDatabaseContainer;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.containers.OracleContainer;
import org.testcontainers.containers.PostgreSQLContainer;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

public class DBFunctionConsistencyTest {

private enum Type {
ORACLE {
@Override String query(final String sqlFunction) {
return "SELECT " + sqlFunction + " FROM DUAL";
}
}, POSTGRES_9_6, POSTGRES_12_2, MYSQL;

String query(String sqlFunction) {
return "SELECT " + sqlFunction;
}
}

private final static Map<Type, JdbcDatabaseContainer<?>> DBS = initContainers();

private static Map<Type, JdbcDatabaseContainer<?>> initContainers() {
Map<Type, JdbcDatabaseContainer<?>> dbs = new HashMap<>();
dbs.put(Type.POSTGRES_9_6, new PostgreSQLContainer<>("postgres:9.6"));
dbs.put(Type.POSTGRES_12_2, new PostgreSQLContainer<>("postgres:12.2"));
dbs.put(Type.MYSQL, new MySQLContainer<>());
dbs.put(Type.ORACLE, new OracleContainer(
"gvenzl/oracle-xe:21-slim-faststart"));
return dbs;
}

@BeforeAll
static void setup() throws SQLException {
DBS.values().forEach(GenericContainer::start);
}

@AfterAll
static void teardown() {
DBS.values().forEach(GenericContainer::stop);
}

@ParameterizedTest
@CsvSource({
"SQRT(4),ORACLE",
"SQRT(4),POSTGRES_9_6",
"SQRT(4),POSTGRES_12_2",
"SQRT(4),MYSQL",
"SQRT(-1),ORACLE",
"SQRT(-1),POSTGRES_9_6",
"SQRT(-1),POSTGRES_12_2",
"SQRT(-1),MYSQL"
})
void testFunction(String function, String db) {
String calciteResult = execute(Type.valueOf(db), true, function);
String rawResult = execute(Type.valueOf(db), false, function);
Assertions.assertEquals(rawResult, calciteResult);
}

String execute(Type dbType, boolean useCalcite, String exp) {
JdbcDatabaseContainer<?> db = DBS.get(dbType);
final String url;
final String query;
if (useCalcite) {
url = String.format(
"jdbc:calcite:schemaType=JDBC; schema.jdbcUser=%s; schema.jdbcPassword=%s; schema"
+ ".jdbcUrl=%s", db.getUsername(), db.getPassword(), db.getJdbcUrl());
query = "SELECT " + exp;
} else {
url = db.getJdbcUrl();
query = dbType.query(exp);
}
try (Connection c = DriverManager.getConnection(url, db.getUsername(), db.getPassword())) {
try (PreparedStatement stmt = c.prepareStatement(query)) {
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
return rs.getString(1);
} else {
return "";
}
}
}
} catch (Exception e) {
return e.getMessage();
}
}
}
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ natty.version=0.13
ojdbc8.version=19.3.0.0
opencsv.version=2.3
oshi-core.version=6.4.9
oracle-xe.version=1.19.6
pig.version=0.16.0
pigunit.version=0.16.0
postgresql.version=9.3-1102-jdbc41
Expand Down

0 comments on commit f18d884

Please sign in to comment.