Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: impl databend parameter metadata #131

Merged
merged 12 commits into from
Jan 19, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.Map;
import java.util.OptionalLong;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Logger;

import static com.databend.client.JsonCodec.jsonCodec;
import static com.google.common.base.MoreObjects.firstNonNull;
Expand Down Expand Up @@ -60,6 +61,7 @@ public class DatabendClientV1
// client session
private final AtomicReference<DatabendSession> databendSession;
private final AtomicReference<QueryResults> currentResults = new AtomicReference<>();
private static final Logger logger = Logger.getLogger(DatabendClientV1.class.getPackage().getName());

public DatabendClientV1(OkHttpClient httpClient, String sql, ClientSettings settings) {
requireNonNull(httpClient, "httpClient is null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public enum DatabendDataType {
MAP(Types.OTHER, DatabendTypes.MAP, false, 0, false, "Map"),
BITMAP(Types.OTHER, DatabendTypes.MAP, false, 0, false, "Bitmap"),
TUPLE(Types.OTHER, DatabendTypes.TUPLE, false, 0, false, "Tuple"),
VARIANT(Types.OTHER, DatabendTypes.VARIANT, false, 0, false, "Variant", "Json"),
VARIANT(Types.VARCHAR, DatabendTypes.VARIANT, false, 0, false, "Variant", "Json"),

BINARY(Types.BINARY, DatabendTypes.BINARY, false, 0, false, "Binary"),

Expand Down Expand Up @@ -87,7 +87,7 @@ public static DatabendDataType getByTypeName(String typeName) {
return INT_16;
} else if (DatabendTypes.UINT16.equalsIgnoreCase(typeName)) {
return UNSIGNED_INT_16;
} else if (DatabendTypes.INT32.equalsIgnoreCase(typeName)) {
} else if (DatabendTypes.INT32.equalsIgnoreCase(typeName) || "int".equalsIgnoreCase(typeName)) {
return INT_32;
} else if (DatabendTypes.UINT32.equalsIgnoreCase(typeName)) {
return UNSIGNED_INT_32;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ public final class DatabendTypes {
public static final String VARIANT_ARRAY = "variantarray";
public static final String VARIANT_OBJECT = "variantobject";
public static final String INTERVAL = "interval";
public static final String DECIMAL = "Decimal";
public static final String DECIMAL = "decimal";
}
4 changes: 0 additions & 4 deletions databend-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@
<artifactId>slf4j-api</artifactId>
<version>1.7.6</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.slf4j</groupId>-->
<!-- <artifactId>slf4j-simple</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ public DatabendColumnInfo(int columnType, List<Integer> columnParameterTypes, Da

public static DatabendColumnInfo of(String name, DatabendRawType type) {
Preconditions.checkArgument(!Strings.isNullOrEmpty(name), "Provided name is null or empty");
return newBuilder(name, type).build();
Builder builder = newBuilder(name, type);
setTypeInfo(builder, type);
return builder.build();
}

public static void setTypeInfo(Builder builder, DatabendRawType type) {
Expand Down Expand Up @@ -163,7 +165,9 @@ public static void setTypeInfo(Builder builder, DatabendRawType type) {
}

public static Builder newBuilder(String name, DatabendRawType type) {
return (new Builder()).setColumnName(name).setColumnType(type.getDataType().getSqlType());
return (new Builder())
.setColumnName(name)
.setDatabendRawType(type);
}

public int getColumnType() {
Expand Down Expand Up @@ -273,6 +277,11 @@ public Builder setColumnType(int columnType) {
return this;
}

public Builder setDatabendRawType(DatabendRawType type) {
this.type = type;
return this;
}

public void setColumnParameterTypes(List<Integer> columnParameterTypes) {
this.columnParameterTypes = ImmutableList.copyOf(requireNonNull(columnParameterTypes, "columnParameterTypes is null"));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.databend.jdbc;

import java.sql.ParameterMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import static com.databend.jdbc.DatabendResultSetMetaData.getTypeClassName;
import static java.util.Objects.requireNonNull;

public class DatabendParameterMetaData extends JdbcWrapper implements ParameterMetaData {
protected final List<DatabendColumnInfo> params;
protected final JdbcTypeMapping mapper;

protected DatabendParameterMetaData(List<DatabendColumnInfo> params, JdbcTypeMapping mapper) {
this.params = requireNonNull(params, "connection is null");
this.mapper = mapper;
}

protected DatabendColumnInfo getParameter(int param) throws SQLException {
if (param < 1 || param > params.size()) {
throw new RuntimeException(format("Parameter index should between 1 and %d but we got %d", params.size(), param));
}

return params.get(param - 1);
}

public static String format(String template, Object... args) {
return String.format(Locale.ROOT, template, args);
}

@Override
public int getParameterCount() throws SQLException {
return params.size();
}

@Override
public int isNullable(int param) throws SQLException {
DatabendColumnInfo p = getParameter(param);
if (p == null) {
return ParameterMetaData.parameterNullableUnknown;
}

return p.getType().isNullable() ? ParameterMetaData.parameterNullable : ParameterMetaData.parameterNoNulls;
}

@Override
public boolean isSigned(int param) throws SQLException {
DatabendColumnInfo p = getParameter(param);
return p != null && p.isSigned();
}

@Override
public int getPrecision(int param) throws SQLException {
DatabendColumnInfo p = getParameter(param);
return p != null ? p.getPrecision() : 0;
}

@Override
public int getScale(int param) throws SQLException {
DatabendColumnInfo p = getParameter(param);
return p != null ? p.getScale() : 0;
}

@Override
public int getParameterType(int param) throws SQLException {
DatabendColumnInfo p = getParameter(param);
return p != null ? mapper.toSqlType(p) : Types.OTHER;
}

@Override
public String getParameterTypeName(int param) throws SQLException {
DatabendColumnInfo p = getParameter(param);
return p != null ? p.getColumnTypeName() : "<unknown>";
}

@Override
public String getParameterClassName(int param) throws SQLException {
DatabendColumnInfo p = getParameter(param);
return getTypeClassName(p.getColumnType());
}

@Override
public int getParameterMode(int param) throws SQLException {
return ParameterMetaData.parameterModeIn;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.databend.jdbc;

import com.databend.client.StageAttachment;
import com.databend.client.data.DatabendDataType;
import com.databend.client.data.DatabendRawType;
import com.databend.jdbc.cloud.DatabendCopyParams;
import com.databend.jdbc.cloud.DatabendStage;
import com.databend.jdbc.parser.BatchInsertUtils;
Expand Down Expand Up @@ -63,6 +65,7 @@ public class DatabendPreparedStatement extends DatabendStatement implements Prep
private final RawStatementWrapper rawStatement;
static final DateTimeFormatter TIME_FORMATTER = DateTimeFormat.forPattern("HH:mm:ss.SSS");
static final DateTimeFormatter TIMESTAMP_FORMATTER = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS");
private final DatabendParameterMetaData paramMetaData;
private static final java.time.format.DateTimeFormatter LOCAL_DATE_TIME_FORMATTER =
new DateTimeFormatterBuilder()
.append(ISO_LOCAL_DATE)
Expand All @@ -87,6 +90,13 @@ public class DatabendPreparedStatement extends DatabendStatement implements Prep
this.batchValues = new ArrayList<>();
this.batchInsertUtils = BatchInsertUtils.tryParseInsertSql(sql);
this.rawStatement = StatementUtil.parseToRawStatementWrapper(sql);
Map<Integer, String> params = StatementUtil.extractColumnTypes(sql);
List<DatabendColumnInfo> list = params.entrySet().stream().map(entry -> {
String type = entry.getValue();
DatabendRawType databendRawType = new DatabendRawType(type);
return DatabendColumnInfo.of(entry.getKey().toString(), databendRawType);
}).collect(Collectors.toList());
this.paramMetaData = new DatabendParameterMetaData(Collections.unmodifiableList(list), new JdbcTypeMapping());
}

private static String formatBooleanLiteral(boolean x) {
Expand Down Expand Up @@ -199,6 +209,7 @@ private StageAttachment uploadBatches() throws SQLException {
LocalDateTime.now().getSecond(),
uuid);
String fileName = saved.getName();
// upload to stage
c.uploadStream(null, stagePrefix, fis, fileName, saved.length(), false);
String stagePath = "@~/" + stagePrefix + fileName;
StageAttachment attachment = buildStateAttachment(c, stagePath);
Expand Down Expand Up @@ -753,9 +764,13 @@ public void setBlob(int i, Blob x)
}

@Override
public void setClob(int i, Clob clob)
public void setClob(int i, Clob x)
throws SQLException {
throw new SQLFeatureNotSupportedException("PreparedStatement", "setClob");
if (x != null) {
setCharacterStream(i, x.getCharacterStream());
} else {
setNull(i, Types.CLOB);
}
}

@Override
Expand Down Expand Up @@ -800,10 +815,11 @@ public void setURL(int i, URL url)
throw new SQLFeatureNotSupportedException("PreparedStatement", "setURL");
}

// If you want to use ps.getParameterMetaData().* methods, you need to use a valid sql such as
// insert into table_name (col1 type1, col2 typ2, col3 type3) values (?, ?, ?)
@Override
public ParameterMetaData getParameterMetaData()
throws SQLException {
return null;
public ParameterMetaData getParameterMetaData() throws SQLException {
return paramMetaData;
}

@Override
Expand Down Expand Up @@ -951,6 +967,7 @@ public void setNClob(int i, Reader reader)
throw new SQLFeatureNotSupportedException("PreparedStatement", "setNClob");
}


private String toDateLiteral(Object value) throws IllegalArgumentException {
requireNonNull(value, "value is null");
if (value instanceof java.util.Date) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class DatabendResultSetMetaData implements ResultSetMetaData {
this.databendColumnInfo = databendColumnInfo;
}

static String getType(int type) {
static String getTypeClassName(int type) {
// see javax.sql.rowset.RowSetMetaDataImpl
switch (type) {
case Types.NUMERIC:
Expand Down Expand Up @@ -198,7 +198,7 @@ public boolean isDefinitelyWritable(int i)
@Override
public String getColumnClassName(int i)
throws SQLException {
return getType(column(i).getColumnType());
return getTypeClassName(column(i).getColumnType());
}

@Override
Expand Down
Loading
Loading