Skip to content

Commit

Permalink
Replace PrestoWarning with new client Warning
Browse files Browse the repository at this point in the history
  • Loading branch information
electrum committed Jan 27, 2019
1 parent d5dfb31 commit 904d511
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
package io.prestosql.cli;

import io.prestosql.spi.PrestoWarning;
import io.prestosql.client.Warning;

import java.util.List;
import java.util.OptionalInt;
Expand All @@ -38,7 +38,7 @@ abstract class AbstractWarningsPrinter
this.maxWarnings = requireNonNull(maxWarnings, "maxWarnings is null");
}

private String getWarningMessage(PrestoWarning warning)
private String getWarningMessage(Warning warning)
{
// If this is a real terminal color the warnings yellow
if (REAL_TERMINAL) {
Expand All @@ -47,7 +47,7 @@ private String getWarningMessage(PrestoWarning warning)
return format("WARNING: %s", warning.getMessage());
}

private List<String> getNewWarnings(List<PrestoWarning> warnings)
private List<String> getNewWarnings(List<Warning> warnings)
{
int end = warnings.size();
if (maxWarnings.isPresent()) {
Expand Down Expand Up @@ -94,7 +94,7 @@ private void printWithTrailingSeparator(List<String> warnings)
}

@Override
public void print(List<PrestoWarning> warnings, boolean withInitialSeparator, boolean withTrailingSeparator)
public void print(List<Warning> warnings, boolean withInitialSeparator, boolean withTrailingSeparator)
{
requireNonNull(warnings, "warnings is null");
List<String> newWarnings = getNewWarnings(warnings);
Expand Down
4 changes: 2 additions & 2 deletions presto-cli/src/main/java/io/prestosql/cli/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import io.prestosql.client.QueryError;
import io.prestosql.client.QueryStatusInfo;
import io.prestosql.client.StatementClient;
import io.prestosql.spi.PrestoWarning;
import io.prestosql.client.Warning;
import org.fusesource.jansi.Ansi;
import sun.misc.Signal;
import sun.misc.SignalHandler;
Expand Down Expand Up @@ -194,7 +194,7 @@ private void processInitialStatusUpdates(WarningsPrinter warningsPrinter)
warningsPrinter.print(client.currentStatusInfo().getWarnings(), true, false);
client.advance();
}
List<PrestoWarning> warnings;
List<Warning> warnings;
if (client.isRunning()) {
warnings = client.currentStatusInfo().getWarnings();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
*/
package io.prestosql.cli;

import io.prestosql.spi.PrestoWarning;
import io.prestosql.client.Warning;

import java.util.List;

public interface WarningsPrinter
{
void print(List<PrestoWarning> warnings, boolean withInitialSeparator, boolean withTrailingSeparator);
void print(List<Warning> warnings, boolean withInitialSeparator, boolean withTrailingSeparator);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.ImmutableList;
import io.prestosql.spi.PrestoWarning;

import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
Expand All @@ -43,7 +42,7 @@ public class QueryResults
private final Iterable<List<Object>> data;
private final StatementStats stats;
private final QueryError error;
private final List<PrestoWarning> warnings;
private final List<Warning> warnings;
private final String updateType;
private final Long updateCount;

Expand All @@ -57,7 +56,7 @@ public QueryResults(
@JsonProperty("data") List<List<Object>> data,
@JsonProperty("stats") StatementStats stats,
@JsonProperty("error") QueryError error,
@JsonProperty("warnings") List<PrestoWarning> warnings,
@JsonProperty("warnings") List<Warning> warnings,
@JsonProperty("updateType") String updateType,
@JsonProperty("updateCount") Long updateCount)
{
Expand All @@ -84,7 +83,7 @@ public QueryResults(
Iterable<List<Object>> data,
StatementStats stats,
QueryError error,
List<PrestoWarning> warnings,
List<Warning> warnings,
String updateType,
Long updateCount)
{
Expand Down Expand Up @@ -165,7 +164,7 @@ public QueryError getError()

@JsonProperty
@Override
public List<PrestoWarning> getWarnings()
public List<Warning> getWarnings()
{
return warnings;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
*/
package io.prestosql.client;

import io.prestosql.spi.PrestoWarning;

import java.net.URI;
import java.util.List;

Expand All @@ -34,7 +32,7 @@ public interface QueryStatusInfo

QueryError getError();

List<PrestoWarning> getWarnings();
List<Warning> getWarnings();

String getUpdateType();

Expand Down
128 changes: 128 additions & 0 deletions presto-client/src/main/java/io/prestosql/client/Warning.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.prestosql.client;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.Objects;

import static java.lang.String.format;
import static java.util.Objects.requireNonNull;

public final class Warning
{
private final Code warningCode;
private final String message;

@JsonCreator
public Warning(
@JsonProperty("warningCode") Code warningCode,
@JsonProperty("message") String message)
{
this.warningCode = requireNonNull(warningCode, "warningCode is null");
this.message = requireNonNull(message, "message is null");
}

@JsonProperty
public Code getWarningCode()
{
return warningCode;
}

@JsonProperty
public String getMessage()
{
return message;
}

@Override
public boolean equals(Object obj)
{
if (this == obj) {
return true;
}
if ((obj == null) || (getClass() != obj.getClass())) {
return false;
}
Warning that = (Warning) obj;
return Objects.equals(warningCode, that.warningCode) &&
Objects.equals(message, that.message);
}

@Override
public int hashCode()
{
return Objects.hash(warningCode, message);
}

@Override
public String toString()
{
return format("%s, %s", warningCode, message);
}

public static final class Code
{
private final int code;
private final String name;

@JsonCreator
public Code(
@JsonProperty("code") int code,
@JsonProperty("name") String name)
{
this.code = code;
this.name = requireNonNull(name, "name is null");
}

@JsonProperty
public int getCode()
{
return code;
}

@JsonProperty
public String getName()
{
return name;
}

@Override
public boolean equals(Object obj)
{
if (this == obj) {
return true;
}
if ((obj == null) || (getClass() != obj.getClass())) {
return false;
}
Code other = (Code) obj;
return (code == other.code) &&
Objects.equals(name, other.name);
}

@Override
public int hashCode()
{
return Objects.hash(code, name);
}

@Override
public String toString()
{
return name + ":" + code;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,14 @@
*/
package io.prestosql.jdbc;

import io.prestosql.spi.PrestoWarning;
import io.prestosql.client.Warning;

import java.sql.SQLWarning;

public class PrestoSqlWarning
extends SQLWarning
{
public PrestoSqlWarning()
{
super();
}

public PrestoSqlWarning(PrestoWarning warning)
public PrestoSqlWarning(Warning warning)
{
//TODO: enforce that sqlState is 01[5,6,7,8,9,I-Z][0-9A-Z]{3}
// From the SQL Standard ISO_IEC_9075-2E_2016 24.1 SQLState: warning codes have class 01
Expand Down
11 changes: 5 additions & 6 deletions presto-jdbc/src/main/java/io/prestosql/jdbc/WarningsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
*/
package io.prestosql.jdbc;

import io.prestosql.spi.PrestoWarning;
import io.prestosql.spi.WarningCode;
import io.prestosql.client.Warning;

import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.ThreadSafe;
Expand All @@ -30,7 +29,7 @@
public class WarningsManager
{
@GuardedBy("this")
private Set<WarningCode> warningsSeen = new HashSet<>();
private final Set<Warning.Code> warningsSeen = new HashSet<>();

@GuardedBy("this")
private SQLWarning firstWarning;
Expand All @@ -39,7 +38,7 @@ public class WarningsManager
@GuardedBy("this")
private SQLWarning lastWarning;

private synchronized void addWarning(PrestoWarning warning)
private synchronized void addWarning(Warning warning)
{
requireNonNull(warning, "warning is null");
if (lastWarning == null) {
Expand All @@ -56,9 +55,9 @@ private synchronized void addWarning(PrestoWarning warning)
}
}

public synchronized void addWarnings(List<PrestoWarning> warnings)
public synchronized void addWarnings(List<Warning> warnings)
{
for (PrestoWarning warning : warnings) {
for (Warning warning : warnings) {
if (warningsSeen.add(warning.getWarningCode())) {
addWarning(warning);
}
Expand Down
24 changes: 18 additions & 6 deletions presto-jdbc/src/test/java/io/prestosql/jdbc/TestJdbcWarnings.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterators;
import com.google.common.util.concurrent.AbstractFuture;
import io.prestosql.client.Warning;
import io.prestosql.execution.QueryInfo;
import io.prestosql.execution.warnings.WarningCollectorConfig;
import io.prestosql.plugin.blackhole.BlackHolePlugin;
Expand Down Expand Up @@ -211,7 +212,7 @@ public void testExecuteQueryWarnings()
TestingWarningCollector warningCollector = new TestingWarningCollector(new WarningCollectorConfig(), warningCollectorConfig);
List<PrestoWarning> expectedWarnings = warningCollector.getWarnings();
for (PrestoWarning prestoWarning : expectedWarnings) {
assertTrue(currentWarnings.contains(new WarningEntry(new PrestoSqlWarning(prestoWarning))));
assertTrue(currentWarnings.contains(new WarningEntry(toPrestoSqlWarning(prestoWarning))));
}
}
}
Expand All @@ -226,25 +227,36 @@ public void testSqlWarning()
List<PrestoWarning> warnings = builder.build();
SQLWarning warning = fromPrestoWarnings(warnings);
assertEquals(Iterators.size(warning.iterator()), warnings.size());
assertWarningsEqual(warning, new PrestoSqlWarning(warnings.get(0)));
assertWarningsEqual(warning.getNextWarning(), new PrestoSqlWarning(warnings.get(1)));
assertWarningsEqual(warning.getNextWarning().getNextWarning(), new PrestoSqlWarning(warnings.get(2)));
assertWarningsEqual(warning, toPrestoSqlWarning(warnings.get(0)));
assertWarningsEqual(warning.getNextWarning(), toPrestoSqlWarning(warnings.get(1)));
assertWarningsEqual(warning.getNextWarning().getNextWarning(), toPrestoSqlWarning(warnings.get(2)));
}

private static SQLWarning fromPrestoWarnings(List<PrestoWarning> warnings)
{
requireNonNull(warnings, "warnings is null");
assertFalse(warnings.isEmpty());
Iterator<PrestoWarning> iterator = warnings.iterator();
PrestoSqlWarning first = new PrestoSqlWarning(iterator.next());
PrestoSqlWarning first = toPrestoSqlWarning(iterator.next());
SQLWarning current = first;
while (iterator.hasNext()) {
current.setNextWarning(new PrestoSqlWarning(iterator.next()));
current.setNextWarning(toPrestoSqlWarning(iterator.next()));
current = current.getNextWarning();
}
return first;
}

private static PrestoSqlWarning toPrestoSqlWarning(PrestoWarning warning)
{
return new PrestoSqlWarning(toClientWarning(warning));
}

private static Warning toClientWarning(PrestoWarning warning)
{
WarningCode code = warning.getWarningCode();
return new Warning(new Warning.Code(code.getCode(), code.getName()), warning.getMessage());
}

private static void assertWarningsEqual(SQLWarning actual, SQLWarning expected)
{
assertEquals(actual.getMessage(), expected.getMessage());
Expand Down
Loading

0 comments on commit 904d511

Please sign in to comment.