use of io.prestosql.sql.tree.Literal in project hetu-core by openlookeng.
the class RowExpressionVerifier method visitCast.
@Override
protected Boolean visitCast(Cast expected, RowExpression actual) {
// TODO: clean up cast path
if (actual instanceof ConstantExpression && expected.getExpression() instanceof Literal && expected.getType().equals(actual.getType().toString())) {
Literal literal = (Literal) expected.getExpression();
if (literal instanceof StringLiteral) {
Object value = LiteralInterpreter.evaluate((ConstantExpression) actual);
String actualString = value instanceof Slice ? ((Slice) value).toStringUtf8() : String.valueOf(value);
return ((StringLiteral) literal).getValue().equals(actualString);
}
return getValueFromLiteral(literal).equals(String.valueOf(LiteralInterpreter.evaluate((ConstantExpression) actual)));
}
if (actual instanceof VariableReferenceExpression && expected.getExpression() instanceof SymbolReference && expected.getType().equals(actual.getType().toString())) {
return visitSymbolReference((SymbolReference) expected.getExpression(), actual);
}
if (!(actual instanceof CallExpression) || !functionResolution.isCastFunction(((CallExpression) actual).getFunctionHandle())) {
return false;
}
if (!expected.getType().equalsIgnoreCase(actual.getType().toString()) && !(expected.getType().toLowerCase(ENGLISH).equals(VARCHAR) && actual.getType().getTypeSignature().getBase().equals(VARCHAR))) {
return false;
}
return process(expected.getExpression(), ((CallExpression) actual).getArguments().get(0));
}
use of io.prestosql.sql.tree.Literal in project hetu-core by openlookeng.
the class CubeOptimizerUtil method extractMappedValue.
private static Optional<Object> extractMappedValue(Symbol symbol, ProjectNode projectNode) {
Map<Symbol, RowExpression> assignments = projectNode.getAssignments().getMap();
RowExpression rowExpression = assignments.get(symbol);
if (rowExpression == null) {
return Optional.empty();
}
if (OriginalExpressionUtils.isExpression(rowExpression)) {
Expression expression = castToExpression(rowExpression);
if (expression instanceof Cast) {
expression = ((Cast) expression).getExpression();
}
if (expression instanceof SymbolReference) {
return Optional.of(((SymbolReference) expression).getName());
} else if (expression instanceof Literal) {
return Optional.of(expression);
}
} else {
if (rowExpression instanceof CallExpression) {
// Extract the column symbols from CAST expressions
while (rowExpression instanceof CallExpression) {
rowExpression = ((CallExpression) rowExpression).getArguments().get(0);
}
}
if (!(rowExpression instanceof VariableReferenceExpression)) {
return Optional.empty();
}
return Optional.of(((VariableReferenceExpression) rowExpression).getName());
}
return Optional.empty();
}
use of io.prestosql.sql.tree.Literal 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;
}
use of io.prestosql.sql.tree.Literal in project hetu-core by openlookeng.
the class CubeConsole method isSupportedExpression.
/**
* Gets whether the expression is supported for processing the create cube query
*
* @param createCube createCube
* @return void
*/
private boolean isSupportedExpression(CreateCube createCube, QueryRunner queryRunner, ClientOptions.OutputFormat outputFormat, Runnable schemaChanged, boolean usePager, boolean showProgress, Terminal terminal, PrintStream out, PrintStream errorChannel) {
boolean supportedExpression = false;
boolean success = true;
Optional<Expression> expression = createCube.getWhere();
if (expression.isPresent()) {
ImmutableSet.Builder<Identifier> identifierBuilder = new ImmutableSet.Builder<>();
new DefaultExpressionTraversalVisitor<Void, ImmutableSet.Builder<Identifier>>() {
@Override
protected Void visitIdentifier(Identifier node, ImmutableSet.Builder<Identifier> builder) {
builder.add(node);
return null;
}
}.process(expression.get(), identifierBuilder);
int sizeIdentifiers = identifierBuilder.build().asList().size();
if (sizeIdentifiers == SUPPORTED_INDENTIFIER_SIZE) {
String whereClause = createCube.getWhere().get().toString();
QualifiedName sourceTableName = createCube.getSourceTableName();
if (expression.get() instanceof BetweenPredicate) {
BetweenPredicate betweenPredicate = (BetweenPredicate) (expression.get());
String columnName = betweenPredicate.getValue().toString();
String columnDataTypeQuery;
String catalogName;
String tableName = sourceTableName.getSuffix();
checkArgument(tableName.matches("[\\p{Alnum}_]+"), "Invalid table name");
if (hasInvalidSymbol(columnName)) {
return false;
}
if (sourceTableName.getPrefix().isPresent() && sourceTableName.getPrefix().get().getPrefix().isPresent()) {
catalogName = sourceTableName.getPrefix().get().getPrefix().get().toString();
checkArgument(catalogName.matches("[\\p{Alnum}_]+"), "Invalid catalog name");
columnDataTypeQuery = String.format(SELECT_DATA_TYPE_STRING, catalogName, tableName, columnName);
} else if (queryRunner.getSession().getCatalog() != null) {
catalogName = queryRunner.getSession().getCatalog();
checkArgument(catalogName.matches("[\\p{Alnum}_]+"), "Invalid catalog name");
columnDataTypeQuery = String.format(SELECT_DATA_TYPE_STRING, catalogName, tableName, columnName);
} else {
return false;
}
if (!processCubeInitialQuery(queryRunner, columnDataTypeQuery, outputFormat, schemaChanged, usePager, showProgress, terminal, out, errorChannel)) {
return false;
}
String resInitCubeQuery;
resInitCubeQuery = getResultInitCubeQuery();
if (resInitCubeQuery != null) {
cubeColumnDataType = resInitCubeQuery;
}
if (cubeColumnDataType.contains(DATATYPE_DECIMAL)) {
cubeColumnDataType = DATATYPE_DECIMAL;
}
if (cubeColumnDataType.contains(DATATYPE_VARCHAR)) {
cubeColumnDataType = DATATYPE_VARCHAR;
}
if (!isSupportedDatatype(cubeColumnDataType)) {
return false;
}
if (betweenPredicate.getMin() instanceof LongLiteral || betweenPredicate.getMin() instanceof LongLiteral || betweenPredicate.getMin() instanceof TimestampLiteral || betweenPredicate.getMin() instanceof TimestampLiteral || betweenPredicate.getMin() instanceof GenericLiteral || betweenPredicate.getMin() instanceof StringLiteral || betweenPredicate.getMin() instanceof DoubleLiteral) {
if (betweenPredicate.getMax() instanceof LongLiteral || betweenPredicate.getMax() instanceof LongLiteral || betweenPredicate.getMax() instanceof TimestampLiteral || betweenPredicate.getMax() instanceof TimestampLiteral || betweenPredicate.getMax() instanceof GenericLiteral || betweenPredicate.getMax() instanceof StringLiteral || betweenPredicate.getMax() instanceof DoubleLiteral) {
// initial query to get the total number of distinct column values in the table
String countDistinctQuery = String.format(SELECT_COUNT_DISTINCT_FROM_STRING, columnName, sourceTableName.toString(), whereClause);
if (!processCubeInitialQuery(queryRunner, countDistinctQuery, outputFormat, schemaChanged, usePager, showProgress, terminal, out, errorChannel)) {
return false;
}
Long valueCountDistinctQuery = INITIAL_QUERY_RESULT_VALUE;
resInitCubeQuery = getResultInitCubeQuery();
if (resInitCubeQuery != null) {
valueCountDistinctQuery = Long.parseLong(resInitCubeQuery);
}
if (valueCountDistinctQuery < MAX_BUFFERED_ROWS && valueCountDistinctQuery * rowBufferTempMultiplier < Integer.MAX_VALUE) {
supportedExpression = true;
rowBufferListSize = (int) ((valueCountDistinctQuery).intValue() * rowBufferTempMultiplier);
}
}
}
}
if (expression.get() instanceof ComparisonExpression) {
ComparisonExpression comparisonExpression = (ComparisonExpression) (createCube.getWhere().get());
ComparisonExpression.Operator operator = comparisonExpression.getOperator();
Expression left = comparisonExpression.getLeft();
Expression right = comparisonExpression.getRight();
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);
}
if (comparisonExpression.getRight() instanceof LongLiteral) {
supportedExpression = true;
}
String catalogName;
String tableName = sourceTableName.getSuffix();
String columnName = comparisonExpression.getLeft().toString();
String columnDataTypeQuery;
checkArgument(tableName.matches("[\\p{Alnum}_]+"), "Invalid table name");
if (hasInvalidSymbol(columnName)) {
return false;
}
if (sourceTableName.getPrefix().isPresent() && sourceTableName.getPrefix().get().getPrefix().isPresent()) {
catalogName = sourceTableName.getPrefix().get().getPrefix().get().toString();
checkArgument(catalogName.matches("[\\p{Alnum}_]+"), "Invalid catalog name");
columnDataTypeQuery = String.format(SELECT_DATA_TYPE_STRING, catalogName, tableName, columnName);
} else if (queryRunner.getSession().getCatalog() != null) {
catalogName = queryRunner.getSession().getCatalog();
checkArgument(catalogName.matches("[\\p{Alnum}_]+"), "Invalid catalog name");
columnDataTypeQuery = String.format(SELECT_DATA_TYPE_STRING, catalogName, tableName, columnName);
} else {
return false;
}
if (!processCubeInitialQuery(queryRunner, columnDataTypeQuery, outputFormat, schemaChanged, usePager, showProgress, terminal, out, errorChannel)) {
return false;
}
String resInitCubeQuery;
resInitCubeQuery = getResultInitCubeQuery();
if (resInitCubeQuery != null) {
cubeColumnDataType = resInitCubeQuery.toLowerCase(Locale.ENGLISH);
}
if (cubeColumnDataType.contains(DATATYPE_DECIMAL)) {
cubeColumnDataType = DATATYPE_DECIMAL;
}
if (cubeColumnDataType.contains(DATATYPE_VARCHAR)) {
cubeColumnDataType = DATATYPE_VARCHAR;
}
if (!isSupportedDatatype(cubeColumnDataType)) {
return false;
}
if (comparisonExpression.getRight() instanceof GenericLiteral || comparisonExpression.getRight() instanceof StringLiteral || comparisonExpression.getRight() instanceof DoubleLiteral || comparisonExpression.getRight() instanceof LongLiteral || comparisonExpression.getRight() instanceof TimestampLiteral) {
// initial query to get the total number of distinct column values in the table
String countDistinctQuery = String.format(SELECT_COUNT_DISTINCT_FROM_STRING, columnName, sourceTableName.toString(), whereClause);
if (!processCubeInitialQuery(queryRunner, countDistinctQuery, outputFormat, schemaChanged, usePager, showProgress, terminal, out, errorChannel)) {
return false;
}
Long valueCountDistinctQuery = INITIAL_QUERY_RESULT_VALUE;
resInitCubeQuery = getResultInitCubeQuery();
if (resInitCubeQuery != null) {
valueCountDistinctQuery = Long.parseLong(resInitCubeQuery);
}
if (valueCountDistinctQuery < MAX_BUFFERED_ROWS) {
supportedExpression = true;
}
}
}
}
}
return supportedExpression;
}
Aggregations