Skip to content

Commit

Permalink
Catalog granularity accepts query format (#16680)
Browse files Browse the repository at this point in the history
Previously, the segment granularity for tables in the catalog had to be defined in period format, ie `'PT1H'` , `'P1D'`, etc. This disallows a user from defining segment granularity of `'ALL'` for a table in the catalog, which may be a valid use case. This change makes it so that a user may define the segment granularity of a table in the catalog, as any string that results in a valid granularity using either the `Granularity.fromString(str)` method, or `new PeriodGranularity(new Period(value), null, null)`, and that granularity maps to a standard supported granularity, where `GranularityType.isStandard(granularity)` returns true. As a result a user may who wants to assign a catalog table's segment granularity to be hourly, may assign the segment granularity property of the table to be either `PT1H`, or `HOUR`. These are the same formats accepted at query time.
  • Loading branch information
zachjsh authored Jul 2, 2024
1 parent bd49ecf commit 5e05858
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void testInsertImplicitCast() throws Exception
{
String queryFile = "/catalog/implicitCast_select.sql";
String tableName = "testImplicitCast" + operationName;
TableMetadata table = TableBuilder.datasource(tableName, "P1D")
TableMetadata table = TableBuilder.datasource(tableName, "DAY")
.column(Columns.TIME_COLUMN, Columns.LONG)
.column("double_col1", "DOUBLE")
.build();
Expand Down Expand Up @@ -179,7 +179,7 @@ public void testInsertWithClusteringFromCatalog() throws Exception
{
String queryFile = "/catalog/clustering_select.sql";
String tableName = "testWithClusteringFromCatalog" + operationName;
TableMetadata table = TableBuilder.datasource(tableName, "P1D")
TableMetadata table = TableBuilder.datasource(tableName, "ALL")
.column(Columns.TIME_COLUMN, Columns.LONG)
.column("bigint_col1", "BIGINT")
.property(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.apache.druid.java.util.common.granularity.PeriodGranularity;
import org.joda.time.Period;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import java.net.URI;
Expand Down Expand Up @@ -63,17 +64,25 @@ public static List<String> columnNames(List<ColumnSpec> columns)
* For the odd interval, the interval name is also accepted (for the other
* intervals, the interval name is the descriptive string).
*/
public static Granularity asDruidGranularity(String value)
public static Granularity asDruidGranularity(@Nonnull String value)
{
if (Strings.isNullOrEmpty(value) || value.equalsIgnoreCase(DatasourceDefn.ALL_GRANULARITY)) {
if (value.equalsIgnoreCase(DatasourceDefn.ALL_GRANULARITY)) {
return Granularities.ALL;
}
Granularity granularity;
try {
return new PeriodGranularity(new Period(value), null, null);
granularity = Granularity.fromString(value);
}
catch (IllegalArgumentException e) {
throw new IAE(StringUtils.format("'%s' is an invalid period string", value));
try {
granularity = new PeriodGranularity(new Period(value), null, null);
}
catch (IllegalArgumentException e2) {
throw new IAE("[%s] is an invalid granularity string.", value);
}
}

return granularity;
}

/**
Expand Down Expand Up @@ -275,18 +284,12 @@ public static Map<String, Object> mergeProperties(
return merged;
}

public static void validateGranularity(String value)
public static void validateGranularity(final String value)
{
if (value == null) {
return;
}
Granularity granularity;
try {
granularity = new PeriodGranularity(new Period(value), null, null);
}
catch (IllegalArgumentException e) {
throw new IAE(StringUtils.format("[%s] is an invalid granularity string", value));
}
final Granularity granularity = asDruidGranularity(value);
if (!GranularityType.isStandard(granularity)) {
throw new IAE(
"Unsupported segment graularity. "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import org.apache.druid.java.util.common.logger.Logger;
import org.apache.druid.segment.column.ColumnType;

import javax.annotation.Nullable;

import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -122,6 +124,7 @@ public String segmentGranularityString()
return stringProperty(DatasourceDefn.SEGMENT_GRANULARITY_PROPERTY);
}

@Nullable
public Granularity segmentGranularity()
{
String definedGranularity = segmentGranularityString();
Expand Down

0 comments on commit 5e05858

Please sign in to comment.