use of io.prestosql.sql.tree.TimestampLiteral in project hetu-core by openlookeng.
the class TestSqlParser method testLiterals.
@Test
public void testLiterals() {
assertExpression("TIME" + " 'abc'", new TimeLiteral("abc"));
assertExpression("TIMESTAMP" + " 'abc'", new TimestampLiteral("abc"));
assertExpression("INTERVAL '33' day", new IntervalLiteral("33", Sign.POSITIVE, IntervalField.DAY, Optional.empty()));
assertExpression("INTERVAL '33' day to second", new IntervalLiteral("33", Sign.POSITIVE, IntervalField.DAY, Optional.of(IntervalField.SECOND)));
assertExpression("CHAR 'abc'", new CharLiteral("abc"));
}
use of io.prestosql.sql.tree.TimestampLiteral 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