improved sql parser odps support

This commit is contained in:
shaojin.wensj
2015-07-12 16:20:28 +08:00
parent 72270f5248
commit d5a198a39f
43 changed files with 1906 additions and 56 deletions
@@ -0,0 +1,45 @@
package com.alibaba.druid.bvt.sql.odps;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
import junit.framework.TestCase;
public class OdpsAddStatisticTest extends TestCase {
public void test_table_count() throws Exception {
String sql = "add statistic tbl_shop table_count;";
Assert.assertEquals("ADD STATISTIC tbl_shop TABLE_COUNT"
+ "\n;", SQLUtils.formatOdps(sql));
}
public void test_null_value() throws Exception {
String sql = "add statistic tbl_shop null_value shop_name;";
Assert.assertEquals("ADD STATISTIC tbl_shop NULL_VALUE shop_name"
+ "\n;", SQLUtils.formatOdps(sql));
}
public void test_column_sum() throws Exception {
String sql = "add statistic tbl_shop column_sum shop_name;";
Assert.assertEquals("ADD STATISTIC tbl_shop COLUMN_SUM shop_name"
+ "\n;", SQLUtils.formatOdps(sql));
}
public void test_column_max() throws Exception {
String sql = "add statistic tbl_shop column_max shop_name;";
Assert.assertEquals("ADD STATISTIC tbl_shop COLUMN_MAX shop_name"
+ "\n;", SQLUtils.formatOdps(sql));
}
public void test_column_min() throws Exception {
String sql = "add statistic tbl_shop column_min shop_name;";
Assert.assertEquals("ADD STATISTIC tbl_shop COLUMN_MIN shop_name"
+ "\n;", SQLUtils.formatOdps(sql));
}
public void test_expression_condition() throws Exception {
String sql = "add statistic tbl_shop expression_condition tbl_shop='hangzhou';";
Assert.assertEquals("ADD STATISTIC tbl_shop EXPRESSION_CONDITION tbl_shop = 'hangzhou'"
+ "\n;", SQLUtils.formatOdps(sql));
}
}
@@ -0,0 +1,17 @@
package com.alibaba.druid.bvt.sql.odps;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
import junit.framework.TestCase;
public class OdpsAlterTableAddPartitionTest extends TestCase {
public void test_if() throws Exception {
String sql = "alter table sale_detail add if not exists partition (sale_date='201312', region='hangzhou');";
Assert.assertEquals("ALTER TABLE sale_detail" //
+ "\n\tADD IF NOT EXISTS PARTITION (sale_date = '201312', region = 'hangzhou')"
+ "\n;", SQLUtils.formatOdps(sql));
}
}
@@ -0,0 +1,25 @@
package com.alibaba.druid.bvt.sql.odps;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
import junit.framework.TestCase;
public class OdpsAlterTableDisableLifecycle extends TestCase {
public void test_no_partition() throws Exception {
String sql = "ALTER TABLE trans DISABLE LIFECYCLE;";
Assert.assertEquals("ALTER TABLE trans" //
+ "\n\tDISABLE LIFECYCLE" //
+ "\n;", SQLUtils.formatOdps(sql));
}
public void test_has_partition() throws Exception {
String sql = "ALTER TABLE trans PARTITION(dt='20141111') DISABLE LIFECYCLE;";
Assert.assertEquals("ALTER TABLE trans"
+ "\n\tPARTITION (dt = '20141111') DISABLE LIFECYCLE"
+ "\n;", SQLUtils.formatOdps(sql));
}
}
@@ -0,0 +1,24 @@
package com.alibaba.druid.bvt.sql.odps;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
import junit.framework.TestCase;
public class OdpsAlterTableDropPartitionTest extends TestCase {
public void test_if() throws Exception {
String sql = "alter table sale_detail drop if not exists partition (sale_date='201312', region='hangzhou');";
Assert.assertEquals("ALTER TABLE sale_detail" //
+ "\n\tDROP IF NOT EXISTS PARTITION (sale_date = '201312', region = 'hangzhou')"
+ "\n;", SQLUtils.formatOdps(sql));
}
public void test_purge() throws Exception {
String sql = "alter table my_log drop partition (ds='20150618') purge;";
Assert.assertEquals("ALTER TABLE my_log"
+ "\n\tDROP PARTITION (ds = '20150618') PURGE"
+ "\n;", SQLUtils.formatOdps(sql));
}
}
@@ -0,0 +1,25 @@
package com.alibaba.druid.bvt.sql.odps;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
import junit.framework.TestCase;
public class OdpsAlterTableEnableLifecycle extends TestCase {
public void test_no_partition() throws Exception {
String sql = "ALTER TABLE trans ENABLE LIFECYCLE;";
Assert.assertEquals("ALTER TABLE trans" //
+ "\n\tENABLE LIFECYCLE" //
+ "\n;", SQLUtils.formatOdps(sql));
}
public void test_has_partition() throws Exception {
String sql = "ALTER TABLE trans PARTITION(dt='20141111') ENABLE LIFECYCLE;";
Assert.assertEquals("ALTER TABLE trans"
+ "\n\tPARTITION (dt = '20141111') ENABLE LIFECYCLE"
+ "\n;", SQLUtils.formatOdps(sql));
}
}
@@ -0,0 +1,18 @@
package com.alibaba.druid.bvt.sql.odps;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
import junit.framework.TestCase;
public class OdpsAlterTableRenamePartitionTest extends TestCase {
public void test_if() throws Exception {
String sql = "alter table sale_detail partition (sale_date='201312', region='hangzhou')"
+ "\nrename to partition(sale_date='201313', region='hangzhou');";
Assert.assertEquals("ALTER TABLE sale_detail"
+ "\n\tPARTITION (sale_date = '201312', region = 'hangzhou') RENAME TO PARTITION(sale_date = '201313', region = 'hangzhou')"
+ "\n;", SQLUtils.formatOdps(sql));
}
}
@@ -0,0 +1,17 @@
package com.alibaba.druid.bvt.sql.odps;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
import junit.framework.TestCase;
public class OdpsAlterTableSetCommentTest extends TestCase {
public void test_if() throws Exception {
String sql = "alter table sale_detail set comment 'new coments for table sale_detail';";
Assert.assertEquals("ALTER TABLE sale_detail" //
+ "\n\tSET COMMENT 'new coments for table sale_detail'" //
+ "\n;", SQLUtils.formatOdps(sql));
}
}
@@ -0,0 +1,17 @@
package com.alibaba.druid.bvt.sql.odps;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
import junit.framework.TestCase;
public class OdpsAlterTableSetLifecycle extends TestCase {
public void test_if() throws Exception {
String sql = "alter table test_lifecycle set lifecycle 50;";
Assert.assertEquals("ALTER TABLE test_lifecycle" //
+ "\n\tSET LIFECYCLE 50" //
+ "\n;", SQLUtils.formatOdps(sql));
}
}
@@ -0,0 +1,24 @@
package com.alibaba.druid.bvt.sql.odps;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
import junit.framework.TestCase;
public class OdpsAlterTableTouchTest extends TestCase {
public void test_touch() throws Exception {
String sql = "alter table test_lifecycle touch;";
Assert.assertEquals("ALTER TABLE test_lifecycle" //
+ "\n\tTOUCH" //
+ "\n;", SQLUtils.formatOdps(sql));
}
public void test_touch_partition() throws Exception {
String sql = "alter table test_lifecycle touch PARTITION (dt='20141111');";
Assert.assertEquals("ALTER TABLE test_lifecycle" //
+ "\n\tTOUCH PARTITION (dt = '20141111')" //
+ "\n;", SQLUtils.formatOdps(sql));
}
}
@@ -0,0 +1,16 @@
package com.alibaba.druid.bvt.sql.odps;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
import junit.framework.TestCase;
public class OdpsAlterViewTest extends TestCase {
public void test_if() throws Exception {
String sql = "alter view view_name rename to new_view_name;";
Assert.assertEquals("ALTER VIEW view_name RENAME TO new_view_name"
+ "\n;", SQLUtils.formatOdps(sql));
}
}
@@ -0,0 +1,20 @@
package com.alibaba.druid.bvt.sql.odps;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
import junit.framework.TestCase;
public class OdpsAnalyzeTableTest extends TestCase {
public void test_0() throws Exception {
String sql = "analyze table t partition(pt='1') compute statistics";
Assert.assertEquals("ANALYZE TABLE t PARTITION (pt = '1') COMPUTE STATISTICS", SQLUtils.formatOdps(sql));
}
public void test_no_partition() throws Exception {
String sql = "analyze table t compute statistics";
Assert.assertEquals("ANALYZE TABLE t COMPUTE STATISTICS", SQLUtils.formatOdps(sql));
}
}
@@ -0,0 +1,33 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.odps;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
import junit.framework.TestCase;
public class OdpsDropViewTest extends TestCase {
public void test_column_comment() throws Exception {
String sql = "drop view if exists view_name;";
Assert.assertEquals("DROP VIEW IF EXISTS view_name" //
+ "\n;", SQLUtils.formatOdps(sql));
}
}
@@ -0,0 +1,16 @@
package com.alibaba.druid.bvt.sql.odps;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
import junit.framework.TestCase;
public class OdpsIfTest extends TestCase {
public void test_if() throws Exception {
String sql = "select sum(if(a > 0, 1, 0)) from t1";
Assert.assertEquals("SELECT SUM(if(a > 0, 1, 0))" //
+ "\nFROM t1", SQLUtils.formatOdps(sql));
}
}
@@ -0,0 +1,45 @@
package com.alibaba.druid.bvt.sql.odps;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
import junit.framework.TestCase;
public class OdpsRemoveStatisticTest extends TestCase {
public void test_table_count() throws Exception {
String sql = "remove statistic tbl_shop table_count;";
Assert.assertEquals("REMOVE STATISTIC tbl_shop TABLE_COUNT"
+ "\n;", SQLUtils.formatOdps(sql));
}
public void test_null_value() throws Exception {
String sql = "remove statistic tbl_shop null_value shop_name;";
Assert.assertEquals("REMOVE STATISTIC tbl_shop NULL_VALUE shop_name"
+ "\n;", SQLUtils.formatOdps(sql));
}
public void test_column_sum() throws Exception {
String sql = "remove statistic tbl_shop column_sum shop_name;";
Assert.assertEquals("REMOVE STATISTIC tbl_shop COLUMN_SUM shop_name"
+ "\n;", SQLUtils.formatOdps(sql));
}
public void test_column_max() throws Exception {
String sql = "remove statistic tbl_shop column_max shop_name;";
Assert.assertEquals("REMOVE STATISTIC tbl_shop COLUMN_MAX shop_name"
+ "\n;", SQLUtils.formatOdps(sql));
}
public void test_column_min() throws Exception {
String sql = "remove statistic tbl_shop column_min shop_name;";
Assert.assertEquals("REMOVE STATISTIC tbl_shop COLUMN_MIN shop_name"
+ "\n;", SQLUtils.formatOdps(sql));
}
public void test_expression_condition() throws Exception {
String sql = "remove statistic tbl_shop expression_condition tbl_shop='hangzhou';";
Assert.assertEquals("REMOVE STATISTIC tbl_shop EXPRESSION_CONDITION tbl_shop = 'hangzhou'"
+ "\n;", SQLUtils.formatOdps(sql));
}
}
@@ -0,0 +1,42 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.odps;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
import junit.framework.TestCase;
public class OdpsSelectTest_mapjoin extends TestCase {
public void test_column_comment() throws Exception {
String sql = " select /* + mapjoin(a) */"
+ "\na.shop_name,"
+ "\nb.customer_id,"
+ "\nb.total_price"
+ "\nfrom shop a join sale_detail b"
+ "\non a.shop_name = b.shop_name;";
Assert.assertEquals("SELECT /*+ mapjoin(a) */ a.shop_name, "
+ "\n\tb.customer_id, "
+ "\n\tb.total_price"
+ "\nFROM shop a"
+ "\nJOIN sale_detail b ON a.shop_name = b.shop_name"
+ "\n;", SQLUtils.formatOdps(sql));
}
}