private Query rewriteInsertQuery(Connection connection, Query query, Insert statement) throws SQLException, QueryRewriteException { QualifiedName temporaryTableName = generateTemporaryTableName(statement.getTarget()); Statement createTemporaryTable = new CreateTable(temporaryTableName, ImmutableList.of(new LikeClause(statement.getTarget(), Optional.of(INCLUDING))), true, ImmutableList.of(), Optional.empty()); String createTemporaryTableSql = formatSql(createTemporaryTable, Optional.empty()); String insertSql = formatSql(new Insert(temporaryTableName, statement.getColumns(), statement.getQuery()), Optional.empty()); String checksumSql = checksumSql(getColumnsForTable(connection, query.getCatalog(), query.getSchema(), statement.getTarget().toString()), temporaryTableName); String dropTableSql = dropTableSql(temporaryTableName); return new Query(query.getCatalog(), query.getSchema(), ImmutableList.of(createTemporaryTableSql, insertSql), checksumSql, ImmutableList.of(dropTableSql), query.getUsername(), query.getPassword(), query.getSessionProperties()); }
@Test public void testCreateTableNotExistsTrue() { CreateTable statement = new CreateTable(QualifiedName.of("test_table"), ImmutableList.of(new ColumnDefinition(identifier("a"), "BIGINT", emptyList(), Optional.empty())), true, ImmutableList.of(), Optional.empty()); getFutureValue(new CreateTableTask().internalExecute(statement, metadata, new AllowAllAccessControl(), testSession, emptyList())); assertEquals(metadata.getCreateTableCallCount(), 1); }
@Override public Node visitCreateTable(SqlBaseParser.CreateTableContext context) { Optional<String> comment = Optional.empty(); if (context.COMMENT() != null) { comment = Optional.of(((StringLiteral) visit(context.string())).getValue()); } List<Property> properties = ImmutableList.of(); if (context.properties() != null) { properties = visit(context.properties().property(), Property.class); } return new CreateTable( getLocation(context), getQualifiedName(context.qualifiedName()), visit(context.tableElement(), TableElement.class), context.EXISTS() != null, properties, comment); }
@Test public void testCreateTableNotExistsFalse() { CreateTable statement = new CreateTable(QualifiedName.of("test_table"), ImmutableList.of(new ColumnDefinition(identifier("a"), "BIGINT", emptyList(), Optional.empty())), false, ImmutableList.of(), Optional.empty()); try { getFutureValue(new CreateTableTask().internalExecute(statement, metadata, new AllowAllAccessControl(), testSession, emptyList())); fail("expected exception"); } catch (RuntimeException e) { // Expected assertTrue(e instanceof PrestoException); PrestoException prestoException = (PrestoException) e; assertEquals(prestoException.getErrorCode(), ALREADY_EXISTS.toErrorCode()); } assertEquals(metadata.getCreateTableCallCount(), 1); }
List<Property> propertyNodes = buildProperties(objectName, Optional.empty(), INVALID_TABLE_PROPERTY, properties, allTableProperties); CreateTable createTable = new CreateTable( QualifiedName.of(objectName.getCatalogName(), objectName.getSchemaName(), objectName.getObjectName()), columns,
new CreateTable(QualifiedName.of("foo"), ImmutableList.of( new ColumnDefinition(identifier("a"), "VARCHAR", emptyList(), Optional.empty()), Optional.empty())); assertStatement("CREATE TABLE IF NOT EXISTS bar (c TIMESTAMP)", new CreateTable(QualifiedName.of("bar"), ImmutableList.of(new ColumnDefinition(identifier("c"), "TIMESTAMP", emptyList(), Optional.empty())), true, new CreateTable(QualifiedName.of("bar"), ImmutableList.of(new ColumnDefinition(identifier("c"), "TIMESTAMP", ImmutableList.of( new Property(new Identifier("nullable"), BooleanLiteral.TRUE_LITERAL), new CreateTable(QualifiedName.of("bar"), ImmutableList.of( new LikeClause(QualifiedName.of("like_table"), Optional.empty())); assertStatement("CREATE TABLE IF NOT EXISTS bar (c TIMESTAMP, LIKE like_table)", new CreateTable(QualifiedName.of("bar"), ImmutableList.of( new ColumnDefinition(identifier("c"), "TIMESTAMP", emptyList(), Optional.empty()), Optional.empty())); assertStatement("CREATE TABLE IF NOT EXISTS bar (c TIMESTAMP, LIKE like_table, d DATE)", new CreateTable(QualifiedName.of("bar"), ImmutableList.of( new ColumnDefinition(identifier("c"), "TIMESTAMP", emptyList(), Optional.empty()),
@Test public void testCreateTable() throws Exception { assertStatement("CREATE TABLE foo (a VARCHAR, b BIGINT)", new CreateTable(QualifiedName.of("foo"), ImmutableList.of(new TableElement("a", "VARCHAR"), new TableElement("b", "BIGINT")), false, ImmutableMap.of())); assertStatement("CREATE TABLE IF NOT EXISTS bar (c TIMESTAMP)", new CreateTable(QualifiedName.of("bar"), ImmutableList.of(new TableElement("c", "TIMESTAMP")), true, ImmutableMap.of())); }
@Override public Node visitCreateTable(SqlBaseParser.CreateTableContext context) { Optional<String> comment = Optional.empty(); if (context.COMMENT() != null) { comment = Optional.of(((StringLiteral) visit(context.string())).getValue()); } List<Property> properties = ImmutableList.of(); if (context.properties() != null) { properties = visit(context.properties().property(), Property.class); } return new CreateTable( getLocation(context), getQualifiedName(context.qualifiedName()), visit(context.tableElement(), TableElement.class), context.EXISTS() != null, properties, comment); }
@Override public Node visitCreateTable(SqlBaseParser.CreateTableContext context) { return new CreateTable(getLocation(context), getQualifiedName(context.qualifiedName()), visit(context.tableElement(), TableElement.class), context.EXISTS() != null, processTableProperties(context.tableProperties())); }
new CreateTable(QualifiedName.of("foo"), ImmutableList.of( new ColumnDefinition(identifier("a"), "VARCHAR", emptyList(), Optional.empty()), Optional.empty())); assertStatement("CREATE TABLE IF NOT EXISTS bar (c TIMESTAMP)", new CreateTable(QualifiedName.of("bar"), ImmutableList.of(new ColumnDefinition(identifier("c"), "TIMESTAMP", emptyList(), Optional.empty())), true, new CreateTable(QualifiedName.of("bar"), ImmutableList.of(new ColumnDefinition(identifier("c"), "TIMESTAMP", ImmutableList.of( new Property(new Identifier("nullable"), BooleanLiteral.TRUE_LITERAL), new CreateTable(QualifiedName.of("bar"), ImmutableList.of( new LikeClause(QualifiedName.of("like_table"), Optional.empty())); assertStatement("CREATE TABLE IF NOT EXISTS bar (c TIMESTAMP, LIKE like_table)", new CreateTable(QualifiedName.of("bar"), ImmutableList.of( new ColumnDefinition(identifier("c"), "TIMESTAMP", emptyList(), Optional.empty()), Optional.empty())); assertStatement("CREATE TABLE IF NOT EXISTS bar (c TIMESTAMP, LIKE like_table, d DATE)", new CreateTable(QualifiedName.of("bar"), ImmutableList.of( new ColumnDefinition(identifier("c"), "TIMESTAMP", emptyList(), Optional.empty()),