Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,24 @@ public boolean supportsGroupingSets() {
return dialectVersion.isUnknownOrAtLeast(22, 9);
}

/**
* {@code CREATE DATABASE [IF NOT EXISTS] "s"}: a schema in ClickHouse is a database; {@code CREATE SCHEMA} is a
* syntax error (Code 62) in every version.
*/
@Override
public String createSchema(String schemaName, boolean ifNotExists) {
return "CREATE DATABASE " + (ifNotExists ? "IF NOT EXISTS " : "") + quoteIdentifier(schemaName);
}

/**
* {@code DROP DATABASE [IF EXISTS] "s"}, which drops its tables with it: ClickHouse has no {@code CASCADE} here,
* and {@code DROP SCHEMA} is a syntax error (Code 62) in every version.
*/
@Override
public String dropSchema(String schemaName, boolean ifExists, boolean cascade) {
return "DROP DATABASE " + (ifExists ? "IF EXISTS " : "") + quoteIdentifier(schemaName);
}

@Override
public boolean supportsSequences() {
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Copyright (c) 2026 Contributors to the Eclipse Foundation.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.daanse.sql.dialect.db.clickhouse.container;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.eclipse.daanse.sql.dialect.api.DialectInitData;
import org.eclipse.daanse.sql.dialect.api.generator.DdlGenerator;
import org.eclipse.daanse.sql.dialect.db.clickhouse.ClickHouseDialect;
import org.eclipse.daanse.sql.model.schema.SchemaReference;
import org.eclipse.daanse.sql.model.schema.TableReference;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.testcontainers.clickhouse.ClickHouseContainer;

/**
* A schema in ClickHouse is a database: {@code CREATE SCHEMA} and {@code DROP SCHEMA} are syntax errors in every
* version. The dialect is built from a live connection and creates a schema with {@code CREATE DATABASE}, drops it with
* {@code DROP DATABASE}, which takes its tables along, with no CASCADE: the schema is there, takes a table and its
* rows, and is dropped with them; IF [NOT] EXISTS spares an error, and without it a second create and a second drop
* fail.
*/
@EnabledIfSystemProperty(named = "integration.docker", matches = "true")
class ClickHouseSchemaTest {

private static final TableReference T = new TableReference(
Optional.of(new SchemaReference(Optional.empty(), "s")), "t", TableReference.TYPE_TABLE);

private static final String DATABASES = "SELECT name FROM system.databases WHERE name = 's'";

@ParameterizedTest(name = "clickhouse:{0}")
@ValueSource(strings = { "21.3", "21.8", "23.8", "24.8", "25.8", "26.6" })
void schemas(String tag) throws Exception {
try (ClickHouseContainer ch = ClickHouseContainers.container(tag)) {
ch.start();
try (Connection c = DriverManager.getConnection(ClickHouseContainers.jdbcUrl(ch, tag), ch.getUsername(),
ch.getPassword()); Statement st = c.createStatement()) {
ClickHouseDialect dialect = new ClickHouseDialect(DialectInitData.fromConnection(c));
DdlGenerator ddl = dialect.ddlGenerator();
assertThat(ddl.supportsSchemas()).as("supportsSchemas").isTrue();

assertThatThrownBy(() -> st.execute("CREATE SCHEMA IF NOT EXISTS \"s\""))
.as("CREATE SCHEMA, a syntax error").isInstanceOf(SQLException.class);
assertThatThrownBy(() -> st.execute("DROP SCHEMA IF EXISTS \"s\" CASCADE"))
.as("DROP SCHEMA, a syntax error").isInstanceOf(SQLException.class);

String createIfNotExists = ddl.createSchema("s", true);
assertThat(createIfNotExists).startsWith("CREATE DATABASE IF NOT EXISTS ");
execute(st, createIfNotExists);
execute(st, createIfNotExists);
assertThat(rows(st, DATABASES)).as("the schema, a database").containsExactly("s");
assertThatThrownBy(() -> st.execute(ddl.createSchema("s", false))).as("a second one of the name")
.isInstanceOf(SQLException.class);

execute(st, "CREATE TABLE " + dialect.qualified(T) + " (a Int32) ENGINE = MergeTree() ORDER BY a");
execute(st, "INSERT INTO " + dialect.qualified(T) + " VALUES (1)");
assertThat(rows(st, "SELECT toString(count()) FROM " + dialect.qualified(T)))
.as("a table of the schema").containsExactly("1");

String drop = ddl.dropSchema("s", false, true);
assertThat(drop).as("no CASCADE, asked for or not").startsWith("DROP DATABASE ")
.doesNotContain("CASCADE");
execute(st, drop);
assertThat(rows(st, DATABASES)).as("dropped").isEmpty();
assertThatThrownBy(() -> rows(st, "SELECT count() FROM " + dialect.qualified(T)))
.as("the table gone with it").isInstanceOf(SQLException.class);
assertThatThrownBy(() -> st.execute(drop)).as("a missing one").isInstanceOf(SQLException.class);
execute(st, ddl.dropSchema("s", true, true));
}
}
}

private static void execute(Statement st, String sql) throws SQLException {
try {
st.execute(sql);
} catch (SQLException e) {
throw new SQLException(e.getMessage() + " in " + sql, e);
}
}

private static List<String> rows(Statement st, String sql) throws SQLException {
List<String> out = new ArrayList<>();
try (ResultSet rs = st.executeQuery(sql)) {
while (rs.next()) {
out.add(rs.getString(1));
}
}
return out;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,52 @@ public Optional<String> dropUser(String userName, boolean ifExists) {
+ quoteIdentifier(userName));
}

/**
* A schema in Oracle is a user: one is created as a user without credentials, as {@link #supportsUsers} needs
* (Oracle 18), so on 11 there are no schemas to create. {@code CREATE SCHEMA} is Oracle's
* {@code CREATE SCHEMA AUTHORIZATION}, objects for a user there already, and refused here (ORA-02420), as
* {@code DROP SCHEMA} is (ORA-00950). A dialect made without a connection takes a version from 18 on.
*/
@Override
public boolean supportsSchemas() {
return supportsUsers();
}

/**
* {@code CREATE USER [IF NOT EXISTS] "s" NO AUTHENTICATION QUOTA UNLIMITED ON USERS}: without a quota a table of
* the schema takes no row (ORA-01950). {@code USERS}, the default tablespace of XE and Free, must be there.
* {@code IF NOT EXISTS} from Oracle 23; before, and for an unknown version, it is left out. {@code null} where
* {@link #supportsSchemas} says no.
*/
@Override
public String createSchema(String schemaName, boolean ifNotExists) {
if (!supportsSchemas()) {
return null;
}
return "CREATE USER " + (ifNotExists && dialectVersion.atLeast(23, 0) ? "IF NOT EXISTS " : "")
+ quoteIdentifier(schemaName) + " NO AUTHENTICATION QUOTA UNLIMITED ON USERS";
}

/**
* {@code DROP USER [IF EXISTS] "s" CASCADE}, always with CASCADE: Oracle drops no user of objects without it
* (ORA-01922). {@code IF EXISTS} from Oracle 23; before, and for an unknown version, it is left out. {@code null}
* where {@link #supportsSchemas} says no.
*/
@Override
public String dropSchema(String schemaName, boolean ifExists, boolean cascade) {
if (!supportsSchemas()) {
return null;
}
return "DROP USER " + (ifExists && dialectVersion.atLeast(23, 0) ? "IF EXISTS " : "")
+ quoteIdentifier(schemaName) + " CASCADE";
}

/** {@code IF EXISTS} on a schema's drop from Oracle 23. */
@Override
public boolean supportsDropSchemaIfExists() {
return dialectVersion.atLeast(23, 0);
}

/**
* Oracle builds a materialized view's rows when it is created and refreshes them on demand, its
* defaults. {@code IF [NOT] EXISTS} from Oracle 23; before, and for an unknown version, it is left
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* Copyright (c) 2026 Contributors to the Eclipse Foundation.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.daanse.sql.dialect.db.oracle.container;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.eclipse.daanse.sql.dialect.api.DialectInitData;
import org.eclipse.daanse.sql.dialect.api.generator.DdlGenerator;
import org.eclipse.daanse.sql.dialect.db.oracle.OracleDialect;
import org.eclipse.daanse.sql.model.schema.SchemaReference;
import org.eclipse.daanse.sql.model.schema.TableReference;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.testcontainers.containers.JdbcDatabaseContainer;

/**
* A schema in Oracle is a user: {@code CREATE SCHEMA} is Oracle's {@code CREATE SCHEMA AUTHORIZATION}, objects for a
* user there already (ORA-02420), and {@code DROP SCHEMA} no statement (ORA-00950). The dialect, built from a live
* connection of SYSTEM, creates a schema as a user without credentials with a quota on USERS, from 18 on, and drops it
* with CASCADE: the schema is there, takes a table and its rows, and is dropped with them; from 23 on IF [NOT] EXISTS
* spares an error; on 18 and 21 it is left out, for the server refuses it, and a second create fails with ORA-01920.
*/
@EnabledIfSystemProperty(named = "integration.docker", matches = "true")
class OracleSchemaTest {

private static final String SCHEMA = "RT_SCHEMA";
private static final TableReference T = new TableReference(
Optional.of(new SchemaReference(Optional.empty(), SCHEMA)), "T", TableReference.TYPE_TABLE);

@ParameterizedTest(name = "{0} -> IF [NOT] EXISTS {1}")
@CsvSource({ "gvenzl/oracle-xe:18-slim-faststart, false", "gvenzl/oracle-xe:21-slim-faststart, false",
"gvenzl/oracle-free:23.9-slim-faststart, true",
"gvenzl/oracle-free:23.26.4-slim-faststart, true" })
void schemas(String image, boolean ifExists) throws Exception {
try (JdbcDatabaseContainer<?> ora = OracleContainers.of(image)) {
ora.start();
// SYSTEM may create users and read DBA_USERS; the container's user may not
try (Connection c = DriverManager.getConnection(ora.getJdbcUrl(), "system", ora.getPassword());
Statement st = c.createStatement()) {
OracleDialect dialect = new OracleDialect(DialectInitData.fromConnection(c));
DdlGenerator ddl = dialect.ddlGenerator();
assertThat(ddl.supportsSchemas()).as("supportsSchemas from 18").isTrue();
assertThat(dialect.supportsDropSchemaIfExists()).as("supportsDropSchemaIfExists").isEqualTo(ifExists);

assertThatThrownBy(() -> st.execute("CREATE SCHEMA \"" + SCHEMA + "\""))
.as("CREATE SCHEMA is no schema's creation").isInstanceOf(SQLException.class)
.hasMessageContaining("ORA-02420");
assertThatThrownBy(() -> st.execute("DROP SCHEMA \"" + SCHEMA + "\""))
.as("nor DROP SCHEMA a drop").isInstanceOf(SQLException.class)
.hasMessageContaining("ORA-00950");

String create = ddl.createSchema(SCHEMA, false);
assertThat(create).isEqualTo("CREATE USER \"RT_SCHEMA\" NO AUTHENTICATION QUOTA UNLIMITED ON USERS");
st.execute(create);
assertThat(rows(st, "SELECT AUTHENTICATION_TYPE FROM DBA_USERS WHERE USERNAME = '" + SCHEMA + "'"))
.as("the schema, a user authenticated by nothing").containsExactly("NONE");

st.execute("CREATE TABLE " + dialect.qualified(T) + " (A NUMBER)");
st.execute("INSERT INTO " + dialect.qualified(T) + " VALUES (1)");
assertThat(rows(st, "SELECT TO_CHAR(COUNT(*)) FROM " + dialect.qualified(T)))
.as("a table of the schema takes rows: the quota").containsExactly("1");

assertThatThrownBy(() -> st.execute(create)).as("a second one of the name")
.isInstanceOf(SQLException.class).hasMessageContaining("ORA-01920");
String createIfNotExists = ddl.createSchema(SCHEMA, true);
String dropIfExists = ddl.dropSchema(SCHEMA, true, false);
if (ifExists) {
assertThat(createIfNotExists).as("IF NOT EXISTS from 23").startsWith("CREATE USER IF NOT EXISTS ");
st.execute(createIfNotExists);
} else {
assertThat(createIfNotExists).as("no IF NOT EXISTS before 23").isEqualTo(create);
assertThat(dropIfExists).as("no IF EXISTS before 23").doesNotContain("IF EXISTS");
}

String drop = ddl.dropSchema(SCHEMA, false, false);
assertThat(drop).as("CASCADE, asked for or not").isEqualTo("DROP USER \"RT_SCHEMA\" CASCADE");
st.execute(drop);
assertThat(rows(st, "SELECT USERNAME FROM DBA_USERS WHERE USERNAME = '" + SCHEMA + "'"))
.as("dropped, with its table").isEmpty();
assertThatThrownBy(() -> rows(st, "SELECT COUNT(*) FROM " + dialect.qualified(T)))
.as("the table gone with it").isInstanceOf(SQLException.class)
.hasMessageContaining("ORA-00942");
if (ifExists) {
assertThat(dropIfExists).as("IF EXISTS from 23")
.isEqualTo("DROP USER IF EXISTS \"RT_SCHEMA\" CASCADE");
st.execute(dropIfExists);
} else {
assertThatThrownBy(() -> st.execute(drop)).as("a missing one").isInstanceOf(SQLException.class)
.hasMessageContaining("ORA-01918");
}
}
}
}

private static List<String> rows(Statement st, String sql) throws SQLException {
List<String> out = new ArrayList<>();
try (ResultSet rs = st.executeQuery(sql)) {
while (rs.next()) {
out.add(rs.getString(1));
}
}
return out;
}
}
Loading