Skip to content

Commit

Permalink
Remove ParsingOptions from AnalyzerOptions
Browse files Browse the repository at this point in the history
Usage of the ParsingOptions creates a dependency on the presto-parser
for analyzer interfaces. Presto analyzer interfaces would move to the
presto-spi package and should be independent of the presto-parser.
Hence removing ParsionOptions from the AnalyzerOptions.
  • Loading branch information
jainxrohit committed Jan 4, 2023
1 parent f92c65f commit 001dbd5
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,48 @@
package com.facebook.presto.sql.analyzer;

import com.facebook.presto.common.WarningHandlingLevel;
import com.facebook.presto.sql.parser.ParsingOptions;
import com.facebook.presto.spi.WarningCollector;

import static com.facebook.presto.common.WarningHandlingLevel.NORMAL;
import static java.util.Objects.requireNonNull;

/**
* Various options required at different stage of query analysis.
*/
public class AnalyzerOptions
{
private final ParsingOptions parsingOptions;
private final boolean isParseDecimalLiteralsAsDouble;
private final boolean isLogFormattedQueryEnabled;
private final WarningHandlingLevel warningHandlingLevel;
private final WarningCollector warningCollector;

private AnalyzerOptions(
ParsingOptions parsingOptions,
boolean isParseDecimalLiteralsAsDouble,
boolean isLogFormattedQueryEnabled,
WarningCollector warningCollector,
WarningHandlingLevel warningHandlingLevel)
{
this.parsingOptions = parsingOptions;
this.isParseDecimalLiteralsAsDouble = isParseDecimalLiteralsAsDouble;
this.isLogFormattedQueryEnabled = isLogFormattedQueryEnabled;
this.warningHandlingLevel = warningHandlingLevel;
this.warningCollector = requireNonNull(warningCollector, "warningCollector is null");
this.warningHandlingLevel = requireNonNull(warningHandlingLevel, "warningHandlingLevel is null");
}

public ParsingOptions getParsingOptions()
public boolean isParseDecimalLiteralsAsDouble()
{
return parsingOptions;
return isParseDecimalLiteralsAsDouble;
}

public boolean isLogFormattedQueryEnabled()
{
return isLogFormattedQueryEnabled;
}

public WarningCollector getWarningCollector()
{
return warningCollector;
}

public WarningHandlingLevel getWarningHandlingLevel()
{
return warningHandlingLevel;
Expand All @@ -59,15 +68,16 @@ public static Builder builder()

public static class Builder
{
private ParsingOptions parsingOptions = ParsingOptions.builder().build();
private boolean isParseDecimalLiteralsAsDouble;
private boolean isLogFormattedQueryEnabled;
private WarningCollector warningCollector;
private WarningHandlingLevel warningHandlingLevel = NORMAL;

private Builder() {}

public Builder setParsingOptions(ParsingOptions parsingOptions)
public Builder setParseDecimalLiteralsAsDouble(boolean parseDecimalLiteralsAsDouble)
{
this.parsingOptions = parsingOptions;
isParseDecimalLiteralsAsDouble = parseDecimalLiteralsAsDouble;
return this;
}

Expand All @@ -77,6 +87,12 @@ public Builder setLogFormattedQueryEnabled(boolean logFormattedQueryEnabled)
return this;
}

public Builder setWarningCollector(WarningCollector warningCollector)
{
this.warningCollector = warningCollector;
return this;
}

public Builder setWarningHandlingLevel(WarningHandlingLevel warningHandlingLevel)
{
this.warningHandlingLevel = warningHandlingLevel;
Expand All @@ -85,7 +101,7 @@ public Builder setWarningHandlingLevel(WarningHandlingLevel warningHandlingLevel

public AnalyzerOptions build()
{
return new AnalyzerOptions(parsingOptions, isLogFormattedQueryEnabled, warningHandlingLevel);
return new AnalyzerOptions(isParseDecimalLiteralsAsDouble, isLogFormattedQueryEnabled, warningCollector, warningHandlingLevel);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import static com.facebook.presto.sql.SqlFormatter.formatSql;
import static com.facebook.presto.sql.analyzer.ConstantExpressionVerifier.verifyExpressionIsConstant;
import static com.facebook.presto.sql.analyzer.SemanticErrorCode.INVALID_PARAMETER_USAGE;
import static com.facebook.presto.sql.analyzer.utils.AnalyzerUtil.createParsingOptions;
import static com.facebook.presto.sql.analyzer.utils.ParameterExtractor.getParameterCount;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
Expand All @@ -63,7 +64,7 @@ public BuiltInQueryPreparer(SqlParser sqlParser)
public BuiltInPreparedQuery prepareQuery(AnalyzerOptions analyzerOptions, String query, Map<String, String> preparedStatements, WarningCollector warningCollector)
throws ParsingException, PrestoException, SemanticException
{
Statement wrappedStatement = sqlParser.createStatement(query, analyzerOptions.getParsingOptions());
Statement wrappedStatement = sqlParser.createStatement(query, createParsingOptions(analyzerOptions));
if (warningCollector.hasWarnings() && analyzerOptions.getWarningHandlingLevel() == AS_ERROR) {
throw new PrestoException(WARNING_AS_ERROR, format("Warning handling level set to AS_ERROR. Warnings: %n %s",
warningCollector.getWarnings().stream()
Expand All @@ -82,7 +83,7 @@ public BuiltInPreparedQuery prepareQuery(AnalyzerOptions analyzerOptions, Statem
String preparedStatementName = ((Execute) statement).getName().getValue();
prepareSql = Optional.ofNullable(preparedStatements.get(preparedStatementName));
String query = prepareSql.orElseThrow(() -> new PrestoException(NOT_FOUND, "Prepared statement not found: " + preparedStatementName));
statement = sqlParser.createStatement(query, analyzerOptions.getParsingOptions());
statement = sqlParser.createStatement(query, createParsingOptions(analyzerOptions));
}

if (statement instanceof Explain && ((Explain) statement).isAnalyze()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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 com.facebook.presto.sql.analyzer.utils;

import com.facebook.presto.spi.PrestoWarning;
import com.facebook.presto.sql.analyzer.AnalyzerOptions;
import com.facebook.presto.sql.parser.ParsingOptions;

import static com.facebook.presto.spi.StandardWarningCode.PARSER_WARNING;
import static com.facebook.presto.sql.parser.ParsingOptions.DecimalLiteralTreatment.AS_DECIMAL;
import static com.facebook.presto.sql.parser.ParsingOptions.DecimalLiteralTreatment.AS_DOUBLE;

public class AnalyzerUtil
{
private AnalyzerUtil() {}

public static ParsingOptions createParsingOptions(AnalyzerOptions analyzerOptions)
{
return ParsingOptions.builder()
.setDecimalLiteralTreatment(analyzerOptions.isParseDecimalLiteralsAsDouble() ? AS_DOUBLE : AS_DECIMAL)
.setWarningConsumer(warning -> analyzerOptions.getWarningCollector().add(new PrestoWarning(PARSER_WARNING, warning.getMessage())))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ public static AnalyzerOptions createAnalyzerOptions(Session session)

public static AnalyzerOptions createAnalyzerOptions(Session session, WarningCollector warningCollector)
{
ParsingOptions parsingOptions = createParsingOptions(session, warningCollector);
return AnalyzerOptions.builder()
.setParsingOptions(parsingOptions)
.setParseDecimalLiteralsAsDouble(isParseDecimalLiteralsAsDouble(session))
.setLogFormattedQueryEnabled(isLogFormattedQueryEnabled(session))
.setWarningHandlingLevel(getWarningHandlingLevel(session))
.setWarningCollector(warningCollector)
.build();
}
}

0 comments on commit 001dbd5

Please sign in to comment.