public static SelectItem aliasedName(String name, String alias) { return new SingleColumn(identifier(name), identifier(alias)); }
public static Relation aliased(Relation relation, String alias, List<String> columnAliases) { return new AliasedRelation( relation, identifier(alias), columnAliases.stream() .map(QueryUtil::identifier) .collect(Collectors.toList())); }
public static Relation aliased(Relation relation, String alias, List<String> columnAliases) { return new AliasedRelation( relation, identifier(alias), columnAliases.stream() .map(QueryUtil::identifier) .collect(Collectors.toList())); }
public static SelectItem unaliasedName(String name) { return new SingleColumn(identifier(name)); }
public static SelectItem aliasedNullToEmpty(String column, String alias) { return new SingleColumn(new CoalesceExpression(identifier(column), new StringLiteral("")), identifier(alias)); }
@Test public void testRenameColumn() { assertStatement("ALTER TABLE foo.t RENAME COLUMN a TO b", new RenameColumn(QualifiedName.of("foo", "t"), identifier("a"), identifier("b"))); }
@Test public void testExecute() { assertStatement("EXECUTE myquery", new Execute(identifier("myquery"), emptyList())); }
@Test public void testShowSchemas() { assertStatement("SHOW SCHEMAS", new ShowSchemas(Optional.empty(), Optional.empty(), Optional.empty())); assertStatement("SHOW SCHEMAS FROM foo", new ShowSchemas(Optional.of(identifier("foo")), Optional.empty(), Optional.empty())); assertStatement("SHOW SCHEMAS IN foo LIKE '%'", new ShowSchemas(Optional.of(identifier("foo")), Optional.of("%"), Optional.empty())); assertStatement("SHOW SCHEMAS IN foo LIKE '%$_%' ESCAPE '$'", new ShowSchemas(Optional.of(identifier("foo")), Optional.of("%$_%"), Optional.of("$"))); }
@Test public void testDeallocatePrepare() { assertStatement("DEALLOCATE PREPARE myquery", new Deallocate(identifier("myquery"))); }
@Test public void testDescribeOutput() { assertStatement("DESCRIBE OUTPUT myquery", new DescribeOutput(identifier("myquery"))); }
@Test public void testDescribeInput() { assertStatement("DESCRIBE INPUT myquery", new DescribeInput(identifier("myquery"))); }
@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); }
@Test public void testAddColumn() { assertStatement("ALTER TABLE foo.t ADD COLUMN c bigint", new AddColumn(QualifiedName.of("foo", "t"), new ColumnDefinition(identifier("c"), "bigint", emptyList(), Optional.empty()))); }
@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); }
@Test public void testInsertInto() { QualifiedName table = QualifiedName.of("a"); Query query = simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("t"))); assertStatement("INSERT INTO a SELECT * FROM t", new Insert(table, Optional.empty(), query)); assertStatement("INSERT INTO a (c1, c2) SELECT * FROM t", new Insert(table, Optional.of(ImmutableList.of(identifier("c1"), identifier("c2"))), query)); }
@Test public void testRenameSchema() { assertStatement("ALTER SCHEMA foo RENAME TO bar", new RenameSchema(QualifiedName.of("foo"), identifier("bar"))); assertStatement("ALTER SCHEMA foo.bar RENAME TO baz", new RenameSchema(QualifiedName.of("foo", "bar"), identifier("baz"))); assertStatement("ALTER SCHEMA \"awesome schema\".\"awesome table\" RENAME TO \"even more awesome table\"", new RenameSchema(QualifiedName.of("awesome schema", "awesome table"), quotedIdentifier("even more awesome table"))); }
@Test public void testExecuteWithUsing() { assertStatement("EXECUTE myquery USING 1, 'abc', ARRAY ['hello']", new Execute(identifier("myquery"), ImmutableList.of(new LongLiteral("1"), new StringLiteral("abc"), new ArrayConstructor(ImmutableList.of(new StringLiteral("hello")))))); }
@Test public void testDropColumn() { assertStatement("ALTER TABLE foo.t DROP COLUMN c", new DropColumn(QualifiedName.of("foo", "t"), identifier("c"))); assertStatement("ALTER TABLE \"t x\" DROP COLUMN \"c d\"", new DropColumn(QualifiedName.of("t x"), quotedIdentifier("c d"))); }
@Test public void testPrepare() { assertStatement("PREPARE myquery FROM select * from foo", new Prepare(identifier("myquery"), simpleQuery( selectList(new AllColumns()), table(QualifiedName.of("foo"))))); }
@Test public void testPrepareWithParameters() { assertStatement("PREPARE myquery FROM SELECT ?, ? FROM foo", new Prepare(identifier("myquery"), simpleQuery( selectList(new Parameter(0), new Parameter(1)), table(QualifiedName.of("foo"))))); }