Exemplo n.º 1
0
func parseAndNormalizeExpr(t *testing.T, sql string) (parser.Expr, qvalMap) {
	q, err := parser.ParseTraditional("SELECT " + sql)
	if err != nil {
		t.Fatalf("%s: %v", sql, err)
	}
	expr := q[0].(*parser.Select).Exprs[0].Expr
	expr, err = (parser.EvalContext{}).NormalizeExpr(expr)
	if err != nil {
		t.Fatalf("%s: %v", sql, err)
	}

	// Perform qualified name resolution because {analyze,simplify}Expr want
	// expressions containing qvalues.
	s := &scanNode{}
	s.desc = testTableDesc()
	s.visibleCols = s.desc.Columns

	if err := s.desc.AllocateIDs(); err != nil {
		t.Fatal(err)
	}

	expr, err = s.resolveQNames(expr)
	if err != nil {
		t.Fatalf("%s: %v", sql, err)
	}
	if _, err := parser.TypeCheckExpr(expr); err != nil {
		t.Fatalf("%s: %v", sql, err)
	}
	return expr, s.qvals
}
Exemplo n.º 2
0
// Test that we can resolve the qnames in an expression that has already been
// resolved.
func TestRetryResolveQNames(t *testing.T) {
	defer leaktest.AfterTest(t)

	q, err := parser.ParseTraditional(`SELECT COUNT(a)`)
	if err != nil {
		t.Fatal(err)
	}
	expr := q[0].(*parser.Select).Exprs[0].Expr

	for i := 0; i < 2; i++ {
		s := &scanNode{}
		s.desc = testTableDesc()
		s.visibleCols = s.desc.Columns
		if err := s.desc.AllocateIDs(); err != nil {
			t.Fatal(err)
		}

		_, err = s.resolveQNames(expr)
		if err != nil {
			t.Fatal(err)
		}
		if len(s.qvals) != 1 {
			t.Fatalf("%d: expected 1 qvalue, but found %d", i, len(s.qvals))
		}
		if _, ok := s.qvals[ColumnID(1)]; !ok {
			t.Fatalf("%d: unable to find qvalue for column ID 1", i)
		}
	}
}
Exemplo n.º 3
0
func parseAndNormalizeExpr(t *testing.T, sql string) (parser.Expr, qvalMap) {
	q, err := parser.ParseTraditional("SELECT " + sql)
	if err != nil {
		t.Fatalf("%s: %v", sql, err)
	}
	expr := q[0].(*parser.Select).Exprs[0].Expr
	r, err := parser.NormalizeExpr(expr)
	if err != nil {
		t.Fatalf("%s: %v", sql, err)
	}

	// Perform qualified name resolution because {analyze,simplify}Expr want
	// expressions containing qvalues.
	s := &scanNode{}
	s.desc = &TableDescriptor{
		Columns: []ColumnDescriptor{
			{Name: "a", ID: 1, Type: ColumnType{Kind: ColumnType_INT}},
			{Name: "b", ID: 2, Type: ColumnType{Kind: ColumnType_INT}},
			{Name: "c", ID: 3, Type: ColumnType{Kind: ColumnType_INT}},
			{Name: "d", ID: 4, Type: ColumnType{Kind: ColumnType_INT}},
			{Name: "e", ID: 5, Type: ColumnType{Kind: ColumnType_INT}},
		},
	}
	s.visibleCols = s.desc.Columns

	r, err = s.resolveQNames(r)
	if err != nil {
		t.Fatalf("%s: %v", sql, err)
	}
	return r, s.qvals
}
Exemplo n.º 4
0
func (p *planner) query(sql string) (planNode, error) {
	stmts, err := parser.ParseTraditional(sql)
	if err != nil {
		return nil, err
	}
	if len(stmts) != 1 {
		return nil, util.Errorf("expected single statement, found %d", len(stmts))
	}
	return p.makePlan(stmts[0])
}
Exemplo n.º 5
0
func TestPrimaryKeyUnspecified(t *testing.T) {
	defer leaktest.AfterTest(t)
	stmt, err := parser.ParseTraditional(
		"CREATE TABLE foo.test (a INT, b INT, CONSTRAINT c UNIQUE (b))")
	if err != nil {
		t.Fatal(err)
	}
	create := stmt[0].(*parser.CreateTable)
	if err := create.Table.NormalizeTableName(""); err != nil {
		t.Fatal(err)
	}
	desc, err := makeTableDesc(create, 1)
	if err != nil {
		t.Fatal(err)
	}
	if err := desc.AllocateIDs(); err != errMissingPrimaryKey {
		t.Fatal(err)
	}
}
Exemplo n.º 6
0
func createSystemTable(id ID, cmd string) TableDescriptor {
	stmts, err := parser.ParseTraditional(cmd)
	if err != nil {
		log.Fatal(err)
	}

	desc, err := makeTableDesc(stmts[0].(*parser.CreateTable))
	if err != nil {
		log.Fatal(err)
	}

	// Assign max privileges to root user.
	desc.Privileges = NewPrivilegeDescriptor(security.RootUser,
		SystemAllowedPrivileges[id])

	desc.ID = id
	if err := desc.AllocateIDs(); err != nil {
		log.Fatal(err)
	}

	return desc
}
Exemplo n.º 7
0
func TestMakeTableDescColumns(t *testing.T) {
	defer leaktest.AfterTest(t)

	testData := []struct {
		sqlType  string
		colType  ColumnType
		nullable bool
	}{
		{
			"BIT(1)",
			ColumnType{Kind: ColumnType_INT, Width: 1},
			true,
		},
		{
			"BOOLEAN",
			ColumnType{Kind: ColumnType_BOOL},
			true,
		},
		{
			"INT",
			ColumnType{Kind: ColumnType_INT},
			true,
		},
		{
			"FLOAT(3)",
			ColumnType{Kind: ColumnType_FLOAT, Precision: 3},
			true,
		},
		{
			"DECIMAL(5,6)",
			ColumnType{Kind: ColumnType_DECIMAL, Precision: 5, Width: 6},
			true,
		},
		{
			"DATE",
			ColumnType{Kind: ColumnType_DATE},
			true,
		},
		{
			"TIMESTAMP",
			ColumnType{Kind: ColumnType_TIMESTAMP},
			true,
		},
		{
			"INTERVAL",
			ColumnType{Kind: ColumnType_INTERVAL},
			true,
		},
		{
			"CHAR",
			ColumnType{Kind: ColumnType_STRING},
			true,
		},
		{
			"TEXT",
			ColumnType{Kind: ColumnType_STRING},
			true,
		},
		{
			"BLOB",
			ColumnType{Kind: ColumnType_BYTES},
			true,
		},
		{
			"INT NOT NULL",
			ColumnType{Kind: ColumnType_INT},
			false,
		},
		{
			"INT NULL",
			ColumnType{Kind: ColumnType_INT},
			true,
		},
	}
	for i, d := range testData {
		stmt, err := parser.ParseTraditional(
			"CREATE TABLE foo.test (a " + d.sqlType + " PRIMARY KEY)")
		if err != nil {
			t.Fatalf("%d: %v", i, err)
		}
		create := stmt[0].(*parser.CreateTable)
		if err := create.Table.NormalizeTableName(""); err != nil {
			t.Fatalf("%d: %v", i, err)
		}
		schema, err := makeTableDesc(create, 1)
		if err != nil {
			t.Fatalf("%d: %v", i, err)
		}
		if !reflect.DeepEqual(d.colType, schema.Columns[0].Type) {
			t.Fatalf("%d: expected %+v, but got %+v", i, d.colType, schema.Columns[0])
		}
		if d.nullable != schema.Columns[0].Nullable {
			t.Fatalf("%d: expected %+v, but got %+v", i, d.nullable, schema.Columns[0].Nullable)
		}
	}
}
Exemplo n.º 8
0
func TestMakeTableDescIndexes(t *testing.T) {
	defer leaktest.AfterTest(t)

	testData := []struct {
		sql     string
		primary IndexDescriptor
		indexes []IndexDescriptor
	}{
		{
			"a INT PRIMARY KEY",
			IndexDescriptor{
				Name:        PrimaryKeyIndexName,
				Unique:      true,
				ColumnNames: []string{"a"},
			},
			[]IndexDescriptor{},
		},
		{
			"a INT UNIQUE, b INT PRIMARY KEY",
			IndexDescriptor{
				Name:        "primary",
				Unique:      true,
				ColumnNames: []string{"b"},
			},
			[]IndexDescriptor{
				{
					Name:        "",
					Unique:      true,
					ColumnNames: []string{"a"},
				},
			},
		},
		{
			"a INT, b INT, CONSTRAINT c PRIMARY KEY (a, b)",
			IndexDescriptor{
				Name:        "c",
				Unique:      true,
				ColumnNames: []string{"a", "b"},
			},
			[]IndexDescriptor{},
		},
		{
			"a INT, b INT, CONSTRAINT c UNIQUE (b), PRIMARY KEY (a, b)",
			IndexDescriptor{
				Name:        "primary",
				Unique:      true,
				ColumnNames: []string{"a", "b"},
			},
			[]IndexDescriptor{
				{
					Name:        "c",
					Unique:      true,
					ColumnNames: []string{"b"},
				},
			},
		},
		{
			"a INT, b INT, PRIMARY KEY (a, b)",
			IndexDescriptor{
				Name:        PrimaryKeyIndexName,
				Unique:      true,
				ColumnNames: []string{"a", "b"},
			},
			[]IndexDescriptor{},
		},
	}
	for i, d := range testData {
		stmt, err := parser.ParseTraditional("CREATE TABLE foo.test (" + d.sql + ")")
		if err != nil {
			t.Fatalf("%d: %v", i, err)
		}
		create := stmt[0].(*parser.CreateTable)
		if err := create.Table.NormalizeTableName(""); err != nil {
			t.Fatalf("%d: %v", i, err)
		}
		schema, err := makeTableDesc(create, 1)
		if err != nil {
			t.Fatalf("%d: %v", i, err)
		}
		if !reflect.DeepEqual(d.primary, schema.PrimaryIndex) {
			t.Fatalf("%d: expected %+v, but got %+v", i, d.primary, schema.PrimaryIndex)
		}
		if !reflect.DeepEqual(d.indexes, append([]IndexDescriptor{}, schema.Indexes...)) {
			t.Fatalf("%d: expected %+v, but got %+v", i, d.indexes, schema.Indexes)
		}

	}
}