public boolean canResolve(QualifiedName name) { if (!this.name.isPresent()) { return false; } // TODO: need to know whether the qualified name and the name of this field were quoted return matchesPrefix(name.getPrefix()) && this.name.get().equalsIgnoreCase(name.getSuffix()); }
@Test public void testCreateTableAsSelectDifferentCatalog() throws Exception { handle.execute("CREATE TABLE \"my_test_table2\" (column1 BIGINT, column2 DOUBLE)"); SqlParser parser = new SqlParser(); Query query = new Query(CATALOG, SCHEMA, ImmutableList.of(), "CREATE TABLE public.my_test_table2 AS SELECT 1 column1, 2E0 column2", ImmutableList.of(), null, null, ImmutableMap.of()); QueryRewriter rewriter = new QueryRewriter(parser, URL, QualifiedName.of("other_catalog", "other_schema", "tmp_"), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), 1, new Duration(10, SECONDS)); Query rewrittenQuery = rewriter.shadowQuery(query); assertEquals(rewrittenQuery.getPreQueries().size(), 1); CreateTableAsSelect createTableAs = (CreateTableAsSelect) parser.createStatement(rewrittenQuery.getPreQueries().get(0)); assertEquals(createTableAs.getName().getParts().size(), 3); assertEquals(createTableAs.getName().getPrefix().get(), QualifiedName.of("other_catalog", "other_schema")); assertTrue(createTableAs.getName().getSuffix().startsWith("tmp_")); assertFalse(createTableAs.getName().getSuffix().contains("my_test_table")); }
@Override protected Scope visitTable(Table table, Optional<Scope> scope) if (!table.getName().getPrefix().isPresent()) {
CreateTable createTable = (CreateTable) parser.createStatement(rewrittenQuery.getPreQueries().get(0)); assertEquals(createTable.getName().getParts().size(), 3); assertEquals(createTable.getName().getPrefix().get(), QualifiedName.of("other_catalog", "other_schema")); assertTrue(createTable.getName().getSuffix().startsWith("tmp_")); assertFalse(createTable.getName().getSuffix().contains("test_insert_table"));
private Function<QualifiedName, String> tableNameMapper(RequestContext context, Map<MaterializedView, MaterializedViewExecution> materializedViews, Optional<QuerySampling> sample, String defaultSchema, Map<String, String> sessionParameters) { return (node) -> { if (node.getPrefix().isPresent() && node.getPrefix().get().toString().equals("materialized")) { MaterializedView materializedView; try { materializedView = materializedViewService.get(context.project, node.getSuffix()); } catch (NotExistsException e) { throw new MaterializedViewNotExists(node.getSuffix()); } MaterializedViewExecution materializedViewExecution = materializedViews.computeIfAbsent(materializedView, (key) -> materializedViewService.lockAndUpdateView(context, materializedView)); if (materializedViewExecution == null) { throw new IllegalStateException(); } return materializedViewExecution.computeQuery; } if (!node.getPrefix().isPresent() && defaultSchema != null) { node = QualifiedName.of(defaultSchema, node.getSuffix()); } return executor.formatTableReference(context.project, node, sample, sessionParameters); }; }
@Override protected Void visitTable(Table node, Integer indent) { if (!node.getName().getPrefix().isPresent() && queryWithTables.contains(node.getName().getSuffix().toLowerCase(Locale.ENGLISH))) { builder.append(formatName(node.getName().toString(), escapeIdentifier)); return null; } builder.append(tableNameMapper.apply(node.getName())); return null; }
private QueryExecution getSingleQueryExecution(String query, List<CustomDataSource> type) { char seperator = dbSeparator(type.get(0).type); StringBuilder builder = new StringBuilder(); Statement statement = sqlParser.createStatement(query, new ParsingOptions()); try { new RakamSqlFormatter.Formatter(builder, qualifiedName -> { String schema = qualifiedName.getPrefix().get().toString(); CustomDataSource customDataSource1 = type.stream() .filter(e -> e.schemaName.equals(schema)).findAny() .orElseThrow(() -> new RakamException("Cross database operations are not supported.", BAD_REQUEST)); return SupportedCustomDatabase.getAdapter(customDataSource1.type).getTableMapper() .apply(customDataSource1.options, qualifiedName.getSuffix()); }, seperator) { }.process(statement, 1); } catch (UnsupportedOperationException e) { return null; } String sqlQuery = builder.toString(); return new JDBCQueryExecution(() -> SupportedCustomDatabase.getAdapter(type.get(0).type).getDataSource().openConnection(type.get(0).options), sqlQuery, false, Optional.empty(), false); }
@Override public String formatTableReference(String project, QualifiedName node, Optional<QuerySampling> sample, Map<String, String> sessionParameters) { String prefix = node.getPrefix().map(e -> e.toString()).orElse(null); String suffix = node.getSuffix(); if ("materialized".equals(prefix)) {
@Override public String formatTableReference(String project, QualifiedName name, Optional<QuerySampling> sample, Map<String, String> sessionParameters) { if (name.getPrefix().isPresent()) { String prefix = name.getPrefix().get().toString(); switch (prefix) { case "collection": default: if (customDataSource == null) { throw new RakamException("Schema does not exist: " + name.getPrefix().get().toString(), BAD_REQUEST); if (name.getSuffix().equals("_all") && !name.getPrefix().isPresent()) { List<Map.Entry<String, List<SchemaField>>> collections = metastore.getCollections(project).entrySet().stream() .collect(Collectors.toList());
if (name.getPrefix().map(prefix -> prefix.equals("collection")).orElse(true)) { referencedCollections.add(name.getSuffix());
private void assertColumnPrefix(QualifiedName qualifiedName, Expression node) { // Recursively check if its prefix is a column. while (qualifiedName.getPrefix().isPresent()) { qualifiedName = qualifiedName.getPrefix().get(); List<Field> matches = tupleDescriptor.resolveFields(qualifiedName); if (!matches.isEmpty()) { // The AMBIGUOUS_ATTRIBUTE exception will be thrown later with the right node if matches.size() > 1 return; } } throw createMissingAttributeException(node); }
public boolean canResolve(QualifiedName name) { if (!this.name.isPresent()) { return false; } // TODO: need to know whether the qualified name and the name of this field were quoted return matchesPrefix(name.getPrefix()) && this.name.get().equalsIgnoreCase(name.getSuffix()); }
public static Symbol fromQualifiedName(QualifiedName name) { Preconditions.checkArgument(!name.getPrefix().isPresent(), "Can't create a symbol from a qualified name with prefix"); return new Symbol(name.getSuffix()); }
/** * This is basically called for every identifier. Capture is by default * false, except when we tell it to be true (which is in the case of when * we call process from within these overrides.) This is so then we do not * translate identifiers which are apart of joins, or other under-the-hood * things we insert. */ @Override public Void visitQualifiedNameReference( QualifiedNameReference node, Boolean capture) { if (capture) { // If it has a table name on it, decide if we need to include it in // the translation. if (node.getName().getPrefix().isPresent()) { // Include it in the translation only if it would be ambiguous // what table the column is coming from if we didn't. if (isAmbiguousWithoutTable( node.getSuffix().toString(), inferrer)) { String prefix = node.getName().getPrefix().get().toString(); // Remove any trailing s's for grammar's sake. if (prefix.endsWith("s")) { prefix = prefix.substring(0, prefix.length()-1); } translation.append(prefix).append(" "); } } translation.append(node.getSuffix()); } return null; }
public String formatTableReference(String project, QualifiedName node, Optional<Sample> sample) if (node.getPrefix().isPresent()) { String prefix = node.getPrefix().get().toString(); if (prefix.equals("continuous")) { return ".`" + project + "`.`$continuous_" + checkCollection(node.getSuffix(), '`'); if (node.getSuffix().equals("_all") && !node.getPrefix().isPresent()) { List<Map.Entry<String, List<SchemaField>>> collections = metastore.getCollections(project).entrySet().stream() .filter(c -> !c.getKey().startsWith("_"))
@Override protected Object visitQualifiedNameReference(QualifiedNameReference node, Object context) { if (node.getName().getPrefix().isPresent()) { // not a symbol return node; } Symbol symbol = Symbol.fromQualifiedName(node.getName()); return ((SymbolResolver) context).getValue(symbol); }
if (expression instanceof QualifiedNameReference && !((QualifiedNameReference) expression).getName().getPrefix().isPresent()) {
@Override protected RelationType visitTable(Table table, AnalysisContext context) if (!table.getName().getPrefix().isPresent()) {