private Query rewriteCreateTableAsSelect(Connection connection, Query query, CreateTableAsSelect statement) throws SQLException, QueryRewriteException { QualifiedName temporaryTableName = generateTemporaryTableName(statement.getName()); Statement rewritten = new CreateTableAsSelect(temporaryTableName, statement.getQuery(), statement.isNotExists(), statement.getProperties(), statement.isWithData(), statement.getColumnAliases(), Optional.empty()); String createTableAsSql = formatSql(rewritten, Optional.empty()); String checksumSql = checksumSql(getColumns(connection, statement), temporaryTableName); String dropTableSql = dropTableSql(temporaryTableName); return new Query(query.getCatalog(), query.getSchema(), ImmutableList.of(createTableAsSql), checksumSql, ImmutableList.of(dropTableSql), query.getUsername(), query.getPassword(), query.getSessionProperties()); }
@Override public Node visitCreateTableAsSelect(SqlBaseParser.CreateTableAsSelectContext context) { Optional<String> comment = Optional.empty(); if (context.COMMENT() != null) { comment = Optional.of(((StringLiteral) visit(context.string())).getValue()); } Optional<List<Identifier>> columnAliases = Optional.empty(); if (context.columnAliases() != null) { columnAliases = Optional.of(visit(context.columnAliases().identifier(), Identifier.class)); } List<Property> properties = ImmutableList.of(); if (context.properties() != null) { properties = visit(context.properties().property(), Property.class); } return new CreateTableAsSelect( getLocation(context), getQualifiedName(context.qualifiedName()), (Query) visit(context.query()), context.EXISTS() != null, properties, context.NO() == null, columnAliases, comment); }
Optional.empty(), Optional.empty()); assertStatement(queryParenthesizedWith, new CreateTableAsSelect(table, query, false, ImmutableList.of(), false, Optional.empty(), Optional.empty())); assertStatement(queryUnparenthesizedWith, new CreateTableAsSelect(table, query, false, ImmutableList.of(), false, Optional.empty(), Optional.empty())); assertStatement(queryParenthesizedWithHasAlias, new CreateTableAsSelect(table, query, false, ImmutableList.of(), false, Optional.of(ImmutableList.of(new Identifier("a"))), Optional.empty())); assertStatement(queryUnparenthesizedWithHasAlias, new CreateTableAsSelect(table, query, false, ImmutableList.of(), false, Optional.of(ImmutableList.of(new Identifier("a"))), Optional.empty()));
new CreateTableAsSelect(table, query, false, ImmutableList.of(), true, Optional.empty(), Optional.empty())); assertStatement("CREATE TABLE foo(x) AS SELECT a FROM t", new CreateTableAsSelect(table, querySelectColumn, false, ImmutableList.of(), true, Optional.of(ImmutableList.of(new Identifier("x"))), Optional.empty())); assertStatement("CREATE TABLE foo(x,y) AS SELECT a,b FROM t", new CreateTableAsSelect(table, querySelectColumns, false, ImmutableList.of(), true, Optional.of(ImmutableList.of(new Identifier("x"), new Identifier("y"))), Optional.empty())); new CreateTableAsSelect(table, query, true, ImmutableList.of(), true, Optional.empty(), Optional.empty())); assertStatement("CREATE TABLE IF NOT EXISTS foo(x) AS SELECT a FROM t", new CreateTableAsSelect(table, querySelectColumn, true, ImmutableList.of(), true, Optional.of(ImmutableList.of(new Identifier("x"))), Optional.empty())); assertStatement("CREATE TABLE IF NOT EXISTS foo(x,y) AS SELECT a,b FROM t", new CreateTableAsSelect(table, querySelectColumns, true, ImmutableList.of(), true, Optional.of(ImmutableList.of(new Identifier("x"), new Identifier("y"))), Optional.empty())); new CreateTableAsSelect(table, query, false, ImmutableList.of(), false, Optional.empty(), Optional.empty())); assertStatement("CREATE TABLE foo(x) AS SELECT a FROM t WITH NO DATA", new CreateTableAsSelect(table, querySelectColumn, false, ImmutableList.of(), false, Optional.of(ImmutableList.of(new Identifier("x"))), Optional.empty())); assertStatement("CREATE TABLE foo(x,y) AS SELECT a,b FROM t WITH NO DATA", new CreateTableAsSelect(table, querySelectColumns, false, ImmutableList.of(), false, Optional.of(ImmutableList.of(new Identifier("x"), new Identifier("y"))), Optional.empty())); "AS " + "SELECT * FROM t", new CreateTableAsSelect(table, query, false, properties, true, Optional.empty(), Optional.empty())); assertStatement("CREATE TABLE foo(x) " + "WITH ( string = 'bar', long = 42, computed = 'ban' || 'ana', a = ARRAY[ 'v1', 'v2' ] ) " + "AS " + "SELECT a FROM t", new CreateTableAsSelect(table, querySelectColumn, false, properties, true, Optional.of(ImmutableList.of(new Identifier("x"))), Optional.empty())); assertStatement("CREATE TABLE foo(x,y) " + "WITH ( string = 'bar', long = 42, computed = 'ban' || 'ana', a = ARRAY[ 'v1', 'v2' ] ) " + "AS " +
@Override public Node visitCreateTableAsSelect(SqlBaseParser.CreateTableAsSelectContext context) { Optional<String> comment = Optional.empty(); if (context.COMMENT() != null) { comment = Optional.of(((StringLiteral) visit(context.string())).getValue()); } Optional<List<Identifier>> columnAliases = Optional.empty(); if (context.columnAliases() != null) { columnAliases = Optional.of(visit(context.columnAliases().identifier(), Identifier.class)); } List<Property> properties = ImmutableList.of(); if (context.properties() != null) { properties = visit(context.properties().property(), Property.class); } return new CreateTableAsSelect( getLocation(context), getQualifiedName(context.qualifiedName()), (Query) visit(context.query()), context.EXISTS() != null, properties, context.NO() == null, columnAliases, comment); }
@Override public Node visitCreateTableAsSelect(SqlBaseParser.CreateTableAsSelectContext context) { return new CreateTableAsSelect(getLocation(context), getQualifiedName(context.qualifiedName()), (Query) visit(context.query()), context.EXISTS() != null, processTableProperties(context.tableProperties()), context.NO() == null); }
new CreateTableAsSelect(table, query, false, ImmutableMap.of(), true)); new CreateTableAsSelect(table, query, true, ImmutableMap.of(), true)); new CreateTableAsSelect(table, query, false, ImmutableMap.of(), true)); new CreateTableAsSelect(table, query, false, ImmutableMap.of(), false)); "AS " + "SELECT * FROM t", new CreateTableAsSelect(table, query, false, properties, true)); "SELECT * FROM t " + "WITH NO DATA", new CreateTableAsSelect(table, query, false, properties, false));
Optional.empty(), Optional.empty()); assertStatement(queryParenthesizedWith, new CreateTableAsSelect(table, query, false, ImmutableList.of(), false, Optional.empty(), Optional.empty())); assertStatement(queryUnparenthesizedWith, new CreateTableAsSelect(table, query, false, ImmutableList.of(), false, Optional.empty(), Optional.empty())); assertStatement(queryParenthesizedWithHasAlias, new CreateTableAsSelect(table, query, false, ImmutableList.of(), false, Optional.of(ImmutableList.of(new Identifier("a"))), Optional.empty())); assertStatement(queryUnparenthesizedWithHasAlias, new CreateTableAsSelect(table, query, false, ImmutableList.of(), false, Optional.of(ImmutableList.of(new Identifier("a"))), Optional.empty()));
new CreateTableAsSelect(table, query, false, ImmutableList.of(), true, Optional.empty(), Optional.empty())); assertStatement("CREATE TABLE foo(x) AS SELECT a FROM t", new CreateTableAsSelect(table, querySelectColumn, false, ImmutableList.of(), true, Optional.of(ImmutableList.of(new Identifier("x"))), Optional.empty())); assertStatement("CREATE TABLE foo(x,y) AS SELECT a,b FROM t", new CreateTableAsSelect(table, querySelectColumns, false, ImmutableList.of(), true, Optional.of(ImmutableList.of(new Identifier("x"), new Identifier("y"))), Optional.empty())); new CreateTableAsSelect(table, query, true, ImmutableList.of(), true, Optional.empty(), Optional.empty())); assertStatement("CREATE TABLE IF NOT EXISTS foo(x) AS SELECT a FROM t", new CreateTableAsSelect(table, querySelectColumn, true, ImmutableList.of(), true, Optional.of(ImmutableList.of(new Identifier("x"))), Optional.empty())); assertStatement("CREATE TABLE IF NOT EXISTS foo(x,y) AS SELECT a,b FROM t", new CreateTableAsSelect(table, querySelectColumns, true, ImmutableList.of(), true, Optional.of(ImmutableList.of(new Identifier("x"), new Identifier("y"))), Optional.empty())); new CreateTableAsSelect(table, query, false, ImmutableList.of(), false, Optional.empty(), Optional.empty())); assertStatement("CREATE TABLE foo(x) AS SELECT a FROM t WITH NO DATA", new CreateTableAsSelect(table, querySelectColumn, false, ImmutableList.of(), false, Optional.of(ImmutableList.of(new Identifier("x"))), Optional.empty())); assertStatement("CREATE TABLE foo(x,y) AS SELECT a,b FROM t WITH NO DATA", new CreateTableAsSelect(table, querySelectColumns, false, ImmutableList.of(), false, Optional.of(ImmutableList.of(new Identifier("x"), new Identifier("y"))), Optional.empty())); "AS " + "SELECT * FROM t", new CreateTableAsSelect(table, query, false, properties, true, Optional.empty(), Optional.empty())); assertStatement("CREATE TABLE foo(x) " + "WITH ( string = 'bar', long = 42, computed = 'ban' || 'ana', a = ARRAY[ 'v1', 'v2' ] ) " + "AS " + "SELECT a FROM t", new CreateTableAsSelect(table, querySelectColumn, false, properties, true, Optional.of(ImmutableList.of(new Identifier("x"))), Optional.empty())); assertStatement("CREATE TABLE foo(x,y) " + "WITH ( string = 'bar', long = 42, computed = 'ban' || 'ana', a = ARRAY[ 'v1', 'v2' ] ) " + "AS " +