ByteSlice Conv_IntToDouble(ByteSlice value, int ResultSize, DbFunctionTools tools) { int x = tools.GetInt(value); DbValue v = tools.AllocValue((double)x); return(v.Eval()); }
ByteSlice Conv_LongToDouble(ByteSlice value, int ResultSize, DbFunctionTools tools) { long x = tools.GetLong(value); DbValue v = tools.AllocValue((double)x); return(v.Eval()); }
public static DbValue NULLIF(DbFunctionTools tools, DbFunctionArguments args) { string FunctionName = "NULLIF"; args.EnsureCount(FunctionName, 2); DbType arg0type; ByteSlice arg0 = args[0].Eval(out arg0type); ImmediateValue argval = null; argval = tools.AllocValue(arg0type.ID); argval.SetValue(arg0); DbFunctionArguments compareargs = new DbFunctionArguments(new DbValue[2]); compareargs[0] = argval; compareargs[1] = args[1]; DbValue result = COMPARE(tools, compareargs); int iresult = tools.GetInt(result); if (iresult == 0) { List <byte> buf = tools.AllocBuffer(arg0type.Size); buf.Add(1); for (int i = 0; i < arg0type.Size - 1; i++) { buf.Add(0); } return(tools.AllocValue(ByteSlice.Prepare(buf), arg0type)); } else { return(args[0]); } }
public void FreeLastValue(DbValue val) { if (curval <= 0 || !object.ReferenceEquals(values[curval - 1], val)) { throw new Exception("Mismatched AllocValue / FreeLastValue"); } curval--; }
ByteSlice Conv_String(ByteSlice value, int ResultSize, DbFunctionTools tools) { mstring x = tools.GetString(value); x = x.ToUpperM(); DbValue v = tools.AllocValue(x, ResultSize); return(v.Eval()); }
public static DbValue ExecDbScalarFunction(string name, DbFunctionTools tools, DbFunctionArguments args) { DbValue result = TryExecDbScalarFunction(name, tools, args); if (null == result) { throw new DbExecException("No such scalar function named '" + name + "'"); } return(result); }
public DateTime GetDateTime(DbValue input) { DbType type; ByteSlice bs = input.Eval(out type); if (DbTypeID.DATETIME != type.ID) { throw new Exception("Expected DATETIME, not " + type.Name.ToUpper()); } return GetDateTime(bs); }
public long GetLong(DbValue input) { DbType type; ByteSlice bs = input.Eval(out type); if (DbTypeID.LONG != type.ID) { throw new Exception("Expected LONG, not " + type.Name.ToUpper()); } return GetLong(bs); }
public int GetInt(DbValue input) { DbType type; ByteSlice bs = input.Eval(out type); if (DbTypeID.INT != type.ID) { throw new Exception("Expected INT, not " + type.Name.ToUpper()); } return GetInt(bs); }
public static DbValue ExecDbAggregator(string name, DbFunctionTools tools, DbAggregatorArguments args) { DbValue result = TryExecDbAggregator(name, tools, args); if (null == result) { throw new DbExecException("No such aggregate function named '" + name + "'"); } return(result); }
public double GetDouble(DbValue input) { DbType type; ByteSlice bs = input.Eval(out type); if (DbTypeID.DOUBLE != type.ID) { throw new Exception("Expected DOUBLE, not " + type.Name.ToUpper()); } return(GetDouble(bs)); }
public int GetInt(DbValue input) { DbType type; ByteSlice bs = input.Eval(out type); if (DbTypeID.INT != type.ID) { throw new Exception("Expected INT, not " + type.Name.ToUpper()); } return(GetInt(bs)); }
public long GetLong(DbValue input) { DbType type; ByteSlice bs = input.Eval(out type); if (DbTypeID.LONG != type.ID) { throw new Exception("Expected LONG, not " + type.Name.ToUpper()); } return(GetLong(bs)); }
public DateTime GetDateTime(DbValue input) { DbType type; ByteSlice bs = input.Eval(out type); if (DbTypeID.DATETIME != type.ID) { throw new Exception("Expected DATETIME, not " + type.Name.ToUpper()); } return(GetDateTime(bs)); }
public mstring GetString(DbValue input) { DbType type; ByteSlice bs = input.Eval(out type); if (DbTypeID.CHARS != type.ID) { throw new Exception("Expected CHAR(n), not " + type.Name.ToUpper()); } return(GetString(bs)); }
public CompareEval(IValueContext Context, DbValue val1, DbValue val2) : base(Context) { this.val1 = val1; this.val2 = val2; List <DbValue> argsbuf = new List <DbValue>(2); argsbuf.Add(val1); argsbuf.Add(val2); args = new DbFunctionArguments(argsbuf); }
// 0 is match, <0 is less than, >0 is greater than. public int Compare() { DbValue result = Context.ExecDbFunction("COMPARE", args); DbType typeresult; ByteSlice bsresult = result.Eval(out typeresult); #if DEBUG if (DbTypeID.INT != typeresult.ID) { throw new Exception("DEBUG: COMPARE should return INT, not " + typeresult.Name.ToUpper()); } #endif return(Context.Tools.GetInt(bsresult)); }
public static DbValue LESSEREQUAL(DbFunctionTools tools, DbFunctionArguments args) { DbValue dbvcompare = COMPARE(tools, args); DbType typecompare; ByteSlice bsresult = dbvcompare.Eval(out typecompare); if (DbTypeID.INT != typecompare.ID) { return(tools.AllocValue(bsresult, typecompare)); } int compare = tools.GetInt(bsresult); return(tools.AllocValue(compare <= 0 ? 1 : 0)); }
public static DbValue MAX(DbFunctionTools tools, DbAggregatorArguments args) { string AggregatorName = "MAX"; DbValue highest = null; DbValue nullest = null; DbFunctionArguments compareargs = new DbFunctionArguments(new DbValue[2]); ImmediateValue argval = null; for (int iarg = 0; iarg < args.Length; iarg++) { args[iarg].EnsureCount(AggregatorName, 1); DbType arg0type; ByteSlice arg0 = args[iarg][0].Eval(out arg0type); if (Types.IsNullValue(arg0)) { if (null == nullest) { nullest = tools.AllocValue(arg0, arg0type); } } else { if (null == argval) { argval = tools.AllocValue(arg0type.ID); } argval.SetValue(arg0); compareargs[0] = argval; compareargs[1] = highest; if (null == highest || tools.GetInt(DbFunctions.COMPARE(tools, compareargs)) > 0) { highest = argval; // Keep this one. argval = null; // New one next time. } } } if (null == highest) { if (null == nullest) { return(tools.AllocNullValue()); } return(nullest); } return(highest); }
// frowsIndex is not preserved to the caller! List <DbValue> _ProcessSelectCallInfo(CallInfo ci) { int frowsTotal = frows.Count; List <DbValue> results = functools.AllocDbValueList(); bool IsAggregator = (null != ci.func && DbExec.DbAggregatorExists(ci.func)); List <DbFunctionArguments> fargs = _CallArgsToExecArgs(ci); if (IsAggregator) { DbValue value = DbExec.ExecDbAggregator(ci.func, functools, new DbAggregatorArguments(fargs)); results.Add(value); } else { int fargsCount = fargs.Count; if (null != ci.func) { for (int ifarg = 0; ifarg < fargsCount; ifarg++) { DbValue value = DbExec.ExecDbScalarFunction(ci.func, functools, fargs[ifarg]); results.Add(value); } } else //if (null == ci.func) { for (int ifarg = 0; ifarg < fargsCount; ifarg++) { #if DEBUG if (1 != fargs[ifarg].Length) { throw new Exception("DEBUG: (1 != fargs[ifarg].Length)"); } #endif DbValue value = fargs[ifarg][0]; results.Add(value); } } } return(results); }
public CompareNotEqualEval(IValueContext Context, DbValue val1, DbValue val2) : base(Context, val1, val2) { }
public void FreeLastValue(DbValue val) { if (curval <= 0 || !object.ReferenceEquals(values[curval - 1], val)) { throw new Exception("Mismatched AllocValue / FreeLastValue"); } curval--; }
CallInfo _GetSelectPartCalls(ref string calls) { CallInfo ci = new CallInfo(); { string colpart = Qa.NextPart(ref calls); if (0 == colpart.Length) { throw new Exception("Expected value"); } if ("-" == colpart || "+" == colpart) { colpart += Qa.NextPart(ref calls); } string s; s = Qa.NextPart(ref calls); if ("(" == s) { List <CallInfoArg> args = new List <CallInfoArg>(4); { string xcalls = calls; if (")" == Qa.NextPart(ref xcalls)) { calls = xcalls; ci.func = colpart; ci.args = args; return(ci); } } for (; ;) { CallInfo nestci = _GetSelectPartCalls(ref calls); CallInfoArg arg = new CallInfoArg(); if (nestci.func == null) { #if DEBUG if (1 != nestci.args.Count) { throw new Exception("DEBUG: (1 != nestci.args.Count)"); } #endif arg = nestci.args[0]; } else { arg.nest = nestci; } args.Add(arg); s = Qa.NextPart(ref calls); if (0 == string.Compare("AS", s, true)) { arg = new CallInfoArg(); { StringBuilder sbas = new StringBuilder(); int asparens = 0; for (; ;) { s = Qa.NextPart(ref calls); if (0 == s.Length || "," == s) { //calls += s; break; } else if ("(" == s) { asparens++; sbas.Append(s); } else if (")" == s) { if (0 == asparens) { //calls += s; break; } asparens--; sbas.Append(s); } else { sbas.Append(s); } } if (0 == sbas.Length) { throw new Exception("Expected type after AS"); } { DbValue iterval = functools.AllocValue(mstring.Prepare("AS " + sbas.ToString())); // Need to copy the value out of the functools memory // so that it survives this map/reduce iteration... DbType xtype; ByteSlice iterbs = iterval.Eval(out xtype); List <byte> newbuf = new List <byte>(iterbs.Length); iterbs.AppendTo(newbuf); arg.value = new ImmediateValue(null, ByteSlice.Prepare(newbuf), xtype); } args.Add(arg); } //s = Qa.NextPart(ref calls); } if (s == ",") { continue; } if (s == ")") { string xnc = calls; s = Qa.NextPart(ref xnc); if (0 == s.Length || "," == s || ")" == s || 0 == string.Compare(s, "AS", true)) { ci.func = colpart; ci.args = args; return(ci); } else { throw new Exception("Unexpected: " + s); } break; } else { if (s.Length != 0) { throw new Exception("Unexpected: " + s); } throw new Exception("Unexpected end of select clause"); } } // Doesn't reach here. } else { //if (0 == s.Length || "," == s || ")" == s) { calls = s + " " + calls; // Undo. //ci.func = null; CallInfoArg arg; arg = new CallInfoArg(); //arg.s = colpart; if (colpart.Length > 0 && (char.IsLetter(colpart[0]) || '_' == colpart[0])) { int icol = DbColumn.IndexOf(cols, colpart); if (-1 == icol) { throw new Exception("No such column named " + colpart); } arg.value = new ColValue(this, cols[icol]); } else { DbTypeID typeid; ByteSlice bs = Types.LiteralToValue(colpart, out typeid); arg.value = new ImmediateValue(null, bs, DbType.Prepare(bs.Length, typeid)); } ci.args = new CallInfoArg[1]; ci.args[0] = arg; return(ci); } /*else * { * throw new Exception("Unexpected: " + s); * }*/ } } }
// Converts the CallInfoArgs of a CallInfo into arguments for DbExec. // frowsIndex is not preserved to the caller! List <DbFunctionArguments> _CallArgsToExecArgs(CallInfo ci) { List <List <DbValue> > fvargs = _NextCallList(); int frowsCount = frows.Count; for (int irow = 0; irow < frowsCount; irow++) { fvargs.Add(functools.AllocDbValueList()); } int argsCount = ci.args.Count; for (int iarg = 0; iarg < argsCount; iarg++) { if (null != ci.args[iarg].value) { DbValue favalue = ci.args[iarg].value; for (this.frowsIndex = 0; this.frowsIndex < frowsCount; this.frowsIndex++) { // Evaluate favalue now that this.frowsIndex (IValueContext) is updated. // Field evaluation... DbType etype; ByteSlice ebs = favalue.Eval(out etype); DbValue evalue = functools.AllocValue(ebs, etype); fvargs[this.frowsIndex].Add(evalue); } } else //if (null == args[iarg].value) { CallInfo nestci = ci.args[iarg].nest; List <DbValue> nestresults; { int save_frowsIndex = this.frowsIndex; nestresults = _ProcessSelectCallInfo(nestci); this.frowsIndex = save_frowsIndex; } int nestresultsCount = nestresults.Count; if (1 == nestresultsCount) { DbValue favalue = nestresults[0]; for (int irow = 0; irow < frowsCount; irow++) { List <DbValue> dbvaluelist = fvargs[irow]; fvargs[irow].Add(favalue); fvargs[irow] = dbvaluelist; } } else if (frowsCount == nestresultsCount) { for (int irow = 0; irow < frowsCount; irow++) { List <DbValue> dbvaluelist = fvargs[irow]; fvargs[irow].Add(nestresults[irow]); fvargs[irow] = dbvaluelist; } } else { throw new Exception("Unexpected number of values returned; on a group of " + frowsCount.ToString() + " rows, function returned " + nestresultsCount.ToString() + " values"); } } } List <DbFunctionArguments> fargs = _NextCallArgs(); for (int irow = 0; irow < frowsCount; irow++) { DbFunctionArguments fa = new DbFunctionArguments(fvargs[irow]); fargs.Add(fa); } return(fargs); }
public override void Map(ByteSlice row, MapOutput output) { if (JoinType.X == type) { ftools = new DbFunctionTools(); string QlLeftTableName = DSpace_ExecArgs[0]; LeftTableName = Qa.QlArgsUnescape(QlLeftTableName); string stype = DSpace_ExecArgs[1]; string QlRightTableName = DSpace_ExecArgs[2]; RightTableName = Qa.QlArgsUnescape(QlRightTableName); string QlOn = DSpace_ExecArgs[3]; On = Qa.QlArgsUnescape(QlOn); { string LeftColInfo = Qa.QlArgsUnescape(DSpace_ExecArgs[4]); int ileq = LeftColInfo.LastIndexOf('='); string sLeftType = LeftColInfo.Substring(ileq + 1); LeftType = DbType.Prepare(sLeftType); LeftConv = NeedConv(LeftType, RightType); string[] st = LeftColInfo.Substring(0, ileq).Split(','); LeftOffset = int.Parse(st[0]); LeftSize = int.Parse(st[1]); } { string RightColInfo = Qa.QlArgsUnescape(DSpace_ExecArgs[5]); int ileq = RightColInfo.LastIndexOf('='); string sRightType = RightColInfo.Substring(ileq + 1); RightType = DbType.Prepare(sRightType); RightConv = NeedConv(RightType, LeftType); string[] st = RightColInfo.Substring(0, ileq).Split(','); RightOffset = int.Parse(st[0]); RightSize = int.Parse(st[1]); } if (0 == string.Compare("INNER", stype, true)) { type = JoinType.INNER_JOIN; } else if (0 == string.Compare("LEFT_OUTER", stype, true)) { type = JoinType.LEFT_OUTER_JOIN; } else if (0 == string.Compare("RIGHT_OUTER", stype, true)) { type = JoinType.RIGHT_OUTER_JOIN; } else { throw new NotSupportedException("DEBUG: JOIN type not supported: " + stype); } string DfsTableFilesInput = DSpace_ExecArgs[6]; /* * TableFileNames = DfsTableFilesInput.Split(';'); * if (2 != TableFileNames.Length) * { * throw new Exception("DEBUG: Invalid number of tables"); * } * for (int it = 0; it < TableFileNames.Length; it++) * { * if (TableFileNames[it].StartsWith("dfs://", StringComparison.OrdinalIgnoreCase)) * { * TableFileNames[it] = TableFileNames[it].Substring(6); * } * int iat = TableFileNames[it].IndexOf('@'); * if (-1 != iat) * { * TableFileNames[it] = TableFileNames[it].Substring(0, iat); * } * } * */ } int tableid = GetTableID(row); ByteSlice key; if (0 == tableid) { key = ByteSlice.Prepare(row, LeftOffset, LeftSize); if (null != LeftConv) { key = LeftConv(key, DSpace_KeyLength, ftools); } } else if (1 == tableid) { key = ByteSlice.Prepare(row, RightOffset, RightSize); if (null != RightConv) { key = RightConv(key, DSpace_KeyLength, ftools); } } else { throw new Exception("Map: Unexpected TableID: " + tableid); } List <byte> valuebuf = ftools.AllocBuffer(1 + 4 + row.Length); { DbValue tableiddbvalue = ftools.AllocValue(tableid); tableiddbvalue.Eval().AppendTo(valuebuf); } row.AppendTo(valuebuf); output.Add(key, ByteSlice.Prepare(valuebuf)); ftools.ResetBuffers(); }
public static void DbFunctions_CAST() { DbFunctionTools tools = new DbFunctionTools(); DateTime dt = DateTime.Now; DbValue[] conv = new DbValue[] { tools.AllocValue((int)-372), tools.AllocValue((int)-372), tools.AllocValue((int)-372), tools.AllocValue((long)-372), tools.AllocValue((int)-372), tools.AllocValue((double)-372), //tools.AllocValue((int)dt.Ticks), tools.AllocValue(new DateTime((int)dt.Ticks)), tools.AllocValue((int)-372), tools.AllocValue(mstring.Prepare("-372")), tools.AllocValue((int)-372), tools.AllocValue(mstring.Prepare("-372"), 51), tools.AllocValue((long)-372), tools.AllocValue((int)-372), tools.AllocValue((long)-372), tools.AllocValue((long)-372), tools.AllocValue((long)-372), tools.AllocValue((double)-372), tools.AllocValue((long)dt.Ticks), tools.AllocValue(new DateTime((long)dt.Ticks)), tools.AllocValue((long)-372), tools.AllocValue(mstring.Prepare("-372")), tools.AllocValue((long)-372), tools.AllocValue(mstring.Prepare("-372"), 51), tools.AllocValue((double)-372), tools.AllocValue((int)-372), tools.AllocValue((double)-372), tools.AllocValue((long)-372), tools.AllocValue((double)-372), tools.AllocValue((double)-372), tools.AllocValue((double)dt.Ticks), tools.AllocValue(new DateTime((long)(double)dt.Ticks)), tools.AllocValue((double)-372), tools.AllocValue(mstring.Prepare("-372")), tools.AllocValue((double)-372), tools.AllocValue(mstring.Prepare("-372"), 51), // Extra double ones: tools.AllocValue((double)101.1), tools.AllocValue((int)101), tools.AllocValue((double)101.1), tools.AllocValue((long)101), tools.AllocValue((double)101.1), tools.AllocValue(mstring.Prepare((double)101.1)), tools.AllocValue((double)(22.0/7.0)), tools.AllocValue(mstring.Prepare((double)(22.0/7.0))), tools.AllocValue(dt), tools.AllocValue((int)dt.Ticks), tools.AllocValue(dt), tools.AllocValue((long)dt.Ticks), tools.AllocValue(dt), tools.AllocValue((double)dt.Ticks), tools.AllocValue(dt), tools.AllocValue(dt), tools.AllocValue(dt), tools.AllocValue(mstring.Prepare(dt.ToString())), tools.AllocValue(dt), tools.AllocValue(mstring.Prepare(dt.ToString()), 51), tools.AllocValue(mstring.Prepare("-372")), tools.AllocValue((int)-372), tools.AllocValue(mstring.Prepare("-372")), tools.AllocValue((long)-372), tools.AllocValue(mstring.Prepare("-372")), tools.AllocValue((double)-372), tools.AllocValue(mstring.Prepare(dt.ToString())), tools.AllocValue(dt), tools.AllocValue(mstring.Prepare("-372")), tools.AllocValue(mstring.Prepare("-372")), tools.AllocValue(mstring.Prepare("-372")), tools.AllocValue(mstring.Prepare("-372"), 51), // Extra string ones: tools.AllocValue(mstring.Prepare("-372"), 51), tools.AllocValue(mstring.Prepare("-372"), 101), tools.AllocValue(mstring.Prepare("-372"), 101), tools.AllocValue(mstring.Prepare("-372"), 51), null }; for (int ic = 0; ic + 2 <= conv.Length; ic += 2) { DbValue a = conv[ic + 0]; DbType atype; ByteSlice abytes = a.Eval(out atype); DbValue b = conv[ic + 1]; DbType btype; ByteSlice bbytes = b.Eval(out btype); Console.WriteLine("Testing DbFunctions.CAST({0} AS {1})...", atype.Name, btype.Name); List<DbValue> args = new List<DbValue>(); args.Add(a); args.Add(tools.AllocValue(mstring.Prepare("AS " + btype.Name))); DbFunctionArguments fargs = new DbFunctionArguments(args); DbValue r = DbFunctions.CAST(tools, fargs); DbType rtype; ByteSlice rbytes = r.Eval(out rtype); if (rtype.ID != btype.ID) { throw new Exception(string.Format("DbFunctions.CAST({0} AS {1}) resulted in type {2}: result has unexpected ID of {3}", atype.Name, btype.Name, btype.Name, rtype.ID)); } if (rtype.Size != btype.Size) { throw new Exception(string.Format("DbFunctions.CAST({0} AS {1}) resulted in type {2}: result has unexpected size of {3}", atype.Name, btype.Name, btype.Name, rtype.Size)); } if (rtype.ID == DbTypeID.DATETIME) { if (tools.GetDateTime(rbytes).ToString() != tools.GetDateTime(bbytes).ToString()) { throw new Exception(string.Format("DbFunctions.CAST({0} AS {1}) resulted in type {2}: result has unexpected value", atype.Name, btype.Name, btype.Name)); } } else { for (int ix = 0; ix < rtype.Size; ix++) { if (rbytes[ix] != bbytes[ix]) { throw new Exception(string.Format("DbFunctions.CAST({0} AS {1}) resulted in type {2}: result has unexpected value", atype.Name, btype.Name, btype.Name)); } } } } }
public LessThanOrEqualEval(IValueContext Context, DbValue val1, DbValue val2) : base(Context, val1, val2) { }
public DbEvalIntBoolValue(IValueContext Context, DbValue val) : base(Context) { this.val = val; }
public CompareNotEqualEval(IValueContext Context, DbValue val1, DbValue val2) : base(Context, val1, val2) { }
public GreaterThanOrEqualEval(IValueContext Context, DbValue val1, DbValue val2) : base(Context, val1, val2) { }
public CompareEval(IValueContext Context, DbValue val1, DbValue val2) : base(Context) { this.val1 = val1; this.val2 = val2; List<DbValue> argsbuf = new List<DbValue>(2); argsbuf.Add(val1); argsbuf.Add(val2); args = new DbFunctionArguments(argsbuf); }
public mstring GetString(DbValue input) { DbType type; ByteSlice bs = input.Eval(out type); if (DbTypeID.CHARS != type.ID) { throw new Exception("Expected CHAR(n), not " + type.Name.ToUpper()); } return GetString(bs); }
public DbEvalIntBoolValue(IValueContext Context, DbValue val) : base(Context) { this.val = val; }
public LessThanOrEqualEval(IValueContext Context, DbValue val1, DbValue val2) : base(Context, val1, val2) { }
public GreaterThanOrEqualEval(IValueContext Context, DbValue val1, DbValue val2) : base(Context, val1, val2) { }
public override ByteSlice Eval(out DbType type) { DbValue ret = Context.ExecDbFunction(funcname, args); return(ret.Eval(out type)); }
public double GetDouble(DbValue input) { DbType type; ByteSlice bs = input.Eval(out type); if (DbTypeID.DOUBLE != type.ID) { throw new Exception("Expected DOUBLE, not " + type.Name.ToUpper()); } return GetDouble(bs); }