@Override protected Void visitDereferenceExpression(DereferenceExpression node, Integer indentLevel) { QualifiedName resolved = resolvedNameReferences.get(node); String resolvedName = ""; if (resolved != null) { resolvedName = "=>" + resolved.toString(); } print(indentLevel, "DereferenceExpression[" + node + resolvedName + "]"); return null; }
@Override protected Void visitIdentifier(Identifier node, Integer indentLevel) { QualifiedName resolved = resolvedNameReferences.get(node); String resolvedName = ""; if (resolved != null) { resolvedName = "=>" + resolved.toString(); } print(indentLevel, "Identifier[" + node.getValue() + resolvedName + "]"); return null; }
private static boolean isSupportedSpatialFunction(FunctionCall functionCall) { String functionName = functionCall.getName().toString(); return functionName.equalsIgnoreCase(ST_CONTAINS) || functionName.equalsIgnoreCase(ST_WITHIN) || functionName.equalsIgnoreCase(ST_INTERSECTS); }
private static boolean isSTDistance(Expression expression) { if (expression instanceof FunctionCall) { return ((FunctionCall) expression).getName().toString().equalsIgnoreCase(ST_DISTANCE); } return false; }
private static boolean hasSpatialPartitioningAggregation(AggregationNode aggregation) { return aggregation.getAggregations().values().stream() .map(Aggregation::getCall) .anyMatch(call -> call.getName().toString().equals(NAME) && call.getArguments().size() == 1); }
private SpatialPredicate spatialTest(FunctionCall functionCall, boolean probeFirst, Optional<ComparisonExpression.Operator> comparisonOperator) switch (functionCall.getName().toString().toLowerCase(Locale.ENGLISH)) { case ST_CONTAINS: if (probeFirst) {
@Override public ListenableFuture<?> execute(ResetSession statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, QueryStateMachine stateMachine, List<Expression> parameters) { List<String> parts = statement.getName().getParts(); if (parts.size() > 2) { throw new SemanticException(INVALID_SESSION_PROPERTY, statement, "Invalid session property '%s'", statement.getName()); } // validate the property name if (parts.size() == 1) { metadata.getSessionPropertyManager().getSystemSessionPropertyMetadata(parts.get(0)) .orElseThrow(() -> new SemanticException(INVALID_SESSION_PROPERTY, statement, "Session property %s does not exist", statement.getName())); } else { ConnectorId connectorId = metadata.getCatalogHandle(stateMachine.getSession(), parts.get(0)) .orElseThrow(() -> new SemanticException(MISSING_CATALOG, statement, "Catalog %s does not exist", parts.get(0))); metadata.getSessionPropertyManager().getConnectorSessionPropertyMetadata(connectorId, parts.get(1)) .orElseThrow(() -> new SemanticException(INVALID_SESSION_PROPERTY, statement, "Session property %s does not exist", statement.getName())); } stateMachine.addResetSessionProperties(statement.getName().toString()); return immediateFuture(null); } }
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()); }
if (!ALLOWED_FUNCTIONS.contains(aggregation.getCall().getName().toString()) && !aggregation.getCall().isDistinct()) { return context.defaultRewrite(node);
stateMachine.addSetSessionProperties(propertyName.toString(), value);
@Test public void testQualifiedName() { assertEquals(QualifiedName.of("a", "b", "c", "d").toString(), "a.b.c.d"); assertEquals(QualifiedName.of("A", "b", "C", "d").toString(), "a.b.c.d"); assertTrue(QualifiedName.of("a", "b", "c", "d").hasSuffix(QualifiedName.of("b", "c", "d"))); assertTrue(QualifiedName.of("a", "b", "c", "d").hasSuffix(QualifiedName.of("a", "b", "c", "d"))); assertFalse(QualifiedName.of("a", "b", "c", "d").hasSuffix(QualifiedName.of("a", "c", "d"))); assertFalse(QualifiedName.of("a", "b", "c", "d").hasSuffix(QualifiedName.of("z", "a", "b", "c", "d"))); assertEquals(QualifiedName.of("a", "b", "c", "d"), QualifiedName.of("a", "b", "c", "d")); }
if (name.toString().equalsIgnoreCase("if")) { check(context.expression().size() == 2 || context.expression().size() == 3, "Invalid number of arguments for 'if' function", context); check(!window.isPresent(), "OVER clause not valid for 'if' function", context); if (name.toString().equalsIgnoreCase("nullif")) { check(context.expression().size() == 2, "Invalid number of arguments for 'nullif' function", context); check(!window.isPresent(), "OVER clause not valid for 'nullif' function", context); if (name.toString().equalsIgnoreCase("coalesce")) { check(context.expression().size() >= 2, "The 'coalesce' function must have at least two arguments", context); check(!window.isPresent(), "OVER clause not valid for 'coalesce' function", context); if (name.toString().equalsIgnoreCase("try")) { check(context.expression().size() == 1, "The 'try' function must have exactly one argument", context); check(!window.isPresent(), "OVER clause not valid for 'try' function", context); if (name.toString().equalsIgnoreCase("$internal$bind")) { check(context.expression().size() >= 1, "The '$internal$bind' function must have at least one arguments", context); check(!window.isPresent(), "OVER clause not valid for '$internal$bind' function", context);
FunctionCall call = aggregation.getCall(); QualifiedName name = call.getName(); if (name.toString().equals(NAME) && call.getArguments().size() == 1) { Expression geometry = getOnlyElement(call.getArguments()); Symbol envelopeSymbol = context.getSymbolAllocator().newSymbol("envelope", metadata.getType(GEOMETRY_TYPE_SIGNATURE)); if (geometry instanceof FunctionCall && ((FunctionCall) geometry).getName().toString().equalsIgnoreCase("ST_Envelope")) { envelopeAssignments.put(envelopeSymbol, geometry);
private void testSetSessionWithParameters(String property, Expression expression, String expectedValue, List<Expression> parameters) { QualifiedName qualifiedPropName = QualifiedName.of(CATALOG_NAME, property); QueryStateMachine stateMachine = QueryStateMachine.begin( format("set %s = 'old_value'", qualifiedPropName), TEST_SESSION, URI.create("fake://uri"), new ResourceGroupId("test"), false, transactionManager, accessControl, executor, metadata, WarningCollector.NOOP); getFutureValue(new SetSessionTask().execute(new SetSession(qualifiedPropName, expression), transactionManager, metadata, accessControl, stateMachine, parameters)); Map<String, String> sessionProperties = stateMachine.getSetSessionProperties(); assertEquals(sessionProperties, ImmutableMap.of(qualifiedPropName.toString(), expectedValue)); } }
checkState(outputColumnType.isOrderable(), "Subquery result type must be orderable"); Symbol minValue = symbolAllocator.newSymbol(MIN.toString(), outputColumnType); Symbol maxValue = symbolAllocator.newSymbol(MAX.toString(), outputColumnType); Symbol countAllValue = symbolAllocator.newSymbol("count_all", BigintType.BIGINT); Symbol countNonNullValue = symbolAllocator.newSymbol("count_non_null", BigintType.BIGINT);
private PlanNode rewriteToDefaultAggregation(ApplyNode parent, Context context) { Symbol count = context.getSymbolAllocator().newSymbol(COUNT.toString(), BIGINT); Symbol exists = getOnlyElement(parent.getSubqueryAssignments().getSymbols()); return new LateralJoinNode( parent.getId(), parent.getInput(), new ProjectNode( context.getIdAllocator().getNextId(), new AggregationNode( context.getIdAllocator().getNextId(), parent.getSubquery(), ImmutableMap.of(count, new Aggregation(COUNT_CALL, countSignature, Optional.empty())), globalAggregation(), ImmutableList.of(), AggregationNode.Step.SINGLE, Optional.empty(), Optional.empty()), Assignments.of(exists, new ComparisonExpression(GREATER_THAN, count.toSymbolReference(), new Cast(new LongLiteral("0"), BIGINT.toString())))), parent.getCorrelation(), INNER, parent.getOriginSubquery()); } }
@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); }
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); }; }
@Test public void testName() throws Exception { String sql = "select selami:timestamp, melami:varchar from deneme where ali:timestamp is not null and veli is null group by demo"; SqlParserOptions options = new SqlParserOptions().allowIdentifierSymbol(IdentifierSymbol.COLON); Statement statement = new SqlParser(options).createStatement(sql, new ParsingOptions(ParsingOptions.DecimalLiteralTreatment.AS_DOUBLE)); String s = RakamSqlFormatter.formatSql(statement, name -> String.format("(SELECT * FROM events WHERE collection_name = '%s')", name.toString()), name -> "\"$data\"['" + name + "']", '"'); System.out.println(s); } }