Skip to content

Commit

Permalink
Fix SQL Innterval.of() error message
Browse files Browse the repository at this point in the history
  • Loading branch information
rash67 committed Jan 16, 2024
1 parent 18d2a89 commit ae27630
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,7 +40,12 @@ public static Interval utc(long startInstant, long endInstant)

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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -78,4 +79,11 @@ public void testFindOverlappingInterval()
);
}

@Test
public void testInvalidInterval()
{
DruidExceptionMatcher.invalidInput().assertThrowsAndMatches(
() -> Intervals.of("invalid string")
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -697,25 +697,6 @@ public void testMarkAsUsedNonOvershadowedSegmentsInInterval() throws IOException
);
}

@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
{
Expand Down Expand Up @@ -833,25 +814,6 @@ public void testMarkAsUnusedSegmentsInInterval() throws IOException
);
}

@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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
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;
Expand Down Expand Up @@ -611,19 +612,9 @@ public void testMarkAsUnusedAllSegmentsInDataSourceBadRequest()
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)

Check notice

Code scanning / CodeQL

Deprecated method or constructor invocation Note test

Invoking
DataSourcesResource.markAsUnusedAllSegmentsOrKillUnusedSegmentsInInterval
should be avoided because it has been deprecated.
);
}

@Test
Expand Down

0 comments on commit ae27630

Please sign in to comment.