Skip to content

Commit 1279a27

Browse files
Liu ZhengyunCRZbulabula
authored andcommitted
add error check IT for forecast tvf
1 parent b9d371d commit 1279a27

File tree

2 files changed

+73
-4
lines changed

2 files changed

+73
-4
lines changed

integration-test/src/test/java/org/apache/iotdb/ainode/it/AINodeForecastIT.java

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,14 @@
4444
public class AINodeForecastIT {
4545

4646
private static final String FORECAST_TABLE_FUNCTION_SQL_TEMPLATE =
47-
"SELECT * FROM FORECAST(model_id=>'%s', targets=>(SELECT time, s%d FROM db.AI) ORDER BY time)";
47+
"SELECT * FROM FORECAST(" +
48+
"model_id=>'%s', " +
49+
"targets=>(SELECT time, s%d FROM db.AI), " +
50+
"output_start_time=>%d, " +
51+
"output_length=>%d, " +
52+
"output_interval=>'%s', " +
53+
"timecol=>'%s'" +
54+
")";
4855

4956
@BeforeClass
5057
public static void setUp() throws Exception {
@@ -84,7 +91,11 @@ public void forecastTableFunctionTest(
8491
// Invoke forecast table function for specified models, there should exist result.
8592
for (int i = 0; i < 4; i++) {
8693
String forecastTableFunctionSQL =
87-
String.format(FORECAST_TABLE_FUNCTION_SQL_TEMPLATE, modelInfo.getModelId(), i);
94+
String.format(
95+
FORECAST_TABLE_FUNCTION_SQL_TEMPLATE,
96+
modelInfo.getModelId(),
97+
i, 2880, 96, "1s", "time"
98+
);
8899
try (ResultSet resultSet = statement.executeQuery(forecastTableFunctionSQL)) {
89100
int count = 0;
90101
while (resultSet.next()) {
@@ -95,4 +106,59 @@ public void forecastTableFunctionTest(
95106
}
96107
}
97108
}
109+
110+
@Test
111+
public void forecastTableFunctionErrorTest() throws SQLException {
112+
for (AINodeTestUtils.FakeModelInfo modelInfo : BUILTIN_MODEL_MAP.values()) {
113+
try (Connection connection = EnvFactory.getEnv().getConnection(BaseEnv.TABLE_SQL_DIALECT);
114+
Statement statement = connection.createStatement()) {
115+
forecastTableFunctionErrorTest(statement, modelInfo);
116+
}
117+
}
118+
}
119+
public void forecastTableFunctionErrorTest(
120+
Statement statement, AINodeTestUtils.FakeModelInfo modelInfo) throws SQLException {
121+
// OUTPUT_START_TIME error
122+
String invalidOutputStartTimeSQL = String.format(
123+
FORECAST_TABLE_FUNCTION_SQL_TEMPLATE,
124+
modelInfo.getModelId(),
125+
0, 2879, 96, "1s", "time"
126+
);
127+
errorTest(statement, invalidOutputStartTimeSQL,
128+
"The OUTPUT_START_TIME should be greater than the maximum timestamp of target time series. Expected greater than [2879] but found [2879].");
129+
130+
// OUTPUT_LENGTH error
131+
String invalidOutputLengthSQL = String.format(
132+
FORECAST_TABLE_FUNCTION_SQL_TEMPLATE,
133+
modelInfo.getModelId(),
134+
0, 2880, 0, "1s", "time"
135+
);
136+
errorTest(statement, invalidOutputLengthSQL, "OUTPUT_LENGTH should be greater than 0");
137+
138+
// OUTPUT_INTERVAL error
139+
String invalidOutputIntervalSQL = String.format(
140+
FORECAST_TABLE_FUNCTION_SQL_TEMPLATE,
141+
modelInfo.getModelId(),
142+
0, 2880, 96, "0s", "time"
143+
);
144+
errorTest(statement, invalidOutputIntervalSQL, "OUTPUT_INTERVAL should be greater than 0");
145+
146+
// TIMECOL error-1
147+
String invalidTimecolSQL1 = String.format(
148+
FORECAST_TABLE_FUNCTION_SQL_TEMPLATE,
149+
modelInfo.getModelId(),
150+
0, 2880, 96, "1s", "nonexistent_column"
151+
);
152+
errorTest(statement, invalidTimecolSQL1,
153+
"Required column [nonexistent_column] not found in the source table argument.");
154+
155+
// TIMECOL error-2
156+
String invalidTimecolSQL2 = String.format(
157+
FORECAST_TABLE_FUNCTION_SQL_TEMPLATE,
158+
modelInfo.getModelId(),
159+
0, 2880, 96, "1s", "s0"
160+
);
161+
errorTest(statement, invalidTimecolSQL2, "The type of the column s0 is not as expected.");
162+
}
163+
98164
}

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/function/tvf/ForecastTableFunction.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,16 @@ public TableFunctionAnalysis analyze(Map<String, Argument> arguments) {
269269
String timeColumn =
270270
((String) ((ScalarArgument) arguments.get(TIMECOL_PARAMETER_NAME)).getValue())
271271
.toLowerCase(Locale.ENGLISH);
272-
273272
if (timeColumn.isEmpty()) {
274273
throw new SemanticException(
275274
String.format("%s should never be null or empty.", TIMECOL_PARAMETER_NAME));
276275
}
277276

277+
long outputInterval = (long) ((ScalarArgument) arguments.get(OUTPUT_INTERVAL)).getValue();
278+
if (outputInterval <= 0) {
279+
throw new SemanticException(String.format("%s should be greater than 0", OUTPUT_INTERVAL));
280+
}
281+
278282
// predicated columns should never contain partition by columns and time column
279283
Set<String> excludedColumns =
280284
targets.getPartitionBy().stream()
@@ -323,7 +327,6 @@ public TableFunctionAnalysis analyze(Map<String, Argument> arguments) {
323327
}
324328

325329
long outputStartTime = (long) ((ScalarArgument) arguments.get(OUTPUT_START_TIME)).getValue();
326-
long outputInterval = (long) ((ScalarArgument) arguments.get(OUTPUT_INTERVAL)).getValue();
327330
String options = (String) ((ScalarArgument) arguments.get(OPTIONS_PARAMETER_NAME)).getValue();
328331

329332
ForecastTableFunctionHandle functionHandle =

0 commit comments

Comments
 (0)