Skip to content

Commit

Permalink
Merge pull request #154 from aliyun/metabase
Browse files Browse the repository at this point in the history
update version to 3.6.0-rc0
  • Loading branch information
dingxin-tech authored Aug 2, 2024
2 parents 51b2a8e + 4ed697b commit a72db69
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 34 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ jobs:
with:
gpg_private_key: ${{ secrets.gpg_private_key }}
gpg_passphrase: ${{ secrets.gpg_passphrase }}
nexus_username: ${{ secrets.nexus_username }}
nexus_password: ${{ secrets.nexus_password }}
nexus_username: ${{ secrets.nexus_token_username }}
nexus_password: ${{ secrets.nexus_token_password }}
server_id: ossrh
maven_args: -DskipTests

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.aliyun.odps</groupId>
<artifactId>odps-jdbc</artifactId>
<version>3.5.8</version>
<version>3.6.0-rc0</version>
<name>odps-jdbc</name>
<description>Aliyun ODPS JDBC driver</description>
<url>http://odps.aliyun.com</url>
Expand Down
4 changes: 2 additions & 2 deletions settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<servers>
<server>
<id>ossrh</id>
<username>${env.nexus_username}</username>
<password>${env.nexus_password}</password>
<username>${env.nexus_token_username}</username>
<password>${env.nexus_token_password}</password>
</server>
</servers>
</settings>
4 changes: 2 additions & 2 deletions src/main/java/com/aliyun/odps/jdbc/OdpsConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -651,8 +651,8 @@ public OdpsStatement createStatement(int resultSetType, int resultSetConcurrency
@Override
public Statement createStatement(int resultSetType, int resultSetConcurrency,
int resultSetHoldability) throws SQLException {
log.error(Thread.currentThread().getStackTrace()[1].getMethodName() + " is not supported!!!");
throw new SQLFeatureNotSupportedException();
log.warn("Ingore resultSetHoldability when create Statement");
return createStatement(resultSetType, resultSetConcurrency);
}

@Override
Expand Down
37 changes: 29 additions & 8 deletions src/main/java/com/aliyun/odps/jdbc/OdpsDatabaseMetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -1132,12 +1132,31 @@ public ResultSet getColumns(
col.getComment(),
i + 1);
Object[] rowVals =
{catalog, jdbcCol.getTableSchema(), jdbcCol.getTableName(), jdbcCol.getColumnName(),
(long) jdbcCol.getType(), jdbcCol.getTypeName(), null, null,
(long) jdbcCol.getDecimalDigits(), (long) jdbcCol.getNumPercRaidx(),
(long) jdbcCol.getIsNullable(), jdbcCol.getComment(), null, null, null, null,
(long) jdbcCol.getOrdinalPos(), jdbcCol.getIsNullableString(), null, null, null,
null};
{catalog, // table catalog (odps project)
jdbcCol.getTableSchema(), // table schema (odps project)
jdbcCol.getTableName(), // table name
jdbcCol.getColumnName(), // column name
(long) jdbcCol.getType(), // SQL type from java.sql.Types
jdbcCol.getTypeName(), // Data source dependent type name, actually odps typeInfo name
null, // column size
null, // not used
(long) jdbcCol.getDecimalDigits(), // the number of fractional digits.
(long) jdbcCol.getNumPercRaidx(), // Radix (typically either 10 or 2)
(long) jdbcCol.getIsNullable(), // is NULL allowed.
jdbcCol.getComment(), // comment describing column (may be null)
null, // default value for the column
null, // unused
null, // unused
null, // for char types the maximum number of bytes in the column
(long) jdbcCol.getOrdinalPos(), // index of column in table (start at 1)
jdbcCol.getIsNullableString(), // ISO rules are used to determine the nullability for a column.
null, // SCOPE_CATALOG
null, // SCOPE_SCHEMA
null, // SCOPE_TABLE
null, // SOURCE_DATA_TYPE
"NO", // IS_AUTOINCREMENT
"NO", // IS_GENERATEDCOLUMN
};

rows.add(rowVals);
}
Expand All @@ -1161,7 +1180,8 @@ public ResultSet getColumns(
"SQL_DATA_TYPE", "SQL_DATETIME_SUB",
"CHAR_OCTET_LENGTH", "ORDINAL_POSITION",
"IS_NULLABLE", "SCOPE_CATALOG", "SCOPE_SCHEMA",
"SCOPE_TABLE", "SOURCE_DATA_TYPE"),
"SCOPE_TABLE", "SOURCE_DATA_TYPE",
"IS_AUTOINCREMENT", "IS_GENERATEDCOLUMN"),
Arrays.asList(TypeInfoFactory.STRING, TypeInfoFactory.STRING,
TypeInfoFactory.STRING,
TypeInfoFactory.STRING, TypeInfoFactory.BIGINT,
Expand All @@ -1176,7 +1196,8 @@ public ResultSet getColumns(
TypeInfoFactory.STRING,
TypeInfoFactory.STRING, TypeInfoFactory.STRING,
TypeInfoFactory.STRING,
TypeInfoFactory.BIGINT));
TypeInfoFactory.BIGINT, TypeInfoFactory.STRING,
TypeInfoFactory.STRING));

return new OdpsStaticResultSet(getConnection(), meta, rows.iterator());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import com.aliyun.odps.data.Record;
import com.aliyun.odps.tunnel.InstanceTunnel.DownloadSession;
Expand Down
38 changes: 37 additions & 1 deletion src/main/java/com/aliyun/odps/jdbc/OdpsPreparedStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.SQLType;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
Expand Down Expand Up @@ -67,7 +69,8 @@ public class OdpsPreparedStatement extends AbstractOdpsPreparedStatement {

private final String PREP_INSERT_WITH_SPEC_PARTITION =
"(?i)^" + "\\s*" + "insert" + "\\s+" + "into" + "\\s+" + TABLE_NAME + "\\s*" + SPEC_COLUMN
+ "\\s+" + "partition" + SPEC_PARTITION + "\\s+" + "values" + "\\s*" + PREP_VALUES + "\\s*" + ";?\\s*$";
+ "\\s+" + "partition" + SPEC_PARTITION + "\\s+" + "values" + "\\s*" + PREP_VALUES + "\\s*"
+ ";?\\s*$";

private final String EXAMPLE =
"INSERT INTO table [(c1, c2)] [partition(p1=a,p2=b,...)] VALUES (?, ?);";
Expand All @@ -89,10 +92,17 @@ public class OdpsPreparedStatement extends AbstractOdpsPreparedStatement {
static ThreadLocal<SimpleDateFormat>
DATE_FORMAT =
ThreadLocal.withInitial(() -> new SimpleDateFormat(JdbcColumn.ODPS_DATE_FORMAT));

static ThreadLocal<DateTimeFormatter>
LOCAL_DATE_FORMAT =
ThreadLocal.withInitial(() -> DateTimeFormatter.ofPattern(JdbcColumn.ODPS_DATE_FORMAT)
.withZone(ZoneId.systemDefault()));

static ThreadLocal<DateTimeFormatter>
ZONED_DATETIME_FORMAT =
ThreadLocal.withInitial(() -> DateTimeFormatter.ofPattern(JdbcColumn.ODPS_DATETIME_FORMAT)
.withZone(ZoneId.systemDefault()));

static ThreadLocal<DateTimeFormatter>
ZONED_TIMESTAMP_FORMAT =
ThreadLocal.withInitial(() -> DateTimeFormatter.ofPattern(JdbcColumn.ODPS_TIMESTAMP_FORMAT)
Expand Down Expand Up @@ -370,6 +380,8 @@ public void setObject(int parameterIndex, Object x) throws SQLException {
setDate(parameterIndex, (Date) x);
} else if (x instanceof java.util.Date) {
parameters.put(parameterIndex, x);
} else if (x instanceof LocalDate) {
parameters.put(parameterIndex, x);
} else if (x instanceof ZonedDateTime) {
parameters.put(parameterIndex, x);
} else if (x instanceof Instant) {
Expand Down Expand Up @@ -490,6 +502,7 @@ private String updateSql(String sql, HashMap<Integer, Object> parameters) throws
* java.sql.Time -> odps DateTime
* java.sql.Timestamp -> odps Timestamp
* java.util.Date -> odps Date
* java.time.LocalDate -> odps Date
* java.time.ZonedDateTime -> odps DateTime
* java.time.Instant -> odps Timestamp
*
Expand Down Expand Up @@ -552,6 +565,8 @@ private String convertJavaTypeToSqlString(Object x) throws SQLException {
DATE_FORMAT.get().setCalendar(calendar);
return String.format("DATE'%s'", DATE_FORMAT.get().format(x));
}
} else if (x instanceof LocalDate) {
return String.format("DATE'%s'", LOCAL_DATE_FORMAT.get().format((LocalDate) x));
} else if (x instanceof ZonedDateTime) {
return String.format("DATETIME'%s'",
ZONED_DATETIME_FORMAT.get().format((ZonedDateTime) x));
Expand Down Expand Up @@ -584,4 +599,25 @@ private boolean isIllegal(String str) {
return matcher.find();
}

@Override
public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException {
setObject(parameterIndex, x);
}

@Override
public void setObject(int parameterIndex, Object x, int targetSqlType, int scaleOrLength)
throws SQLException {
setObject(parameterIndex, x);
}

@Override
public void setObject(int parameterIndex, Object x, SQLType targetSqlType) throws SQLException {
setObject(parameterIndex, x);
}

@Override
public void setObject(int parameterIndex, Object x, SQLType targetSqlType, int scaleOrLength)
throws SQLException {
setObject(parameterIndex, x);
}
}
36 changes: 18 additions & 18 deletions src/main/java/com/aliyun/odps/jdbc/OdpsResultSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.util.Map;
import java.util.TimeZone;

import com.aliyun.odps.jdbc.utils.Utils;
import com.aliyun.odps.jdbc.utils.transformer.to.jdbc.AbstractToJdbcDateTypeTransformer;
import com.aliyun.odps.jdbc.utils.transformer.to.jdbc.AbstractToJdbcTransformer;
import com.aliyun.odps.jdbc.utils.transformer.to.jdbc.ToJdbcTransformerFactory;
Expand All @@ -56,11 +57,23 @@ public abstract class OdpsResultSet extends WrapperAdapter implements ResultSet
private boolean wasNull = false;

private SQLWarning warningChain = null;
protected TimeZone timeZone;

OdpsResultSet(OdpsConnection conn, OdpsStatement stmt, OdpsResultSetMetaData meta) {
this.stmt = stmt;
this.meta = meta;
this.conn = conn;

if (stmt != null) {
String sessionTimeZoneId =
stmt.getSqlTaskProperties().getProperty("odps.sql.timezone", null);
sessionTimeZoneId = stmt.getInputProperties().getProperty("odps.sql.timezone", sessionTimeZoneId);
if (sessionTimeZoneId != null) {
timeZone = TimeZone.getTimeZone(sessionTimeZoneId);
} else {
timeZone = conn.isUseProjectTimeZone() ? conn.getProjectTimeZone() : null;
}
}
}

@Override
Expand Down Expand Up @@ -161,7 +174,7 @@ public BigDecimal getBigDecimal(String columnLabel) throws SQLException {
public int findColumn(String columnLabel) throws SQLException {
int index = getMetaData().getColumnIndex(columnLabel);
if (index == -1) {
throw new SQLException("the column label is invalid");
throw new SQLException("the column label is invalid: [" + columnLabel + "]");
}
return index;
}
Expand Down Expand Up @@ -215,23 +228,23 @@ public Object getObject(String columnLabel) throws SQLException {
@Override
public Object getObject(int columnIndex, Map<String, Class<?>> map)
throws SQLException {
throw new SQLFeatureNotSupportedException();
return getObject(columnIndex);
}

@Override
public Object getObject(String columnLabel, Map<String, Class<?>> map)
throws SQLException {
throw new SQLFeatureNotSupportedException();
return getObject(columnLabel);
}

@Override
public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
throw new SQLFeatureNotSupportedException();
return Utils.convertToSqlType(getObject(columnIndex), type, timeZone);
}

@Override
public <T> T getObject(String columnLabel, Class<T> type) throws SQLException {
throw new SQLFeatureNotSupportedException();
return Utils.convertToSqlType(getObject(columnLabel), type, timeZone);
}

@Override
Expand Down Expand Up @@ -666,19 +679,6 @@ private Object transformToJdbcType(Object o, Class jdbcCls, Calendar cal, TypeIn
throws SQLException {
AbstractToJdbcTransformer transformer = ToJdbcTransformerFactory.getTransformer(jdbcCls);

TimeZone timeZone = null;

if (stmt != null) {
String sessionTimeZoneId =
stmt.getSqlTaskProperties().getProperty("odps.sql.timezone", null);
sessionTimeZoneId = stmt.getInputProperties().getProperty("odps.sql.timezone", sessionTimeZoneId);
if (sessionTimeZoneId != null) {
timeZone = TimeZone.getTimeZone(sessionTimeZoneId);
} else {
timeZone = conn.isUseProjectTimeZone() ? conn.getProjectTimeZone() : null;
}
}

return ((AbstractToJdbcDateTypeTransformer) transformer)
.transform(o, conn.getCharset(), cal, timeZone, typeInfo);
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/aliyun/odps/jdbc/utils/JdbcColumn.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class JdbcColumn {
ODPS_SQLTYPE_MAPPER.put(OdpsType.SMALLINT, java.sql.Types.SMALLINT);
ODPS_SQLTYPE_MAPPER.put(OdpsType.DATE, java.sql.Types.DATE);
ODPS_SQLTYPE_MAPPER.put(OdpsType.TIMESTAMP, java.sql.Types.TIMESTAMP);
ODPS_SQLTYPE_MAPPER.put(OdpsType.TIMESTAMP_NTZ, java.sql.Types.TIMESTAMP);
ODPS_SQLTYPE_MAPPER.put(OdpsType.FLOAT, java.sql.Types.FLOAT);
ODPS_SQLTYPE_MAPPER.put(OdpsType.CHAR, java.sql.Types.CHAR);
ODPS_SQLTYPE_MAPPER.put(OdpsType.BINARY, java.sql.Types.BINARY);
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/com/aliyun/odps/jdbc/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@

import java.io.IOException;
import java.sql.SQLException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -185,4 +190,37 @@ private static String removePattern(String sql, Pattern p) {
return sql;
}

public static <T> T convertToSqlType(Object object, Class<T> type, TimeZone timeZone) {
if (object == null) {
return null;
}
if (type == String.class) {
return (T) Objects.toString(object);
}
if (object instanceof ZonedDateTime) {
ZonedDateTime zonedDateTime = (ZonedDateTime) object;
ZonedDateTime utcZonedDateTime = zonedDateTime.withZoneSameInstant(timeZone.toZoneId());

if (type == ZonedDateTime.class) {
return (T) utcZonedDateTime;
} else if (type == LocalDateTime.class) {
return (T) utcZonedDateTime.toLocalDateTime();
}
} else if (object instanceof LocalDateTime) {
LocalDateTime localDateTime = (LocalDateTime) object;

if (type == ZonedDateTime.class) {
return (T) localDateTime.atZone(timeZone.toZoneId());
}
} else if (object instanceof Instant) {
Instant instant = (Instant) object;
if (type == LocalDateTime.class) {
return (T) LocalDateTime.ofInstant(instant, timeZone.toZoneId());
} else if (type == ZonedDateTime.class) {
return (T) LocalDateTime.ofInstant(instant, timeZone.toZoneId())
.atZone(timeZone.toZoneId());
}
}
return (T) object;
}
}

0 comments on commit a72db69

Please sign in to comment.