Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to pass step length in Loki connector. #24862

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion plugin/trino-loki/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
<dependency>
<groupId>io.github.jeschkies</groupId>
<artifactId>loki-client</artifactId>
<version>0.0.2</version>
<version>0.0.3</version>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Separate a commit.

</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ public LokiRecordSet(LokiClient lokiClient, LokiSplit split, List<LokiColumnHand

// Execute the query
try {
this.result = lokiClient.rangeQuery(split.query(), split.start(), split.end());
int step = 0;
if (split.step() != null) {
step = split.step().intValue();
}
this.result = lokiClient.rangeQuery(split.query(), split.start(), split.end(), step);
}
catch (LokiClientException e) {
throw new TrinoException(LOKI_CLIENT_ERROR, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import static java.util.Objects.requireNonNull;

public record LokiSplit(String query, Instant start, Instant end)
public record LokiSplit(String query, Instant start, Instant end, Long step)
implements ConnectorSplit
{
public LokiSplit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public ConnectorSplitSource getSplits(
{
final LokiTableHandle table = (LokiTableHandle) connectorTableHandle;

List<ConnectorSplit> splits = ImmutableList.of(new LokiSplit(table.query(), table.start(), table.end()));
List<ConnectorSplit> splits = ImmutableList.of(new LokiSplit(table.query(), table.start(), table.end(), table.step()));

log.debug("created %d splits", splits.size());
return new FixedSplitSource(splits);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import static java.util.Objects.requireNonNull;

public record LokiTableHandle(String query, Instant start, Instant end, List<ColumnHandle> columnHandles)
public record LokiTableHandle(String query, Instant start, Instant end, Long step, List<ColumnHandle> columnHandles)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change Long to int.

implements ConnectorTableHandle
{
public LokiTableHandle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import static com.google.common.collect.ImmutableList.toImmutableList;
import static io.trino.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT;
import static io.trino.spi.function.table.ReturnTypeSpecification.GenericTable.GENERIC_TABLE;
import static io.trino.spi.type.IntegerType.INTEGER;
import static io.trino.spi.type.TimestampWithTimeZoneType.TIMESTAMP_TZ_NANOS;
import static io.trino.spi.type.Timestamps.MILLISECONDS_PER_SECOND;
import static io.trino.spi.type.Timestamps.NANOSECONDS_PER_MILLISECOND;
Expand Down Expand Up @@ -74,6 +75,11 @@ public QueryRangeTableFunction(LokiMetadata metadata)
ScalarArgumentSpecification.builder()
.name("END")
.type(TIMESTAMP_TZ_NANOS)
.build(),
ScalarArgumentSpecification.builder()
.name("STEP")
.type(INTEGER)
.defaultValue(0L)
.build()),
GENERIC_TABLE);

Expand All @@ -89,6 +95,11 @@ public TableFunctionAnalysis analyze(ConnectorSession session, ConnectorTransact
LongTimestampWithTimeZone startArgument = (LongTimestampWithTimeZone) ((ScalarArgument) arguments.get("START")).getValue();
LongTimestampWithTimeZone endArgument = (LongTimestampWithTimeZone) ((ScalarArgument) arguments.get("END")).getValue();

Long step = (Long) ((ScalarArgument) arguments.get("STEP")).getValue();
if (step == null) {
step = 0L;
}
Comment on lines +98 to +101
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if we specify a negative value in step argument? Please add a test.


if (Strings.isNullOrEmpty(query)) {
throw new TrinoException(INVALID_FUNCTION_ARGUMENT, query);
}
Expand Down Expand Up @@ -118,6 +129,7 @@ public TableFunctionAnalysis analyze(ConnectorSession session, ConnectorTransact
query,
start,
end,
step,
columnHandles);

return TableFunctionAnalysis.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

import static io.trino.type.DateTimes.MILLISECONDS_PER_DAY;
import static java.lang.String.format;

final class TestLokiIntegration
Expand All @@ -37,6 +35,7 @@ final class TestLokiIntegration

private static final DateTimeFormatter timestampFormatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS'Z'").withZone(ZoneOffset.UTC);
private static final DateTimeFormatter timestampFormatterAtEasternTime = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss'-05:00'").withZone(ZoneId.of("US/Eastern"));
private static final DateTimeFormatter isoTimestampFormatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSS'Z'").withZone(ZoneOffset.UTC);

@Override
protected QueryRunner createQueryRunner()
Expand Down Expand Up @@ -155,7 +154,6 @@ void testLabelsComplex()
void testSelectTimestampLogsQuery()
throws Exception
{
DateTimeFormatter isoTimestampFormatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSS'Z'").withZone(ZoneOffset.UTC);
Instant start = Instant.now().truncatedTo(ChronoUnit.DAYS).minus(Duration.ofHours(12));
Instant end = start.plus(Duration.ofHours(4));
Instant firstLineTimestamp = start.truncatedTo(ChronoUnit.MILLIS);
Expand Down Expand Up @@ -184,24 +182,24 @@ void testSelectTimestampLogsQuery()
void testTimestampMetricsQuery()
throws Exception
{
LocalDate baseLineDate = LocalDate.now();
Instant start = Instant.ofEpochMilli(baseLineDate.toEpochDay() * MILLISECONDS_PER_DAY);
Instant end = start.plus(Duration.ofHours(4));
Instant start = Instant.now().truncatedTo(ChronoUnit.HOURS).minus(Duration.ofHours(4));
Instant end = start.plus(Duration.ofHours(3));

this.client.pushLogLine("line 1", start.plus(Duration.ofHours(1)), ImmutableMap.of("test", "timestamp_metrics_query"));
this.client.pushLogLine("line 1", start.plus(Duration.ofMinutes(4)), ImmutableMap.of("test", "timestamp_metrics_query"));
this.client.pushLogLine("line 2", start.plus(Duration.ofHours(2)), ImmutableMap.of("test", "timestamp_metrics_query"));
this.client.pushLogLine("line 3", start.plus(Duration.ofHours(3)), ImmutableMap.of("test", "timestamp_metrics_query"));
this.client.flush();
assertQuery(format("""
SELECT CAST(timestamp AS DATE) FROM
SELECT to_iso8601(timestamp), value FROM
TABLE(system.query_range(
'count_over_time({test="timestamp_metrics_query"}[5m])',
TIMESTAMP '%s',
TIMESTAMP '%s'
TIMESTAMP '%s',
300
))
LIMIT 1
""", timestampFormatter.format(start), timestampFormatter.format(end)),
"VALUES DATE '%s'".formatted(baseLineDate));
"VALUES ('%s', 1.0)".formatted(isoTimestampFormatter.format(start.plus(Duration.ofMinutes(5)))));
}

@Test
Expand Down