Skip to content
Merged
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 @@ -5,6 +5,7 @@
import com.clickhouse.client.api.enums.SSLMode;
import com.clickhouse.client.api.internal.CompressedBlockOutputStream;
import com.clickhouse.client.api.internal.ConfigDescription;
import com.clickhouse.client.api.internal.ValidationUtils;
import com.clickhouse.client.api.internal.WorkloadIdentitySupport;
import com.clickhouse.data.ClickHouseDataType;
import com.clickhouse.data.ClickHouseFormat;
Expand Down Expand Up @@ -491,7 +492,7 @@ public Object parseValue(String value) {
}

if (valueType.equals(TimeZone.class)) {
return TimeZone.getTimeZone(value);
return ValidationUtils.toTimeZone(value, key);
Comment thread
chernser marked this conversation as resolved.
Resolved
}

if (valueType.equals(Map.class)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.clickhouse.client.api.internal;

import java.time.DateTimeException;
import java.time.Instant;
import java.time.ZoneId;
import java.time.zone.ZoneRules;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.TimeZone;

public class ValidationUtils {

Expand Down Expand Up @@ -45,6 +50,43 @@ public static void checkValueFromSet(Object value, String name, Set<?> validValu
}
}

/**
* Converts a time zone ID to a {@link TimeZone}. Unlike {@link TimeZone#getTimeZone(String)}, which returns
* GMT for an ID that it does not recognise, this method throws an exception for an unknown or malformed ID.
* IDs are validated by {@link ZoneId#of(String)}.
*
* @param timeZoneId - time zone ID, for example {@code Europe/Berlin}, {@code UTC} or {@code +05:30}
* @param name - name of the setting, used in the error message
* @return time zone for the ID
* @throws SettingsValidationException if the ID is not a valid time zone ID, or if it is a fixed offset
* with seconds, for example {@code +05:30:45} (on Java 17 and earlier,
* {@link TimeZone#getTimeZone(ZoneId)} returns GMT for it)
*/
public static TimeZone toTimeZone(String timeZoneId, String name) {
ZoneId zoneId;
try {
zoneId = ZoneId.of(timeZoneId);
} catch (DateTimeException e) {
SettingsValidationException error = new SettingsValidationException(name,
"\"" + name + "\" must be a valid time zone ID, but was '" + timeZoneId + "'");
error.initCause(e);
throw error;
}
String id = zoneId.getId();
if (id.startsWith("UTC+") || id.startsWith("UTC-") || id.startsWith("UT+") || id.startsWith("UT-")) {
// TimeZone does not recognise these offset IDs and returns GMT for them. Use the offset itself.
zoneId = zoneId.normalized();
}
ZoneRules rules = zoneId.getRules();
if (rules.isFixedOffset() && rules.getOffset(Instant.EPOCH).getTotalSeconds() % 60 != 0) {
// On Java 17 and earlier, TimeZone.getTimeZone returns GMT for an offset with seconds. Reject such
// offsets on every Java version, so that the result does not depend on the runtime.
throw new SettingsValidationException(name, "\"" + name
+ "\" must be a time zone ID with an offset in whole minutes, but was '" + timeZoneId + "'");
}
return TimeZone.getTimeZone(zoneId);
}

/**
* Creates a unmodifiable set from the given values.
* @param values
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ public QuerySettings setUseTimeZone(String timeZone) {
throw new ValidationUtils.SettingsValidationException(ClientConfigProperties.USE_TIMEZONE.getKey(),
"Cannot set both use_time_zone and use_server_time_zone");
}
settings.setOption(ClientConfigProperties.USE_TIMEZONE.getKey(), TimeZone.getTimeZone(timeZone));
settings.setOption(ClientConfigProperties.USE_TIMEZONE.getKey(),
ValidationUtils.toTimeZone(timeZone, ClientConfigProperties.USE_TIMEZONE.getKey()));
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.clickhouse.client.api.internal.SslContextProvider;
import com.clickhouse.client.api.query.QuerySettings;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import javax.net.ssl.SSLContext;
Expand All @@ -20,6 +21,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;

@Test(groups = {"unit"})
Expand Down Expand Up @@ -238,6 +240,50 @@ void testQuerySettingsSpecific() throws Exception {
}
}

@DataProvider(name = "validTimeZoneIds")
public static Object[][] validTimeZoneIds() {
return new Object[][]{
// time zone ID -> expected TimeZone ID
{"America/Los_Angeles", "America/Los_Angeles"},
{"UTC", "UTC"},
{"GMT", "GMT"},
{"Etc/GMT+5", "Etc/GMT+5"},
{"Africa/Monrovia", "Africa/Monrovia"},
{"GMT+05:30", "GMT+05:30"},
{"+05:30", "GMT+05:30"},
{"+05:30:00", "GMT+05:30"},
{"-09:30", "GMT-09:30"},
{"UTC+05:30", "GMT+05:30"},
{"UT-03:00", "GMT-03:00"},
};
}

@Test(dataProvider = "validTimeZoneIds")
void testQuerySettingsUseTimeZone(String timeZoneId, String expectedId) {
QuerySettings settings = new QuerySettings().setUseTimeZone(timeZoneId);
TimeZone timeZone = (TimeZone) settings.getAllSettings().get(ClientConfigProperties.USE_TIMEZONE.getKey());
Assert.assertEquals(timeZone.getID(), expectedId);
Assert.assertEquals(timeZone, TimeZone.getTimeZone(expectedId));
}

@DataProvider(name = "invalidTimeZoneIds")
public static Object[][] invalidTimeZoneIds() {
return new Object[][]{
{"Mars/Olympus_Mons"}, {"America/Los_Angels"}, {""}, {" UTC"}, {"+25:00"}, {"UTC+5:3"},
{"+05:30:45"}, {"-05:30:45"}, {"UTC+05:30:45"}, {"GMT+05:30:45"},
};
}

@Test(dataProvider = "invalidTimeZoneIds")
void testQuerySettingsUseTimeZoneRejectsInvalidId(String timeZoneId) {
QuerySettings settings = new QuerySettings();
IllegalArgumentException e = Assert.expectThrows(IllegalArgumentException.class,
() -> settings.setUseTimeZone(timeZoneId));
Assert.assertTrue(e.getMessage().contains(ClientConfigProperties.USE_TIMEZONE.getKey()), e.getMessage());
Assert.assertTrue(e.getMessage().contains("'" + timeZoneId + "'"), e.getMessage());
Assert.assertFalse(settings.getAllSettings().containsKey(ClientConfigProperties.USE_TIMEZONE.getKey()));
}

@Test
public void testInsertSettingsSpecific() throws Exception {
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;


public class ClientConfigPropertiesTest {
Expand Down Expand Up @@ -133,6 +134,23 @@ public void testFromKeyValuePairsRoundTrip() {
Assert.assertEquals(new ArrayList<>(parsed.entrySet()), new ArrayList<>(pairs.entrySet()));
}

@DataProvider(name = "timeZoneProperties")
public static Object[][] timeZoneProperties() {
return new Object[][]{
{ClientConfigProperties.USE_TIMEZONE}, {ClientConfigProperties.SERVER_TIMEZONE},
};
}

@Test(groups = {"unit"}, dataProvider = "timeZoneProperties")
public void testTimeZoneParsing(ClientConfigProperties property) {
Assert.assertEquals(property.parseValue("America/Los_Angeles"), TimeZone.getTimeZone("America/Los_Angeles"));
Assert.assertEquals(property.parseValue("UTC+05:30"), TimeZone.getTimeZone("GMT+05:30"));
IllegalArgumentException e = Assert.expectThrows(IllegalArgumentException.class,
() -> property.parseValue("Mars/Olympus_Mons"));
Assert.assertTrue(e.getMessage().contains(property.getKey()), e.getMessage());
Assert.assertTrue(e.getMessage().contains("'Mars/Olympus_Mons'"), e.getMessage());
}

@Test(groups = {"unit"})
public void testDescriptions() {
Map<ClientConfigProperties, String> descriptions = ClientConfigPropertiesDescriptions.getDescriptions();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
Expand Down Expand Up @@ -2047,6 +2048,37 @@ public void testClientUseOwnTimeZone() {
}
}

@DataProvider(name = "useTimeZones")
public static Object[][] useTimeZones() {
return new Object[][]{
// time zone ID -> epoch 0 as local date-time in that time zone
{"Europe/Berlin", LocalDateTime.of(1970, 1, 1, 1, 0)},
{"UTC+05:30", LocalDateTime.of(1970, 1, 1, 5, 30)},
{"+05:30", LocalDateTime.of(1970, 1, 1, 5, 30)},
};
}

@Test(groups = {"integration"}, dataProvider = "useTimeZones")
public void testUseTimeZone(String timeZoneId, LocalDateTime expected) throws Exception {
try (Client client = newClient().useTimeZone(timeZoneId).build()) {
assertEpochReadAs(client, new QuerySettings(), expected);
}
try (Client client = newClient().useTimeZone("UTC").build()) {
assertEpochReadAs(client, new QuerySettings().setUseTimeZone(timeZoneId), expected);
}
}

private static void assertEpochReadAs(Client client, QuerySettings settings, LocalDateTime expected)
throws Exception {
try (QueryResponse response = client.query("SELECT toDateTime(0) AS t", settings).get(10, TimeUnit.SECONDS)) {
ClickHouseBinaryFormatReader reader = client.newBinaryFormatReader(response);
Assert.assertNotNull(reader.next());
ZonedDateTime t = reader.getZonedDateTime("t");
Assert.assertEquals(t.toInstant(), Instant.EPOCH);
Assert.assertEquals(t.toLocalDateTime(), expected);
}
}

@Test
public void testAsyncQuery() {
try (Client client = newClient().useAsyncRequests(true).build()){
Expand Down
Loading
Loading