Skip to content

Commit d412a20

Browse files
authored
Reject insert without field column (apache#15017)
* Reject insert without field column * Reject insert without field column * Fix IT * fix review * Fix IT
1 parent 83d5372 commit d412a20

File tree

10 files changed

+256
-153
lines changed

10 files changed

+256
-153
lines changed

integration-test/src/test/java/org/apache/iotdb/db/it/IoTDBLoadTsFileIT.java

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121

2222
import org.apache.iotdb.commons.auth.entity.PrivilegeType;
2323
import org.apache.iotdb.commons.schema.column.ColumnHeaderConstant;
24-
import org.apache.iotdb.db.storageengine.dataregion.compaction.tablemodel.CompactionTableModelTestFileWriter;
25-
import org.apache.iotdb.db.storageengine.dataregion.tsfile.TsFileResource;
2624
import org.apache.iotdb.it.env.EnvFactory;
2725
import org.apache.iotdb.it.framework.IoTDBTestRunner;
2826
import org.apache.iotdb.it.utils.TsFileGenerator;
@@ -33,10 +31,8 @@
3331
import org.apache.iotdb.jdbc.IoTDBSQLException;
3432

3533
import org.apache.tsfile.enums.TSDataType;
36-
import org.apache.tsfile.file.metadata.enums.CompressionType;
3734
import org.apache.tsfile.file.metadata.enums.TSEncoding;
3835
import org.apache.tsfile.read.common.Path;
39-
import org.apache.tsfile.read.common.TimeRange;
4036
import org.apache.tsfile.utils.Pair;
4137
import org.apache.tsfile.write.record.Tablet;
4238
import org.apache.tsfile.write.schema.IMeasurementSchema;
@@ -500,75 +496,6 @@ public void testAuth() throws Exception {
500496
String.format("load \"%s\" sgLevel=2", tmpDir.getAbsolutePath()), "test", "test123");
501497
}
502498

503-
@Test
504-
public void testTableAuth() throws Exception {
505-
createUser("test", "test123");
506-
507-
final TsFileResource resource4 = new TsFileResource(new File(tmpDir, "test1-0-0-0.tsfile"));
508-
try (final CompactionTableModelTestFileWriter writer =
509-
new CompactionTableModelTestFileWriter(resource4)) {
510-
writer.registerTableSchema("t2", Arrays.asList("id1", "id2", "id3"));
511-
writer.startChunkGroup("t2", Arrays.asList("id_field1", "id_field2", "id_field3"));
512-
writer.generateSimpleNonAlignedSeriesToCurrentDevice(
513-
"s1",
514-
new TimeRange[][][] {new TimeRange[][] {new TimeRange[] {new TimeRange(20, 22)}}},
515-
TSEncoding.PLAIN,
516-
CompressionType.LZ4);
517-
writer.generateSimpleNonAlignedSeriesToCurrentDevice(
518-
"s2",
519-
new TimeRange[][][] {new TimeRange[][] {new TimeRange[] {new TimeRange(20, 22)}}},
520-
TSEncoding.PLAIN,
521-
CompressionType.LZ4);
522-
writer.endChunkGroup();
523-
writer.endFile();
524-
}
525-
526-
try (final Connection userCon =
527-
EnvFactory.getEnv().getConnection("test", "test123", BaseEnv.TABLE_SQL_DIALECT);
528-
final Statement userStmt = userCon.createStatement()) {
529-
Assert.assertThrows(
530-
SQLException.class,
531-
() -> {
532-
userStmt.execute(
533-
String.format(
534-
"load '%s' with ('database-level'='2', 'database-name'='test')",
535-
tmpDir.getAbsolutePath()));
536-
});
537-
}
538-
539-
try (Connection adminCon = EnvFactory.getEnv().getConnection(BaseEnv.TABLE_SQL_DIALECT);
540-
Statement adminStmt = adminCon.createStatement()) {
541-
adminStmt.execute("grant create on database test to user test");
542-
}
543-
544-
try (final Connection userCon =
545-
EnvFactory.getEnv().getConnection("test", "test123", BaseEnv.TABLE_SQL_DIALECT);
546-
final Statement userStmt = userCon.createStatement()) {
547-
Assert.assertThrows(
548-
SQLException.class,
549-
() -> {
550-
userStmt.execute(
551-
String.format(
552-
"load '%s' with ('database-level'='2', 'database-name'='test')",
553-
tmpDir.getAbsolutePath()));
554-
});
555-
}
556-
557-
try (final Connection adminCon = EnvFactory.getEnv().getConnection(BaseEnv.TABLE_SQL_DIALECT);
558-
final Statement adminStmt = adminCon.createStatement()) {
559-
adminStmt.execute("grant insert on any to user test");
560-
}
561-
562-
try (final Connection userCon =
563-
EnvFactory.getEnv().getConnection("test", "test123", BaseEnv.TABLE_SQL_DIALECT);
564-
final Statement userStmt = userCon.createStatement()) {
565-
userStmt.execute(
566-
String.format(
567-
"load '%s' with ('database-level'='2', 'database-name'='test')",
568-
tmpDir.getAbsolutePath()));
569-
}
570-
}
571-
572499
@Test
573500
public void testLoadWithOnSuccess() throws Exception {
574501
final File file1 = new File(tmpDir, "1-0-0-0.tsfile");

integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/IoTDBAuthenticationTableIT.java

Lines changed: 105 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,48 @@
1414

1515
package org.apache.iotdb.relational.it.db.it;
1616

17+
import org.apache.iotdb.db.storageengine.dataregion.compaction.tablemodel.CompactionTableModelTestFileWriter;
18+
import org.apache.iotdb.db.storageengine.dataregion.tsfile.TsFileResource;
1719
import org.apache.iotdb.isession.ITableSession;
1820
import org.apache.iotdb.it.env.EnvFactory;
21+
import org.apache.iotdb.it.framework.IoTDBTestRunner;
22+
import org.apache.iotdb.itbase.category.TableClusterIT;
23+
import org.apache.iotdb.itbase.category.TableLocalStandaloneIT;
24+
import org.apache.iotdb.itbase.env.BaseEnv;
1925
import org.apache.iotdb.rpc.IoTDBConnectionException;
2026
import org.apache.iotdb.rpc.StatementExecutionException;
2127

2228
import org.apache.tsfile.enums.TSDataType;
29+
import org.apache.tsfile.file.metadata.enums.CompressionType;
30+
import org.apache.tsfile.file.metadata.enums.TSEncoding;
31+
import org.apache.tsfile.read.common.TimeRange;
2332
import org.apache.tsfile.write.record.Tablet;
2433
import org.apache.tsfile.write.record.Tablet.ColumnCategory;
2534
import org.junit.After;
2635
import org.junit.AfterClass;
36+
import org.junit.Assert;
2737
import org.junit.BeforeClass;
2838
import org.junit.Test;
29-
39+
import org.junit.experimental.categories.Category;
40+
import org.junit.runner.RunWith;
41+
42+
import java.io.File;
43+
import java.nio.file.Files;
44+
import java.sql.Connection;
45+
import java.sql.SQLException;
46+
import java.sql.Statement;
3047
import java.util.Arrays;
3148
import java.util.Locale;
3249

50+
import static org.apache.iotdb.db.it.utils.TestUtils.createUser;
3351
import static org.junit.Assert.assertEquals;
3452
import static org.junit.Assert.fail;
3553

54+
@RunWith(IoTDBTestRunner.class)
55+
@Category({TableLocalStandaloneIT.class, TableClusterIT.class})
3656
public class IoTDBAuthenticationTableIT {
3757
@BeforeClass
38-
public static void setUpClass() {
58+
public static void setUpClass() throws Exception {
3959
Locale.setDefault(Locale.ENGLISH);
4060
EnvFactory.getEnv().initClusterEnvironment();
4161
}
@@ -70,7 +90,7 @@ public static void tearDownClass() {
7090
public void testInsert() throws IoTDBConnectionException, StatementExecutionException {
7191

7292
try (ITableSession sessionRoot = EnvFactory.getEnv().getTableSessionConnection()) {
73-
sessionRoot.executeNonQueryStatement("CREATE DATABASE test");
93+
sessionRoot.executeNonQueryStatement("CREATE DATABASE IF NOT EXISTS test");
7494
sessionRoot.executeNonQueryStatement("USE test");
7595

7696
// insert by root
@@ -95,19 +115,22 @@ public void testInsert() throws IoTDBConnectionException, StatementExecutionExce
95115
sessionRoot.executeNonQueryStatement("REVOKE INSERT ON ANY FROM USER root");
96116
fail("Should have thrown an exception");
97117
} catch (StatementExecutionException e) {
98-
assertEquals("803: Cannot grant/revoke privileges to/from admin", e.getMessage());
118+
assertEquals(
119+
"803: Access Denied: Cannot grant/revoke privileges of admin user", e.getMessage());
99120
}
100121
try {
101122
sessionRoot.executeNonQueryStatement("REVOKE INSERT ON DATABASE test FROM USER root");
102123
fail("Should have thrown an exception");
103124
} catch (StatementExecutionException e) {
104-
assertEquals("803: Cannot grant/revoke privileges of admin user", e.getMessage());
125+
assertEquals(
126+
"803: Access Denied: Cannot grant/revoke privileges of admin user", e.getMessage());
105127
}
106128
try {
107129
sessionRoot.executeNonQueryStatement("REVOKE INSERT ON TABLE table1 FROM USER root");
108130
fail("Should have thrown an exception");
109131
} catch (StatementExecutionException e) {
110-
assertEquals("803: Cannot grant/revoke privileges of admin user", e.getMessage());
132+
assertEquals(
133+
"803: Access Denied: Cannot grant/revoke privileges of admin user", e.getMessage());
111134
}
112135

113136
// test users
@@ -606,19 +629,22 @@ public void testDelete() throws IoTDBConnectionException, StatementExecutionExce
606629
sessionRoot.executeNonQueryStatement("REVOKE DELETE ON ANY FROM USER root");
607630
fail("Should have thrown an exception");
608631
} catch (StatementExecutionException e) {
609-
assertEquals("803: Cannot grant/revoke privileges to/from admin", e.getMessage());
632+
assertEquals(
633+
"803: Access Denied: Cannot grant/revoke privileges of admin user", e.getMessage());
610634
}
611635
try {
612636
sessionRoot.executeNonQueryStatement("REVOKE DELETE ON DATABASE test FROM USER root");
613637
fail("Should have thrown an exception");
614638
} catch (StatementExecutionException e) {
615-
assertEquals("803: Cannot grant/revoke privileges of admin user", e.getMessage());
639+
assertEquals(
640+
"803: Access Denied: Cannot grant/revoke privileges of admin user", e.getMessage());
616641
}
617642
try {
618643
sessionRoot.executeNonQueryStatement("REVOKE DELETE ON TABLE table1 FROM USER root");
619644
fail("Should have thrown an exception");
620645
} catch (StatementExecutionException e) {
621-
assertEquals("803: Cannot grant/revoke privileges of admin user", e.getMessage());
646+
assertEquals(
647+
"803: Access Denied: Cannot grant/revoke privileges of admin user", e.getMessage());
622648
}
623649

624650
// test users
@@ -998,4 +1024,74 @@ public void testDelete() throws IoTDBConnectionException, StatementExecutionExce
9981024
}
9991025
}
10001026
}
1027+
1028+
@Test
1029+
public void testTableAuth() throws Exception {
1030+
File tmpDir = new File(Files.createTempDirectory("load").toUri());
1031+
createUser("test", "test123");
1032+
1033+
final TsFileResource resource4 = new TsFileResource(new File(tmpDir, "test1-0-0-0.tsfile"));
1034+
try (final CompactionTableModelTestFileWriter writer =
1035+
new CompactionTableModelTestFileWriter(resource4)) {
1036+
writer.registerTableSchemaWithTagField("t2", Arrays.asList("id1", "id2", "id3"));
1037+
writer.startChunkGroup("t2", Arrays.asList("id_field1", "id_field2", "id_field3"));
1038+
writer.generateSimpleNonAlignedSeriesToCurrentDevice(
1039+
"s1",
1040+
new TimeRange[][][] {new TimeRange[][] {new TimeRange[] {new TimeRange(20, 22)}}},
1041+
TSEncoding.PLAIN,
1042+
CompressionType.LZ4);
1043+
writer.generateSimpleNonAlignedSeriesToCurrentDevice(
1044+
"s2",
1045+
new TimeRange[][][] {new TimeRange[][] {new TimeRange[] {new TimeRange(20, 22)}}},
1046+
TSEncoding.PLAIN,
1047+
CompressionType.LZ4);
1048+
writer.endChunkGroup();
1049+
writer.endFile();
1050+
}
1051+
1052+
try (final Connection userCon =
1053+
EnvFactory.getEnv().getConnection("test", "test123", BaseEnv.TABLE_SQL_DIALECT);
1054+
final Statement userStmt = userCon.createStatement()) {
1055+
Assert.assertThrows(
1056+
SQLException.class,
1057+
() -> {
1058+
userStmt.execute(
1059+
String.format(
1060+
"load '%s' with ('database-level'='2', 'database-name'='test')",
1061+
tmpDir.getAbsolutePath()));
1062+
});
1063+
}
1064+
1065+
try (Connection adminCon = EnvFactory.getEnv().getConnection(BaseEnv.TABLE_SQL_DIALECT);
1066+
Statement adminStmt = adminCon.createStatement()) {
1067+
adminStmt.execute("grant create on database test to user test");
1068+
}
1069+
1070+
try (final Connection userCon =
1071+
EnvFactory.getEnv().getConnection("test", "test123", BaseEnv.TABLE_SQL_DIALECT);
1072+
final Statement userStmt = userCon.createStatement()) {
1073+
Assert.assertThrows(
1074+
SQLException.class,
1075+
() -> {
1076+
userStmt.execute(
1077+
String.format(
1078+
"load '%s' with ('database-level'='2', 'database-name'='test')",
1079+
tmpDir.getAbsolutePath()));
1080+
});
1081+
}
1082+
1083+
try (final Connection adminCon = EnvFactory.getEnv().getConnection(BaseEnv.TABLE_SQL_DIALECT);
1084+
final Statement adminStmt = adminCon.createStatement()) {
1085+
adminStmt.execute("grant insert on any to user test");
1086+
}
1087+
1088+
try (final Connection userCon =
1089+
EnvFactory.getEnv().getConnection("test", "test123", BaseEnv.TABLE_SQL_DIALECT);
1090+
final Statement userStmt = userCon.createStatement()) {
1091+
userStmt.execute(
1092+
String.format(
1093+
"load '%s' with ('database-level'='2', 'database-name'='test')",
1094+
tmpDir.getAbsolutePath()));
1095+
}
1096+
}
10011097
}

integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/IoTDBInsertTableIT.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import org.junit.AfterClass;
4141
import org.junit.Assert;
4242
import org.junit.BeforeClass;
43-
import org.junit.Ignore;
4443
import org.junit.Test;
4544
import org.junit.experimental.categories.Category;
4645
import org.junit.runner.RunWith;
@@ -61,7 +60,7 @@
6160
import java.util.stream.Collectors;
6261

6362
import static org.apache.iotdb.db.it.utils.TestUtils.assertTableNonQueryTestFail;
64-
import static org.apache.iotdb.db.it.utils.TestUtils.resultSetEqualTest;
63+
import static org.apache.iotdb.db.it.utils.TestUtils.tableResultSetEqualTest;
6564
import static org.junit.Assert.assertEquals;
6665
import static org.junit.Assert.assertFalse;
6766
import static org.junit.Assert.assertNotNull;
@@ -348,7 +347,7 @@ public void testInsertNaN() throws SQLException {
348347
}
349348
}
350349

351-
@Ignore // aggregation
350+
// aggregation
352351
@Test
353352
public void testInsertWithoutTime() {
354353
try (Connection connection = EnvFactory.getEnv().getConnection(BaseEnv.TABLE_SQL_DIALECT);
@@ -370,9 +369,10 @@ public void testInsertWithoutTime() {
370369
fail(e.getMessage());
371370
}
372371

373-
String expectedHeader = "count(s1),count(s2),count(s3),";
372+
String[] expectedHeader = new String[] {"_col0", "_col1", "_col2"};
374373
String[] retArray = new String[] {"4,4,4,"};
375-
resultSetEqualTest("select count(s1), count(s2), count(s3) from sg9", expectedHeader, retArray);
374+
tableResultSetEqualTest(
375+
"select count(s1), count(s2), count(s3) from sg9", expectedHeader, retArray, "test");
376376
}
377377

378378
@Test
@@ -399,7 +399,7 @@ public void testInsertWithMultiTimesColumns() {
399399
"test");
400400
}
401401

402-
@Ignore // aggregation
402+
// aggregation
403403
@Test
404404
public void testInsertMultiRow2() throws SQLException {
405405
try (Connection connection = EnvFactory.getEnv().getConnection(BaseEnv.TABLE_SQL_DIALECT)) {
@@ -884,19 +884,26 @@ public void testInsertSingleColumn() throws SQLException, InterruptedException {
884884
st1.execute(
885885
"create table if not exists sg21 (tag1 string tag, ss1 string attribute, ss2 int32 field)");
886886
// only tag
887-
st1.execute("insert into sg21(tag1) values('1')");
887+
try {
888+
st1.execute("insert into sg21(tag1) values('1')");
889+
} catch (SQLException e) {
890+
assertEquals("507: No Field column present, please check the request", e.getMessage());
891+
}
888892
// only time
889893
try {
890894
st1.execute("insert into sg21(time) values(1)");
891895
} catch (SQLException e) {
892896
assertEquals(
893-
"305: [INTERNAL_SERVER_ERROR(305)] Exception occurred: \"insert into sg21(time) values(1)\". executeStatement failed. No column other than Time present, please check the request",
894-
e.getMessage());
897+
"507: No column other than Time present, please check the request", e.getMessage());
895898
}
896899
// sleep a while to avoid the same timestamp between two insertions
897900
Thread.sleep(10);
898901
// only attribute
899-
st1.execute("insert into sg21(ss1) values('1')");
902+
try {
903+
st1.execute("insert into sg21(ss1) values('1')");
904+
} catch (SQLException e) {
905+
assertEquals("507: No Field column present, please check the request", e.getMessage());
906+
}
900907
// sleep a while to avoid the same timestamp between two insertions
901908
Thread.sleep(10);
902909
// only field
@@ -906,25 +913,18 @@ public void testInsertSingleColumn() throws SQLException, InterruptedException {
906913
assertTrue(rs1.next());
907914
// from "insert into sg21(ss2) values(1)"
908915
assertEquals(null, rs1.getString("tag1"));
909-
assertTrue(rs1.next());
916+
assertFalse(rs1.next());
910917
// from "insert into sg21(tag1) values('1')"
911-
assertEquals("1", rs1.getString("tag1"));
918+
assertEquals(null, rs1.getString("tag1"));
912919
assertFalse(rs1.next());
913920

914921
rs1 = st1.executeQuery("select time, ss1, ss2 from sg21 order by time");
915922
assertTrue(rs1.next());
916923
rs1.getString("ss1");
917924
assertTrue(rs1.wasNull());
918925
rs1.getInt("ss2");
919-
assertTrue(rs1.wasNull());
920-
921-
assertTrue(rs1.next());
922-
assertEquals("1", rs1.getString("ss1"));
923-
rs1.getInt("ss2");
924-
assertTrue(rs1.wasNull());
925-
assertTrue(rs1.next());
926-
assertEquals("1", rs1.getString("ss1"));
927926
assertEquals(1, rs1.getInt("ss2"));
927+
928928
assertFalse(rs1.next());
929929
}
930930
}

integration-test/src/test/java/org/apache/iotdb/relational/it/query/old/builtinfunction/scalar/IoTDBFormatFunctionTableIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public class IoTDBFormatFunctionTableIT {
7676
"INSERT INTO date_table(time, device_id, s1) VALUES (20, 'd1', '2006-07-04')",
7777

7878
// data for special series
79-
"INSERT INTO null_table(time, device_id) VALUES (10, 'd1')",
79+
"INSERT INTO null_table(time, device_id, s1) VALUES (10, 'd1', null)",
8080
};
8181

8282
@BeforeClass

0 commit comments

Comments
 (0)