public QueryExecution executeQuery(String project, String sqlQuery, Optional<QuerySampling> sample, String defaultSchema, ZoneId zoneId, int limit) { return executeQuery(new RequestContext(project, null), sqlQuery, sample, defaultSchema, zoneId, limit); }
public QueryExecution executeQuery(String project, String sqlQuery, ZoneId timezone) { return executeQuery(new RequestContext(project, null), sqlQuery, timezone); }
default QueryExecution executeRawStatement(String sqlQuery) { return executeRawQuery(new RequestContext(null, null), sqlQuery); }
@Override public User get() { if (user == null) { String userAttr = event.getAttribute("_user"); if (userAttr != null) { user = userStorage.getUser(new RequestContext(event.project(), null), userAttr).join(); } } return user; } };
public void createTable(String project) { String query = format("select date_trunc('minute', cast(%s as timestamp)) as _time, cast(_collection as varchar) as _collection, count(*) as total from _all group by 1, 2", checkTableColumn(projectConfig.getTimeColumn())); MaterializedView report = new MaterializedView(tableName(), format("Event explorer metrics"), query, Duration.ofHours(1), true, true, ImmutableMap.of()); materializedViewService.create(new RequestContext(project, null), report).join(); } }
@Override public void dropProjectIfExists(String project) { ProjectCollection userTable = getUserTable(project, false); String table = checkProject(userTable.project, '"') + "." + checkCollection(userTable.collection); QueryResult result = queryExecutor.executeRawStatement(new RequestContext(project, null), format("DROP TABLE IF EXISTS %s", table)).getResult().join(); propertyCache.invalidateAll(); userTypeCache.invalidateAll(); if (result.isFailed()) { throw new IllegalStateException(result.toString()); } }
@GET @Consumes("text/event-stream") @IgnoreApi @ApiOperation(value = "Analyze events asynchronously", request = QueryRequest.class, authorizations = @Authorization(value = "read_key") ) @Path("/execute") public void execute(RakamHttpRequest request, @QueryParam("read_key") String apiKey) { handleServerSentQueryExecution(request, QueryRequest.class, (project, query) -> executorService.executeQuery(new RequestContext(project, apiKey), query.query, query.sample, query.defaultSchema, query.timezone, query.limit == null ? DEFAULT_QUERY_RESULT_COUNT : query.limit)); }
@Test public void testCollectionSingleName() throws Exception { QueryResult test = getEventExplorer().getEventStatistics(new RequestContext(PROJECT_NAME, null), Optional.of(ImmutableSet.of("test")), Optional.empty(), LocalDate.ofEpochDay(0), LocalDate.ofEpochDay(SCALE_FACTOR), ZoneOffset.UTC).join(); assertFalse(test.isFailed()); assertEquals(copyOf(test.getResult()), ImmutableSet.of(of("test", 100L))); }
@Test public void testTotalStatistics() throws Exception { QueryResult test = getEventExplorer().getEventStatistics(new RequestContext(PROJECT_NAME, null), Optional.empty(), Optional.empty(), LocalDate.ofEpochDay(0), LocalDate.ofEpochDay(SCALE_FACTOR), ZoneOffset.UTC).join(); assertFalse(test.isFailed()); assertEquals(copyOf(test.getResult()), ImmutableSet.of(of("test", 100L))); }
@Override public void createProjectIfNotExists(String project, boolean userIdIsNumeric) { ProjectCollection userTable = getUserTable(project, false); String table = checkProject(userTable.project, '"') + "." + checkCollection(userTable.collection); QueryResult join = queryExecutor.executeRawStatement(new RequestContext(project, null), format("CREATE TABLE IF NOT EXISTS %s (" + " %s " + (userIdIsNumeric ? "serial" : "text") + " NOT NULL,\n" + " created_at timestamp NOT NULL,\n" + " PRIMARY KEY (%s)" + ")", table, PRIMARY_KEY, PRIMARY_KEY)).getResult().join(); if (join.isFailed()) { throw new RakamException(join.getError().toString(), INTERNAL_SERVER_ERROR); } }
@Test(invocationCount = 5) public void testIncrementalRealtime() throws Exception { MaterializedView view = new MaterializedView("testview", "testview", "select count(*) as count from test", Duration.ofDays(1), true, true, ImmutableMap.of()); getMaterializedViewService().create(new RequestContext(PROJECT_NAME), view).join(); QueryResult result = getQueryService().executeQuery(PROJECT_NAME, "select * from materialized.testview", ZoneId.systemDefault()).getResult().join(); assertFalse(result.isFailed()); assertEquals(1, result.getResult().size()); assertEquals(1, result.getResult().get(0).size()); assertEquals(SCALE_FACTOR, ((Number) result.getResult().get(0).get(0)).intValue()); }
@Test public void testCollectionNotExisting() throws Exception { QueryResult test = getEventExplorer().getEventStatistics(new RequestContext(PROJECT_NAME, null), Optional.of(ImmutableSet.of()), Optional.empty(), LocalDate.ofEpochDay(0), LocalDate.ofEpochDay(SCALE_FACTOR), ZoneOffset.UTC).join(); assertFalse(test.isFailed()); assertEquals(test.getResult(), of()); }
@Test public void testApproximate() throws Exception { QueryResult query = getFunnelQueryExecutor().query(new RequestContext(PROJECT_NAME, null), of(new FunnelStep("test0", Optional.of("teststr = 'test1'")), new FunnelStep("test1", Optional.of("teststr = 'test1'"))), Optional.empty(), Optional.empty(), LocalDate.ofEpochDay(0), LocalDate.ofEpochDay(SCALE_FACTOR), Optional.empty(), UTC, Optional.empty(), APPROXIMATE).getResult().join(); assertFalse(query.isFailed()); assertEquals(query.getResult(), of(of("Step 1", 3L), of("Step 2", 3L))); }
@Test public void testIncremental() throws Exception { MaterializedView view = new MaterializedView("testview", "testview", "select count(*) as count from test", Duration.ofDays(1), true, false, ImmutableMap.of()); getMaterializedViewService().create(new RequestContext(PROJECT_NAME), view).join(); QueryResult result = getQueryService().executeQuery(PROJECT_NAME, "select * from materialized.testview", ZoneId.systemDefault()).getResult().join(); assertFalse(result.isFailed()); assertEquals(1, result.getResult().size()); assertEquals(1, result.getResult().get(0).size()); assertEquals(SCALE_FACTOR, ((Number) result.getResult().get(0).get(0)).intValue()); }
@Test(dataProvider = "types") public void testFilter(FunnelQueryExecutor.FunnelType funnelType) throws Exception { QueryResult query = getFunnelQueryExecutor().query(new RequestContext(PROJECT_NAME, null), of(new FunnelStep("test0", Optional.of("teststr = 'test1'")), new FunnelStep("test1", Optional.of("teststr = 'test1'"))), Optional.of("teststr"), Optional.empty(), LocalDate.ofEpochDay(0), LocalDate.ofEpochDay(SCALE_FACTOR), Optional.of(new FunnelWindow(30, DAY)), UTC, Optional.empty(), funnelType).getResult().join(); assertFalse(query.isFailed()); assertEquals(query.getResult(), of(of("Step 1", "test1", 3L), of("Step 2", "test1", 3L))); }
@Test public void testSingleStepApproximate() throws Exception { QueryResult query = getFunnelQueryExecutor().query(new RequestContext(PROJECT_NAME, null), of(new FunnelStep("test0", null)), Optional.empty(), Optional.empty(), LocalDate.ofEpochDay(0), LocalDate.ofEpochDay(SCALE_FACTOR), Optional.empty(), UTC, Optional.empty(), FunnelQueryExecutor.FunnelType.APPROXIMATE).getResult().join(); assertFalse(query.isFailed()); }
@Test public void testFilterApproximate() throws Exception { QueryResult query = getFunnelQueryExecutor().query(new RequestContext(PROJECT_NAME, null), of(new FunnelStep("test0", Optional.of("teststr = 'test1'")), new FunnelStep("test1", Optional.of("teststr = 'test1'"))), Optional.of("teststr"), Optional.empty(), LocalDate.ofEpochDay(0), LocalDate.ofEpochDay(SCALE_FACTOR), Optional.empty(), UTC, Optional.empty(), APPROXIMATE).getResult().join(); assertFalse(query.isFailed()); }
@Test public void testSegment() throws Exception { QueryResult query = getFunnelQueryExecutor().query(new RequestContext(PROJECT_NAME, null), of(new FunnelStep("test0", Optional.of("teststr = 'test1'")), new FunnelStep("test1", Optional.of("teststr = 'test1'"))), Optional.of("_time"), Optional.of(FunnelQueryExecutor.FunnelTimestampSegments.DAY_OF_MONTH.value()), LocalDate.ofEpochDay(0), LocalDate.ofEpochDay(SCALE_FACTOR), Optional.empty(), UTC, Optional.empty(), NORMAL).getResult().join(); assertFalse(query.isFailed()); assertEquals(query.getResult(), of(of("Step 1", "1th day", 3L), of("Step 2", "1th day", 3L))); }
@Test public void testSegmentOrdered() throws Exception { QueryResult query = getFunnelQueryExecutor().query(new RequestContext(PROJECT_NAME, null), of(new FunnelStep("test0", Optional.of("teststr = 'test1'")), new FunnelStep("test1", Optional.of("teststr = 'test1'"))), Optional.of("_time"), Optional.of(FunnelQueryExecutor.FunnelTimestampSegments.DAY_OF_MONTH.value()), LocalDate.ofEpochDay(0), LocalDate.ofEpochDay(SCALE_FACTOR), Optional.empty(), UTC, Optional.empty(), ORDERED).getResult().join(); assertFalse(query.isFailed()); assertEquals(query.getResult(), of(of("Step 1", "1th day", 3L), of("Step 2", "1th day", 3L))); }
@Test public void testSegmentApproximate() throws Exception { QueryResult query = getFunnelQueryExecutor().query(new RequestContext(PROJECT_NAME, null), of(new FunnelStep("test0", Optional.of("teststr = 'test1'")), new FunnelStep("test1", Optional.of("teststr = 'test1'"))), Optional.of("_time"), Optional.of(FunnelQueryExecutor.FunnelTimestampSegments.DAY_OF_MONTH.value()), LocalDate.ofEpochDay(0), LocalDate.ofEpochDay(SCALE_FACTOR), Optional.empty(), UTC, Optional.empty(), APPROXIMATE).getResult().join(); assertFalse(query.isFailed()); assertEquals(query.getResult(), of(of("Step 1", "1th day", 3L), of("Step 2", "1th day", 3L))); }