Kinetica   C#   API  Version 7.2.3.1
AsyncSqlExecutionTests.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Threading.Tasks;
5 using Xunit;
7 using kinetica;
8 
10 {
15  [Trait("Category", "Integration")]
16  [Trait("Category", "Async")]
18  {
19  [Fact]
20  public async Task TestExecuteSqlSelectAsync()
21  {
22  using var ctx = new TestContext("async_sql_select");
23 
24  // Create and populate table
25  await ctx.Kinetica.ExecuteSqlAsync($@"
26  CREATE TABLE {ctx.QualifiedTable("sql_test")}
27  (id INT NOT NULL, name VARCHAR(64), value DOUBLE, PRIMARY KEY (id))
28  ");
29 
30  var tableName = ctx.QualifiedTable("sql_test");
31 
32  // Insert test data
33  await ctx.Kinetica.ExecuteSqlAsync($"INSERT INTO {tableName} (id, name, value) VALUES (1, 'Alice', 10.5)");
34  await ctx.Kinetica.ExecuteSqlAsync($"INSERT INTO {tableName} (id, name, value) VALUES (2, 'Bob', 20.7)");
35  await ctx.Kinetica.ExecuteSqlAsync($"INSERT INTO {tableName} (id, name, value) VALUES (3, 'Charlie', 30.9)");
36 
37  // Execute SELECT asynchronously
38  var response = await ctx.Kinetica.ExecuteSqlAsync($"SELECT * FROM {tableName} ORDER BY id");
39 
40  Assert.Equal(3, response.total_number_of_records);
41  Assert.True(response.has_more_records == false);
42  }
43 
44  [Fact]
45  public async Task TestExecuteSqlWithLimitAsync()
46  {
47  using var ctx = new TestContext("async_sql_limit");
48 
49  await ctx.Kinetica.ExecuteSqlAsync($@"
50  CREATE TABLE {ctx.QualifiedTable("limit_test")}
51  (id INT NOT NULL, data VARCHAR(32), PRIMARY KEY (id))
52  ");
53 
54  var tableName = ctx.QualifiedTable("limit_test");
55 
56  // Insert 50 records
57  for (int i = 0; i < 50; i++)
58  {
59  await ctx.Kinetica.ExecuteSqlAsync($"INSERT INTO {tableName} (id, data) VALUES ({i}, 'record_{i}')");
60  }
61 
62  // Execute SELECT with LIMIT
63  var response = await ctx.Kinetica.ExecuteSqlAsync($"SELECT * FROM {tableName} LIMIT 10");
64 
65  Assert.Equal(10, response.total_number_of_records);
66  }
67 
68  [Fact]
69  public async Task TestExecuteSqlAggregateAsync()
70  {
71  using var ctx = new TestContext("async_sql_aggregate");
72 
73  await ctx.Kinetica.ExecuteSqlAsync($@"
74  CREATE TABLE {ctx.QualifiedTable("agg_test")}
75  (id INT NOT NULL, category VARCHAR(32), amount DOUBLE, PRIMARY KEY (id))
76  ");
77 
78  var tableName = ctx.QualifiedTable("agg_test");
79 
80  // Insert test data with categories
81  await ctx.Kinetica.ExecuteSqlAsync($"INSERT INTO {tableName} (id, category, amount) VALUES (1, 'A', 100.0)");
82  await ctx.Kinetica.ExecuteSqlAsync($"INSERT INTO {tableName} (id, category, amount) VALUES (2, 'A', 150.0)");
83  await ctx.Kinetica.ExecuteSqlAsync($"INSERT INTO {tableName} (id, category, amount) VALUES (3, 'B', 200.0)");
84  await ctx.Kinetica.ExecuteSqlAsync($"INSERT INTO {tableName} (id, category, amount) VALUES (4, 'B', 250.0)");
85 
86  // Execute GROUP BY query asynchronously
87  var response = await ctx.Kinetica.ExecuteSqlAsync($"SELECT category, SUM(amount) as total FROM {tableName} GROUP BY category ORDER BY category");
88 
89  Assert.Equal(2, response.total_number_of_records);
90  }
91 
92  [Fact]
94  {
95  using var ctx = new TestContext("async_sql_create_drop");
96 
97  var tableName = ctx.QualifiedTable("temp_table");
98 
99  // Create table
100  await ctx.Kinetica.ExecuteSqlAsync($"CREATE TABLE {tableName} (id INT, data VARCHAR(32))");
101 
102  // Verify table exists
103  var hasTableResp = await ctx.Kinetica.HasTableAsync(tableName, new Dictionary<string, string>());
104  Assert.True(hasTableResp.table_exists);
105 
106  // Drop table
107  await ctx.Kinetica.ExecuteSqlAsync($"DROP TABLE {tableName}");
108 
109  // Verify table no longer exists
110  var hasTableResp2 = await ctx.Kinetica.HasTableAsync(tableName, new Dictionary<string, string>());
111  Assert.False(hasTableResp2.table_exists);
112  }
113 
114  [Fact]
115  public async Task TestExecuteSqlWithFilterAsync()
116  {
117  using var ctx = new TestContext("async_sql_filter");
118 
119  await ctx.Kinetica.ExecuteSqlAsync($@"
120  CREATE TABLE {ctx.QualifiedTable("filter_test")}
121  (id INT NOT NULL, status VARCHAR(16), score INT, PRIMARY KEY (id))
122  ");
123 
124  var tableName = ctx.QualifiedTable("filter_test");
125 
126  // Insert test data
127  await ctx.Kinetica.ExecuteSqlAsync($"INSERT INTO {tableName} (id, status, score) VALUES (1, 'active', 85)");
128  await ctx.Kinetica.ExecuteSqlAsync($"INSERT INTO {tableName} (id, status, score) VALUES (2, 'inactive', 60)");
129  await ctx.Kinetica.ExecuteSqlAsync($"INSERT INTO {tableName} (id, status, score) VALUES (3, 'active', 92)");
130  await ctx.Kinetica.ExecuteSqlAsync($"INSERT INTO {tableName} (id, status, score) VALUES (4, 'active', 78)");
131 
132  // Execute filtered query
133  var response = await ctx.Kinetica.ExecuteSqlAsync($"SELECT * FROM {tableName} WHERE status = 'active' AND score > 80");
134 
135  Assert.Equal(2, response.total_number_of_records);
136  }
137 
138  [Fact]
139  public async Task TestConcurrentSqlQueriesAsync()
140  {
141  using var ctx = new TestContext("async_concurrent_sql");
142 
143  await ctx.Kinetica.ExecuteSqlAsync($@"
144  CREATE TABLE {ctx.QualifiedTable("concurrent_test")}
145  (id INT NOT NULL, value INT, PRIMARY KEY (id))
146  ");
147 
148  var tableName = ctx.QualifiedTable("concurrent_test");
149 
150  // Insert initial data
151  for (int i = 0; i < 20; i++)
152  {
153  await ctx.Kinetica.ExecuteSqlAsync($"INSERT INTO {tableName} (id, value) VALUES ({i}, {i * 10})");
154  }
155 
156  // Execute multiple SELECT queries concurrently
157  var queryTasks = new List<Task<ExecuteSqlResponse>>();
158 
159  for (int i = 0; i < 10; i++)
160  {
161  int minId = i * 2;
162  int maxId = minId + 1;
163  queryTasks.Add(ctx.Kinetica.ExecuteSqlAsync($"SELECT * FROM {tableName} WHERE id >= {minId} AND id <= {maxId}"));
164  }
165 
166  var results = await Task.WhenAll(queryTasks);
167 
168  // Verify all queries completed successfully
169  Assert.Equal(10, results.Length);
170  foreach (var result in results)
171  {
172  Assert.True(result.total_number_of_records >= 1, "Each query should return at least 1 record");
173  }
174  }
175 
176  [Fact]
177  public async Task TestExecuteSqlShowTablesAsync()
178  {
179  using var ctx = new TestContext("async_show_tables");
180 
181  // Create a few tables
182  await ctx.Kinetica.ExecuteSqlAsync($"CREATE TABLE {ctx.QualifiedTable("table1")} (id INT)");
183  await ctx.Kinetica.ExecuteSqlAsync($"CREATE TABLE {ctx.QualifiedTable("table2")} (id INT)");
184  await ctx.Kinetica.ExecuteSqlAsync($"CREATE TABLE {ctx.QualifiedTable("table3")} (id INT)");
185 
186  // Show all tables
187  var response = await ctx.Kinetica.ExecuteSqlAsync("SHOW TABLES");
188 
189  // Should have at least some tables
190  Assert.True(response.total_number_of_records >= 0, "SHOW TABLES should return successfully");
191  }
192  }
193 }
Test context that manages schema and cleanup for integration tests.
Definition: TestContext.cs:11