Fix SQL Innterval.of() error message (#15454)

Better error message for poorly constructed intervals
This commit is contained in:
Sam Rash 2024-01-15 20:34:35 -08:00 committed by GitHub
parent d359fb3d68
commit 072b16c6df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 52 deletions

View File

@ -20,6 +20,7 @@
package org.apache.druid.java.util.common;
import com.google.common.collect.ImmutableList;
import org.apache.druid.error.InvalidInput;
import org.apache.druid.java.util.common.guava.Comparators;
import org.joda.time.DateTime;
import org.joda.time.Interval;
@ -39,7 +40,12 @@ public final class Intervals
public static Interval of(String interval)
{
return new Interval(interval, ISOChronology.getInstanceUTC());
try {
return new Interval(interval, ISOChronology.getInstanceUTC());
}
catch (IllegalArgumentException e) {
throw InvalidInput.exception(e, "Bad Interval[%s]: [%s]", interval, e.getMessage());
}
}
public static Interval of(String format, Object... formatArgs)

View File

@ -19,6 +19,7 @@
package org.apache.druid.java.util.common;
import org.apache.druid.error.DruidExceptionMatcher;
import org.apache.druid.java.util.common.guava.Comparators;
import org.joda.time.Interval;
import org.junit.Assert;
@ -78,4 +79,11 @@ public class IntervalsTest
);
}
@Test
public void testInvalidInterval()
{
DruidExceptionMatcher.invalidInput().assertThrowsAndMatches(
() -> Intervals.of("invalid string")
);
}
}

View File

@ -697,25 +697,6 @@ public class SqlSegmentsMetadataManagerTest
);
}
@Test(expected = IllegalArgumentException.class)
public void testMarkAsUsedNonOvershadowedSegmentsInIntervalWithInvalidInterval() throws IOException
{
sqlSegmentsMetadataManager.startPollingDatabasePeriodically();
sqlSegmentsMetadataManager.poll();
Assert.assertTrue(sqlSegmentsMetadataManager.isPollingDatabasePeriodically());
final String newDataSource = "wikipedia2";
final DataSegment newSegment1 = createNewSegment1(newDataSource);
final DataSegment newSegment2 = createNewSegment2(newDataSource);
publish(newSegment1, false);
publish(newSegment2, false);
// invalid interval start > end
final Interval theInterval = Intervals.of("2017-10-22T00:00:00.000/2017-10-02T00:00:00.000");
sqlSegmentsMetadataManager.markAsUsedNonOvershadowedSegmentsInInterval(newDataSource, theInterval);
}
@Test
public void testMarkAsUsedNonOvershadowedSegmentsInIntervalWithOverlappingInterval() throws IOException
{
@ -833,25 +814,6 @@ public class SqlSegmentsMetadataManagerTest
);
}
@Test(expected = IllegalArgumentException.class)
public void testMarkAsUnusedSegmentsInIntervalWithInvalidInterval() throws IOException
{
sqlSegmentsMetadataManager.startPollingDatabasePeriodically();
sqlSegmentsMetadataManager.poll();
Assert.assertTrue(sqlSegmentsMetadataManager.isPollingDatabasePeriodically());
final String newDataSource = "wikipedia2";
final DataSegment newSegment1 = createNewSegment1(newDataSource);
final DataSegment newSegment2 = createNewSegment2(newDataSource);
publisher.publishSegment(newSegment1);
publisher.publishSegment(newSegment2);
// invalid interval start > end
final Interval theInterval = Intervals.of("2017-10-22T00:00:00.000/2017-10-02T00:00:00.000");
sqlSegmentsMetadataManager.markAsUnusedSegmentsInInterval(newDataSource, theInterval);
}
@Test
public void testMarkAsUnusedSegmentsInIntervalWithOverlappingInterval() throws IOException
{

View File

@ -35,6 +35,7 @@ import org.apache.druid.client.DruidServer;
import org.apache.druid.client.ImmutableDruidDataSource;
import org.apache.druid.client.ImmutableSegmentLoadInfo;
import org.apache.druid.client.SegmentLoadInfo;
import org.apache.druid.error.DruidExceptionMatcher;
import org.apache.druid.java.util.common.Intervals;
import org.apache.druid.metadata.MetadataRuleManager;
import org.apache.druid.metadata.SegmentsMetadataManager;
@ -611,19 +612,9 @@ public class DataSourcesResourceTest
EasyMock.replay(overlordClient, server);
DataSourcesResource dataSourcesResource =
new DataSourcesResource(inventoryView, null, null, overlordClient, null, null, auditManager);
try {
Response response =
dataSourcesResource.markAsUnusedAllSegmentsOrKillUnusedSegmentsInInterval("datasource", "true", "???", request);
// 400 (Bad Request) or an IllegalArgumentException is expected.
Assert.assertEquals(400, response.getStatus());
Assert.assertNotNull(response.getEntity());
Assert.assertTrue(response.getEntity().toString().contains("java.lang.IllegalArgumentException"));
}
catch (IllegalArgumentException ignore) {
// expected
}
EasyMock.verify(overlordClient, server);
DruidExceptionMatcher.invalidInput().assertThrowsAndMatches(
() -> dataSourcesResource.markAsUnusedAllSegmentsOrKillUnusedSegmentsInInterval("datasource", "true", "???", request)
);
}
@Test