Add pipes_as_concat_mode (#2252)
This commit will add a new sql mode named MODE_PIPES_AS_CONCAT:
Description:
1、If this mode is active, '||' will be handled different from the original way ('||' and 'or' are seen as the same symbols in Doris) that it can be used to concat two exps and returns a new string. For example, 'a' || 'b' = 'ab' and 1 || 0 = '10'.
2. User can active this mode by "SET sql_mode = PIPES_AS_CONCAT", and deactive it by "SET sql_mode = '' ".
This commit is contained in:
79
fe/src/test/java/org/apache/doris/analysis/SqlModeTest.java
Normal file
79
fe/src/test/java/org/apache/doris/analysis/SqlModeTest.java
Normal file
@ -0,0 +1,79 @@
|
||||
// Licensed to the Apache Software Foundation (ASF) under one
|
||||
// or more contributor license agreements. See the NOTICE file
|
||||
// distributed with this work for additional information
|
||||
// regarding copyright ownership. The ASF licenses this file
|
||||
// to you 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 org.apache.doris.analysis;
|
||||
|
||||
import org.apache.doris.qe.SqlModeHelper;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.StringReader;
|
||||
|
||||
public class SqlModeTest {
|
||||
|
||||
@Test
|
||||
public void testScannerConstructor() {
|
||||
String stmt = new String("SELECT * FROM db1.tbl1 WHERE name = 'BILL GATES'");
|
||||
SqlParser parser = new SqlParser(new SqlScanner(new StringReader(stmt)));
|
||||
SelectStmt selectStmt = null;
|
||||
try {
|
||||
selectStmt = (SelectStmt) parser.parse().value;
|
||||
} catch (Exception e) {
|
||||
Assert.fail(e.getMessage());
|
||||
}
|
||||
Assert.assertEquals("SELECT FROM `db1`.`tbl1` WHERE `name` = 'BILL GATES'", selectStmt.toSql());
|
||||
|
||||
parser = new SqlParser(new SqlScanner(new StringReader(stmt), SqlModeHelper.MODE_DEFAULT));
|
||||
try {
|
||||
selectStmt = (SelectStmt) parser.parse().value;
|
||||
} catch (Exception e) {
|
||||
Assert.fail(e.getMessage());
|
||||
}
|
||||
Assert.assertEquals("SELECT FROM `db1`.`tbl1` WHERE `name` = 'BILL GATES'", selectStmt.toSql());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPipesAsConcatMode() {
|
||||
// Mode Active
|
||||
String stmt = new String("SELECT 'a' || 'b' || 'c'");
|
||||
SqlParser parser = new SqlParser(new SqlScanner(new StringReader(stmt), SqlModeHelper.MODE_PIPES_AS_CONCAT));
|
||||
SelectStmt selectStmt = null;
|
||||
try {
|
||||
selectStmt = (SelectStmt) parser.parse().value;
|
||||
} catch (Exception e) {
|
||||
Assert.fail(e.getMessage());
|
||||
}
|
||||
Expr expr = selectStmt.getSelectList().getItems().get(0).getExpr();
|
||||
if (!(expr instanceof FunctionCallExpr)) {
|
||||
Assert.fail("Mode not working");
|
||||
}
|
||||
Assert.assertEquals("concat('a', 'b', 'c')", expr.toSql());
|
||||
|
||||
// Mode DeActive
|
||||
parser = new SqlParser(new SqlScanner(new StringReader(stmt), SqlModeHelper.MODE_DEFAULT));
|
||||
try {
|
||||
selectStmt = (SelectStmt) parser.parse().value;
|
||||
} catch (Exception e) {
|
||||
Assert.fail(e.getMessage());
|
||||
}
|
||||
expr = selectStmt.getSelectList().getItems().get(0).getExpr();
|
||||
if (!(expr instanceof CompoundPredicate)) {
|
||||
Assert.fail();
|
||||
}
|
||||
Assert.assertEquals("(('a') OR ('b')) OR ('c')", expr.toSql());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user