use of com.facebook.presto.sql.analyzer.SemanticException in project presto by prestodb.
the class AbstractTestQueries method testExecuteUsingSelectStarFails.
@Test
public void testExecuteUsingSelectStarFails() {
try {
String query = "SELECT ?";
Session session = Session.builder(getSession()).addPreparedStatement("my_query", query).build();
computeActual(session, "EXECUTE my_query USING (SELECT * from nation)");
fail("nonLiteral parameters should fail");
} catch (SemanticException e) {
assertEquals(e.getCode(), EXPRESSION_NOT_CONSTANT);
} catch (RuntimeException e) {
assertEquals(e.getMessage(), "line 1:24: Constant expression cannot contain column references");
}
}
use of com.facebook.presto.sql.analyzer.SemanticException in project presto by prestodb.
the class TestArrayOperators method testSubscript.
@Test
public void testSubscript() {
String outOfBounds = "Array subscript out of bounds";
String negativeIndex = "Array subscript is negative";
String indexIsZero = "SQL array indices start at 1";
assertInvalidFunction("ARRAY [][1]", outOfBounds);
assertInvalidFunction("ARRAY [null][-1]", negativeIndex);
assertInvalidFunction("ARRAY [1, 2, 3][0]", indexIsZero);
assertInvalidFunction("ARRAY [1, 2, 3][-1]", negativeIndex);
assertInvalidFunction("ARRAY [1, 2, 3][4]", outOfBounds);
try {
assertFunction("ARRAY [1, 2, 3][1.1E0]", BIGINT, null);
fail("Access to array with double subscript should fail");
} catch (SemanticException e) {
assertTrue(e.getCode() == TYPE_MISMATCH);
}
assertFunction("ARRAY[NULL][1]", UNKNOWN, null);
assertFunction("ARRAY[NULL, NULL, NULL][3]", UNKNOWN, null);
assertFunction("1 + ARRAY [2, 1, 3][2]", INTEGER, 2);
assertFunction("ARRAY [2, 1, 3][2]", INTEGER, 1);
assertFunction("ARRAY [2, NULL, 3][2]", INTEGER, null);
assertFunction("ARRAY [1.0E0, 2.5E0, 3.5E0][3]", DOUBLE, 3.5);
assertFunction("ARRAY [ARRAY[1, 2], ARRAY[3]][2]", new ArrayType(INTEGER), ImmutableList.of(3));
assertFunction("ARRAY [ARRAY[1, 2], NULL, ARRAY[3]][2]", new ArrayType(INTEGER), null);
assertFunction("ARRAY [ARRAY[1, 2], ARRAY[3]][2][1]", INTEGER, 3);
assertFunction("ARRAY ['puppies', 'kittens'][2]", createVarcharType(7), "kittens");
assertFunction("ARRAY ['puppies', 'kittens', NULL][3]", createVarcharType(7), null);
assertFunction("ARRAY [TRUE, FALSE][2]", BOOLEAN, false);
assertFunction("ARRAY [TIMESTAMP '1970-01-01 00:00:01', TIMESTAMP '1973-07-08 22:00:01'][1]", TIMESTAMP, sqlTimestampOf(1970, 1, 1, 0, 0, 1, 0, TEST_SESSION));
assertFunction("ARRAY [infinity()][1]", DOUBLE, POSITIVE_INFINITY);
assertFunction("ARRAY [-infinity()][1]", DOUBLE, NEGATIVE_INFINITY);
assertFunction("ARRAY [sqrt(-1)][1]", DOUBLE, NaN);
assertDecimalFunction("ARRAY [2.1, 2.2, 2.3][3]", decimal("2.3"));
assertDecimalFunction("ARRAY [2.111111222111111114111, 2.22222222222222222, 2.222222222222223][3]", decimal("2.222222222222223000000"));
assertDecimalFunction("ARRAY [1.9, 2, 2.3][3]", decimal("0000000002.3"));
assertDecimalFunction("ARRAY [2.22222222222222222, 2.3][1]", decimal("2.22222222222222222"));
}
use of com.facebook.presto.sql.analyzer.SemanticException in project presto by prestodb.
the class RenameColumnTask method execute.
@Override
public ListenableFuture<?> execute(RenameColumn statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, Session session, List<Expression> parameters, WarningCollector warningCollector) {
QualifiedObjectName tableName = createQualifiedObjectName(session, statement, statement.getTable());
Optional<TableHandle> tableHandleOptional = metadata.getTableHandle(session, tableName);
if (!tableHandleOptional.isPresent()) {
if (!statement.isTableExists()) {
throw new SemanticException(MISSING_TABLE, statement, "Table '%s' does not exist", tableName);
}
return immediateFuture(null);
}
Optional<ConnectorMaterializedViewDefinition> optionalMaterializedView = metadata.getMaterializedView(session, tableName);
if (optionalMaterializedView.isPresent()) {
if (!statement.isTableExists()) {
throw new SemanticException(NOT_SUPPORTED, statement, "'%s' is a materialized view, and rename column is not supported", tableName);
}
return immediateFuture(null);
}
TableHandle tableHandle = tableHandleOptional.get();
String source = statement.getSource().getValue().toLowerCase(ENGLISH);
String target = statement.getTarget().getValue().toLowerCase(ENGLISH);
accessControl.checkCanRenameColumn(session.getRequiredTransactionId(), session.getIdentity(), session.getAccessControlContext(), tableName);
Map<String, ColumnHandle> columnHandles = metadata.getColumnHandles(session, tableHandle);
ColumnHandle columnHandle = columnHandles.get(source);
if (columnHandle == null) {
if (!statement.isColumnExists()) {
throw new SemanticException(MISSING_COLUMN, statement, "Column '%s' does not exist", source);
}
return immediateFuture(null);
}
if (columnHandles.containsKey(target)) {
throw new SemanticException(COLUMN_ALREADY_EXISTS, statement, "Column '%s' already exists", target);
}
if (metadata.getColumnMetadata(session, tableHandle, columnHandle).isHidden()) {
throw new SemanticException(NOT_SUPPORTED, statement, "Cannot rename hidden column");
}
metadata.renameColumn(session, tableHandle, columnHandle, target);
return immediateFuture(null);
}
use of com.facebook.presto.sql.analyzer.SemanticException in project presto by prestodb.
the class RenameTableTask method execute.
@Override
public ListenableFuture<?> execute(RenameTable statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, Session session, List<Expression> parameters, WarningCollector warningCollector) {
QualifiedObjectName tableName = createQualifiedObjectName(session, statement, statement.getSource());
Optional<TableHandle> tableHandle = metadata.getTableHandle(session, tableName);
if (!tableHandle.isPresent()) {
if (!statement.isExists()) {
throw new SemanticException(MISSING_TABLE, statement, "Table '%s' does not exist", tableName);
}
return immediateFuture(null);
}
Optional<ConnectorMaterializedViewDefinition> optionalMaterializedView = metadata.getMaterializedView(session, tableName);
if (optionalMaterializedView.isPresent()) {
if (!statement.isExists()) {
throw new SemanticException(NOT_SUPPORTED, statement, "'%s' is a materialized view, and rename is not supported", tableName);
}
return immediateFuture(null);
}
QualifiedObjectName target = createQualifiedObjectName(session, statement, statement.getTarget());
if (!metadata.getCatalogHandle(session, target.getCatalogName()).isPresent()) {
throw new SemanticException(MISSING_CATALOG, statement, "Target catalog '%s' does not exist", target.getCatalogName());
}
if (metadata.getTableHandle(session, target).isPresent()) {
throw new SemanticException(TABLE_ALREADY_EXISTS, statement, "Target table '%s' already exists", target);
}
if (!tableName.getCatalogName().equals(target.getCatalogName())) {
throw new SemanticException(NOT_SUPPORTED, statement, "Table rename across catalogs is not supported");
}
accessControl.checkCanRenameTable(session.getRequiredTransactionId(), session.getIdentity(), session.getAccessControlContext(), tableName, target);
metadata.renameTable(session, tableHandle.get(), target);
return immediateFuture(null);
}
use of com.facebook.presto.sql.analyzer.SemanticException in project presto by prestodb.
the class RenameSchemaTask method execute.
@Override
public ListenableFuture<?> execute(RenameSchema statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, Session session, List<Expression> parameters, WarningCollector warningCollector) {
CatalogSchemaName source = createCatalogSchemaName(session, statement, Optional.of(statement.getSource()));
CatalogSchemaName target = new CatalogSchemaName(source.getCatalogName(), statement.getTarget().getValue());
if (!metadata.schemaExists(session, source)) {
throw new SemanticException(MISSING_SCHEMA, statement, "Schema '%s' does not exist", source);
}
if (metadata.schemaExists(session, target)) {
throw new SemanticException(SCHEMA_ALREADY_EXISTS, statement, "Target schema '%s' already exists", target);
}
accessControl.checkCanRenameSchema(session.getRequiredTransactionId(), session.getIdentity(), session.getAccessControlContext(), source, statement.getTarget().getValue());
metadata.renameSchema(session, source, statement.getTarget().getValue());
return immediateFuture(null);
}
Aggregations