Skip to content

Commit

Permalink
Merge branch 'clickhouse_0.7_forpr' into clickhouse_0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
flaming-archer committed Dec 13, 2024
2 parents 3e14cbf + b89a6fe commit a2cbba3
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@

import java.util.Map;
import org.apache.gravitino.catalog.clickhouse.converter.ClickHouseColumnDefaultValueConverter;
import org.apache.gravitino.catalog.clickhouse.converter.ClickHouseExceptionConverter;
import org.apache.gravitino.catalog.clickhouse.converter.ClickHouseTypeConverter;
import org.apache.gravitino.catalog.clickhouse.operation.ClickHouseDatabaseOperations;
import org.apache.gravitino.catalog.clickhouse.operation.ClickHouseTableOperations;
import org.apache.gravitino.catalog.jdbc.JdbcCatalog;
import org.apache.gravitino.catalog.jdbc.JdbcCatalogOperations;
import org.apache.gravitino.catalog.jdbc.converter.JdbcColumnDefaultValueConverter;
import org.apache.gravitino.catalog.jdbc.converter.JdbcExceptionConverter;
import org.apache.gravitino.catalog.jdbc.converter.JdbcTypeConverter;
import org.apache.gravitino.catalog.jdbc.operation.JdbcDatabaseOperations;
import org.apache.gravitino.catalog.jdbc.operation.JdbcTableOperations;
Expand Down Expand Up @@ -60,6 +62,11 @@ public Capability newCapability() {
return new ClickHouseCatalogCapability();
}

@Override
protected JdbcExceptionConverter createExceptionConverter() {
return new ClickHouseExceptionConverter();
}

@Override
protected JdbcTypeConverter createJdbcTypeConverter() {
return new ClickHouseTypeConverter();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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.gravitino.catalog.clickhouse.converter;

import java.sql.SQLException;
import org.apache.gravitino.catalog.jdbc.converter.JdbcExceptionConverter;
import org.apache.gravitino.exceptions.GravitinoRuntimeException;
import org.apache.gravitino.exceptions.NoSuchSchemaException;
import org.apache.gravitino.exceptions.NoSuchTableException;
import org.apache.gravitino.exceptions.SchemaAlreadyExistsException;
import org.apache.gravitino.exceptions.TableAlreadyExistsException;

/** Exception converter to Apache Gravitino exception for ClickHouse. */
public class ClickHouseExceptionConverter extends JdbcExceptionConverter {

// see: SELECT concat('\t', name, ' = ', toString(number))
// FROM
// (
// SELECT
// number,
// errorCodeToName(number) AS name
// FROM system.numbers
// LIMIT 2000
// )
// WHERE NOT empty(errorCodeToName(number))
static final int UNKNOWN_DATABASE = 81;
static final int DATABASE_ALREADY_EXISTS = 82;

static final int TABLE_ALREADY_EXISTS = 57;
static final int TABLE_IS_DROPPED = 218;

@SuppressWarnings("FormatStringAnnotation")
@Override
public GravitinoRuntimeException toGravitinoException(SQLException sqlException) {
int errorCode = sqlException.getErrorCode();
switch (errorCode) {
case DATABASE_ALREADY_EXISTS:
return new SchemaAlreadyExistsException(sqlException, sqlException.getMessage());
case TABLE_ALREADY_EXISTS:
return new TableAlreadyExistsException(sqlException, sqlException.getMessage());
case UNKNOWN_DATABASE:
return new NoSuchSchemaException(sqlException, sqlException.getMessage());
case TABLE_IS_DROPPED:
return new NoSuchTableException(sqlException, sqlException.getMessage());
default:
return new GravitinoRuntimeException(sqlException, sqlException.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,6 @@ public boolean delete(String databaseName, boolean cascade) {
LOG.info("Finished dropping database {}", databaseName);
} catch (NoSuchSchemaException e) {
return false;
} catch (Exception e) {
if (e.getMessage() != null
&& (e.getMessage().contains("Database " + databaseName + " does not exist.")
|| e.getMessage().contains("Database `" + databaseName + "` does not exist."))) {
return false;
}

if (e.getMessage() != null
&& e.getMessage()
.contains(
"Database "
+ databaseName
+ " is not empty, the value of cascade should be true.")) {
throw e;
}
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ void testOperationClickhouseSchema() {
// drop schema failed check.
Assertions.assertFalse(schemas.dropSchema(schemaIdent.name(), true));
Assertions.assertFalse(schemas.dropSchema(schemaIdent.name(), false));
Assertions.assertThrows(RuntimeException.class, () -> tableCatalog.dropTable(table));
Assertions.assertFalse(() -> tableCatalog.dropTable(table));
clickhouseNamespaces = clickhouseService.listSchemas(Namespace.empty());
schemaNames =
Arrays.stream(clickhouseNamespaces).map(NameIdentifier::name).collect(Collectors.toSet());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
import java.util.Map;
import javax.sql.DataSource;
import org.apache.gravitino.catalog.clickhouse.converter.ClickHouseColumnDefaultValueConverter;
import org.apache.gravitino.catalog.clickhouse.converter.ClickHouseExceptionConverter;
import org.apache.gravitino.catalog.clickhouse.converter.ClickHouseTypeConverter;
import org.apache.gravitino.catalog.jdbc.TestJdbc;
import org.apache.gravitino.catalog.jdbc.config.JdbcConfig;
import org.apache.gravitino.catalog.jdbc.converter.JdbcExceptionConverter;
import org.apache.gravitino.catalog.jdbc.utils.DataSourceUtils;
import org.apache.gravitino.integration.test.container.ClickHouseContainer;
import org.apache.gravitino.integration.test.container.ContainerSuite;
Expand All @@ -48,7 +48,7 @@ public static void startup() throws Exception {

DATABASE_OPERATIONS = new ClickHouseDatabaseOperations();
TABLE_OPERATIONS = new ClickHouseTableOperations();
JDBC_EXCEPTION_CONVERTER = new JdbcExceptionConverter();
JDBC_EXCEPTION_CONVERTER = new ClickHouseExceptionConverter();
DATABASE_OPERATIONS.initialize(dataSource, JDBC_EXCEPTION_CONVERTER, Collections.emptyMap());
TABLE_OPERATIONS.initialize(
dataSource,
Expand Down

0 comments on commit a2cbba3

Please sign in to comment.