コード例 #1
0
ファイル: data_test.go プロジェクト: alaypatel07/cockroach
func TestSetGetChecked(t *testing.T) {
	v := Value{}

	v.SetBytes(nil)
	if _, err := v.GetBytes(); err != nil {
		t.Fatal(err)
	}

	f := 1.1
	v.SetFloat(f)
	if r, err := v.GetFloat(); err != nil {
		t.Fatal(err)
	} else if f != r {
		t.Errorf("set %f on a value and extracted it, expected %f back, but got %f", f, f, r)
	}

	i := int64(1)
	v.SetInt(i)
	if r, err := v.GetInt(); err != nil {
		t.Fatal(err)
	} else if i != r {
		t.Errorf("set %d on a value and extracted it, expected %d back, but got %d", i, i, r)
	}

	d := decimal.New(11, -1)
	if err := v.SetDecimal(d); err != nil {
		t.Fatal(err)
	}
	if r, err := v.GetDecimal(); err != nil {
		t.Fatal(err)
	} else if !d.Equals(r) {
		t.Errorf("set %s on a value and extracted it, expected %s back, but got %s", d, d, r)
	}

	if err := v.SetProto(&Value{}); err != nil {
		t.Fatal(err)
	}
	if err := v.GetProto(&Value{}); err != nil {
		t.Fatal(err)
	}
	if _, err := v.GetBytes(); err != nil {
		t.Fatal(err)
	}

	if err := v.SetProto(&InternalTimeSeriesData{}); err != nil {
		t.Fatal(err)
	}
	if _, err := v.GetTimeseries(); err != nil {
		t.Fatal(err)
	}

	ti := time.Time{}
	v.SetTime(ti)
	if r, err := v.GetTime(); err != nil {
		t.Fatal(err)
	} else if !ti.Equal(r) {
		t.Errorf("set %s on a value and extracted it, expected %s back, but got %s", ti, ti, r)
	}
}
コード例 #2
0
ファイル: group.go プロジェクト: kimh/cockroach
func (a *avgAggregate) result() (parser.Datum, error) {
	sum, err := a.sumAggregate.result()
	if err != nil {
		return parser.DNull, err
	}
	if sum == parser.DNull {
		return sum, nil
	}
	switch t := sum.(type) {
	case parser.DInt:
		// TODO(nvanbenschoten) decimal: this should be a numeric, once
		// better type coercion semantics are defined.
		return parser.DFloat(t) / parser.DFloat(a.count), nil
	case parser.DFloat:
		return t / parser.DFloat(a.count), nil
	case parser.DDecimal:
		return parser.DDecimal{Decimal: t.Div(decimal.New(int64(a.count), 0))}, nil
	default:
		return parser.DNull, util.Errorf("unexpected SUM result type: %s", t.Type())
	}
}
コード例 #3
0
ファイル: set.go プロジェクト: alaypatel07/cockroach
func (p *planner) SetTimeZone(n *parser.SetTimeZone) (planNode, *roachpb.Error) {
	d, err := n.Value.Eval(p.evalCtx)
	if err != nil {
		return nil, roachpb.NewError(err)
	}
	var offset int64
	switch v := d.(type) {
	case parser.DString:
		location := string(v)
		if location == "DEFAULT" || location == "LOCAL" {
			location = "UTC"
		}
		if _, err := time.LoadLocation(location); err != nil {
			return nil, roachpb.NewUErrorf("cannot find time zone %q: %v", location, err)
		}
		p.session.Timezone = &Session_Location{Location: location}

	case parser.DInterval:
		offset = int64(v.Duration / time.Second)

	case parser.DInt:
		offset = int64(v) * 60 * 60

	case parser.DFloat:
		offset = int64(float64(v) * 60.0 * 60.0)

	case parser.DDecimal:
		sixty := decimal.New(60, 0)
		offset = v.Mul(sixty).Mul(sixty).IntPart()

	default:
		return nil, roachpb.NewUErrorf("bad time zone value: %v", n.Value)
	}
	if offset != 0 {
		p.session.Timezone = &Session_Offset{Offset: offset}
	}
	p.evalCtx.GetLocation = p.session.getLocation
	return &valuesNode{}, nil
}
コード例 #4
0
ファイル: values_test.go プロジェクト: alaypatel07/cockroach
func TestGolangParams(t *testing.T) {
	defer leaktest.AfterTest(t)
	// Each test case pairs an arbitrary value and parser.Datum which has the same
	// type
	testCases := []struct {
		value        interface{}
		expectedType reflect.Type
	}{
		// Null type.
		{nil, reflect.TypeOf(parser.DNull)},

		// Bool type.
		{true, reflect.TypeOf(parser.DummyBool)},

		// Primitive Integer types.
		{int(1), reflect.TypeOf(parser.DummyInt)},
		{int8(1), reflect.TypeOf(parser.DummyInt)},
		{int16(1), reflect.TypeOf(parser.DummyInt)},
		{int32(1), reflect.TypeOf(parser.DummyInt)},
		{int64(1), reflect.TypeOf(parser.DummyInt)},
		{uint(1), reflect.TypeOf(parser.DummyInt)},
		{uint8(1), reflect.TypeOf(parser.DummyInt)},
		{uint16(1), reflect.TypeOf(parser.DummyInt)},
		{uint32(1), reflect.TypeOf(parser.DummyInt)},
		{uint64(1), reflect.TypeOf(parser.DummyInt)},

		// Primitive Float types.
		{float32(1.0), reflect.TypeOf(parser.DummyFloat)},
		{float64(1.0), reflect.TypeOf(parser.DummyFloat)},

		// Decimal type.
		{decimal.New(55, -1), reflect.TypeOf(parser.DummyDecimal)},

		// String type.
		{"test", reflect.TypeOf(parser.DummyString)},

		// Bytes type.
		{[]byte("abc"), reflect.TypeOf(parser.DummyBytes)},

		// Interval and timestamp.
		{time.Duration(1), reflect.TypeOf(parser.DummyInterval)},
		{time.Now(), reflect.TypeOf(parser.DummyTimestamp)},

		// Primitive type aliases.
		{roachpb.NodeID(1), reflect.TypeOf(parser.DummyInt)},
		{ID(1), reflect.TypeOf(parser.DummyInt)},
		{floatAlias(1), reflect.TypeOf(parser.DummyFloat)},
		{boolAlias(true), reflect.TypeOf(parser.DummyBool)},
		{stringAlias("string"), reflect.TypeOf(parser.DummyString)},

		// Byte slice aliases.
		{roachpb.Key("key"), reflect.TypeOf(parser.DummyBytes)},
		{roachpb.RKey("key"), reflect.TypeOf(parser.DummyBytes)},
	}

	for i, tcase := range testCases {
		params := golangParameters([]interface{}{tcase.value})
		output, valid := params.Arg("1")
		if !valid {
			t.Errorf("case %d failed: argument was invalid", i)
			continue
		}
		if a, e := reflect.TypeOf(output), tcase.expectedType; a != e {
			t.Errorf("case %d failed: expected type %s, got %s", i, e.String(), a.String())
		}
	}
}
コード例 #5
0
ファイル: decimal_test.go プロジェクト: alaypatel07/cockroach
func TestEncodeDecimal(t *testing.T) {
	testCases := []struct {
		Value    decimal.Decimal
		Encoding []byte
	}{
		{decimal.NewFromFloat(-math.MaxFloat64), []byte{0x04, 0x64, 0xfc, 0x60, 0x66, 0x44, 0xe4, 0x9e, 0x82, 0xc0, 0x8d, 0x0}},
		{decimal.New(-1, 308), []byte{0x04, 0x64, 0xfd, 0x0}},
		// Four duplicates to make sure -1*10^4 <= -10*10^3 <= -100*10^2 <= -1*10^4
		{decimal.New(-1, 4), []byte{0x0c, 0xfd, 0x0}},
		{decimal.New(-10, 3), []byte{0x0c, 0xfd, 0x0}},
		{decimal.New(-100, 2), []byte{0x0c, 0xfd, 0x0}},
		{decimal.New(-1, 4), []byte{0x0c, 0xfd, 0x0}},
		{decimal.New(-9999, 0), []byte{0x0d, 0x38, 0x39, 0x00}},
		{decimal.New(-10, 1), []byte{0x0d, 0xfd, 0x00}},
		{decimal.New(-99, 0), []byte{0x0e, 0x39, 0x00}},
		{decimal.New(-1, 0), []byte{0x0e, 0xfd, 0x0}},
		{decimal.New(-123, -5), []byte{0x10, 0x1, 0xe6, 0xc3, 0x0}},
		{decimal.New(-1, -307), []byte{0x10, 0x99, 0xeb, 0x0}},
		{decimal.NewFromFloat(-math.SmallestNonzeroFloat64), []byte{0x10, 0xa1, 0xf5, 0x0}},
		{decimal.New(0, 0), []byte{0x11}},
		{decimal.NewFromFloat(math.SmallestNonzeroFloat64), []byte{0x12, 0x5e, 0xa, 0x0}},
		{decimal.New(1, -307), []byte{0x12, 0x66, 0x14, 0x0}},
		{decimal.New(123, -5), []byte{0x12, 0xfe, 0x19, 0x3c, 0x0}},
		{decimal.New(123, -4), []byte{0x13, 0x03, 0x2e, 0x0}},
		{decimal.New(123, -3), []byte{0x13, 0x19, 0x3c, 0x0}},
		{decimal.New(1, 0), []byte{0x14, 0x02, 0x0}},
		{decimal.New(1, 1), []byte{0x14, 0x14, 0x0}},
		{decimal.New(12345, -3), []byte{0x14, 0x19, 0x45, 0x64, 0x0}},
		{decimal.New(990, -1), []byte{0x14, 0xc6, 0x0}},
		{decimal.New(990001, -4), []byte{0x14, 0xc7, 0x01, 0x02, 0x0}},
		{decimal.New(9901, -2), []byte{0x14, 0xc7, 0x02, 0x0}},
		{decimal.New(10, 1), []byte{0x15, 0x02, 0x0}},
		{decimal.New(10001, -2), []byte{0x15, 0x03, 0x01, 0x02, 0x0}},
		{decimal.New(1001, -1), []byte{0x15, 0x03, 0x01, 0x14, 0x0}},
		{decimal.New(1234, 0), []byte{0x15, 0x19, 0x44, 0x0}},
		{decimal.New(12345, -1), []byte{0x15, 0x19, 0x45, 0x64, 0x0}},
		{decimal.New(9999, 0), []byte{0x15, 0xc7, 0xc6, 0x0}},
		{decimal.New(9999000001, -6), []byte{0x15, 0xc7, 0xc7, 0x01, 0x01, 0x02, 0x0}},
		{decimal.New(9999000009, -6), []byte{0x15, 0xc7, 0xc7, 0x01, 0x01, 0x12, 0x0}},
		{decimal.New(9999000010, -6), []byte{0x15, 0xc7, 0xc7, 0x01, 0x01, 0x14, 0x0}},
		{decimal.New(9999000090, -6), []byte{0x15, 0xc7, 0xc7, 0x01, 0x01, 0xb4, 0x0}},
		{decimal.New(9999000099, -6), []byte{0x15, 0xc7, 0xc7, 0x01, 0x01, 0xc6, 0x0}},
		{decimal.New(99990001, -4), []byte{0x15, 0xc7, 0xc7, 0x01, 0x02, 0x0}},
		{decimal.New(9999001, -3), []byte{0x15, 0xc7, 0xc7, 0x01, 0x14, 0x0}},
		{decimal.New(999901, -2), []byte{0x15, 0xc7, 0xc7, 0x02, 0x0}},
		{decimal.New(99991, -1), []byte{0x15, 0xc7, 0xc7, 0x14, 0x0}},
		{decimal.New(10000, 0), []byte{0x16, 0x02, 0x0}},
		{decimal.New(10001, 0), []byte{0x16, 0x03, 0x01, 0x02, 0x0}},
		{decimal.New(12345, 0), []byte{0x16, 0x03, 0x2f, 0x5a, 0x0}},
		{decimal.New(123450, 0), []byte{0x16, 0x19, 0x45, 0x64, 0x0}},
		{decimal.New(1, 308), []byte{0x1e, 0x9b, 0x2, 0x0}},
		{decimal.NewFromFloat(math.MaxFloat64), []byte{0x1e, 0x9b, 0x3, 0x9f, 0x99, 0xbb, 0x1b, 0x61, 0x7d, 0x3f, 0x72, 0x0}},
	}

	var lastEncoded []byte
	for _, dir := range []Direction{Ascending, Descending} {
		for i, c := range testCases {
			var enc []byte
			var err error
			var dec decimal.Decimal
			if dir == Ascending {
				enc = EncodeDecimalAscending(nil, c.Value)
				_, dec, err = DecodeDecimalAscending(enc, nil)
			} else {
				enc = EncodeDecimalDescending(nil, c.Value)
				_, dec, err = DecodeDecimalDescending(enc, nil)
			}
			if dir == Ascending && !bytes.Equal(enc, c.Encoding) {
				t.Errorf("unexpected mismatch for %s. expected [% x], got [% x]",
					c.Value, c.Encoding, enc)
			}
			if i > 0 {
				if (bytes.Compare(lastEncoded, enc) > 0 && dir == Ascending) ||
					(bytes.Compare(lastEncoded, enc) < 0 && dir == Descending) {
					t.Errorf("%v: expected [% x] to be less than or equal to [% x]",
						c.Value, testCases[i-1].Encoding, enc)
				}
			}
			if err != nil {
				t.Error(err)
				continue
			}
			if !dec.Equals(c.Value) {
				t.Errorf("%d unexpected mismatch for %v. got %v", i, c.Value, dec)
			}
			lastEncoded = enc
		}

		// Test that appending the decimal to an existing buffer works.
		var enc []byte
		var dec decimal.Decimal
		other := decimal.NewFromFloat(1.23)
		if dir == Ascending {
			enc = EncodeDecimalAscending([]byte("hello"), other)
			_, dec, _ = DecodeDecimalAscending(enc[5:], nil)
		} else {
			enc = EncodeDecimalDescending([]byte("hello"), other)
			_, dec, _ = DecodeDecimalDescending(enc[5:], nil)
		}
		if !dec.Equals(other) {
			t.Errorf("unexpected mismatch for %v. got %v", 1.23, other)
		}
	}
}
コード例 #6
0
ファイル: printer_test.go プロジェクト: alaypatel07/cockroach
func TestPrettyPrint(t *testing.T) {
	defer leaktest.AfterTest(t)

	tm, _ := time.Parse(time.UnixDate, "Sat Mar  7 11:06:39 UTC 2015")

	testCases := []struct {
		key roachpb.Key
		exp string
	}{
		// local
		{StoreIdentKey(), "/Local/Store/storeIdent"},
		{StoreGossipKey(), "/Local/Store/gossipBootstrap"},
		{SequenceCacheKeyPrefix(roachpb.RangeID(1000001), []byte("test0")), `/Local/RangeID/1000001/SequenceCache/"test0"`},
		{SequenceCacheKey(roachpb.RangeID(1000001), []byte("test0"), uint32(111), uint32(222)), `/Local/RangeID/1000001/SequenceCache/"test0"/epoch:111/seq:222`},
		{RaftLeaderLeaseKey(roachpb.RangeID(1000001)), "/Local/RangeID/1000001/RaftLeaderLease"},
		{RaftTombstoneKey(roachpb.RangeID(1000001)), "/Local/RangeID/1000001/RaftTombstone"},
		{RaftHardStateKey(roachpb.RangeID(1000001)), "/Local/RangeID/1000001/RaftHardState"},
		{RaftAppliedIndexKey(roachpb.RangeID(1000001)), "/Local/RangeID/1000001/RaftAppliedIndex"},
		{RaftLogKey(roachpb.RangeID(1000001), uint64(200001)), "/Local/RangeID/1000001/RaftLog/logIndex:200001"},
		{RaftTruncatedStateKey(roachpb.RangeID(1000001)), "/Local/RangeID/1000001/RaftTruncatedState"},
		{RaftLastIndexKey(roachpb.RangeID(1000001)), "/Local/RangeID/1000001/RaftLastIndex"},
		{RangeLastVerificationTimestampKey(roachpb.RangeID(1000001)), "/Local/RangeID/1000001/RangeLastVerificationTimestamp"},
		{RangeStatsKey(roachpb.RangeID(1000001)), "/Local/RangeID/1000001/RangeStats"},

		{MakeRangeKeyPrefix(roachpb.RKey("ok")), `/Local/Range/"ok"`},
		{RangeDescriptorKey(roachpb.RKey("111")), `/Local/Range/RangeDescriptor/"111"`},
		{RangeTreeNodeKey(roachpb.RKey("111")), `/Local/Range/RangeTreeNode/"111"`},
		{TransactionKey(roachpb.Key("111"), []byte("22222")), `/Local/Range/Transaction/addrKey:/"111"/id:"22222"`},

		{LocalMax, "/Local/Max"},

		// system
		{roachpb.MakeKey(Meta2Prefix, roachpb.Key("foo")), `/Meta2/"foo"`},
		{roachpb.MakeKey(Meta1Prefix, roachpb.Key("foo")), `/Meta1/"foo"`},

		{StoreStatusKey(2222), "/System/StatusStore/2222"},
		{NodeStatusKey(1111), "/System/StatusNode/1111"},

		{SystemMax, "/System/Max"},

		// table
		{UserTableDataMin, "/Table/50"},
		{MakeTablePrefix(111), "/Table/111"},
		{MakeKey(MakeTablePrefix(42), roachpb.RKey("foo")), `/Table/42/"foo"`},
		{MakeKey(MakeTablePrefix(42),
			roachpb.RKey(encoding.EncodeFloatAscending(nil, float64(233.221112)))),
			"/Table/42/233.221112"},
		{MakeKey(MakeTablePrefix(42),
			roachpb.RKey(encoding.EncodeFloatDescending(nil, float64(-233.221112)))),
			"/Table/42/233.221112"},
		{MakeKey(MakeTablePrefix(42),
			roachpb.RKey(encoding.EncodeFloatAscending(nil, math.Inf(1)))),
			"/Table/42/+Inf"},
		{MakeKey(MakeTablePrefix(42),
			roachpb.RKey(encoding.EncodeFloatAscending(nil, math.NaN()))),
			"/Table/42/NaN"},
		{MakeKey(MakeTablePrefix(42),
			roachpb.RKey(encoding.EncodeVarintAscending(nil, 1222)),
			roachpb.RKey(encoding.EncodeStringAscending(nil, "handsome man"))),
			`/Table/42/1222/"handsome man"`},
		{MakeKey(MakeTablePrefix(42),
			roachpb.RKey(encoding.EncodeVarintAscending(nil, 1222))),
			`/Table/42/1222`},
		{MakeKey(MakeTablePrefix(42),
			roachpb.RKey(encoding.EncodeVarintDescending(nil, 1222))),
			`/Table/42/-1223`},
		{MakeKey(MakeTablePrefix(42),
			roachpb.RKey(encoding.EncodeBytesAscending(nil, []byte{1, 2, 8, 255}))),
			`/Table/42/"\x01\x02\b\xff"`},
		{MakeKey(MakeTablePrefix(42),
			roachpb.RKey(encoding.EncodeBytesAscending(nil, []byte{1, 2, 8, 255})),
			roachpb.RKey("bar")), `/Table/42/"\x01\x02\b\xff"/"bar"`},
		{MakeKey(MakeTablePrefix(42),
			roachpb.RKey(encoding.EncodeBytesDescending(nil, []byte{1, 2, 8, 255})),
			roachpb.RKey("bar")), `/Table/42/"\x01\x02\b\xff"/"bar"`},
		{MakeKey(MakeTablePrefix(42),
			roachpb.RKey(encoding.EncodeNullAscending(nil))), "/Table/42/NULL"},
		{MakeKey(MakeTablePrefix(42),
			roachpb.RKey(encoding.EncodeNotNullAscending(nil))), "/Table/42/#"},
		{MakeKey(MakeTablePrefix(42),
			roachpb.RKey(encoding.EncodeTimeAscending(nil, tm))),
			"/Table/42/Sat Mar  7 11:06:39 UTC 2015"},
		{MakeKey(MakeTablePrefix(42),
			roachpb.RKey(encoding.EncodeTimeDescending(nil, tm))),
			"/Table/42/Sat Mar  7 11:06:39 UTC 2015"},
		{MakeKey(MakeTablePrefix(42),
			roachpb.RKey(encoding.EncodeDecimalAscending(nil, decimal.New(1234, -2)))),
			"/Table/42/12.34"},
		{MakeKey(MakeTablePrefix(42),
			roachpb.RKey(encoding.EncodeDecimalDescending(nil, decimal.New(1234, -2)))),
			"/Table/42/-12.34"},

		// others
		{MakeKey([]byte("")), "/Min"},
		{MakeKey(MakeTablePrefix(42), roachpb.RKey([]byte{0x21, 'a', 0x00, 0x02})), "/Table/42/<util/encoding/encoding.go:9999: unknown escape sequence: 0x0 0x2>"},
	}
	for i, test := range testCases {
		keyInfo := MassagePrettyPrintedSpanForTest(PrettyPrint(test.key), nil)
		exp := MassagePrettyPrintedSpanForTest(test.exp, nil)
		if exp != keyInfo {
			t.Fatalf("%d: expected %s, got %s", i, exp, keyInfo)
		}

		if exp != MassagePrettyPrintedSpanForTest(test.key.String(), nil) {
			t.Fatalf("%d: expected %s, got %s", i, exp, test.key.String())
		}
	}
}