-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
4 changed files
with
239 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
...ve-jdbc/src/main/java/com/github/housepower/data/type/complex/DataTypeLowCardinality.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
* 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 com.github.housepower.data.type.complex; | ||
|
||
import com.github.housepower.data.DataTypeFactory; | ||
import com.github.housepower.data.IDataType; | ||
import com.github.housepower.misc.SQLLexer; | ||
import com.github.housepower.misc.Validate; | ||
import com.github.housepower.serde.BinaryDeserializer; | ||
import com.github.housepower.serde.BinarySerializer; | ||
|
||
import java.io.IOException; | ||
import java.sql.SQLException; | ||
|
||
public class DataTypeLowCardinality implements IDataType { | ||
|
||
public static DataTypeCreator creator = (lexer, serverContext) -> { | ||
Validate.isTrue(lexer.character() == '('); | ||
IDataType nestedType = DataTypeFactory.get(lexer, serverContext); | ||
Validate.isTrue(lexer.character() == ')'); | ||
return new DataTypeLowCardinality( | ||
"LowCardinality(" + nestedType.name() + ")", nestedType); | ||
}; | ||
|
||
private final String name; | ||
private final IDataType nestedDataType; | ||
|
||
public DataTypeLowCardinality(String name, IDataType nestedDataType) { | ||
this.name = name; | ||
this.nestedDataType = nestedDataType; | ||
} | ||
|
||
@Override | ||
public String name() { | ||
return this.name; | ||
} | ||
|
||
@Override | ||
public int sqlTypeId() { | ||
return this.nestedDataType.sqlTypeId(); | ||
} | ||
|
||
@Override | ||
public Object defaultValue() { | ||
return this.nestedDataType.defaultValue(); | ||
} | ||
|
||
@Override | ||
public Class javaType() { | ||
return this.nestedDataType.javaType(); | ||
} | ||
|
||
@Override | ||
public Class jdbcJavaType() { | ||
return this.nestedDataType.jdbcJavaType(); | ||
} | ||
|
||
@Override | ||
public boolean nullable() { | ||
return this.nestedDataType.nullable(); | ||
} | ||
|
||
@Override | ||
public int getPrecision() { | ||
return this.nestedDataType.getPrecision(); | ||
} | ||
|
||
@Override | ||
public int getScale() { | ||
return this.nestedDataType.getScale(); | ||
} | ||
|
||
@Override | ||
public Object deserializeText(SQLLexer lexer) throws SQLException { | ||
return this.nestedDataType.deserializeText(lexer); | ||
} | ||
|
||
@Override | ||
public void serializeBinary(Object data, BinarySerializer serializer) throws SQLException, IOException { | ||
this.nestedDataType.serializeBinary(data, serializer); | ||
} | ||
|
||
@Override | ||
public void serializeBinaryBulk(Object[] data, BinarySerializer serializer) throws SQLException, IOException { | ||
this.nestedDataType.serializeBinaryBulk(data, serializer); | ||
} | ||
|
||
@Override | ||
public Object deserializeBinary(BinaryDeserializer deserializer) throws SQLException, IOException { | ||
return this.nestedDataType.deserializeBinary(deserializer); | ||
} | ||
|
||
@Override | ||
public Object[] deserializeBinaryBulk(int rows, BinaryDeserializer deserializer) throws SQLException, IOException { | ||
Object[] data = this.nestedDataType.deserializeBinaryBulk(rows, deserializer); | ||
return data; | ||
} | ||
|
||
@Override | ||
public boolean isSigned() { | ||
return this.nestedDataType.isSigned(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
clickhouse-native-jdbc/src/test/java/com/github/housepower/jdbc/LowCardinalityTypeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* 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 com.github.housepower.jdbc; | ||
|
||
import com.github.housepower.misc.BytesHelper; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.sql.*; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
//Refer to [[https://github.com/housepower/ClickHouse-Native-JDBC/issues/442]] for more details. | ||
public class LowCardinalityTypeTest extends AbstractITest implements BytesHelper { | ||
@Test | ||
public void testAllLowCardinalityTypes() throws Exception { | ||
withStatement(statement -> { | ||
statement.execute("DROP TABLE IF EXISTS low_cardinality_test"); | ||
|
||
StringBuilder createTableSQL = new StringBuilder(); | ||
createTableSQL.append("CREATE TABLE IF NOT EXISTS low_cardinality_test (") | ||
.append("value_string LowCardinality(String), ") | ||
.append("fixed_string LowCardinality(FixedString(10)), ") | ||
.append("date_value LowCardinality(Nullable(Date)), ") | ||
.append("datetime_value LowCardinality(Nullable(DateTime)), ") | ||
.append("number_value LowCardinality(Nullable(Int32))) Engine=Memory()"); | ||
|
||
statement.execute(createTableSQL.toString()); | ||
|
||
String sql = "INSERT INTO low_cardinality_test " + | ||
"(value_string, fixed_string, date_value, datetime_value, number_value) values(?, ?, ?, ?, ?);"; | ||
|
||
try (PreparedStatement pstmt = statement.getConnection().prepareStatement(sql)) { | ||
for (int i = 0; i < 300; i++) { | ||
pstmt.setString(1, "test"); | ||
pstmt.setString(2, "abcdefghij"); | ||
if (i % 50 == 0) { | ||
pstmt.setNull(3, Types.DATE); | ||
pstmt.setNull(4, Types.TIMESTAMP); | ||
pstmt.setNull(5, Types.INTEGER); | ||
} else { | ||
pstmt.setDate(3, new java.sql.Date(System.currentTimeMillis())); | ||
pstmt.setTimestamp(4, new java.sql.Timestamp(System.currentTimeMillis())); | ||
pstmt.setInt(5, i); | ||
} | ||
pstmt.addBatch(); | ||
} | ||
pstmt.executeBatch(); | ||
} | ||
|
||
DatabaseMetaData metaData = statement.getConnection().getMetaData(); | ||
ResultSet columns = metaData.getColumns(null, "default", "low_cardinality_test", "%"); | ||
while (columns.next()) { | ||
String columnName = columns.getString("COLUMN_NAME"); | ||
String columnType = columns.getString("TYPE_NAME"); | ||
switch (columnName) { | ||
case "value_string": | ||
assertEquals(columnType, "LowCardinality(String)"); | ||
break; | ||
case "fixed_string": | ||
assertEquals(columnType, "LowCardinality(FixedString(10))"); | ||
break; | ||
case "date_value": | ||
assertEquals(columnType, "LowCardinality(Nullable(Date))"); | ||
break; | ||
case "datetime_value": | ||
assertEquals(columnType, "LowCardinality(Nullable(DateTime))"); | ||
break; | ||
case "number_value": | ||
assertEquals(columnType, "LowCardinality(Nullable(Int32))"); | ||
break; | ||
} | ||
} | ||
|
||
ResultSet rs = statement.executeQuery("SELECT * FROM low_cardinality_test;"); | ||
int size = 0; | ||
while (rs.next()) { | ||
String valueStr = rs.getString("value_string"); | ||
String fixedStr = rs.getString("fixed_string"); | ||
Date dateValue = rs.getDate("date_value"); | ||
Timestamp datetimeValue = rs.getTimestamp("datetime_value"); | ||
Integer numberValue = (Integer) rs.getObject("number_value"); | ||
|
||
assertEquals("test", valueStr); | ||
assertEquals("abcdefghij", fixedStr); | ||
if (size % 50 == 0) { | ||
assertNull(dateValue); | ||
assertNull(datetimeValue); | ||
assertNull(numberValue); | ||
} else { | ||
assertNotNull(dateValue); | ||
assertNotNull(datetimeValue); | ||
assertTrue(numberValue >= 0 && numberValue < 300); | ||
} | ||
|
||
size++; | ||
} | ||
assertEquals(300, size); | ||
|
||
statement.execute("DROP TABLE IF EXISTS low_cardinality_test"); | ||
}, "allow_suspicious_low_cardinality_types", "1"); | ||
} | ||
} |