Skip to content

Commit

Permalink
Clean up code and enhance enum types
Browse files Browse the repository at this point in the history
  • Loading branch information
zhicwu committed Apr 16, 2022
1 parent 5cd7e0b commit c909574
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ public final class ClickHouseColumn implements Serializable {
private static final String KEYWORD_MAP = ClickHouseDataType.Map.name();
private static final String KEYWORD_NESTED = ClickHouseDataType.Nested.name();

private String originalTypeName;
private int columnCount;
private int columnIndex;
private String columnName;
private String originalTypeName;

private ClickHouseAggregateFunction aggFuncType;
private ClickHouseDataType dataType;
Expand Down Expand Up @@ -449,6 +451,8 @@ private ClickHouseColumn(ClickHouseDataType dataType, String columnName, String
this.aggFuncType = null;
this.dataType = ClickHouseChecker.nonNull(dataType, "dataType");

this.columnCount = 1;
this.columnIndex = 0;
this.columnName = columnName == null ? "" : columnName;
this.originalTypeName = originalTypeName == null ? dataType.name() : originalTypeName;
this.nullable = nullable;
Expand All @@ -474,8 +478,18 @@ private ClickHouseColumn(ClickHouseDataType dataType, String columnName, String
this.estimatedByteLength = 0;
}

/**
* Sets zero-based column index and column count.
*
* @param index zero-based column index, negative number is treated as zero
* @param count column count, should be always greater than one
*/
protected void setColumnIndex(int index, int count) {
this.columnCount = count < 2 ? 1 : count;
this.columnIndex = index < 1 ? 0 : (index < count ? index : count - 1);
}

public boolean isAggregateFunction() {
// || dataType == ClickHouseDataType.SimpleAggregateFunction;
return dataType == ClickHouseDataType.AggregateFunction;

}
Expand Down Expand Up @@ -533,14 +547,30 @@ public int getEstimatedLength() {
return estimatedByteLength;
}

public String getOriginalTypeName() {
return originalTypeName;
public int getColumnCount() {
return columnCount;
}

public int getColumnIndex() {
return columnIndex;
}

public String getColumnName() {
return columnName;
}

public String getOriginalTypeName() {
return originalTypeName;
}

public boolean isFirstColumn() {
return columnCount == 0;
}

public boolean isLastColumn() {
return columnCount - columnIndex == 1;
}

public boolean isNullable() {
return nullable;
}
Expand Down Expand Up @@ -616,12 +646,14 @@ public int hashCode() {
result = prime * result + ((arrayBaseColumn == null) ? 0 : arrayBaseColumn.hashCode());
result = prime * result + ((aggFuncType == null) ? 0 : aggFuncType.hashCode());
result = prime * result + arrayLevel;
result = prime * result + columnCount;
result = prime * result + columnIndex;
result = prime * result + ((columnName == null) ? 0 : columnName.hashCode());
result = prime * result + ((originalTypeName == null) ? 0 : originalTypeName.hashCode());
result = prime * result + ((dataType == null) ? 0 : dataType.hashCode());
result = prime * result + (lowCardinality ? 1231 : 1237);
result = prime * result + ((nested == null) ? 0 : nested.hashCode());
result = prime * result + (nullable ? 1231 : 1237);
result = prime * result + ((originalTypeName == null) ? 0 : originalTypeName.hashCode());
result = prime * result + ((parameters == null) ? 0 : parameters.hashCode());
result = prime * result + precision;
result = prime * result + scale;
Expand All @@ -643,7 +675,8 @@ public boolean equals(Object obj) {

ClickHouseColumn other = (ClickHouseColumn) obj;
return Objects.equals(arrayBaseColumn, other.arrayBaseColumn) && aggFuncType == other.aggFuncType
&& arrayLevel == other.arrayLevel && Objects.equals(columnName, other.columnName)
&& arrayLevel == other.arrayLevel && columnCount == other.columnCount
&& columnIndex == other.columnIndex && Objects.equals(columnName, other.columnName)
&& dataType == other.dataType && lowCardinality == other.lowCardinality
&& Objects.equals(nested, other.nested) && nullable == other.nullable
&& Objects.equals(originalTypeName, other.originalTypeName)
Expand All @@ -654,7 +687,12 @@ public boolean equals(Object obj) {

@Override
public String toString() {
return new StringBuilder().append(columnName == null || columnName.isEmpty() ? "column" : columnName)
.append(' ').append(originalTypeName).toString();
StringBuilder builder = new StringBuilder();
if (columnName == null || columnName.isEmpty()) {
builder.append("column").append(columnIndex);
} else {
builder.append(columnName);
}
return builder.append(' ').append(originalTypeName).toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@
* Supported compression algoritms.
*/
public enum ClickHouseCompression {
NONE("", "", ""), BROTLI("application/x-brotli", "br", "br"), DEFLATE("application/deflate", "deflate", "zz"),
GZIP("application/gzip", "gzip", "gz"), LZ4("application/x-lz4", "lz4", "lz4"),
ZIP("application/zip", "zip", "zip"), ZSTD("application/zstd", "zstd", "zst");
NONE("", "", ""),
BROTLI("application/x-brotli", "br", "br"),
BZ2("application/x-bzip2", "bz2", "bz2"),
DEFLATE("application/deflate", "deflate", "zz"),
GZIP("application/gzip", "gzip", "gz"),
LZMA("application/x-lzma", "lzma", "xz"),
LZ4("application/x-lz4", "lz4", "lz4"),
ZIP("application/zip", "zip", "zip"),
ZSTD("application/zstd", "zstd", "zst");

private String mimeType;
private String encoding;
Expand Down Expand Up @@ -82,8 +88,8 @@ public static ClickHouseCompression fromEncoding(String encoding) {
public static ClickHouseCompression fromFileName(String file) {
ClickHouseCompression compression = NONE;

int index = file == null ? -1 : file.lastIndexOf('.');
if (index > 0) {
int index = 0;
if (file != null && (index = file.lastIndexOf('.')) > 0) {
String ext = file.substring(index + 1).toLowerCase();
for (ClickHouseCompression c : values()) {
if (c.fileExt.equals(ext)) {
Expand Down
Loading

0 comments on commit c909574

Please sign in to comment.