Skip to content
This repository has been archived by the owner on Jan 3, 2022. It is now read-only.

Commit

Permalink
Tests of the SHOW statement and fixes to the query model
Browse files Browse the repository at this point in the history
  • Loading branch information
tsutomi committed Apr 27, 2016
1 parent 70a8e65 commit dc91526
Show file tree
Hide file tree
Showing 15 changed files with 301 additions and 40 deletions.
59 changes: 58 additions & 1 deletion src/deveeldb-nunit/Deveel.Data/ShowTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,76 @@
// limitations under the License.

using System;
using System.Linq;

using Deveel.Data.Security;
using Deveel.Data.Sql;
using Deveel.Data.Sql.Cursors;
using Deveel.Data.Sql.Tables;
using Deveel.Data.Sql.Types;

using NUnit.Framework;

namespace Deveel.Data {
[TestFixture]
public sealed class ShowTests : ContextBasedTest {
protected override bool OnSetUp(string testName, IQuery query) {
var tableName = ObjectName.Parse("SYSTEM.test_table");
var tableInfo = new TableInfo(tableName);
tableInfo.AddColumn("a", PrimitiveTypes.Integer());
tableInfo.AddColumn("b", PrimitiveTypes.String());

query.Access().CreateObject(tableInfo);
query.Access().GrantOnTable(tableName, User.PublicName, Privileges.TableAll);

return true;
}

protected override bool OnTearDown(string testName, IQuery query) {
var tableName = ObjectName.Parse("SYSTEM.test_table");
query.Access().RevokeAllGrantsOn(DbObjectType.Table, tableName);
query.Access().DropObject(DbObjectType.Table, tableName);
return true;
}

[Test]
public void ShowSchema() {
var result = Query.ShowSchema();

Assert.IsNotNull(result);

// TODO: Verify the list of schema is coherent
Row row = null;
Assert.DoesNotThrow(() => row = result.ElementAt(0));
Assert.IsNotNull(row);

var schemaName = row.GetValue(0).Value.ToString();
var schemaType = row.GetValue(1).Value.ToString();

Assert.AreEqual("INFORMATION_SCHEMA", schemaName);
Assert.AreEqual("SYSTEM", schemaType);
}

[Test]
public void ShowTables() {
var result = Query.ShowTables();

Assert.IsNotNull(result);

// TODO: There's probably an error on the views to select
// the tables the current user has access to... so for the moment
// just be happy it doesn't throw an error: we will come back later
}

[Test]
public void ShowProduct() {
var result = Query.ShowProduct();

Assert.IsNotNull(result);

// TODO: the product information come from the variables table,
// that is not yet finalized: so the execution succeeds but
// no data are retrieved. come back later when database vars
// are implemented
}
}
}
12 changes: 12 additions & 0 deletions src/deveeldb/Deveel.Data.Routines/InvokeArgument.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Runtime.Serialization;
using System.Text;

using Deveel.Data.Sql.Expressions;

Expand Down Expand Up @@ -40,5 +41,16 @@ public InvokeArgument(SqlExpression value)
var preparedValue = Value.Prepare(preparer);
return new InvokeArgument(Name, preparedValue);
}

public override string ToString() {
var sb = new StringBuilder();
if (IsNamed) {
sb.AppendFormat("{0} => ", Name);
}

sb.Append(Value);

return sb.ToString();
}
}
}
33 changes: 33 additions & 0 deletions src/deveeldb/Deveel.Data.Routines/SystemFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@


using System;
using System.Globalization;
using System.Text;

using Deveel.Data;
using Deveel.Data.Security;
using Deveel.Data.Sql;
using Deveel.Data.Sql.Expressions;
using Deveel.Data.Sql.Objects;
Expand Down Expand Up @@ -282,6 +285,30 @@ public static class SystemFunctions {

#endregion

public static Field Concat(Field[] args) {
var cc = new StringBuilder();

CultureInfo locale = null;
foreach (var ob in args) {
if (ob.IsNull)
return ob;

cc.Append(ob.Value);

var type1 = ob.Type;
if (locale == null && type1 is StringType) {
var strType = (StringType)type1;
locale = strType.Locale;
}
}

// We inherit the locale from the first string parameter with a locale,
// or use a default VarString if no locale found.
var type = locale != null ? PrimitiveTypes.VarChar(locale) : PrimitiveTypes.VarChar();

return new Field(type, new SqlString(cc.ToString()));
}

internal static InvokeResult Iif(InvokeContext context) {
var result = Field.Null();

Expand Down Expand Up @@ -343,5 +370,11 @@ public static class SystemFunctions {

throw new InvalidOperationException("Unsupported type in function argument");
}

internal static Field PrivilegeString(Field ob) {
int privBit = ((SqlNumber)ob.Value).ToInt32();
var privs = (Privileges)privBit;
return Field.String(privs.ToString());
}
}
}
37 changes: 36 additions & 1 deletion src/deveeldb/Deveel.Data.Routines/SystemFunctionsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

using System;
using System.Collections.Generic;
using System.Globalization;

using Deveel.Data;
using Deveel.Data.Sql;
Expand Down Expand Up @@ -50,6 +51,20 @@ class SystemFunctionsProvider : FunctionProvider {
return exp.ReturnType(context.Request, context.VariableResolver);
}

private static SqlType ConcatReturnType(InvokeContext context) {
// Determine the locale of the first string parameter.
CultureInfo locale = null;
for (int i = 0; i < context.ArgumentCount && locale == null; ++i) {
var type = ReturnType(context.Arguments[i].Value, context);
if (type is StringType) {
StringType strType = (StringType)type;
locale = strType.Locale;
}
}

return locale != null ? PrimitiveTypes.VarChar(locale) : PrimitiveTypes.VarChar();
}

#endregion

#region Aggregate Functions
Expand All @@ -60,7 +75,9 @@ class SystemFunctionsProvider : FunctionProvider {
.Named("aggor")
.WithParameter(p => p.Named("args").Unbounded().OfDynamicType())
.OfAggregateType()
.WhenExecute(context => Binary(context, SystemFunctions.Or)));
.WhenExecute(context => {
return Binary(context, SystemFunctions.Or);
}));

// COUNT
Register(config => config.Named("count")
Expand Down Expand Up @@ -451,6 +468,11 @@ class DistinctComparer : IComparer<int> {
return argType is StringType ? (SqlType) PrimitiveTypes.Numeric() : (SqlType) PrimitiveTypes.String();
}));

Register(config => config.Named("i_privilege_string")
.WithNumericParameter("privBit")
.WhenExecute(context => Simple(context, args => SystemFunctions.PrivilegeString(args[0])))
.ReturnsString());

// VERSION
Register(config => config
.Named("version")
Expand Down Expand Up @@ -544,13 +566,26 @@ class DistinctComparer : IComparer<int> {

#endregion

#region String Functions

private void StringFunctions() {
// CONCAT([STRING])
Register(config => config.Named("concat")
.WithUnoundedParameter("strings", PrimitiveTypes.VarChar())
.WhenExecute(context => Simple(context, args => SystemFunctions.Concat(args)))
.ReturnsType(ConcatReturnType));
}

#endregion

protected override void OnInit() {
AggregateFunctions();

ConversionFunctions();
SecurityFunctions();
SequenceFunctions();
DateFunctions();
StringFunctions();

MiscFunctions();
}
Expand Down
3 changes: 3 additions & 0 deletions src/deveeldb/Deveel.Data.Sql.Cursors/NativeCursor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ class NativeCursor : ICursor {
public CursorStatus Status { get; private set; }

public Row Fetch(FetchDirection direction, int offset) {
if (Result == null)
return null;

if (direction != FetchDirection.Absolute &&
direction != FetchDirection.Relative &&
offset > -1)
Expand Down
15 changes: 15 additions & 0 deletions src/deveeldb/Deveel.Data.Sql.Expressions/PreparerVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,22 @@ private static IEnumerable<T> Prepare<T>(IEnumerable<T> list, IExpressionPrepare
if (nextComposite != null)
nextComposite = (SqlQueryExpression) nextComposite.Prepare(preparer);

var groupBy = query.GroupBy;
if (groupBy != null) {
var newGroupBy = new List<SqlExpression>();
foreach (var groupExpression in groupBy) {
var newGroupByExp = groupExpression.Prepare(preparer);
newGroupBy.Add(newGroupByExp);
}

newExpression.GroupBy = newGroupBy;
}

newExpression.NextComposite = nextComposite;
newExpression.CompositeFunction = query.CompositeFunction;
newExpression.IsCompositeAll = query.IsCompositeAll;

newExpression.Distinct = query.Distinct;

return newExpression;
}
Expand Down
12 changes: 7 additions & 5 deletions src/deveeldb/Deveel.Data.Sql.Query/SubsetNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ private SubsetNode(SerializationInfo info, StreamingContext context)
}

public void SetAliasParentName(ObjectName parentName) {
var aliases = new ObjectName[AliasColumnNames.Length];
for (int i = 0; i < aliases.Length; i++) {
aliases[i] = new ObjectName(parentName, AliasColumnNames[i].Name);
}
if (parentName != null) {
var aliases = new ObjectName[AliasColumnNames.Length];
for (int i = 0; i < aliases.Length; i++) {
aliases[i] = new ObjectName(parentName, AliasColumnNames[i].Name);
}

AliasColumnNames = aliases;
AliasColumnNames = aliases;
}
}

protected override void GetData(SerializationInfo info, StreamingContext context) {
Expand Down
12 changes: 6 additions & 6 deletions src/deveeldb/Deveel.Data.Sql.Schemas/InfortmationSchemaCreate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ class InfortmationSchemaCreate : IDatabaseCreateCallback {
" SELECT \"priv_bit\", \"object\", \"name\", \"grantee\", " +
" \"grant_option\", \"granter\" " +
" FROM " + SystemSchema.GrantsTableName +
" WHERE ( grantee = user() OR grantee = '@PUBLIC' )");
" WHERE ( grantee = user() OR grantee = '" + User.PublicName + "' )");

// This view shows the grants that the user is allowed to see
query.ExecuteQuery("CREATE VIEW " + InformationSchema.ThisUserGrantViewName + " AS " +
" SELECT \"description\", \"object\", \"name\", \"grantee\", " +
" \"grant_option\", \"granter\" " +
" FROM " + SystemSchema.GrantsTableName + ", " + SystemSchema.PrivilegesTableName +
" WHERE ( grantee = user() OR grantee = '@PUBLIC' )" +
" WHERE ( grantee = user() OR grantee = '" + User.PublicName + "' )" +
" AND " + SystemSchema.GrantsTableName + ".priv_bit = " +
SystemSchema.PrivilegesTableName + ".priv_bit");

Expand Down Expand Up @@ -112,9 +112,9 @@ class InfortmationSchemaCreate : IDatabaseCreateCallback {
" \"TABLE_SCHEMA\",\n" +
" \"TABLE_NAME\",\n" +
" \"COLUMN_NAME\",\n" +
" IIF(\"" + InformationSchema.ThisUserGrantViewName + ".granter\" = '@SYSTEM', \n" +
" IIF(\"" + InformationSchema.ThisUserGrantViewName + ".granter\" = '" + User.SystemName + "', \n" +
" NULL, \"ThisUserGrant.granter\") AS \"GRANTOR\",\n" +
" IIF(\"" + InformationSchema.ThisUserGrantViewName + ".grantee\" = '@PUBLIC', \n" +
" IIF(\"" + InformationSchema.ThisUserGrantViewName + ".grantee\" = '" + User.PublicName +"', \n" +
" 'public', \"ThisUserGrant.grantee\") AS \"GRANTEE\",\n" +
" \"" + InformationSchema.ThisUserGrantViewName + ".description\" AS \"PRIVILEGE\",\n" +
" IIF(\"grant_option\" = 'true', 'YES', 'NO') AS \"IS_GRANTABLE\" \n" +
Expand All @@ -128,9 +128,9 @@ class InfortmationSchemaCreate : IDatabaseCreateCallback {
" SELECT \"TABLE_CATALOG\",\n" +
" \"TABLE_SCHEMA\",\n" +
" \"TABLE_NAME\",\n" +
" IIF(\"" + InformationSchema.ThisUserGrantViewName + ".granter\" = '@SYSTEM', \n" +
" IIF(\"" + InformationSchema.ThisUserGrantViewName + ".granter\" = '" + User.SystemName + "', \n" +
" NULL, \"ThisUserGrant.granter\") AS \"GRANTOR\",\n" +
" IIF(\"" + InformationSchema.ThisUserGrantViewName + ".grantee\" = '@PUBLIC', \n" +
" IIF(\"" + InformationSchema.ThisUserGrantViewName + ".grantee\" = '" + User.PublicName + "', \n" +
" 'public', \"ThisUserGrant.grantee\") AS \"GRANTEE\",\n" +
" \"" + InformationSchema.ThisUserGrantViewName + ".description\" AS \"PRIVILEGE\",\n" +
" IIF(\"grant_option\" = 'true', 'YES', 'NO') AS \"IS_GRANTABLE\" \n" +
Expand Down
18 changes: 18 additions & 0 deletions src/deveeldb/Deveel.Data.Sql.Statements/SelectStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,24 @@ private SelectStatement(SerializationInfo info, StreamingContext context)
}
}

protected override SqlStatement PrepareExpressions(IExpressionPreparer preparer) {
var preparedQuery = QueryExpression.Prepare(preparer);
if (!(preparedQuery is SqlQueryExpression))
throw new StatementException("The preparation of the query expression resulted in an invalid expression.");

var orderBy = new List<SortColumn>();
if (OrderBy != null) {
foreach (var column in OrderBy) {
var prepared = (SortColumn) ((IPreparable) column).Prepare(preparer);
orderBy.Add(prepared);
}
}

var query = (SqlQueryExpression) preparedQuery;

return new SelectStatement(query, Limit, orderBy);
}

protected override SqlStatement PrepareStatement(IRequest context) {
var queryPlan = context.Query.Context.QueryPlanner().PlanQuery(new QueryInfo(context, QueryExpression, OrderBy, Limit));
return new Prepared(queryPlan);
Expand Down

0 comments on commit dc91526

Please sign in to comment.