Skip to content

Commit

Permalink
dbeaver#23390 Support REAL_VECTOR type in HANA plugin
Browse files Browse the repository at this point in the history
The new vector data type REAL_VECTOR was introduced with HANA Cloud
Database QRC 1/2024. Details about the new type are available in the
SAP HANA Database Vector Engine Guide.

HANA's JDBC driver natively supports that type starting with version
2.21.5.

This change introduces a new value handler so that vectors are displayed
like arrays. Furthermore, the column type modifiers are adapted to
display vector dimension constraints.
  • Loading branch information
stefanuhrig committed Apr 10, 2024
1 parent 732ffa8 commit d0c624a
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 1 deletion.
1 change: 1 addition & 0 deletions plugins/org.jkiss.dbeaver.ext.hana/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@

<datasource id="hana"/>

<type name="real_vector"/>
<type name="ST_Geometry"/>
<type name="ST_Point"/>
</provider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.jkiss.dbeaver.ext.generic.model.GenericDataSource;
import org.jkiss.dbeaver.ext.generic.model.meta.GenericMetaModel;
import org.jkiss.dbeaver.ext.hana.model.plan.HANAPlanAnalyser;
import org.jkiss.dbeaver.model.DBPDataKind;
import org.jkiss.dbeaver.model.DBPDataSourceContainer;
import org.jkiss.dbeaver.model.DBPDataSourceInfo;
import org.jkiss.dbeaver.model.DBUtils;
Expand Down Expand Up @@ -72,6 +73,14 @@ protected DBPDataSourceInfo createDataSourceInfo(DBRProgressMonitor monitor, @No
return info;
}

@Override
public DBPDataKind resolveDataKind(String typeName, int valueType) {
if ("REAL_VECTOR".equalsIgnoreCase(typeName)) {
return DBPDataKind.ARRAY;
}
return super.resolveDataKind(typeName, valueType);
}

/*
* search
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,13 @@ public String getDualTableName() {
public String getColumnTypeModifiers(@NotNull DBPDataSource dataSource, @NotNull DBSTypedObject column,
@NotNull String typeName, @NotNull DBPDataKind dataKind) {
String ucTypeName = CommonUtils.notEmpty(typeName).toUpperCase(Locale.ENGLISH);
if (("ST_POINT".equals(ucTypeName) || "ST_GEOMETRY".equals(ucTypeName))
if ("REAL_VECTOR".equals(ucTypeName)) {
long dim = column.getMaxLength();
if ((dim > 0) && (dim <= 65000)) {
return "(" + Long.toString(dim) + ")";
}
return "";
} else if (("ST_POINT".equals(ucTypeName) || "ST_GEOMETRY".equals(ucTypeName))
&& (column instanceof HANATableColumn)) {
HANATableColumn hanaColumn = (HANATableColumn) column;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class HANAValueHandlerProvider implements DBDValueHandlerProvider {
public DBDValueHandler getValueHandler(DBPDataSource dataSource, DBDFormatSettings preferences,
DBSTypedObject typedObject) {
switch (typedObject.getTypeName()) {
case "REAL_VECTOR":
return HANAVectorValueHandler.INSTANCE;
case "ST_GEOMETRY":
case "ST_POINT":
return HANAGeometryValueHandler.INSTANCE;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2024 DBeaver Corp and others
*
* Licensed 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.jkiss.dbeaver.ext.hana.model.data;

import java.sql.SQLException;
import java.sql.Types;

import org.jkiss.dbeaver.model.data.DBDCollection;
import org.jkiss.dbeaver.model.exec.DBCException;
import org.jkiss.dbeaver.model.exec.DBCLogicalOperator;
import org.jkiss.dbeaver.model.exec.DBCSession;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCSession;
import org.jkiss.dbeaver.model.impl.jdbc.data.JDBCCollection;
import org.jkiss.dbeaver.model.impl.jdbc.data.handlers.JDBCArrayValueHandler;
import org.jkiss.dbeaver.model.struct.DBSTypedObject;

public class HANAVectorValueHandler extends JDBCArrayValueHandler {

public static final HANAVectorValueHandler INSTANCE = new HANAVectorValueHandler();

@Override
protected boolean useGetArray(DBCSession session, DBSTypedObject type) {
return true;
}

@Override
protected void bindParameter(JDBCSession session, JDBCPreparedStatement statement, DBSTypedObject paramType,
int paramIndex, Object value) throws DBCException, SQLException {
if (value == null) {
statement.setNull(paramIndex, Types.ARRAY);
} else if (value instanceof DBDCollection) {
DBDCollection collection = (DBDCollection) value;
if (collection.isNull()) {
statement.setNull(paramIndex, Types.ARRAY);
} else if (collection instanceof JDBCCollection) {
JDBCCollection jc = (JDBCCollection) collection;
if (jc.getComponentType().getTypeID() != Types.REAL) {
throw new DBCException("Only REAL numbers are allowed in vectors");
}
float[] nvals = new float[jc.size()];
for (int i = 0; i < nvals.length; ++i) {
Float val = (Float) jc.get(i);
if (val == null) {
throw new DBCException("NULL elements are not allowed in vectors");
}
nvals[i] = val;
}
statement.setObject(paramIndex, nvals);
} else {
throw new DBCException("Array parameter type '" + value.getClass().getName() + "' not supported");
}
} else {
throw new DBCException("Array parameter type '" + value.getClass().getName() + "' not supported");
}
}

private static DBCLogicalOperator[] SUPPORTED_OPERATORS = { DBCLogicalOperator.IS_NOT_NULL,
DBCLogicalOperator.IS_NULL };

@Override
public DBCLogicalOperator[] getSupportedOperators(DBSTypedObject attribute) {
return SUPPORTED_OPERATORS;
}
}

0 comments on commit d0c624a

Please sign in to comment.