LogicalGenerator extends SqlBaseBaseListener { long parseTimeFormat(String timestampStr) throws SQLParserException { if (timestampStr == null || timestampStr.trim().equals("")) { throw new SQLParserException("input timestamp cannot be empty"); } long startupNano = IoTDBDescriptor.getInstance().getConfig().getStartUpNanosecond(); if (timestampStr.equalsIgnoreCase(SQLConstant.NOW_FUNC)) { String timePrecision = IoTDBDescriptor.getInstance().getConfig().getTimestampPrecision(); switch (timePrecision) { case "ns": return System.currentTimeMillis() * 1000_000 + (System.nanoTime() - startupNano) % 1000_000; case "us": return System.currentTimeMillis() * 1000 + (System.nanoTime() - startupNano) / 1000 % 1000; default: return System.currentTimeMillis(); } } try { return DatetimeUtils.convertDatetimeStrToLong(timestampStr, zoneId); } catch (Exception e) { throw new SQLParserException(String .format("Input time format %s error. " + "Input like yyyy-MM-dd HH:mm:ss, yyyy-MM-ddTHH:mm:ss or " + "refer to user document for more info.", timestampStr)); } } LogicalGenerator(ZoneId zoneId); @Override void enterCountTimeseries(CountTimeseriesContext ctx); @Override void enterCountDevices(CountDevicesContext ctx); @Override void enterCountStorageGroup(CountStorageGroupContext ctx); @Override void enterFlush(FlushContext ctx); @Override void enterTracingOn(TracingOnContext ctx); @Override void enterTracingOff(TracingOffContext ctx); @Override void enterMerge(MergeContext ctx); @Override void enterFullMerge(FullMergeContext ctx); @Override void enterClearcache(SqlBaseParser.ClearcacheContext ctx); @Override void enterCountNodes(CountNodesContext ctx); @Override void enterShowDevices(ShowDevicesContext ctx); @Override void enterShowChildPaths(ShowChildPathsContext ctx); @Override void enterShowStorageGroup(ShowStorageGroupContext ctx); @Override void enterLoadFiles(LoadFilesContext ctx); @Override void enterMoveFile(MoveFileContext ctx); @Override void enterRemoveFile(RemoveFileContext ctx); @Override void enterLoadConfigurationStatement(LoadConfigurationStatementContext ctx); @Override void enterShowVersion(ShowVersionContext ctx); @Override void enterShowDynamicParameter(SqlBaseParser.ShowDynamicParameterContext ctx); @Override void enterShowFlushTaskInfo(SqlBaseParser.ShowFlushTaskInfoContext ctx); @Override void enterShowTimeseries(ShowTimeseriesContext ctx); @Override void enterCreateTimeseries(CreateTimeseriesContext ctx); @Override void enterAlterTimeseries(SqlBaseParser.AlterTimeseriesContext ctx); @Override void enterAlterClause(SqlBaseParser.AlterClauseContext ctx); @Override void enterAlias(AliasContext ctx); @Override void enterCreateUser(CreateUserContext ctx); @Override void enterCreateRole(CreateRoleContext ctx); @Override void enterAlterUser(AlterUserContext ctx); @Override void enterDropUser(DropUserContext ctx); @Override void enterDropRole(DropRoleContext ctx); @Override void enterGrantUser(GrantUserContext ctx); @Override void enterGrantRole(GrantRoleContext ctx); @Override void enterRevokeUser(RevokeUserContext ctx); @Override void enterRevokeRole(RevokeRoleContext ctx); @Override void enterGrantRoleToUser(GrantRoleToUserContext ctx); @Override void enterRevokeRoleFromUser(RevokeRoleFromUserContext ctx); @Override void enterLoadStatement(LoadStatementContext ctx); @Override void enterGrantWatermarkEmbedding(GrantWatermarkEmbeddingContext ctx); @Override void enterRevokeWatermarkEmbedding(RevokeWatermarkEmbeddingContext ctx); @Override void enterListUser(ListUserContext ctx); @Override void enterListRole(ListRoleContext ctx); @Override void enterListPrivilegesUser(ListPrivilegesUserContext ctx); @Override void enterListPrivilegesRole(ListPrivilegesRoleContext ctx); @Override void enterListUserPrivileges(ListUserPrivilegesContext ctx); @Override void enterListRolePrivileges(ListRolePrivilegesContext ctx); @Override void enterListAllRoleOfUser(ListAllRoleOfUserContext ctx); @Override void enterListAllUserOfRole(ListAllUserOfRoleContext ctx); @Override void enterSetTTLStatement(SetTTLStatementContext ctx); @Override void enterUnsetTTLStatement(UnsetTTLStatementContext ctx); @Override void enterShowTTLStatement(ShowTTLStatementContext ctx); @Override void enterShowAllTTLStatement(ShowAllTTLStatementContext ctx); @Override void enterDeleteTimeseries(DeleteTimeseriesContext ctx); @Override void enterSetStorageGroup(SetStorageGroupContext ctx); @Override void enterDeleteStorageGroup(DeleteStorageGroupContext ctx); @Override void enterDeleteStatement(DeleteStatementContext ctx); @Override void enterDisableAlign(SqlBaseParser.DisableAlignContext ctx); @SuppressWarnings("squid:S3776") // Suppress high Cognitive Complexity warning @Override void enterGroupByFillClause(SqlBaseParser.GroupByFillClauseContext ctx); @Override void enterGroupByTimeClause(GroupByTimeClauseContext ctx); @Override void enterGroupByLevelClause(SqlBaseParser.GroupByLevelClauseContext ctx); @Override void enterFillClause(FillClauseContext ctx); @Override void enterAlignByDeviceClause(AlignByDeviceClauseContext ctx); @Override void enterLimitClause(LimitClauseContext ctx); @Override void enterOffsetClause(OffsetClauseContext ctx); @Override void enterOrderByTimeClause(OrderByTimeClauseContext ctx); @Override void enterSlimitClause(SlimitClauseContext ctx); @Override void enterSoffsetClause(SoffsetClauseContext ctx); @Override void enterInsertColumnSpec(InsertColumnSpecContext ctx); @Override void enterInsertValuesSpec(InsertValuesSpecContext ctx); @Override void enterAttributeClauses(AttributeClausesContext ctx); @Override void enterAliasClause(SqlBaseParser.AliasClauseContext ctx); @Override void enterAttributeClause(AttributeClauseContext ctx); @Override void enterTagClause(TagClauseContext ctx); @Override void enterInsertStatement(InsertStatementContext ctx); @Override void enterUpdateStatement(UpdateStatementContext ctx); @Override void enterSelectStatement(SelectStatementContext ctx); @Override void enterFromClause(FromClauseContext ctx); @Override void enterFunctionElement(FunctionElementContext ctx); @Override void enterSelectElement(SelectElementContext ctx); @Override void enterLastElement(SqlBaseParser.LastElementContext ctx); @Override void enterAsElement(AsElementContext ctx); void parseAsClause(List<AsClauseContext> asClauseContexts); @Override void enterFunctionAsElement(FunctionAsElementContext ctx); @Override void enterSetCol(SetColContext ctx); @Override void enterWhereClause(WhereClauseContext ctx); @Override void enterShowWhereClause(ShowWhereClauseContext ctx); @Override void enterShowMergeStatus(ShowMergeStatusContext ctx); @Override void enterDeletePartition(DeletePartitionContext ctx); @Override void enterCreateSnapshot(CreateSnapshotContext ctx);  }
@Test public void testParseTimeFormatNow() throws LogicalOperatorException { long now = generator.parseTimeFormat(SQLConstant.NOW_FUNC); for (int i = 0; i <= 12; i++) { ZoneOffset offset1, offset2; if (i < 10) { offset1 = ZoneOffset.of("+0" + i + ":00"); offset2 = ZoneOffset.of("-0" + i + ":00"); } else { offset1 = ZoneOffset.of("+" + i + ":00"); offset2 = ZoneOffset.of("-" + i + ":00"); } ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(now), ZoneId.of(offset1.toString())); assertEquals(now, zonedDateTime.toInstant().toEpochMilli()); zonedDateTime = ZonedDateTime .ofInstant(Instant.ofEpochMilli(now), ZoneId.of(offset2.toString())); assertEquals(now, zonedDateTime.toInstant().toEpochMilli()); } }
@Test public void testParseTimeFormatNowPrecision() throws LogicalOperatorException { String timePrecision = IoTDBDescriptor.getInstance().getConfig().getTimestampPrecision(); IoTDBDescriptor.getInstance().getConfig().setTimestampPrecision("ms"); long now_ms = generator.parseTimeFormat(SQLConstant.NOW_FUNC); String ms_str = String.valueOf(now_ms); IoTDBDescriptor.getInstance().getConfig().setTimestampPrecision("us"); long now_us = generator.parseTimeFormat(SQLConstant.NOW_FUNC); String us_str = String.valueOf(now_us); IoTDBDescriptor.getInstance().getConfig().setTimestampPrecision("ns"); long now_ns = generator.parseTimeFormat(SQLConstant.NOW_FUNC); String ns_str = String.valueOf(now_ns); assertEquals(ms_str.length() + 3, (us_str).length()); assertEquals(us_str.length() + 3, (ns_str).length()); IoTDBDescriptor.getInstance().getConfig().setTimestampPrecision(timePrecision); }
@Test(expected = SQLParserException.class) public void testParseTimeFormatFail1() throws LogicalOperatorException { generator.parseTimeFormat(null); }
@Test(expected = SQLParserException.class) public void testParseTimeFormatFail2() throws LogicalOperatorException { generator.parseTimeFormat(""); }