@Override protected Node visitShowCatalogs(ShowCatalogs node, Void context) { List<Expression> rows = listCatalogs(session, metadata, accessControl).keySet().stream() .map(name -> row(new StringLiteral(name))) .collect(toList()); Optional<Expression> predicate = Optional.empty(); Optional<String> likePattern = node.getLikePattern(); if (likePattern.isPresent()) { predicate = Optional.of(new LikePredicate(identifier("Catalog"), new StringLiteral(likePattern.get()), Optional.empty())); } return simpleQuery( selectList(new AllColumns()), aliased(new Values(rows), "catalogs", ImmutableList.of("Catalog")), predicate, Optional.of(ordering(ascending("Catalog")))); }
public static Query singleValueQuery(String columnName, boolean value) { Relation values = values(row(value ? TRUE_LITERAL : FALSE_LITERAL)); return simpleQuery( selectList(new AllColumns()), aliased(values, "t", ImmutableList.of(columnName))); }
public static Query simpleQuery(Select select, Relation from, Optional<Expression> where, Optional<GroupBy> groupBy, Optional<Expression> having, Optional<OrderBy> orderBy, Optional<String> limit) { return query(new QuerySpecification( select, Optional.of(from), where, groupBy, having, orderBy, limit)); }
@Override protected Node visitShowColumns(ShowColumns showColumns, Void context) { QualifiedObjectName tableName = createQualifiedObjectName(session, showColumns, showColumns.getTable()); if (!metadata.getView(session, tableName).isPresent() && !metadata.getTableHandle(session, tableName).isPresent()) { throw new SemanticException(MISSING_TABLE, showColumns, "Table '%s' does not exist", tableName); } return simpleQuery( selectList( aliasedName("column_name", "Column"), aliasedName("data_type", "Type"), aliasedNullToEmpty("extra_info", "Extra"), aliasedNullToEmpty("comment", "Comment")), from(tableName.getCatalogName(), TABLE_COLUMNS), logicalAnd( equal(identifier("table_schema"), new StringLiteral(tableName.getSchemaName())), equal(identifier("table_name"), new StringLiteral(tableName.getObjectName()))), ordering(ascending("ordinal_position"))); }
@Test public void testPrepare() { Query query = simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("foo"))); String sqlString = "PREPARE my_query FROM SELECT * FROM foo"; Map<String, String> statements = executePrepare("my_query", query, sqlString, TEST_SESSION); assertEquals(statements, ImmutableMap.of("my_query", "SELECT *\nFROM\n foo\n")); }
@Test public void testQuantifiedComparison() { assertExpression("col1 < ANY (SELECT col2 FROM table1)", new QuantifiedComparisonExpression( LESS_THAN, QuantifiedComparisonExpression.Quantifier.ANY, identifier("col1"), new SubqueryExpression(simpleQuery(selectList(new SingleColumn(identifier("col2"))), table(QualifiedName.of("table1")))))); assertExpression("col1 = ALL (VALUES ROW(1), ROW(2))", new QuantifiedComparisonExpression( ComparisonExpression.Operator.EQUAL, QuantifiedComparisonExpression.Quantifier.ALL, identifier("col1"), new SubqueryExpression(query(values(row(new LongLiteral("1")), row(new LongLiteral("2"))))))); assertExpression("col1 >= SOME (SELECT 10)", new QuantifiedComparisonExpression( ComparisonExpression.Operator.GREATER_THAN_OR_EQUAL, QuantifiedComparisonExpression.Quantifier.SOME, identifier("col1"), new SubqueryExpression(simpleQuery(selectList(new LongLiteral("10")))))); }
@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)); }
rows.add(row( new StringLiteral(sessionProperty.getFullyQualifiedName()), new StringLiteral(nullToEmpty(value)), rows.add(row(new StringLiteral(""), new StringLiteral(""), new StringLiteral(""), new StringLiteral(""), new StringLiteral(""), FALSE_LITERAL)); return simpleQuery( selectList( aliasedName("name", "Name"), aliasedName("value", "Value"), aliasedName("default", "Default"), aliasedName("type", "Type"), aliasedName("description", "Description")), aliased( new Values(rows.build()), "session", ImmutableList.of("name", "value", "default", "type", "description", "include")), identifier("include"));
rows.add(row( new StringLiteral(function.getSignature().getName()), new StringLiteral(function.getSignature().getReturnType().toString()), .build(); return simpleQuery( selectAll(columns.entrySet().stream() .map(entry -> aliasedName(entry.getKey(), entry.getValue())) .collect(toImmutableList())), aliased(new Values(rows.build()), "functions", ImmutableList.copyOf(columns.keySet())), ordering( new SortItem( functionCall("lower", identifier("function_name")), SortItem.Ordering.ASCENDING, SortItem.NullOrdering.UNDEFINED), ascending("return_type"), ascending("argument_types"), ascending("function_type")));
public static SelectItem aliasedName(String name, String alias) { return new SingleColumn(identifier(name), identifier(alias)); }
@Test public void testLimitAll() { Query valuesQuery = query(values( row(new LongLiteral("1"), new StringLiteral("1")), row(new LongLiteral("2"), new StringLiteral("2")))); assertStatement("SELECT * FROM (VALUES (1, '1'), (2, '2')) LIMIT ALL", simpleQuery(selectList(new AllColumns()), subquery(valuesQuery), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of("ALL"))); }
Optional.empty(), Optional.empty(), Optional.of(new OrderBy(ImmutableList.of(new SortItem(identifier("x"), DESCENDING, UNDEFINED)))), false, ImmutableList.of(identifier("x")))); assertStatement("SELECT array_agg(x ORDER BY t.y) FROM t", new Query( Optional.empty(), new QuerySpecification( selectList( new FunctionCall( QualifiedName.of("array_agg"), Optional.empty(), Optional.empty(), Optional.of(new OrderBy(ImmutableList.of(new SortItem(new DereferenceExpression(new Identifier("t"), identifier("y")), ASCENDING, UNDEFINED)))), false, ImmutableList.of(new Identifier("x")))), Optional.of(table(QualifiedName.of("t"))), Optional.empty(), Optional.empty(),
public static Query simpleQuery(Select select, Relation from, Expression where, OrderBy orderBy) { return simpleQuery(select, from, Optional.of(where), Optional.of(orderBy)); }
@Test public void testImplicitJoin() { assertStatement("SELECT * FROM a, b", simpleQuery(selectList(new AllColumns()), new Join(Join.Type.IMPLICIT, new Table(QualifiedName.of("a")), new Table(QualifiedName.of("b")), Optional.empty()))); }
private Node rewriteShowStats(ShowStats node, Table table, Constraint<ColumnHandle> constraint) { TableHandle tableHandle = getTableHandle(node, table.getName()); TableStatistics tableStatistics = metadata.getTableStatistics(session, tableHandle, constraint); List<String> statsColumnNames = buildColumnsNames(); List<SelectItem> selectItems = buildSelectItems(statsColumnNames); TableMetadata tableMetadata = metadata.getTableMetadata(session, tableHandle); Map<String, ColumnHandle> columnHandles = metadata.getColumnHandles(session, tableHandle); List<Expression> resultRows = buildStatisticsRows(tableMetadata, columnHandles, tableStatistics); return simpleQuery(selectAll(selectItems), aliased(new Values(resultRows), "table_stats_for_" + table.getName(), statsColumnNames)); }
new WithQuery(identifier("t"), query(new Values(ImmutableList.of(new LongLiteral("1")))), Optional.of(ImmutableList.of(identifier("x"))))))), new Table(QualifiedName.of("t")), Optional.empty(),
@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"))); }
public static Select selectList(Expression... expressions) { return selectList(asList(expressions)); }
private static Row createDescribeInputRow(Parameter parameter, Analysis queryAnalysis) { Type type = queryAnalysis.getCoercion(parameter); if (type == null) { type = UNKNOWN; } return row( new LongLiteral(Integer.toString(parameter.getPosition())), new StringLiteral(type.getTypeSignature().getBase())); }
private static Relation from(String catalog, SchemaTableName table) { return table(QualifiedName.of(catalog, table.getSchemaName(), table.getTableName())); }