use of io.prestosql.sql.parser.ParsingOptions in project hetu-core by openlookeng.
the class TestExpressionEquivalence method assertNotEquivalent.
private static void assertNotEquivalent(@Language("SQL") String left, @Language("SQL") String right) {
ParsingOptions parsingOptions = new ParsingOptions(AS_DOUBLE);
Expression leftExpression = rewriteIdentifiersToSymbolReferences(SQL_PARSER.createExpression(left, parsingOptions));
Expression rightExpression = rewriteIdentifiersToSymbolReferences(SQL_PARSER.createExpression(right, parsingOptions));
Set<Symbol> symbols = extractUnique(ImmutableList.of(leftExpression, rightExpression));
TypeProvider types = TypeProvider.copyOf(symbols.stream().collect(toMap(identity(), TestExpressionEquivalence::generateType)));
assertFalse(EQUIVALENCE.areExpressionsEquivalent(TEST_SESSION, leftExpression, rightExpression, types), format("Expected (%s) and (%s) to not be equivalent", left, right));
assertFalse(EQUIVALENCE.areExpressionsEquivalent(TEST_SESSION, rightExpression, leftExpression, types), format("Expected (%s) and (%s) to not be equivalent", right, left));
}
use of io.prestosql.sql.parser.ParsingOptions in project hetu-core by openlookeng.
the class HttpRequestSessionContext method parsePreparedStatementsHeaders.
private static Map<String, String> parsePreparedStatementsHeaders(HttpServletRequest servletRequest) {
ImmutableMap.Builder<String, String> preparedStatementsBuilder = ImmutableMap.builder();
parseProperty(servletRequest, PRESTO_PREPARED_STATEMENT).forEach((key, sqlString) -> {
String statementName;
try {
statementName = urlDecode(key);
} catch (IllegalArgumentException e) {
throw badRequest(format("Invalid %s header: %s", PRESTO_PREPARED_STATEMENT, e.getMessage()));
}
// Validate statement
SqlParser sqlParser = new SqlParser();
try {
sqlParser.createStatement(sqlString, new ParsingOptions(AS_DOUBLE));
} catch (ParsingException e) {
throw badRequest(format("Invalid %s header: %s", PRESTO_PREPARED_STATEMENT, e.getMessage()));
}
preparedStatementsBuilder.put(statementName, sqlString);
});
return preparedStatementsBuilder.build();
}
use of io.prestosql.sql.parser.ParsingOptions in project hetu-core by openlookeng.
the class CubeConsole method processBetweenPredicate.
/**
* Process the Create Cube Query with Between Predicate in where clause.
*
* @param createCube createCube
* @param queryRunner queryRunner
* @param outputFormat outputFormat
* @param schemaChanged schemaChanged
* @param usePager usePager
* @param showProgress showProgress
* @param terminal terminal
* @param out out
* @param errorChannel errorChannel
* @param parser parser
* @return boolean
*/
private boolean processBetweenPredicate(CreateCube createCube, QueryRunner queryRunner, ClientOptions.OutputFormat outputFormat, Runnable schemaChanged, boolean usePager, boolean showProgress, Terminal terminal, PrintStream out, PrintStream errorChannel, SqlParser parser) {
String whereClause = createCube.getWhere().get().toString();
QualifiedName sourceTableName = createCube.getSourceTableName();
QualifiedName cubeName = createCube.getCubeName();
BetweenPredicate betweenPredicate = (BetweenPredicate) (createCube.getWhere().get());
Expression betweenPredicateValue = betweenPredicate.getValue();
String columnName = betweenPredicate.getValue().toString();
BetweenPredicate betweenExpression = ((BetweenPredicate) createCube.getWhere().get());
Expression left = betweenExpression.getMin();
Expression right = betweenExpression.getMax();
// Run Query
String rowCountsDistinctValuesQuery = String.format(SELECT_COLUMN_ROW_COUNT_FROM_STRING, columnName, sourceTableName.toString(), whereClause, columnName, columnName);
if (!processCubeInitialQuery(queryRunner, rowCountsDistinctValuesQuery, outputFormat, schemaChanged, usePager, showProgress, terminal, out, errorChannel)) {
return false;
}
List<List<?>> bufferIterationItems = getListRowBufferIterationItems();
if (bufferIterationItems != null) {
// this loop process the multiple insert query statements
int end = bufferIterationItems.size() - 1;
for (int i = 0; i <= end; i++) {
List<?> rowBufferItems = bufferIterationItems.get(i);
Expression finalPredicate;
String queryInsert;
String minItem = rowBufferItems.get(INDEX_AT_MIN_POSITION).toString();
String maxItem = rowBufferItems.get(INDEX_AT_MAX_POSITION).toString();
Expression finalPredicateMinExp = null;
Expression finalPredicateMaxExp = null;
switch(cubeColumnDataType) {
case DATATYPE_DOUBLE:
{
double parsedMinVal = Double.parseDouble(minItem);
double parsedMaxVal = Double.parseDouble(maxItem);
double userMinVal = Double.parseDouble(extractQuotedValue(left.toString()));
double userMaxVal = Double.parseDouble(extractQuotedValue(right.toString()));
if (parsedMinVal > userMinVal) {
finalPredicateMinExp = left;
} else {
finalPredicateMinExp = parser.createExpression(DATATYPE_DOUBLE + QUOTE_STRING + parsedMinVal + QUOTE_STRING, new ParsingOptions());
}
if (parsedMaxVal < userMaxVal) {
finalPredicateMaxExp = right;
} else {
finalPredicateMaxExp = parser.createExpression(DATATYPE_DOUBLE + QUOTE_STRING + parsedMaxVal + QUOTE_STRING, new ParsingOptions());
}
finalPredicate = new BetweenPredicate(betweenPredicateValue, finalPredicateMinExp, finalPredicateMaxExp);
break;
}
case DATATYPE_REAL:
{
double parsedMinVal = Double.parseDouble(minItem);
double parsedMaxVal = Double.parseDouble(maxItem);
double userMinVal = Double.parseDouble(extractQuotedValue(left.toString()));
double userMaxVal = Double.parseDouble(extractQuotedValue(right.toString()));
if (parsedMinVal > userMinVal) {
finalPredicateMinExp = left;
} else {
finalPredicateMinExp = parser.createExpression(DATATYPE_REAL + QUOTE_STRING + parsedMinVal + QUOTE_STRING, new ParsingOptions());
}
if (parsedMaxVal < userMaxVal) {
finalPredicateMaxExp = right;
} else {
finalPredicateMaxExp = parser.createExpression(DATATYPE_REAL + QUOTE_STRING + parsedMaxVal + QUOTE_STRING, new ParsingOptions());
}
finalPredicate = new BetweenPredicate(betweenPredicateValue, finalPredicateMinExp, finalPredicateMaxExp);
break;
}
case DATATYPE_DECIMAL:
{
double parsedMinVal = Double.parseDouble(minItem);
double parsedMaxVal = Double.parseDouble(maxItem);
double userMinVal = Double.parseDouble(extractQuotedValue(left.toString()));
double userMaxVal = Double.parseDouble(extractQuotedValue(right.toString()));
if (parsedMinVal > userMinVal) {
finalPredicateMinExp = left;
} else {
finalPredicateMinExp = parser.createExpression(DATATYPE_DECIMAL + QUOTE_STRING + parsedMinVal + QUOTE_STRING, new ParsingOptions());
}
if (parsedMaxVal < userMaxVal) {
finalPredicateMaxExp = right;
} else {
finalPredicateMaxExp = parser.createExpression(DATATYPE_DECIMAL + QUOTE_STRING + parsedMaxVal + QUOTE_STRING, new ParsingOptions());
}
finalPredicate = new BetweenPredicate(betweenPredicateValue, finalPredicateMinExp, finalPredicateMaxExp);
break;
}
case DATATYPE_DATE:
{
LocalDate parsedMinVal = LocalDate.parse(minItem);
LocalDate parsedMaxVal = LocalDate.parse(maxItem);
LocalDate userMinVal = LocalDate.parse(extractQuotedValue(left.toString()));
LocalDate userMaxVal = LocalDate.parse(extractQuotedValue(right.toString()));
if (parsedMinVal.compareTo(userMinVal) > 0) {
finalPredicateMinExp = left;
} else {
finalPredicateMinExp = parser.createExpression(DATATYPE_DATE_QUOTE + parsedMinVal + QUOTE_STRING, new ParsingOptions());
}
if (parsedMaxVal.compareTo(userMaxVal) < 0) {
finalPredicateMaxExp = right;
} else {
finalPredicateMaxExp = parser.createExpression(DATATYPE_DATE_QUOTE + parsedMaxVal + QUOTE_STRING, new ParsingOptions());
}
finalPredicate = new BetweenPredicate(betweenPredicateValue, finalPredicateMinExp, finalPredicateMaxExp);
break;
}
case DATATYPE_TIMESTAMP:
{
Timestamp parsedMinVal = Timestamp.valueOf(minItem);
Timestamp parsedMaxVal = Timestamp.valueOf(maxItem);
Timestamp userMinVal = Timestamp.valueOf(extractQuotedValue(left.toString()));
Timestamp userMaxVal = Timestamp.valueOf(extractQuotedValue(right.toString()));
if (parsedMinVal.compareTo(userMinVal) > 0) {
finalPredicateMinExp = left;
} else {
finalPredicateMinExp = parser.createExpression(DATATYPE_TIMESTAMP_QUOTE + parsedMinVal + QUOTE_STRING, new ParsingOptions());
}
if (parsedMaxVal.compareTo(userMaxVal) < 0) {
finalPredicateMaxExp = right;
} else {
finalPredicateMaxExp = parser.createExpression(DATATYPE_TIMESTAMP_QUOTE + parsedMaxVal + QUOTE_STRING, new ParsingOptions());
}
finalPredicate = new BetweenPredicate(betweenPredicateValue, finalPredicateMinExp, finalPredicateMaxExp);
break;
}
case DATATYPE_TINYINT:
{
int parsedMinVal = Integer.parseInt(minItem);
int parsedMaxVal = Integer.parseInt(maxItem);
int userMinVal = Integer.parseInt(extractQuotedValue(left.toString()));
int userMaxVal = Integer.parseInt(extractQuotedValue(right.toString()));
if (parsedMinVal > userMinVal) {
finalPredicateMinExp = left;
} else {
finalPredicateMinExp = parser.createExpression(DATATYPE_TINYINT_QUOTE + parsedMinVal + QUOTE_STRING, new ParsingOptions());
}
if (parsedMaxVal < userMaxVal) {
finalPredicateMaxExp = right;
} else {
finalPredicateMaxExp = parser.createExpression(DATATYPE_TINYINT_QUOTE + parsedMaxVal + QUOTE_STRING, new ParsingOptions());
}
finalPredicate = new BetweenPredicate(betweenPredicateValue, finalPredicateMinExp, finalPredicateMaxExp);
break;
}
case DATATYPE_BIGINT:
{
BigInteger parsedMinVal = new BigInteger(minItem);
BigInteger parsedMaxVal = new BigInteger(maxItem);
BigInteger userMinVal = new BigInteger(extractQuotedValue(left.toString()));
BigInteger userMaxVal = new BigInteger(extractQuotedValue(right.toString()));
if (parsedMinVal.compareTo(userMinVal) > 0) {
finalPredicateMinExp = left;
} else {
finalPredicateMinExp = parser.createExpression(DATATYPE_BIGINT_QUOTE + parsedMinVal + QUOTE_STRING, new ParsingOptions());
}
if (parsedMaxVal.compareTo(userMaxVal) < 0) {
finalPredicateMaxExp = right;
} else {
finalPredicateMaxExp = parser.createExpression(DATATYPE_BIGINT_QUOTE + parsedMaxVal + QUOTE_STRING, new ParsingOptions());
}
finalPredicate = new BetweenPredicate(betweenPredicateValue, finalPredicateMinExp, finalPredicateMaxExp);
break;
}
case DATATYPE_SMALLINT:
{
int parsedMinVal = Integer.parseInt(minItem);
int parsedMaxVal = Integer.parseInt(maxItem);
int userMinVal = Integer.parseInt(extractQuotedValue(left.toString()));
int userMaxVal = Integer.parseInt(extractQuotedValue(right.toString()));
if (parsedMinVal > userMinVal) {
finalPredicateMinExp = left;
} else {
finalPredicateMinExp = parser.createExpression(DATATYPE_SMALLINT_QUOTE + parsedMinVal + QUOTE_STRING, new ParsingOptions());
}
if (parsedMaxVal < userMaxVal) {
finalPredicateMaxExp = right;
} else {
finalPredicateMaxExp = parser.createExpression(DATATYPE_SMALLINT_QUOTE + parsedMaxVal + QUOTE_STRING, new ParsingOptions());
}
finalPredicate = new BetweenPredicate(betweenPredicateValue, finalPredicateMinExp, finalPredicateMaxExp);
break;
}
case DATATYPE_VARCHAR:
{
String userMinVal = extractQuotedValue(left.toString());
String userMaxVal = extractQuotedValue(right.toString());
if (minItem.compareTo(userMinVal) > 0) {
finalPredicateMinExp = left;
} else {
finalPredicateMinExp = parser.createExpression(QUOTE_STRING + minItem + QUOTE_STRING, new ParsingOptions());
}
if (maxItem.compareTo(userMaxVal) < 0) {
finalPredicateMaxExp = right;
} else {
finalPredicateMaxExp = parser.createExpression(QUOTE_STRING + maxItem + QUOTE_STRING, new ParsingOptions());
}
finalPredicate = new BetweenPredicate(betweenPredicateValue, finalPredicateMinExp, finalPredicateMaxExp);
break;
}
default:
{
int parsedMinVal = Integer.parseInt(minItem);
int parsedMaxVal = Integer.parseInt(maxItem);
int userMinVal = Integer.parseInt(extractQuotedValue(left.toString()));
int userMaxVal = Integer.parseInt(extractQuotedValue(right.toString()));
if (parsedMinVal > userMinVal) {
finalPredicateMinExp = left;
} else {
finalPredicateMinExp = parser.createExpression(rowBufferItems.get(INDEX_AT_MIN_POSITION).toString(), new ParsingOptions());
}
if (parsedMaxVal < userMaxVal) {
finalPredicateMaxExp = right;
} else {
finalPredicateMaxExp = parser.createExpression(rowBufferItems.get(INDEX_AT_MAX_POSITION).toString(), new ParsingOptions());
}
finalPredicate = new BetweenPredicate(betweenPredicateValue, finalPredicateMinExp, finalPredicateMaxExp);
break;
}
}
queryInsert = String.format(INSERT_INTO_CUBE_STRING, cubeName, finalPredicate);
if (!console.runQuery(queryRunner, queryInsert, outputFormat, schemaChanged, usePager, showProgress, terminal, out, errorChannel)) {
return false;
}
}
} else {
// if the range is within the processing size limit then we run a single insert query only
String queryInsert = String.format(INSERT_INTO_CUBE_STRING, cubeName, whereClause);
return console.runQuery(queryRunner, queryInsert, outputFormat, schemaChanged, usePager, showProgress, terminal, out, errorChannel);
}
return true;
}
use of io.prestosql.sql.parser.ParsingOptions in project hetu-core by openlookeng.
the class CubeConsole method createCubeCommand.
/**
* Process the Create Cube Query
*
* @param queryRunner queryRunner
* @param outputFormat outputFormat
* @param schemaChanged schemaChanged
* @param usePager usePager
* @param schemaChanged schemaChanged
* @param showProgress showProgress
* @param terminal terminal
* @param out out
* @param errorChannel errorChannel
* @return boolean after processing the create cube query command.
*/
public boolean createCubeCommand(String query, QueryRunner queryRunner, ClientOptions.OutputFormat outputFormat, Runnable schemaChanged, boolean usePager, boolean showProgress, Terminal terminal, PrintStream out, PrintStream errorChannel) {
boolean success = true;
SqlParser parser = new SqlParser();
QualifiedName cubeName = null;
try {
CreateCube createCube = (CreateCube) parser.createStatement(query, new ParsingOptions(ParsingOptions.DecimalLiteralTreatment.AS_DOUBLE));
cubeName = createCube.getCubeName();
QualifiedName sourceTableName = createCube.getSourceTableName();
String whereClause = createCube.getWhere().get().toString();
Set<FunctionCall> aggregations = createCube.getAggregations();
List<Identifier> groupingSet = createCube.getGroupingSet();
List<Property> properties = createCube.getProperties();
boolean notExists = createCube.isNotExists();
CreateCube modifiedCreateCube = new CreateCube(cubeName, sourceTableName, groupingSet, aggregations, notExists, properties, Optional.empty(), createCube.getSourceFilter().orElse(null));
String queryCreateCube = SqlFormatter.formatSql(modifiedCreateCube, Optional.empty());
if (!console.runQuery(queryRunner, queryCreateCube, outputFormat, schemaChanged, usePager, showProgress, terminal, out, errorChannel)) {
return false;
}
// we check whether the create cube expression can be processed
if (isSupportedExpression(createCube, queryRunner, outputFormat, schemaChanged, usePager, showProgress, terminal, out, errorChannel)) {
if (createCube.getWhere().get() instanceof BetweenPredicate) {
// we process the between predicate in the create cube query where clause
success = processBetweenPredicate(createCube, queryRunner, outputFormat, schemaChanged, usePager, showProgress, terminal, out, errorChannel, parser);
}
if (createCube.getWhere().get() instanceof ComparisonExpression) {
// we process the comparison expression in the create cube query where clause
success = processComparisonExpression(createCube, queryRunner, outputFormat, schemaChanged, usePager, showProgress, terminal, out, errorChannel, parser);
}
} else {
// if we donot support processing the create cube query with multiple inserts, then only a single insert is run internally.
String queryInsert = String.format(INSERT_INTO_CUBE_STRING, cubeName, whereClause);
success = console.runQuery(queryRunner, queryInsert, outputFormat, schemaChanged, usePager, showProgress, terminal, out, errorChannel);
}
if (!success) {
// roll back mechanism for unsuccessful create cube query
String dropCubeQuery = String.format(DROP_CUBE_STRING, cubeName);
console.runQuery(queryRunner, dropCubeQuery, outputFormat, schemaChanged, usePager, showProgress, terminal, out, errorChannel);
}
} catch (ParsingException e) {
if (cubeName != null) {
// roll back mechanism for unsuccessful create cube query
String dropCubeQuery = String.format(DROP_CUBE_STRING, cubeName);
console.runQuery(queryRunner, dropCubeQuery, outputFormat, schemaChanged, usePager, showProgress, terminal, out, errorChannel);
}
System.out.println(e.getMessage());
Query.renderErrorLocation(query, new ErrorLocation(e.getLineNumber(), e.getColumnNumber()), errorChannel);
success = false;
} catch (Exception e) {
if (cubeName != null) {
// roll back mechanism for unsuccessful create cube query
String dropCubeQuery = String.format(DROP_CUBE_STRING, cubeName);
console.runQuery(queryRunner, dropCubeQuery, outputFormat, schemaChanged, usePager, showProgress, terminal, out, errorChannel);
}
// Add blank line after progress bar
System.out.println();
System.out.println(e.getMessage());
success = false;
}
return success;
}
use of io.prestosql.sql.parser.ParsingOptions in project hetu-core by openlookeng.
the class SqlFormatterUtil method getFormattedSql.
public static String getFormattedSql(Statement statement, SqlParser sqlParser, Optional<List<Expression>> parameters) {
String sql = SqlFormatter.formatSql(statement, parameters);
// verify round-trip
Statement parsed;
try {
ParsingOptions parsingOptions = new ParsingOptions(REJECT);
parsed = sqlParser.createStatement(sql, parsingOptions);
} catch (ParsingException e) {
throw new PrestoException(GENERIC_INTERNAL_ERROR, "Formatted query does not parse: " + statement);
}
if (!statement.equals(parsed)) {
throw new PrestoException(GENERIC_INTERNAL_ERROR, "Query does not round-trip: " + statement);
}
return sql;
}
Aggregations