use of io.prestosql.sql.tree.BetweenPredicate in project hetu-core by openlookeng.
the class TestSqlParser method testCreateCube.
@Test
public void testCreateCube() {
assertStatement("CREATE CUBE foo ON bar WITH (AGGREGATIONS=(count(c)), GROUP = (a, b)) WHERE d1 > 10", new CreateCube(QualifiedName.of("foo"), QualifiedName.of("bar"), ImmutableList.of(new Identifier("a"), new Identifier("b")), ImmutableSet.of(new FunctionCall(QualifiedName.of("count"), ImmutableList.of(new Identifier("c")))), false, ImmutableList.of(), Optional.of(new ComparisonExpression(GREATER_THAN, new Identifier("d1"), new LongLiteral("10"))), null));
assertStatement("CREATE CUBE foo ON bar WITH (AGGREGATIONS=(count(distinct c), sum(e)), GROUP = (a, b), FILTER = (f between 1 and 10)) WHERE d1 > 10", new CreateCube(QualifiedName.of("foo"), QualifiedName.of("bar"), ImmutableList.of(new Identifier("a"), new Identifier("b")), ImmutableSet.of(new FunctionCall(QualifiedName.of("count"), true, ImmutableList.of(new Identifier("c"))), new FunctionCall(QualifiedName.of("sum"), ImmutableList.of(new Identifier("e")))), false, ImmutableList.of(), Optional.of(new ComparisonExpression(GREATER_THAN, new Identifier("d1"), new LongLiteral("10"))), new BetweenPredicate(new Identifier("f"), new LongLiteral("1"), new LongLiteral("10"))));
assertStatement("CREATE CUBE foo ON bar WITH (AGGREGATIONS=(count(c), sum(d), avg(e)), GROUP = (a, b)) WHERE d1 > 10", new CreateCube(QualifiedName.of("foo"), QualifiedName.of("bar"), ImmutableList.of(new Identifier("a"), new Identifier("b")), ImmutableSet.of(new FunctionCall(QualifiedName.of("count"), ImmutableList.of(new Identifier("c"))), new FunctionCall(QualifiedName.of("sum"), ImmutableList.of(new Identifier("d"))), new FunctionCall(QualifiedName.of("avg"), ImmutableList.of(new Identifier("e"))), new FunctionCall(QualifiedName.of("sum"), ImmutableList.of(new Identifier("e"))), new FunctionCall(QualifiedName.of("count"), ImmutableList.of(new Identifier("e")))), false, ImmutableList.of(), Optional.of(new ComparisonExpression(GREATER_THAN, new Identifier("d1"), new LongLiteral("10"))), null));
assertStatement("CREATE CUBE avgtestcube ON bar WITH (AGGREGATIONS=(count(c), sum(d), avg(e), avg(f)), GROUP = (a, b)) WHERE d1 > 10", new CreateCube(QualifiedName.of("avgtestcube"), QualifiedName.of("bar"), ImmutableList.of(new Identifier("a"), new Identifier("b")), ImmutableSet.of(new FunctionCall(QualifiedName.of("count"), ImmutableList.of(new Identifier("c"))), new FunctionCall(QualifiedName.of("sum"), ImmutableList.of(new Identifier("d"))), new FunctionCall(QualifiedName.of("avg"), ImmutableList.of(new Identifier("e"))), new FunctionCall(QualifiedName.of("sum"), ImmutableList.of(new Identifier("e"))), new FunctionCall(QualifiedName.of("count"), ImmutableList.of(new Identifier("e"))), new FunctionCall(QualifiedName.of("avg"), ImmutableList.of(new Identifier("f"))), new FunctionCall(QualifiedName.of("sum"), ImmutableList.of(new Identifier("f"))), new FunctionCall(QualifiedName.of("count"), ImmutableList.of(new Identifier("f")))), false, ImmutableList.of(), Optional.of(new ComparisonExpression(GREATER_THAN, new Identifier("d1"), new LongLiteral("10"))), null));
assertStatement("CREATE CUBE c1.s1.foo ON c2.s2.bar WITH (AGGREGATIONS=(count(c)), GROUP = (a, b)) WHERE d1 > 10", new CreateCube(QualifiedName.of("c1", "s1", "foo"), QualifiedName.of("c2", "s2", "bar"), ImmutableList.of(new Identifier("a"), new Identifier("b")), ImmutableSet.of(new FunctionCall(QualifiedName.of("count"), ImmutableList.of(new Identifier("c")))), false, ImmutableList.of(), Optional.of(new ComparisonExpression(GREATER_THAN, new Identifier("d1"), new LongLiteral("10"))), null));
assertStatement("CREATE CUBE IF NOT EXISTS foo ON bar WITH (AGGREGATIONS=(count(c)), GROUP = (a, b)) WHERE d1 > 10", new CreateCube(QualifiedName.of("foo"), QualifiedName.of("bar"), ImmutableList.of(new Identifier("a"), new Identifier("b")), ImmutableSet.of(new FunctionCall(QualifiedName.of("count"), ImmutableList.of(new Identifier("c")))), true, ImmutableList.of(), Optional.of(new ComparisonExpression(GREATER_THAN, new Identifier("d1"), new LongLiteral("10"))), null));
assertStatement("CREATE CUBE IF NOT EXISTS foo ON bar WITH (AGGREGATIONS=(count(c)), GROUP = (a, b), format = 'ORC', partitioned_by = ARRAY[ 'd' ]) WHERE d1 > 10", new CreateCube(QualifiedName.of("foo"), QualifiedName.of("bar"), ImmutableList.of(new Identifier("a"), new Identifier("b")), ImmutableSet.of(new FunctionCall(QualifiedName.of("count"), ImmutableList.of(new Identifier("c")))), true, ImmutableList.of(new Property(new Identifier("format"), new StringLiteral("ORC")), new Property(new Identifier("partitioned_by"), new ArrayConstructor(ImmutableList.of(new StringLiteral("d"))))), Optional.of(new ComparisonExpression(GREATER_THAN, new Identifier("d1"), new LongLiteral("10"))), null));
assertStatement("CREATE CUBE IF NOT EXISTS foo ON bar WITH (AGGREGATIONS=(count(c)), GROUP = (a, b), format = 'ORC', partitioned_by = ARRAY[ 'd' ], FILTER = (d2 > 20)) WHERE d1 > 10", new CreateCube(QualifiedName.of("foo"), QualifiedName.of("bar"), ImmutableList.of(new Identifier("a"), new Identifier("b")), ImmutableSet.of(new FunctionCall(QualifiedName.of("count"), ImmutableList.of(new Identifier("c")))), true, ImmutableList.of(new Property(new Identifier("format"), new StringLiteral("ORC")), new Property(new Identifier("partitioned_by"), new ArrayConstructor(ImmutableList.of(new StringLiteral("d"))))), Optional.of(new ComparisonExpression(GREATER_THAN, new Identifier("d1"), new LongLiteral("10"))), new ComparisonExpression(GREATER_THAN, new Identifier("d2"), new LongLiteral("20"))));
}
use of io.prestosql.sql.tree.BetweenPredicate 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.tree.BetweenPredicate 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.tree.BetweenPredicate in project hetu-core by openlookeng.
the class ExpressionDomainTranslator method processRange.
private Expression processRange(Type type, Range range, SymbolReference reference) {
if (range.isAll()) {
return TRUE_LITERAL;
}
if (isBetween(range)) {
// specialize the range with BETWEEN expression if possible b/c it is currently more efficient
return new BetweenPredicate(reference, literalEncoder.toExpression(range.getLow().getValue(), type), literalEncoder.toExpression(range.getHigh().getValue(), type));
}
List<Expression> rangeConjuncts = new ArrayList<>();
if (!range.getLow().isLowerUnbounded()) {
switch(range.getLow().getBound()) {
case ABOVE:
rangeConjuncts.add(new ComparisonExpression(GREATER_THAN, reference, literalEncoder.toExpression(range.getLow().getValue(), type)));
break;
case EXACTLY:
rangeConjuncts.add(new ComparisonExpression(GREATER_THAN_OR_EQUAL, reference, literalEncoder.toExpression(range.getLow().getValue(), type)));
break;
case BELOW:
throw new IllegalStateException("Low Marker should never use BELOW bound: " + range);
default:
throw new AssertionError("Unhandled bound: " + range.getLow().getBound());
}
}
if (!range.getHigh().isUpperUnbounded()) {
switch(range.getHigh().getBound()) {
case ABOVE:
throw new IllegalStateException("High Marker should never use ABOVE bound: " + range);
case EXACTLY:
rangeConjuncts.add(new ComparisonExpression(LESS_THAN_OR_EQUAL, reference, literalEncoder.toExpression(range.getHigh().getValue(), type)));
break;
case BELOW:
rangeConjuncts.add(new ComparisonExpression(LESS_THAN, reference, literalEncoder.toExpression(range.getHigh().getValue(), type)));
break;
default:
throw new AssertionError("Unhandled bound: " + range.getHigh().getBound());
}
}
// If rangeConjuncts is null, then the range was ALL, which should already have been checked for
checkState(!rangeConjuncts.isEmpty());
return combineConjuncts(rangeConjuncts);
}
use of io.prestosql.sql.tree.BetweenPredicate in project hetu-core by openlookeng.
the class CubeConsole method processComparisonExpression.
/**
* Process the Create Cube Query with Comparison Expression 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 processComparisonExpression(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();
ComparisonExpression comparisonExpression = (ComparisonExpression) (createCube.getWhere().get());
ComparisonExpression.Operator operator = comparisonExpression.getOperator();
Expression left = comparisonExpression.getLeft();
Expression right = comparisonExpression.getRight();
boolean notEqualOperator = false;
if (operator.getValue().equalsIgnoreCase(NOT_EQUAL_OPERATOR)) {
notEqualOperator = true;
}
if (!(left instanceof SymbolReference) && right instanceof SymbolReference) {
comparisonExpression = new ComparisonExpression(operator.flip(), right, left);
}
if (left instanceof Literal && !(right instanceof Literal)) {
comparisonExpression = new ComparisonExpression(operator.flip(), right, left);
}
Expression columnName = comparisonExpression.getLeft();
// 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 && bufferIterationItems.size() != EMPTY_ROW_BUFFER_ITERATION_ITEMS) {
// 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;
Expression userBoundaryPredicate = null;
String queryInsert;
String minItem = rowBufferItems.get(INDEX_AT_MIN_POSITION).toString();
String maxItem = rowBufferItems.get(INDEX_AT_MAX_POSITION).toString();
switch(cubeColumnDataType) {
case DATATYPE_DOUBLE:
{
finalPredicate = new BetweenPredicate(columnName, parser.createExpression(DATATYPE_DOUBLE + " " + QUOTE_STRING + minItem + QUOTE_STRING, new ParsingOptions()), parser.createExpression(DATATYPE_DOUBLE + " " + QUOTE_STRING + maxItem + QUOTE_STRING, new ParsingOptions()));
userBoundaryPredicate = new ComparisonExpression(operator, left, right);
break;
}
case DATATYPE_REAL:
{
finalPredicate = new BetweenPredicate(columnName, parser.createExpression(DATATYPE_REAL_QUOTE + minItem + QUOTE_STRING, new ParsingOptions()), parser.createExpression(DATATYPE_REAL_QUOTE + maxItem + QUOTE_STRING, new ParsingOptions()));
userBoundaryPredicate = new ComparisonExpression(operator, left, right);
break;
}
case DATATYPE_DECIMAL:
{
finalPredicate = new BetweenPredicate(columnName, parser.createExpression(DATATYPE_DECIMAL + " " + QUOTE_STRING + minItem + QUOTE_STRING, new ParsingOptions()), parser.createExpression(DATATYPE_DECIMAL + " " + QUOTE_STRING + maxItem + QUOTE_STRING, new ParsingOptions()));
userBoundaryPredicate = new ComparisonExpression(operator, left, right);
break;
}
case DATATYPE_DATE:
{
finalPredicate = new BetweenPredicate(left, parser.createExpression(DATATYPE_DATE_QUOTE + minItem + QUOTE_STRING, new ParsingOptions()), parser.createExpression(DATATYPE_DATE_QUOTE + maxItem + QUOTE_STRING, new ParsingOptions()));
userBoundaryPredicate = new ComparisonExpression(operator, left, right);
break;
}
case DATATYPE_TIMESTAMP:
{
finalPredicate = new BetweenPredicate(left, parser.createExpression(DATATYPE_TIMESTAMP_QUOTE + minItem + QUOTE_STRING, new ParsingOptions()), parser.createExpression(DATATYPE_TIMESTAMP_QUOTE + maxItem + QUOTE_STRING, new ParsingOptions()));
userBoundaryPredicate = new ComparisonExpression(operator, left, right);
break;
}
case DATATYPE_TINYINT:
{
finalPredicate = new BetweenPredicate(left, parser.createExpression(DATATYPE_TINYINT_QUOTE + minItem + QUOTE_STRING, new ParsingOptions()), parser.createExpression(DATATYPE_TINYINT_QUOTE + maxItem + QUOTE_STRING, new ParsingOptions()));
userBoundaryPredicate = new ComparisonExpression(operator, left, right);
break;
}
case DATATYPE_BIGINT:
{
finalPredicate = new BetweenPredicate(left, parser.createExpression(DATATYPE_BIGINT_QUOTE + minItem + QUOTE_STRING, new ParsingOptions()), parser.createExpression(DATATYPE_BIGINT_QUOTE + maxItem + QUOTE_STRING, new ParsingOptions()));
userBoundaryPredicate = new ComparisonExpression(operator, left, right);
break;
}
case DATATYPE_SMALLINT:
{
finalPredicate = new BetweenPredicate(left, parser.createExpression(DATATYPE_SMALLINT_QUOTE + minItem + QUOTE_STRING, new ParsingOptions()), parser.createExpression(DATATYPE_SMALLINT_QUOTE + maxItem + QUOTE_STRING, new ParsingOptions()));
userBoundaryPredicate = new ComparisonExpression(operator, left, right);
break;
}
case DATATYPE_VARCHAR:
{
finalPredicate = new BetweenPredicate(left, parser.createExpression(QUOTE_STRING + minItem + QUOTE_STRING, new ParsingOptions()), parser.createExpression(QUOTE_STRING + maxItem + QUOTE_STRING, new ParsingOptions()));
userBoundaryPredicate = new ComparisonExpression(operator, left, right);
break;
}
default:
{
finalPredicate = new BetweenPredicate(left, parser.createExpression(minItem, new ParsingOptions()), parser.createExpression(maxItem, new ParsingOptions()));
userBoundaryPredicate = new ComparisonExpression(operator, left, right);
break;
}
}
if (notEqualOperator) {
finalPredicate = new ComparisonExpression(ComparisonExpression.Operator.NOT_EQUAL, left, right);
queryInsert = String.format(INSERT_INTO_CUBE_STRING, cubeName, finalPredicate);
} else if (i == end && userBoundaryPredicate != null) {
queryInsert = String.format(INSERT_INTO_CUBE_STRING, cubeName, userBoundaryPredicate);
} else {
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);
if (!console.runQuery(queryRunner, queryInsert, outputFormat, schemaChanged, usePager, showProgress, terminal, out, errorChannel)) {
return false;
}
}
return true;
}
Aggregations