本文整理汇总了Golang中github.com/cockroachdb/cockroach/sql/parser.ParseTraditional函数的典型用法代码示例。如果您正苦于以下问题:Golang ParseTraditional函数的具体用法?Golang ParseTraditional怎么用?Golang ParseTraditional使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ParseTraditional函数的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: parseAndNormalizeExpr
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
}
示例2: TestRetryResolveQNames
// 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)
}
}
}
示例3: parseAndNormalizeExpr
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
}
示例4: query
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])
}
示例5: TestPrimaryKeyUnspecified
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)
}
}
示例6: createSystemTable
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
}
示例7: TestMakeTableDescColumns
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)
}
}
}
示例8: TestMakeTableDescIndexes
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)
}
}
}