Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,21 @@ public ClickHouseValue newValue(ClickHouseDataConfig config) {
return value;
}


/**
* Returns column effective data type. In case of SimpleAggregateFunction
* returns type of the first column.
*
* @return ClickHouseDataType
*/
public ClickHouseDataType getEffectiveDataType() {
ClickHouseDataType columnDataType = getDataType();
if (columnDataType.equals(ClickHouseDataType.SimpleAggregateFunction)){
columnDataType = getNestedColumns().get(0).getDataType();
}
return columnDataType;
}

@Override
public int hashCode() {
final int prime = 31;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@
import com.clickhouse.client.api.data_formats.internal.BinaryStreamReader;
import com.clickhouse.data.ClickHouseDataType;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
import java.util.Objects;

import static com.clickhouse.client.api.data_formats.internal.BinaryStreamReader.BASES;
Expand Down Expand Up @@ -139,4 +146,77 @@ public static Instant instantFromTime64Integer(int precision, long value) {

return Instant.ofEpochSecond(value, nanoSeconds);
}

public static LocalDateTime localTimeFromTime64Integer(int precision, long value) {
int nanoSeconds = 0;
if (precision > 0) {
int factor = BinaryStreamReader.BASES[precision];
nanoSeconds = Math.abs((int) (value % factor)); // nanoseconds are stored separately and only positive values accepted
value /= factor;

if (nanoSeconds > 0L) {
nanoSeconds *= BASES[9 - precision];
}

}

return LocalDateTime.ofEpochSecond(value, nanoSeconds, ZoneOffset.UTC);
}

/**
* Converts a {@link Duration} to a time string in the format {@code [-]HH:mm:ss[.nnnnnnnnn]}.
* <p>
* Unlike standard time formats, hours can exceed 24 and can be negative.
* The precision parameter controls the number of fractional second digits (0-9).
*
* @param duration the duration to convert
* @param precision the number of fractional second digits (0-9)
* @return a string representation like {@code -999:59:59.123456789}
* @throws NullPointerException if {@code duration} is null
*/
public static String durationToTimeString(Duration duration, int precision) {
Objects.requireNonNull(duration, "Duration required for durationToTimeString");

boolean negative = duration.isNegative();
if (negative) {
duration = duration.negated();
}

long totalSeconds = duration.getSeconds();
int nanos = duration.getNano();

long hours = totalSeconds / 3600;
int minutes = (int) ((totalSeconds % 3600) / 60);
int seconds = (int) (totalSeconds % 60);

StringBuilder sb = new StringBuilder();
if (negative) {
sb.append('-');
}
sb.append(hours);
sb.append(':');
if (minutes < 10) {
sb.append('0');
}
sb.append(minutes);
sb.append(':');
if (seconds < 10) {
sb.append('0');
}
sb.append(seconds);

if (precision > 0 && precision <= 9) {
sb.append('.');
// Format nanos with leading zeros, then truncate to precision
String nanosStr = String.format("%09d", nanos);
sb.append(nanosStr, 0, precision);
}

return sb.toString();
}

public static Duration localDateTimeToDuration(LocalDateTime localDateTime) {
return Duration.ofSeconds(localDateTime.toEpochSecond(ZoneOffset.UTC))
.plusNanos(localDateTime.getNano());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.time.temporal.TemporalAmount;
Expand Down Expand Up @@ -551,6 +552,10 @@ public interface ClickHouseBinaryFormatReader extends AutoCloseable {

LocalDate getLocalDate(int index);

LocalTime getLocalTime(String colName);

LocalTime getLocalTime(int index);

LocalDateTime getLocalDateTime(String colName);

LocalDateTime getLocalDateTime(int index);
Expand Down
Loading
Loading